blob: 26a9dd807bd0b0a7f2f68f9d41d90111f3c2f5c9 [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 Dennistondada9602020-11-03 10:04:25 -050029 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040030 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040031 kG,
32 kLine,
33 kLinearGradient,
34 kPath,
35 kPattern,
36 kPolygon,
37 kPolyline,
38 kRadialGradient,
39 kRect,
40 kStop,
41 kSvg,
42 kText,
43 kUse
44};
45
Florin Malita8c425672020-11-06 13:49:37 -050046#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
47private: \
48 bool set##attr_name(SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
49 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
50 return pr.isValid(); \
51 } \
52public: \
53 const attr_type* get##attr_name() const { \
54 return fPresentationAttributes.f##attr_name.getMaybeNull(); \
55 } \
56 void set##attr_name(const attr_type& v) { \
57 if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \
58 fPresentationAttributes.f##attr_name.set(v); \
59 } else { \
60 /* kInherited values are semantically equivalent to \
61 the absence of a local presentation attribute.*/ \
62 fPresentationAttributes.f##attr_name.reset(); \
63 } \
64 } \
65 void set##attr_name(attr_type&& v) { \
66 if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \
67 fPresentationAttributes.f##attr_name.set(std::move(v)); \
68 } else { \
69 /* kInherited values are semantically equivalent to \
70 the absence of a local presentation attribute.*/ \
71 fPresentationAttributes.f##attr_name.reset(); \
72 } \
Florin Malita39fe8c82020-10-20 10:43:03 -040073 }
74
Florin Malitab3418102020-10-15 18:10:29 -040075class SkSVGNode : public SkRefCnt {
76public:
77 ~SkSVGNode() override;
78
79 SkSVGTag tag() const { return fTag; }
80
81 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
82
83 void render(const SkSVGRenderContext&) const;
84 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
85 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040086 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040087
88 void setAttribute(SkSVGAttribute, const SkSVGValue&);
89 bool setAttribute(const char* attributeName, const char* attributeValue);
90
Tyler Denniston57154992020-11-04 16:08:30 -050091 // TODO: consolidate with existing setAttribute
92 virtual bool parseAndSetAttribute(const char* name, const char* value);
93
Florin Malitab3418102020-10-15 18:10:29 -040094 void setColor(const SkSVGColorType&);
Florin Malitab3418102020-10-15 18:10:29 -040095 void setFillOpacity(const SkSVGNumberType&);
Florin Malitab3418102020-10-15 18:10:29 -040096 void setOpacity(const SkSVGNumberType&);
Florin Malitab3418102020-10-15 18:10:29 -040097 void setStrokeDashOffset(const SkSVGLength&);
98 void setStrokeOpacity(const SkSVGNumberType&);
Florin Malitab3418102020-10-15 18:10:29 -040099 void setStrokeMiterLimit(const SkSVGNumberType&);
100 void setStrokeWidth(const SkSVGLength&);
Florin Malitab3418102020-10-15 18:10:29 -0400101
Florin Malita8c425672020-11-06 13:49:37 -0500102 // inherited
103 SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
104 SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
105 SVG_PRES_ATTR(Fill , SkSVGPaint , 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(StrokeLineCap , SkSVGLineCap , true)
113 SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
114 SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
115 SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
116
117 // not inherited
118 SVG_PRES_ATTR(ClipPath , SkSVGClip , false)
119 SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400120
Florin Malitab3418102020-10-15 18:10:29 -0400121protected:
122 SkSVGNode(SkSVGTag);
123
124 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
125 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
126 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
127 // (return false).
128 // Implementations are expected to return true if rendering is to continue, or false if
129 // the node/subtree rendering is disabled.
130 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
131
132 virtual void onRender(const SkSVGRenderContext&) const = 0;
133
134 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
135
136 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
137
138 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&);
139
140 virtual bool hasChildren() const { return false; }
141
Tyler Dennistonf548a022020-10-27 15:02:02 -0400142 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400143 return SkRect::MakeEmpty();
144 }
145
Florin Malitab3418102020-10-15 18:10:29 -0400146private:
147 SkSVGTag fTag;
148
149 // FIXME: this should be sparse
150 SkSVGPresentationAttributes fPresentationAttributes;
151
152 using INHERITED = SkRefCnt;
153};
154
Florin Malita385e7442020-10-21 16:55:46 -0400155#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
156
Tyler Denniston57154992020-11-04 16:08:30 -0500157#define SVG_ATTR(attr_name, attr_type, attr_default) \
158 private: \
159 attr_type f##attr_name = attr_default; \
160 bool set##attr_name( \
161 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
162 if (pr.isValid()) { this->set##attr_name(*pr); } \
163 return pr.isValid(); \
164 } \
165 bool set##attr_name( \
166 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
167 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
168 return pr.isValid(); \
169 } \
170 public: \
171 const attr_type& get##attr_name() const { return f##attr_name; } \
172 void set##attr_name(const attr_type& a) { f##attr_name = a; } \
Florin Malita385e7442020-10-21 16:55:46 -0400173 void set##attr_name(attr_type&& a) { f##attr_name = std::move(a); }
174
Florin Malitab3418102020-10-15 18:10:29 -0400175#endif // SkSVGNode_DEFINED