blob: f7fc6c995db0b21960d7f8ce3b8202b3e7072222 [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 Denniston8ed04432020-12-10 15:51:04 -050032 kFeFlood,
Tyler Denniston187d8112021-01-12 09:34:23 -050033 kFeGaussianBlur,
Tyler Denniston4c89481be2021-01-20 09:41:22 -050034 kFeMorphology,
Tyler Denniston5878ece2021-01-15 09:17:55 -050035 kFeOffset,
Tyler Dennistondada9602020-11-03 10:04:25 -050036 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040037 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040038 kG,
39 kLine,
40 kLinearGradient,
Florin Malita836c2ca2021-01-13 11:48:02 -050041 kMask,
Florin Malitab3418102020-10-15 18:10:29 -040042 kPath,
43 kPattern,
44 kPolygon,
45 kPolyline,
46 kRadialGradient,
47 kRect,
48 kStop,
49 kSvg,
50 kText,
Florin Malita512ff752020-12-06 11:50:52 -050051 kTextLiteral,
Florin Malitafc0ea0a2021-01-12 13:27:01 -050052 kTextPath,
Florin Malita512ff752020-12-06 11:50:52 -050053 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040054 kUse
55};
56
Florin Malita8c425672020-11-06 13:49:37 -050057#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
58private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050059 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
60 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050061 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
62 return pr.isValid(); \
63 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050064 \
Florin Malita8c425672020-11-06 13:49:37 -050065public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050066 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
67 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050068 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050069 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050070 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050071 if (!dest->isInheritable() || v.isValue()) { \
72 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
73 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050074 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050075 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050076 } \
77 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050078 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050079 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050080 if (!dest->isInheritable() || v.isValue()) { \
81 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
82 *dest = std::move(v); \
Florin Malita8c425672020-11-06 13:49:37 -050083 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050084 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050085 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040086 }
87
Florin Malitab3418102020-10-15 18:10:29 -040088class SkSVGNode : public SkRefCnt {
89public:
90 ~SkSVGNode() override;
91
92 SkSVGTag tag() const { return fTag; }
93
94 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
95
96 void render(const SkSVGRenderContext&) const;
97 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
98 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040099 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -0400100
101 void setAttribute(SkSVGAttribute, const SkSVGValue&);
102 bool setAttribute(const char* attributeName, const char* attributeValue);
103
Tyler Denniston57154992020-11-04 16:08:30 -0500104 // TODO: consolidate with existing setAttribute
105 virtual bool parseAndSetAttribute(const char* name, const char* value);
106
Florin Malita8c425672020-11-06 13:49:37 -0500107 // inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500108 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
109 SVG_PRES_ATTR(Color , SkSVGColorType , true)
Florin Malita73d57bf2021-01-15 08:58:09 -0500110 SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500111 SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true)
112 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
113 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
114 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
115 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
116 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
117 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
118 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
119 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
120 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
121 SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true)
122 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
123 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
124 SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true)
125 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
126 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
127 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
128 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500129
130 // not inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500131 SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
132 SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
133 SVG_PRES_ATTR(Filter , SkSVGFuncIRI , false)
134 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
135 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
136 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
137 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
138 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400139
Florin Malitab3418102020-10-15 18:10:29 -0400140protected:
141 SkSVGNode(SkSVGTag);
142
143 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
144 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
145 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
146 // (return false).
147 // Implementations are expected to return true if rendering is to continue, or false if
148 // the node/subtree rendering is disabled.
149 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
150
151 virtual void onRender(const SkSVGRenderContext&) const = 0;
152
153 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
154
155 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
156
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500157 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400158
159 virtual bool hasChildren() const { return false; }
160
Tyler Dennistonf548a022020-10-27 15:02:02 -0400161 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400162 return SkRect::MakeEmpty();
163 }
164
Florin Malitab3418102020-10-15 18:10:29 -0400165private:
166 SkSVGTag fTag;
167
168 // FIXME: this should be sparse
169 SkSVGPresentationAttributes fPresentationAttributes;
170
171 using INHERITED = SkRefCnt;
172};
173
Florin Malita385e7442020-10-21 16:55:46 -0400174#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
175
Tyler Dennistona0a51462020-11-10 13:13:28 -0500176#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
177 private: \
178 bool set##attr_name( \
179 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
180 if (pr.isValid()) { this->set##attr_name(*pr); } \
181 return pr.isValid(); \
182 } \
183 bool set##attr_name( \
184 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
185 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
186 return pr.isValid(); \
187 } \
188 public: \
189 void set##attr_name(const attr_type& a) { set_cp(a); } \
190 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
191
Tyler Denniston57154992020-11-04 16:08:30 -0500192#define SVG_ATTR(attr_name, attr_type, attr_default) \
193 private: \
194 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500195 public: \
196 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500197 _SVG_ATTR_SETTERS( \
198 attr_name, attr_type, attr_default, \
199 [this](const attr_type& a) { this->f##attr_name = a; }, \
200 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
201
202#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
203 private: \
204 SkTLazy<attr_type> f##attr_name; \
205 public: \
206 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
207 _SVG_ATTR_SETTERS( \
208 attr_name, attr_type, attr_default, \
209 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
210 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400211
Florin Malitab3418102020-10-15 18:10:29 -0400212#endif // SkSVGNode_DEFINED