blob: 71c161f08f87b7fccc3a192fcaff55994f2a38ca [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 Denniston8ed04432020-12-10 15:51:04 -050031 kFeFlood,
Tyler Denniston187d8112021-01-12 09:34:23 -050032 kFeGaussianBlur,
Tyler Denniston4c89481be2021-01-20 09:41:22 -050033 kFeMorphology,
Tyler Denniston5878ece2021-01-15 09:17:55 -050034 kFeOffset,
Tyler Dennistondada9602020-11-03 10:04:25 -050035 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040036 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040037 kG,
38 kLine,
39 kLinearGradient,
Florin Malita836c2ca2021-01-13 11:48:02 -050040 kMask,
Florin Malitab3418102020-10-15 18:10:29 -040041 kPath,
42 kPattern,
43 kPolygon,
44 kPolyline,
45 kRadialGradient,
46 kRect,
47 kStop,
48 kSvg,
49 kText,
Florin Malita512ff752020-12-06 11:50:52 -050050 kTextLiteral,
Florin Malitafc0ea0a2021-01-12 13:27:01 -050051 kTextPath,
Florin Malita512ff752020-12-06 11:50:52 -050052 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040053 kUse
54};
55
Florin Malita8c425672020-11-06 13:49:37 -050056#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
57private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050058 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
59 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050060 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
61 return pr.isValid(); \
62 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050063 \
Florin Malita8c425672020-11-06 13:49:37 -050064public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050065 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
66 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050067 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050068 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050069 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050070 if (!dest->isInheritable() || v.isValue()) { \
71 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
72 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050073 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050074 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050075 } \
76 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050077 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050078 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050079 if (!dest->isInheritable() || v.isValue()) { \
80 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
81 *dest = std::move(v); \
Florin Malita8c425672020-11-06 13:49:37 -050082 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050083 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050084 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040085 }
86
Florin Malitab3418102020-10-15 18:10:29 -040087class SkSVGNode : public SkRefCnt {
88public:
89 ~SkSVGNode() override;
90
91 SkSVGTag tag() const { return fTag; }
92
93 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
94
95 void render(const SkSVGRenderContext&) const;
96 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
97 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040098 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040099
100 void setAttribute(SkSVGAttribute, const SkSVGValue&);
101 bool setAttribute(const char* attributeName, const char* attributeValue);
102
Tyler Denniston57154992020-11-04 16:08:30 -0500103 // TODO: consolidate with existing setAttribute
104 virtual bool parseAndSetAttribute(const char* name, const char* value);
105
Florin Malita8c425672020-11-06 13:49:37 -0500106 // inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500107 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
108 SVG_PRES_ATTR(Color , SkSVGColorType , true)
Florin Malita73d57bf2021-01-15 08:58:09 -0500109 SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500110 SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true)
111 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
112 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
113 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
114 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
115 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
116 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
117 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
118 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
119 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
120 SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true)
121 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
122 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
123 SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true)
124 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
125 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
126 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
127 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500128
129 // not inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500130 SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
131 SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
132 SVG_PRES_ATTR(Filter , SkSVGFuncIRI , false)
133 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
134 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
135 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
136 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
137 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400138
Florin Malitab3418102020-10-15 18:10:29 -0400139protected:
140 SkSVGNode(SkSVGTag);
141
142 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
143 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
144 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
145 // (return false).
146 // Implementations are expected to return true if rendering is to continue, or false if
147 // the node/subtree rendering is disabled.
148 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
149
150 virtual void onRender(const SkSVGRenderContext&) const = 0;
151
152 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
153
154 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
155
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500156 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400157
158 virtual bool hasChildren() const { return false; }
159
Tyler Dennistonf548a022020-10-27 15:02:02 -0400160 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400161 return SkRect::MakeEmpty();
162 }
163
Florin Malitab3418102020-10-15 18:10:29 -0400164private:
165 SkSVGTag fTag;
166
167 // FIXME: this should be sparse
168 SkSVGPresentationAttributes fPresentationAttributes;
169
170 using INHERITED = SkRefCnt;
171};
172
Florin Malita385e7442020-10-21 16:55:46 -0400173#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
174
Tyler Dennistona0a51462020-11-10 13:13:28 -0500175#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
176 private: \
177 bool set##attr_name( \
178 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
179 if (pr.isValid()) { this->set##attr_name(*pr); } \
180 return pr.isValid(); \
181 } \
182 bool set##attr_name( \
183 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
184 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
185 return pr.isValid(); \
186 } \
187 public: \
188 void set##attr_name(const attr_type& a) { set_cp(a); } \
189 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
190
Tyler Denniston57154992020-11-04 16:08:30 -0500191#define SVG_ATTR(attr_name, attr_type, attr_default) \
192 private: \
193 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500194 public: \
195 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500196 _SVG_ATTR_SETTERS( \
197 attr_name, attr_type, attr_default, \
198 [this](const attr_type& a) { this->f##attr_name = a; }, \
199 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
200
201#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
202 private: \
203 SkTLazy<attr_type> f##attr_name; \
204 public: \
205 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
206 _SVG_ATTR_SETTERS( \
207 attr_name, attr_type, attr_default, \
208 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
209 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400210
Florin Malitab3418102020-10-15 18:10:29 -0400211#endif // SkSVGNode_DEFINED