blob: c4da7d5fb2ac485286b5b752df87b4facd6d5e9a [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,
Tyler Denniston8ca46262021-02-02 16:16:21 -050043 kImage,
Florin Malitab3418102020-10-15 18:10:29 -040044 kLine,
45 kLinearGradient,
Florin Malita836c2ca2021-01-13 11:48:02 -050046 kMask,
Florin Malitab3418102020-10-15 18:10:29 -040047 kPath,
48 kPattern,
49 kPolygon,
50 kPolyline,
51 kRadialGradient,
52 kRect,
53 kStop,
54 kSvg,
55 kText,
Florin Malita512ff752020-12-06 11:50:52 -050056 kTextLiteral,
Florin Malitafc0ea0a2021-01-12 13:27:01 -050057 kTextPath,
Florin Malita512ff752020-12-06 11:50:52 -050058 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040059 kUse
60};
61
Florin Malita8c425672020-11-06 13:49:37 -050062#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
63private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050064 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
65 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050066 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
67 return pr.isValid(); \
68 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050069 \
Florin Malita8c425672020-11-06 13:49:37 -050070public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050071 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
72 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050073 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050074 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050075 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050076 if (!dest->isInheritable() || v.isValue()) { \
77 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
78 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050079 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050080 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050081 } \
82 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050083 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050084 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050085 if (!dest->isInheritable() || v.isValue()) { \
86 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
87 *dest = std::move(v); \
Florin Malita8c425672020-11-06 13:49:37 -050088 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050089 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050090 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040091 }
92
Florin Malitab3418102020-10-15 18:10:29 -040093class SkSVGNode : public SkRefCnt {
94public:
95 ~SkSVGNode() override;
96
97 SkSVGTag tag() const { return fTag; }
98
99 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
100
101 void render(const SkSVGRenderContext&) const;
102 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
103 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -0400104 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -0400105
106 void setAttribute(SkSVGAttribute, const SkSVGValue&);
107 bool setAttribute(const char* attributeName, const char* attributeValue);
108
Tyler Denniston57154992020-11-04 16:08:30 -0500109 // TODO: consolidate with existing setAttribute
110 virtual bool parseAndSetAttribute(const char* name, const char* value);
111
Florin Malita8c425672020-11-06 13:49:37 -0500112 // inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500113 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
114 SVG_PRES_ATTR(Color , SkSVGColorType , true)
Florin Malita73d57bf2021-01-15 08:58:09 -0500115 SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500116 SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true)
117 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
118 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
119 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
120 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
121 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
122 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
123 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
124 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
125 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
126 SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true)
127 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
128 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
129 SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true)
130 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
131 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
132 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
133 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500134
135 // not inherited
Tyler Denniston7bb85db2021-01-13 12:08:04 -0500136 SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
137 SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
138 SVG_PRES_ATTR(Filter , SkSVGFuncIRI , false)
139 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
140 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
141 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
142 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
143 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
Tyler Denniston32b30892021-01-26 14:36:32 -0500144 SVG_PRES_ATTR(LightingColor , SkSVGColor , false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400145
Florin Malitab3418102020-10-15 18:10:29 -0400146protected:
147 SkSVGNode(SkSVGTag);
148
Tyler Denniston1f4cd072021-02-05 09:08:33 -0500149 static SkMatrix ComputeViewboxMatrix(const SkRect&, const SkRect&, SkSVGPreserveAspectRatio);
150
Florin Malitab3418102020-10-15 18:10:29 -0400151 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
152 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
153 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
154 // (return false).
155 // Implementations are expected to return true if rendering is to continue, or false if
156 // the node/subtree rendering is disabled.
157 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
158
159 virtual void onRender(const SkSVGRenderContext&) const = 0;
160
161 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
162
163 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
164
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500165 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400166
167 virtual bool hasChildren() const { return false; }
168
Tyler Dennistonf548a022020-10-27 15:02:02 -0400169 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400170 return SkRect::MakeEmpty();
171 }
172
Florin Malitab3418102020-10-15 18:10:29 -0400173private:
174 SkSVGTag fTag;
175
176 // FIXME: this should be sparse
177 SkSVGPresentationAttributes fPresentationAttributes;
178
179 using INHERITED = SkRefCnt;
180};
181
Florin Malita385e7442020-10-21 16:55:46 -0400182#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
183
Tyler Dennistona0a51462020-11-10 13:13:28 -0500184#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
185 private: \
186 bool set##attr_name( \
187 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
188 if (pr.isValid()) { this->set##attr_name(*pr); } \
189 return pr.isValid(); \
190 } \
191 bool set##attr_name( \
192 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
193 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
194 return pr.isValid(); \
195 } \
196 public: \
197 void set##attr_name(const attr_type& a) { set_cp(a); } \
198 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
199
Tyler Denniston57154992020-11-04 16:08:30 -0500200#define SVG_ATTR(attr_name, attr_type, attr_default) \
201 private: \
202 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500203 public: \
204 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500205 _SVG_ATTR_SETTERS( \
206 attr_name, attr_type, attr_default, \
207 [this](const attr_type& a) { this->f##attr_name = a; }, \
208 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
209
210#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
211 private: \
212 SkTLazy<attr_type> f##attr_name; \
213 public: \
214 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
215 _SVG_ATTR_SETTERS( \
216 attr_name, attr_type, attr_default, \
217 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
218 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400219
Florin Malitab3418102020-10-15 18:10:29 -0400220#endif // SkSVGNode_DEFINED