blob: ac74ae27d238337d93c74e90ae00f1ba2dba76f3 [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 Dennistondada9602020-11-03 10:04:25 -050030 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040031 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040032 kG,
33 kLine,
34 kLinearGradient,
35 kPath,
36 kPattern,
37 kPolygon,
38 kPolyline,
39 kRadialGradient,
40 kRect,
41 kStop,
42 kSvg,
43 kText,
44 kUse
45};
46
Florin Malita8c425672020-11-06 13:49:37 -050047#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
48private: \
Tyler Denniston79832e32020-11-18 09:34:07 -050049 bool set##attr_name(SkSVGAttributeParser::ParseResult< \
50 SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
Florin Malita8c425672020-11-06 13:49:37 -050051 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
52 return pr.isValid(); \
53 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050054 \
Florin Malita8c425672020-11-06 13:49:37 -050055public: \
Tyler Denniston79832e32020-11-18 09:34:07 -050056 const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
57 return fPresentationAttributes.f##attr_name; \
Florin Malita8c425672020-11-06 13:49:37 -050058 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050059 void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050060 auto* dest = &fPresentationAttributes.f##attr_name; \
Tyler Denniston79832e32020-11-18 09:34:07 -050061 if (!dest->isInheritable() || v.isValue()) { \
62 /* TODO: If dest is not inheritable, handle v == "inherit" */ \
63 *dest = v; \
Florin Malita8c425672020-11-06 13:49:37 -050064 } else { \
Tyler Denniston75c38f92020-11-17 12:26:25 -050065 dest->set(SkSVGPropertyState::kInherit); \
Florin Malita8c425672020-11-06 13:49:37 -050066 } \
67 } \
Tyler Denniston79832e32020-11-18 09:34:07 -050068 void set##attr_name(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 = std::move(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 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040076 }
77
Florin Malitab3418102020-10-15 18:10:29 -040078class SkSVGNode : public SkRefCnt {
79public:
80 ~SkSVGNode() override;
81
82 SkSVGTag tag() const { return fTag; }
83
84 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
85
86 void render(const SkSVGRenderContext&) const;
87 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
88 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040089 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040090
91 void setAttribute(SkSVGAttribute, const SkSVGValue&);
92 bool setAttribute(const char* attributeName, const char* attributeValue);
93
Tyler Denniston57154992020-11-04 16:08:30 -050094 // TODO: consolidate with existing setAttribute
95 virtual bool parseAndSetAttribute(const char* name, const char* value);
96
Florin Malita8c425672020-11-06 13:49:37 -050097 // inherited
Tyler Denniston4c6f57a2020-11-30 15:31:32 -050098 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
99 SVG_PRES_ATTR(Color , SkSVGColorType , true)
100 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
101 SVG_PRES_ATTR(Fill , SkSVGPaint , true)
102 SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
103 SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
104 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
105 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
106 SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
107 SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
108 SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
109 SVG_PRES_ATTR(StrokeDashOffset, SkSVGLength , true)
110 SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
111 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
112 SVG_PRES_ATTR(StrokeMiterLimit, SkSVGNumberType, true)
113 SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
114 SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
115 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
116 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
Florin Malita8c425672020-11-06 13:49:37 -0500117
118 // not inherited
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500119 SVG_PRES_ATTR(ClipPath , SkSVGClip , false)
120 SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
121 SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400122
Florin Malitab3418102020-10-15 18:10:29 -0400123protected:
124 SkSVGNode(SkSVGTag);
125
126 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
127 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
128 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
129 // (return false).
130 // Implementations are expected to return true if rendering is to continue, or false if
131 // the node/subtree rendering is disabled.
132 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
133
134 virtual void onRender(const SkSVGRenderContext&) const = 0;
135
136 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
137
138 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
139
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500140 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
Florin Malitab3418102020-10-15 18:10:29 -0400141
142 virtual bool hasChildren() const { return false; }
143
Tyler Dennistonf548a022020-10-27 15:02:02 -0400144 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400145 return SkRect::MakeEmpty();
146 }
147
Florin Malitab3418102020-10-15 18:10:29 -0400148private:
149 SkSVGTag fTag;
150
151 // FIXME: this should be sparse
152 SkSVGPresentationAttributes fPresentationAttributes;
153
154 using INHERITED = SkRefCnt;
155};
156
Florin Malita385e7442020-10-21 16:55:46 -0400157#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
158
Tyler Dennistona0a51462020-11-10 13:13:28 -0500159#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
160 private: \
161 bool set##attr_name( \
162 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
163 if (pr.isValid()) { this->set##attr_name(*pr); } \
164 return pr.isValid(); \
165 } \
166 bool set##attr_name( \
167 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
168 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
169 return pr.isValid(); \
170 } \
171 public: \
172 void set##attr_name(const attr_type& a) { set_cp(a); } \
173 void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
174
Tyler Denniston57154992020-11-04 16:08:30 -0500175#define SVG_ATTR(attr_name, attr_type, attr_default) \
176 private: \
177 attr_type f##attr_name = attr_default; \
Tyler Denniston57154992020-11-04 16:08:30 -0500178 public: \
179 const attr_type& get##attr_name() const { return f##attr_name; } \
Tyler Dennistona0a51462020-11-10 13:13:28 -0500180 _SVG_ATTR_SETTERS( \
181 attr_name, attr_type, attr_default, \
182 [this](const attr_type& a) { this->f##attr_name = a; }, \
183 [this](attr_type&& a) { this->f##attr_name = std::move(a); })
184
185#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
186 private: \
187 SkTLazy<attr_type> f##attr_name; \
188 public: \
189 const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
190 _SVG_ATTR_SETTERS( \
191 attr_name, attr_type, attr_default, \
192 [this](const attr_type& a) { this->f##attr_name.set(a); }, \
193 [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
Florin Malita385e7442020-10-21 16:55:46 -0400194
Florin Malitab3418102020-10-15 18:10:29 -0400195#endif // SkSVGNode_DEFINED