Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSVGNode.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 SkSVGNode_DEFINED
9#define SkSVGNode_DEFINED
10
14
15class SkCanvas;
16class SkMatrix;
17class SkPaint;
18class SkPath;
21class SkSVGValue;
22
63
64#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
65private: \
66 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
67 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
68 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
69 return pr.isValid(); \
70 } \
71 \
72public: \
73 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
74 return fPresentationAttributes.f##attr_name; \
75 } \
76 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
77 auto* dest = &fPresentationAttributes.f##attr_name; \
78 if (!dest->isInheritable() || v.isValue()) { \
79 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
80 *dest = v; \
81 } else { \
82 dest->set(SkSVGPropertyState::kInherit); \
83 } \
84 } \
85 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
86 auto* dest = &fPresentationAttributes.f##attr_name; \
87 if (!dest->isInheritable() || v.isValue()) { \
88 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
89 *dest = std::move(v); \
90 } else { \
91 dest->set(SkSVGPropertyState::kInherit); \
92 } \
93 }
94
95class SK_API SkSVGNode : public SkRefCnt {
96public:
97 ~SkSVGNode() override;
98
99 SkSVGTag tag() const { return fTag; }
100
101 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
102
103 void render(const SkSVGRenderContext&) const;
104 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
105 SkPath asPath(const SkSVGRenderContext&) const;
106 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
107
108 void setAttribute(SkSVGAttribute, const SkSVGValue&);
109 bool setAttribute(const char* attributeName, const char* attributeValue);
110
111 // TODO: consolidate with existing setAttribute
112 virtual bool parseAndSetAttribute(const char* name, const char* value);
113
114 // inherited
115 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
116 SVG_PRES_ATTR(Color , SkSVGColorType , true)
117 SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
118 SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true)
119 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
120 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
121 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
123 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
124 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
125 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
126 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
127 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
128 SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true)
129 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
130 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
131 SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true)
132 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
133 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
134 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
135 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
136
137 // not inherited
138 SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
139 SVG_PRES_ATTR(Display , SkSVGDisplay , false)
140 SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
142 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
143 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
144 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
145 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
146 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
147 SVG_PRES_ATTR(LightingColor , SkSVGColor , false)
148
149protected:
151
152 static SkMatrix ComputeViewboxMatrix(const SkRect&, const SkRect&, SkSVGPreserveAspectRatio);
153
154 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
155 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
156 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
157 // (return false).
158 // Implementations are expected to return true if rendering is to continue, or false if
159 // the node/subtree rendering is disabled.
160 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
161
162 virtual void onRender(const SkSVGRenderContext&) const = 0;
163
164 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
165
166 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
167
168 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
169
170 virtual bool hasChildren() const { return false; }
171
173 return SkRect::MakeEmpty();
174 }
175
176private:
177 SkSVGTag fTag;
178
179 // FIXME: this should be sparse
180 SkSVGPresentationAttributes fPresentationAttributes;
181
182 using INHERITED = SkRefCnt;
183};
184
185#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
186
187#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
188 private: \
189 bool set##attr_name( \
190 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
191 if (pr.isValid()) { this->set##attr_name(*pr); } \
192 return pr.isValid(); \
193 } \
194 bool set##attr_name( \
195 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
196 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
197 return pr.isValid(); \
198 } \
199 public: \
200 void set##attr_name(const attr_type& a) { set_cp(a); } \
201 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
202
203#define SVG_ATTR(attr_name, attr_type, attr_default) \
204 private: \
205 attr_type f##attr_name = attr_default; \
206 public: \
207 const attr_type& get##attr_name() const { return f##attr_name; } \
208 _SVG_ATTR_SETTERS( \
209 attr_name, attr_type, attr_default, \
210 [this](const attr_type& a) { this->f##attr_name = a; }, \
211 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
212
213#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
214 private: \
215 SkTLazy<attr_type> f##attr_name; \
216 public: \
217 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
218 _SVG_ATTR_SETTERS( \
219 attr_name, attr_type, attr_default, \
220 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
221 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
222
223#endif // SkSVGNode_DEFINED
static sk_sp< SkImage > render(const SkPicture &p)
#define SK_API
Definition SkAPI.h:35
#define INHERITED(method,...)
SkSVGAttribute
SkSVGTag
Definition SkSVGNode.h:23
@ kFeDistantLight
@ kFeColorMatrix
@ kFeSpecularLighting
@ kFeDisplacementMap
@ kFeGaussianBlur
@ kLinearGradient
@ kFeDiffuseLighting
@ kRadialGradient
#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited)
Definition SkSVGNode.h:64
SkSVGDisplay
Definition SkSVGTypes.h:726
SkSVGLineCap
Definition SkSVGTypes.h:288
SkColor SkSVGColorType
Definition SkSVGTypes.h:25
SkSVGColorspace
Definition SkSVGTypes.h:719
SkScalar SkSVGNumberType
Definition SkSVGTypes.h:27
SkFilterMode
virtual SkRect onObjectBoundingBox(const SkSVGRenderContext &) const
Definition SkSVGNode.h:172
virtual void appendChild(sk_sp< SkSVGNode >)=0
virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue &)
Definition SkSVGNode.h:168
virtual bool hasChildren() const
Definition SkSVGNode.h:170
SkSVGTag tag() const
Definition SkSVGNode.h:99
virtual SkPath onAsPath(const SkSVGRenderContext &) const =0
const char * name
Definition fuchsia.cc:50
static constexpr SkRect MakeEmpty()
Definition SkRect.h:595