blob: 9ca1b33b387451e8fa3546bd883a0869a0830849 [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 Denniston8cfe7182021-04-29 14:54:25 -040031 kFeDiffuseLighting,
Tyler Dennistonf005c8a2021-01-25 12:56:13 -050032 kFeDisplacementMap,
Tyler Denniston32b30892021-01-26 14:36:32 -050033 kFeDistantLight,
Tyler Denniston8ed04432020-12-10 15:51:04 -050034 kFeFlood,
Tyler Denniston187d8112021-01-12 09:34:23 -050035 kFeGaussianBlur,
Tyler Denniston95b2cf92021-05-05 09:22:43 -040036 kFeImage,
Tyler Denniston4c89481be2021-01-20 09:41:22 -050037 kFeMorphology,
Tyler Denniston5878ece2021-01-15 09:17:55 -050038 kFeOffset,
Tyler Denniston32b30892021-01-26 14:36:32 -050039 kFePointLight,
Tyler Denniston8eedcd22021-01-27 09:18:06 -050040 kFeSpecularLighting,
Tyler Denniston32b30892021-01-26 14:36:32 -050041 kFeSpotLight,
Tyler Dennistondada9602020-11-03 10:04:25 -050042 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040043 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040044 kG,
Tyler Denniston8ca46262021-02-02 16:16:21 -050045 kImage,
Florin Malitab3418102020-10-15 18:10:29 -040046 kLine,
47 kLinearGradient,
Florin Malita836c2ca2021-01-13 11:48:02 -050048 kMask,
Florin Malitab3418102020-10-15 18:10:29 -040049 kPath,
50 kPattern,
51 kPolygon,
52 kPolyline,
53 kRadialGradient,
54 kRect,
55 kStop,
56 kSvg,
57 kText,
Florin Malita512ff752020-12-06 11:50:52 -050058 kTextLiteral,
Florin Malitafc0ea0a2021-01-12 13:27:01 -050059 kTextPath,
Florin Malita512ff752020-12-06 11:50:52 -050060 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040061 kUse
62};
63
Florin Malita8c425672020-11-06 13:49:37 -050064#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
65private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050066 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
67 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050068 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
69 return pr.isValid(); \
70 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050071 \
Florin Malita8c425672020-11-06 13:49:37 -050072public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050073 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
74 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050075 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050076 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050077 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050078 if (!dest->isInheritable() || v.isValue()) { \
79 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
80 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050081 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050082 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050083 } \
84 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050085 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050086 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050087 if (!dest->isInheritable() || v.isValue()) { \
88 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
89 *dest = std::move(v); \
Florin Malita8c425672020-11-06 13:49:37 -050090 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050091 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050092 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040093 }
94
Florin Malitab3418102020-10-15 18:10:29 -040095class 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;
Tyler Dennistonf548a022020-10-27 15:02:02 -0400106 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -0400107
108 void setAttribute(SkSVGAttribute, const SkSVGValue&);
109 bool setAttribute(const char* attributeName, const char* attributeValue);
110
Tyler Denniston57154992020-11-04 16:08:30 -0500111 // TODO: consolidate with existing setAttribute
112 virtual bool parseAndSetAttribute(const char* name, const char* value);
113
Florin Malita8c425672020-11-06 13:49:37 -0500114 // inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500115 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
116 SVG_PRES_ATTR(Color , SkSVGColorType , true)
Florin Malita73d57bf2021-01-15 08:58:09 -0500117 SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500118 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)
122 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, 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)
Florin Malita8c425672020-11-06 13:49:37 -0500136
137 // not inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500138 SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
Tyler Denniston80ba6172021-05-03 13:12:21 -0400139 SVG_PRES_ATTR(Display , SkSVGDisplay , false)
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500140 SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
141 SVG_PRES_ATTR(Filter , 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)
Tyler Denniston32b30892021-01-26 14:36:32 -0500147 SVG_PRES_ATTR(LightingColor , SkSVGColor , false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400148
Florin Malitab3418102020-10-15 18:10:29 -0400149protected:
150 SkSVGNode(SkSVGTag);
151
Tyler Denniston1f4cd072021-02-05 09:08:33 -0500152 static SkMatrix ComputeViewboxMatrix(const SkRect&, const SkRect&, SkSVGPreserveAspectRatio);
153
Florin Malitab3418102020-10-15 18:10:29 -0400154 // 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
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500168 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400169
170 virtual bool hasChildren() const { return false; }
171
Tyler Dennistonf548a022020-10-27 15:02:02 -0400172 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400173 return SkRect::MakeEmpty();
174 }
175
Florin Malitab3418102020-10-15 18:10:29 -0400176private:
177 SkSVGTag fTag;
178
179 // FIXME: this should be sparse
180 SkSVGPresentationAttributes fPresentationAttributes;
181
182 using INHERITED = SkRefCnt;
183};
184
Florin Malita385e7442020-10-21 16:55:46 -0400185#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
186
Tyler Dennistona0a51462020-11-10 13:13:28 -0500187#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
Tyler Denniston57154992020-11-04 16:08:30 -0500203#define SVG_ATTR(attr_name, attr_type, attr_default) \
204 private: \
205 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500206 public: \
207 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500208 _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)); })
Florin Malita385e7442020-10-21 16:55:46 -0400222
Florin Malitab3418102020-10-15 18:10:29 -0400223#endif // SkSVGNode_DEFINED