Flutter Engine
The Flutter Engine
SkSVGRenderContext.h
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkSVGRenderContext_DEFINED
9#define SkSVGRenderContext_DEFINED
10
12#include "include/core/SkM44.h"
14#include "include/core/SkPath.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
23#include "src/base/SkTLazy.h"
24#include "src/core/SkTHash.h"
25
26class SkCanvas;
27class SkSVGLength;
28
30public:
31 SkSVGLengthContext(const SkSize& viewport, SkScalar dpi = 90)
32 : fViewport(viewport), fDPI(dpi) {}
33
34 enum class LengthType {
35 kHorizontal,
36 kVertical,
37 kOther,
38 };
39
40 const SkSize& viewPort() const { return fViewport; }
41 void setViewPort(const SkSize& viewport) { fViewport = viewport; }
42
43 SkScalar resolve(const SkSVGLength&, LengthType) const;
44 SkRect resolveRect(const SkSVGLength& x, const SkSVGLength& y,
45 const SkSVGLength& w, const SkSVGLength& h) const;
46
47private:
48 SkSize fViewport;
49 SkScalar fDPI;
50};
51
56
58
59 // Inherited presentation attributes, computed for the current node.
61};
62
64public:
65 // Captures data required for object bounding box resolution.
66 struct OBBScope {
69 };
70
72 const sk_sp<SkFontMgr>&,
74 const SkSVGIDMapper&,
75 const SkSVGLengthContext&,
77 const OBBScope&,
81 // Establish a new OBB scope. Normally used when entering a node's render scope.
84
85 const SkSVGLengthContext& lengthContext() const { return *fLengthContext; }
86 SkSVGLengthContext* writableLengthContext() { return fLengthContext.writable(); }
87
88 const SkSVGPresentationContext& presentationContext() const { return *fPresentationContext; }
89
90 SkCanvas* canvas() const { return fCanvas; }
91 void saveOnce();
92
94 kLeaf = 1 << 0, // the target node doesn't have descendants
95 };
96 void applyPresentationAttributes(const SkSVGPresentationAttributes&, uint32_t flags);
97
98 // Scoped wrapper that temporarily clears the original node reference.
100 public:
102 : fOwner(node) {
103 if (fOwner) {
104 fBorrowed = std::move(*fOwner);
105 *fOwner = nullptr;
106 }
107 }
108
110 if (fOwner) {
111 *fOwner = std::move(fBorrowed);
112 }
113 }
114
115 const SkSVGNode* get() const { return fBorrowed.get(); }
116 const SkSVGNode* operator->() const { return fBorrowed.get(); }
117 const SkSVGNode& operator*() const { return *fBorrowed; }
118
119 explicit operator bool() const { return !!fBorrowed; }
120
121 private:
122 // noncopyable
123 BorrowedNode(const BorrowedNode&) = delete;
124 BorrowedNode& operator=(BorrowedNode&) = delete;
125
126 sk_sp<SkSVGNode>* fOwner;
127 sk_sp<SkSVGNode> fBorrowed;
128 };
129
130 // Note: the id->node association is cleared for the lifetime of the returned value
131 // (effectively breaks reference cycles, assuming appropriate return value scoping).
132 BorrowedNode findNodeById(const SkSVGIRI&) const;
133
134 SkTLazy<SkPaint> fillPaint() const;
135 SkTLazy<SkPaint> strokePaint() const;
136
137 SkSVGColorType resolveSvgColor(const SkSVGColor&) const;
138
139 // The local computed clip path (not inherited).
140 const SkPath* clipPath() const { return fClipPath.getMaybeNull(); }
141
143 return fResourceProvider;
144 }
145
147 // It is probably an oversight to try to render <text> without having set the SkFontMgr.
148 // We will assert this in debug mode, but fallback to an empty fontmgr in release builds.
149 SkASSERT(fFontMgr);
150 return fFontMgr ? fFontMgr : SkFontMgr::RefEmpty();
151 }
152
153 // Returns the translate/scale transformation required to map into the current OBB scope,
154 // with the specified units.
157 };
158 OBBTransform transformForCurrentOBB(SkSVGObjectBoundingBoxUnits) const;
159
160 SkRect resolveOBBRect(const SkSVGLength& x, const SkSVGLength& y,
161 const SkSVGLength& w, const SkSVGLength& h,
163
164 std::unique_ptr<SkShaper> makeShaper() const {
165 SkASSERT(fTextShapingFactory);
166 return fTextShapingFactory->makeShaper(this->fontMgr());
167 }
168
169 std::unique_ptr<SkShaper::BiDiRunIterator> makeBidiRunIterator(const char* utf8,
170 size_t utf8Bytes,
171 uint8_t bidiLevel) const {
172 SkASSERT(fTextShapingFactory);
173 return fTextShapingFactory->makeBidiRunIterator(utf8, utf8Bytes, bidiLevel);
174 }
175
176 std::unique_ptr<SkShaper::ScriptRunIterator> makeScriptRunIterator(const char* utf8,
177 size_t utf8Bytes) const {
178 SkASSERT(fTextShapingFactory);
179 constexpr SkFourByteTag unknownScript = SkSetFourByteTag('Z', 'z', 'z', 'z');
180 return fTextShapingFactory->makeScriptRunIterator(utf8, utf8Bytes, unknownScript);
181 }
182
183private:
184 // Stack-only
185 void* operator new(size_t) = delete;
186 void* operator new(size_t, void*) = delete;
187 SkSVGRenderContext& operator=(const SkSVGRenderContext&) = delete;
188
189 void applyOpacity(SkScalar opacity, uint32_t flags, bool hasFilter);
190 void applyFilter(const SkSVGFuncIRI&);
191 void applyClip(const SkSVGFuncIRI&);
192 void applyMask(const SkSVGFuncIRI&);
193
194 SkTLazy<SkPaint> commonPaint(const SkSVGPaint&, float opacity) const;
195
196 const sk_sp<SkFontMgr>& fFontMgr;
197 const sk_sp<SkShapers::Factory>& fTextShapingFactory;
198 const sk_sp<skresources::ResourceProvider>& fResourceProvider;
199 const SkSVGIDMapper& fIDMapper;
202 SkCanvas* fCanvas;
203 // The save count on 'fCanvas' at construction time.
204 // A restoreToCount() will be issued on destruction.
205 int fCanvasSaveCount;
206
207 // clipPath, if present for the current context (not inherited).
208 SkTLazy<SkPath> fClipPath;
209
210 // Deferred opacity optimization for leaf nodes.
211 float fDeferredPaintOpacity = 1;
212
213 // Current object bounding box scope.
214 const OBBScope fOBBScope;
215};
216
217#endif // SkSVGRenderContext_DEFINED
#define SK_API
Definition: SkAPI.h:35
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColor SkSVGColorType
Definition: SkSVGTypes.h:25
uint32_t SkFourByteTag
Definition: SkTypes.h:166
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition: SkTypes.h:167
static sk_sp< SkFontMgr > RefEmpty()
Definition: SkFontMgr.cpp:154
Definition: SkPath.h:59
void setViewPort(const SkSize &viewport)
const SkSize & viewPort() const
SkSVGLengthContext(const SkSize &viewport, SkScalar dpi=90)
BorrowedNode(sk_sp< SkSVGNode > *node)
const SkSVGNode * get() const
const SkSVGNode & operator*() const
const SkSVGNode * operator->() const
SkSVGLengthContext * writableLengthContext()
sk_sp< SkFontMgr > fontMgr() const
const SkSVGPresentationContext & presentationContext() const
std::unique_ptr< SkShaper::ScriptRunIterator > makeScriptRunIterator(const char *utf8, size_t utf8Bytes) const
std::unique_ptr< SkShaper::BiDiRunIterator > makeBidiRunIterator(const char *utf8, size_t utf8Bytes, uint8_t bidiLevel) const
SkCanvas * canvas() const
const SkPath * clipPath() const
const SkSVGLengthContext & lengthContext() const
std::unique_ptr< SkShaper > makeShaper() const
const sk_sp< skresources::ResourceProvider > & resourceProvider() const
sk_sp< SkFontMgr > fontMgr
Definition: examples.cpp:32
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
double y
double x
SkScalar w
SkScalar h
const Scalar scale
SkSVGPresentationAttributes fInherited
SkSVGPresentationContext & operator=(const SkSVGPresentationContext &)=default
SkSVGPresentationContext(const SkSVGPresentationContext &)=default
const SkSVGRenderContext * fCtx
Definition: SkSize.h:52
Definition: SkM44.h:19