blob: b8de10516e36a3ada6488748e4043b26ac89afbb [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 Denniston70bb18d2020-11-06 12:07:53 -050028 kFeColorMatrix,
Tyler Dennistonb25caae2020-11-09 12:46:02 -050029 kFeComposite,
Tyler Denniston8ed04432020-12-10 15:51:04 -050030 kFeFlood,
Tyler Denniston187d8112021-01-12 09:34:23 -050031 kFeGaussianBlur,
Tyler Dennistondada9602020-11-03 10:04:25 -050032 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040033 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040034 kG,
35 kLine,
36 kLinearGradient,
37 kPath,
38 kPattern,
39 kPolygon,
40 kPolyline,
41 kRadialGradient,
42 kRect,
43 kStop,
44 kSvg,
45 kText,
Florin Malita512ff752020-12-06 11:50:52 -050046 kTextLiteral,
Florin Malitafc0ea0a2021-01-12 13:27:01 -050047 kTextPath,
Florin Malita512ff752020-12-06 11:50:52 -050048 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040049 kUse
50};
51
Florin Malita8c425672020-11-06 13:49:37 -050052#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
53private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050054 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
55 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050056 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
57 return pr.isValid(); \
58 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050059 \
Florin Malita8c425672020-11-06 13:49:37 -050060public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050061 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
62 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050063 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050064 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050065 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050066 if (!dest->isInheritable() || v.isValue()) { \
67 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
68 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050069 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050070 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050071 } \
72 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050073 void set##attr_name(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 = std::move(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 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040081 }
82
Florin Malitab3418102020-10-15 18:10:29 -040083class SkSVGNode : public SkRefCnt {
84public:
85 ~SkSVGNode() override;
86
87 SkSVGTag tag() const { return fTag; }
88
89 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
90
91 void render(const SkSVGRenderContext&) const;
92 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
93 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040094 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040095
96 void setAttribute(SkSVGAttribute, const SkSVGValue&);
97 bool setAttribute(const char* attributeName, const char* attributeValue);
98
Tyler Denniston57154992020-11-04 16:08:30 -050099 // TODO: consolidate with existing setAttribute
100 virtual bool parseAndSetAttribute(const char* name, const char* value);
101
Florin Malita8c425672020-11-06 13:49:37 -0500102 // inherited
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500103 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
104 SVG_PRES_ATTR(Color , SkSVGColorType , true)
105 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
106 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
107 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
108 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
109 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
110 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
111 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
112 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
113 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
114 SVG_PRES_ATTR(StrokeDashOffset, SkSVGLength , true)
115 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
116 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
117 SVG_PRES_ATTR(StrokeMiterLimit, SkSVGNumberType, true)
118 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
119 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
120 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
121 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500122
123 // not inherited
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500124 SVG_PRES_ATTR(ClipPath , SkSVGClip , false)
125 SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
126 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
Tyler Denniston04e03bc2020-12-09 14:16:25 -0500127 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
128 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
Tyler Denniston8ed04432020-12-10 15:51:04 -0500129 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
130 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400131
Florin Malitab3418102020-10-15 18:10:29 -0400132protected:
133 SkSVGNode(SkSVGTag);
134
135 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
136 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
137 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
138 // (return false).
139 // Implementations are expected to return true if rendering is to continue, or false if
140 // the node/subtree rendering is disabled.
141 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
142
143 virtual void onRender(const SkSVGRenderContext&) const = 0;
144
145 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
146
147 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
148
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500149 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400150
151 virtual bool hasChildren() const { return false; }
152
Tyler Dennistonf548a022020-10-27 15:02:02 -0400153 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400154 return SkRect::MakeEmpty();
155 }
156
Florin Malitab3418102020-10-15 18:10:29 -0400157private:
158 SkSVGTag fTag;
159
160 // FIXME: this should be sparse
161 SkSVGPresentationAttributes fPresentationAttributes;
162
163 using INHERITED = SkRefCnt;
164};
165
Florin Malita385e7442020-10-21 16:55:46 -0400166#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
167
Tyler Dennistona0a51462020-11-10 13:13:28 -0500168#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
169 private: \
170 bool set##attr_name( \
171 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
172 if (pr.isValid()) { this->set##attr_name(*pr); } \
173 return pr.isValid(); \
174 } \
175 bool set##attr_name( \
176 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
177 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
178 return pr.isValid(); \
179 } \
180 public: \
181 void set##attr_name(const attr_type& a) { set_cp(a); } \
182 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
183
Tyler Denniston57154992020-11-04 16:08:30 -0500184#define SVG_ATTR(attr_name, attr_type, attr_default) \
185 private: \
186 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500187 public: \
188 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500189 _SVG_ATTR_SETTERS( \
190 attr_name, attr_type, attr_default, \
191 [this](const attr_type& a) { this->f##attr_name = a; }, \
192 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
193
194#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
195 private: \
196 SkTLazy<attr_type> f##attr_name; \
197 public: \
198 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
199 _SVG_ATTR_SETTERS( \
200 attr_name, attr_type, attr_default, \
201 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
202 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400203
Florin Malitab3418102020-10-15 18:10:29 -0400204#endif // SkSVGNode_DEFINED