blob: 1ae4bf67d2d878c5387397763b71fc9041d58a81 [file] [log] [blame]
Florin Malitab3418102020-10-15 18:10:29 -04001/*
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
11#include "include/core/SkRefCnt.h"
12#include "modules/svg/include/SkSVGAttribute.h"
Tyler Denniston57154992020-11-04 16:08:30 -050013#include "modules/svg/include/SkSVGAttributeParser.h"
Florin Malitab3418102020-10-15 18:10:29 -040014
15class SkCanvas;
16class SkMatrix;
17class SkPaint;
18class SkPath;
Tyler Denniston53281c72020-10-22 15:54:24 -040019class SkSVGLengthContext;
Florin Malitab3418102020-10-15 18:10:29 -040020class SkSVGRenderContext;
21class SkSVGValue;
22
23enum class SkSVGTag {
24 kCircle,
25 kClipPath,
26 kDefs,
27 kEllipse,
Tyler Dennistona25e1a32021-01-15 12:38:29 -050028 kFeBlend,
Tyler Denniston70bb18d2020-11-06 12:07:53 -050029 kFeColorMatrix,
Tyler Dennistonb25caae2020-11-09 12:46:02 -050030 kFeComposite,
Tyler Dennistonf005c8a2021-01-25 12:56:13 -050031 kFeDisplacementMap,
Tyler Denniston32b30892021-01-26 14:36:32 -050032 kFeDistantLight,
Tyler Denniston8ed04432020-12-10 15:51:04 -050033 kFeFlood,
Tyler Denniston187d8112021-01-12 09:34:23 -050034 kFeGaussianBlur,
Tyler Denniston4c89481be2021-01-20 09:41:22 -050035 kFeMorphology,
Tyler Denniston5878ece2021-01-15 09:17:55 -050036 kFeOffset,
Tyler Denniston32b30892021-01-26 14:36:32 -050037 kFePointLight,
Tyler Denniston8eedcd22021-01-27 09:18:06 -050038 kFeSpecularLighting,
Tyler Denniston32b30892021-01-26 14:36:32 -050039 kFeSpotLight,
Tyler Dennistondada9602020-11-03 10:04:25 -050040 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040041 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040042 kG,
43 kLine,
44 kLinearGradient,
Florin Malita836c2ca2021-01-13 11:48:02 -050045 kMask,
Florin Malitab3418102020-10-15 18:10:29 -040046 kPath,
47 kPattern,
48 kPolygon,
49 kPolyline,
50 kRadialGradient,
51 kRect,
52 kStop,
53 kSvg,
54 kText,
Florin Malita512ff752020-12-06 11:50:52 -050055 kTextLiteral,
Florin Malitafc0ea0a2021-01-12 13:27:01 -050056 kTextPath,
Florin Malita512ff752020-12-06 11:50:52 -050057 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040058 kUse
59};
60
Florin Malita8c425672020-11-06 13:49:37 -050061#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
62private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050063 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
64 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050065 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
66 return pr.isValid(); \
67 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050068 \
Florin Malita8c425672020-11-06 13:49:37 -050069public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050070 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
71 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050072 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050073 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050074 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050075 if (!dest->isInheritable() || v.isValue()) { \
76 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
77 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050078 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050079 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050080 } \
81 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050082 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050083 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050084 if (!dest->isInheritable() || v.isValue()) { \
85 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
86 *dest = std::move(v); \
Florin Malita8c425672020-11-06 13:49:37 -050087 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050088 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050089 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040090 }
91
Florin Malitab3418102020-10-15 18:10:29 -040092class SkSVGNode : public SkRefCnt {
93public:
94 ~SkSVGNode() override;
95
96 SkSVGTag tag() const { return fTag; }
97
98 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
99
100 void render(const SkSVGRenderContext&) const;
101 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
102 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -0400103 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -0400104
105 void setAttribute(SkSVGAttribute, const SkSVGValue&);
106 bool setAttribute(const char* attributeName, const char* attributeValue);
107
Tyler Denniston57154992020-11-04 16:08:30 -0500108 // TODO: consolidate with existing setAttribute
109 virtual bool parseAndSetAttribute(const char* name, const char* value);
110
Florin Malita8c425672020-11-06 13:49:37 -0500111 // inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500112 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
113 SVG_PRES_ATTR(Color , SkSVGColorType , true)
Florin Malita73d57bf2021-01-15 08:58:09 -0500114 SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500115 SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true)
116 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
117 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
118 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
119 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
120 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
121 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
122 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
123 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
124 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
125 SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true)
126 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
127 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
128 SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true)
129 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
130 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
131 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
132 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500133
134 // not inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500135 SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
136 SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
137 SVG_PRES_ATTR(Filter , SkSVGFuncIRI , false)
138 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
139 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
140 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
141 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
142 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
Tyler Denniston32b30892021-01-26 14:36:32 -0500143 SVG_PRES_ATTR(LightingColor , SkSVGColor , false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400144
Florin Malitab3418102020-10-15 18:10:29 -0400145protected:
146 SkSVGNode(SkSVGTag);
147
148 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
149 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
150 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
151 // (return false).
152 // Implementations are expected to return true if rendering is to continue, or false if
153 // the node/subtree rendering is disabled.
154 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
155
156 virtual void onRender(const SkSVGRenderContext&) const = 0;
157
158 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
159
160 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
161
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500162 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400163
164 virtual bool hasChildren() const { return false; }
165
Tyler Dennistonf548a022020-10-27 15:02:02 -0400166 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400167 return SkRect::MakeEmpty();
168 }
169
Florin Malitab3418102020-10-15 18:10:29 -0400170private:
171 SkSVGTag fTag;
172
173 // FIXME: this should be sparse
174 SkSVGPresentationAttributes fPresentationAttributes;
175
176 using INHERITED = SkRefCnt;
177};
178
Florin Malita385e7442020-10-21 16:55:46 -0400179#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
180
Tyler Dennistona0a51462020-11-10 13:13:28 -0500181#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
182 private: \
183 bool set##attr_name( \
184 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
185 if (pr.isValid()) { this->set##attr_name(*pr); } \
186 return pr.isValid(); \
187 } \
188 bool set##attr_name( \
189 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
190 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
191 return pr.isValid(); \
192 } \
193 public: \
194 void set##attr_name(const attr_type& a) { set_cp(a); } \
195 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
196
Tyler Denniston57154992020-11-04 16:08:30 -0500197#define SVG_ATTR(attr_name, attr_type, attr_default) \
198 private: \
199 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500200 public: \
201 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500202 _SVG_ATTR_SETTERS( \
203 attr_name, attr_type, attr_default, \
204 [this](const attr_type& a) { this->f##attr_name = a; }, \
205 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
206
207#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
208 private: \
209 SkTLazy<attr_type> f##attr_name; \
210 public: \
211 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
212 _SVG_ATTR_SETTERS( \
213 attr_name, attr_type, attr_default, \
214 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
215 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400216
Florin Malitab3418102020-10-15 18:10:29 -0400217#endif // SkSVGNode_DEFINED