blob: 1bb3797b6607572b01910971f28fc5795f05286a [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 Dennistondada9602020-11-03 10:04:25 -050031 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040032 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040033 kG,
34 kLine,
35 kLinearGradient,
36 kPath,
37 kPattern,
38 kPolygon,
39 kPolyline,
40 kRadialGradient,
41 kRect,
42 kStop,
43 kSvg,
44 kText,
Florin Malita512ff752020-12-06 11:50:52 -050045 kTextLiteral,
46 kTSpan,
Florin Malitab3418102020-10-15 18:10:29 -040047 kUse
48};
49
Florin Malita8c425672020-11-06 13:49:37 -050050#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
51private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050052 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
53 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050054 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
55 return pr.isValid(); \
56 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050057 \
Florin Malita8c425672020-11-06 13:49:37 -050058public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050059 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
60 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050061 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050062 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050063 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050064 if (!dest->isInheritable() || v.isValue()) { \
65 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
66 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050067 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050068 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050069 } \
70 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050071 void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050072 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050073 if (!dest->isInheritable() || v.isValue()) { \
74 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
75 *dest = std::move(v); \
Florin Malita8c425672020-11-06 13:49:37 -050076 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050077 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050078 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040079 }
80
Florin Malitab3418102020-10-15 18:10:29 -040081class SkSVGNode : public SkRefCnt {
82public:
83 ~SkSVGNode() override;
84
85 SkSVGTag tag() const { return fTag; }
86
87 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
88
89 void render(const SkSVGRenderContext&) const;
90 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
91 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040092 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040093
94 void setAttribute(SkSVGAttribute, const SkSVGValue&);
95 bool setAttribute(const char* attributeName, const char* attributeValue);
96
Tyler Denniston57154992020-11-04 16:08:30 -050097 // TODO: consolidate with existing setAttribute
98 virtual bool parseAndSetAttribute(const char* name, const char* value);
99
Florin Malita8c425672020-11-06 13:49:37 -0500100 // inherited
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500101 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
102 SVG_PRES_ATTR(Color , SkSVGColorType , true)
103 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
104 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
105 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
106 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
107 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
108 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
109 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
110 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
111 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
112 SVG_PRES_ATTR(StrokeDashOffset, SkSVGLength , true)
113 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
114 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
115 SVG_PRES_ATTR(StrokeMiterLimit, SkSVGNumberType, true)
116 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
117 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
118 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
119 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500120
121 // not inherited
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500122 SVG_PRES_ATTR(ClipPath , SkSVGClip , false)
123 SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
124 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
Tyler Denniston04e03bc2020-12-09 14:16:25 -0500125 SVG_PRES_ATTR(StopColor , SkSVGColor , false)
126 SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
Tyler Denniston8ed04432020-12-10 15:51:04 -0500127 SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
128 SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400129
Florin Malitab3418102020-10-15 18:10:29 -0400130protected:
131 SkSVGNode(SkSVGTag);
132
133 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
134 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
135 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
136 // (return false).
137 // Implementations are expected to return true if rendering is to continue, or false if
138 // the node/subtree rendering is disabled.
139 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
140
141 virtual void onRender(const SkSVGRenderContext&) const = 0;
142
143 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
144
145 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
146
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500147 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400148
149 virtual bool hasChildren() const { return false; }
150
Tyler Dennistonf548a022020-10-27 15:02:02 -0400151 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400152 return SkRect::MakeEmpty();
153 }
154
Florin Malitab3418102020-10-15 18:10:29 -0400155private:
156 SkSVGTag fTag;
157
158 // FIXME: this should be sparse
159 SkSVGPresentationAttributes fPresentationAttributes;
160
161 using INHERITED = SkRefCnt;
162};
163
Florin Malita385e7442020-10-21 16:55:46 -0400164#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
165
Tyler Dennistona0a51462020-11-10 13:13:28 -0500166#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
167 private: \
168 bool set##attr_name( \
169 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
170 if (pr.isValid()) { this->set##attr_name(*pr); } \
171 return pr.isValid(); \
172 } \
173 bool set##attr_name( \
174 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
175 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
176 return pr.isValid(); \
177 } \
178 public: \
179 void set##attr_name(const attr_type& a) { set_cp(a); } \
180 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
181
Tyler Denniston57154992020-11-04 16:08:30 -0500182#define SVG_ATTR(attr_name, attr_type, attr_default) \
183 private: \
184 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500185 public: \
186 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500187 _SVG_ATTR_SETTERS( \
188 attr_name, attr_type, attr_default, \
189 [this](const attr_type& a) { this->f##attr_name = a; }, \
190 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
191
192#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
193 private: \
194 SkTLazy<attr_type> f##attr_name; \
195 public: \
196 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
197 _SVG_ATTR_SETTERS( \
198 attr_name, attr_type, attr_default, \
199 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
200 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400201
Florin Malitab3418102020-10-15 18:10:29 -0400202#endif // SkSVGNode_DEFINED