blob: b2529a7f47a31277b1d953834400b9b5ca12e9d7 [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 Malita39fe8c82020-10-20 10:43:03 -040046#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
47 const attr_type* get##attr_name() const { \
48 return fPresentationAttributes.f##attr_name.getMaybeNull(); \
49 } \
50 void set##attr_name(const attr_type& v) { \
51 if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \
52 fPresentationAttributes.f##attr_name.set(v); \
53 } else { \
54 /* kInherited values are semantically equivalent to \
55 the absence of a local presentation attribute.*/ \
56 fPresentationAttributes.f##attr_name.reset(); \
57 } \
58 } \
59 void set##attr_name(attr_type&& v) { \
60 if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \
61 fPresentationAttributes.f##attr_name.set(std::move(v)); \
62 } else { \
63 /* kInherited values are semantically equivalent to \
64 the absence of a local presentation attribute.*/ \
65 fPresentationAttributes.f##attr_name.reset(); \
66 } \
67 }
68
Florin Malitab3418102020-10-15 18:10:29 -040069class SkSVGNode : public SkRefCnt {
70public:
71 ~SkSVGNode() override;
72
73 SkSVGTag tag() const { return fTag; }
74
75 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
76
77 void render(const SkSVGRenderContext&) const;
78 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
79 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040080 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040081
82 void setAttribute(SkSVGAttribute, const SkSVGValue&);
83 bool setAttribute(const char* attributeName, const char* attributeValue);
84
Tyler Denniston57154992020-11-04 16:08:30 -050085 // TODO: consolidate with existing setAttribute
86 virtual bool parseAndSetAttribute(const char* name, const char* value);
87
Florin Malitab3418102020-10-15 18:10:29 -040088 void setClipPath(const SkSVGClip&);
89 void setClipRule(const SkSVGFillRule&);
90 void setColor(const SkSVGColorType&);
91 void setFill(const SkSVGPaint&);
92 void setFillOpacity(const SkSVGNumberType&);
93 void setFillRule(const SkSVGFillRule&);
94 void setOpacity(const SkSVGNumberType&);
95 void setStroke(const SkSVGPaint&);
96 void setStrokeDashArray(const SkSVGDashArray&);
97 void setStrokeDashOffset(const SkSVGLength&);
98 void setStrokeOpacity(const SkSVGNumberType&);
99 void setStrokeLineCap(const SkSVGLineCap&);
100 void setStrokeLineJoin(const SkSVGLineJoin&);
101 void setStrokeMiterLimit(const SkSVGNumberType&);
102 void setStrokeWidth(const SkSVGLength&);
103 void setVisibility(const SkSVGVisibility&);
104
Florin Malita39fe8c82020-10-20 10:43:03 -0400105 SVG_PRES_ATTR(FontFamily, SkSVGFontFamily, true)
106 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
107 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
108 SVG_PRES_ATTR(FontWeight, SkSVGFontWeight, true)
Florin Malita056385b2020-10-27 22:57:56 -0400109 SVG_PRES_ATTR(TextAnchor, SkSVGTextAnchor, true)
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400110 SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400111
Florin Malitab3418102020-10-15 18:10:29 -0400112protected:
113 SkSVGNode(SkSVGTag);
114
115 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
116 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
117 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
118 // (return false).
119 // Implementations are expected to return true if rendering is to continue, or false if
120 // the node/subtree rendering is disabled.
121 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
122
123 virtual void onRender(const SkSVGRenderContext&) const = 0;
124
125 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
126
127 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
128
129 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&);
130
131 virtual bool hasChildren() const { return false; }
132
Tyler Dennistonf548a022020-10-27 15:02:02 -0400133 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400134 return SkRect::MakeEmpty();
135 }
136
Florin Malitab3418102020-10-15 18:10:29 -0400137private:
138 SkSVGTag fTag;
139
140 // FIXME: this should be sparse
141 SkSVGPresentationAttributes fPresentationAttributes;
142
143 using INHERITED = SkRefCnt;
144};
145
Florin Malita385e7442020-10-21 16:55:46 -0400146#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
147
Tyler Denniston57154992020-11-04 16:08:30 -0500148#define SVG_ATTR(attr_name, attr_type, attr_default) \
149 private: \
150 attr_type f##attr_name = attr_default; \
151 bool set##attr_name( \
152 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
153 if (pr.isValid()) { this->set##attr_name(*pr); } \
154 return pr.isValid(); \
155 } \
156 bool set##attr_name( \
157 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
158 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
159 return pr.isValid(); \
160 } \
161 public: \
162 const attr_type& get##attr_name() const { return f##attr_name; } \
163 void set##attr_name(const attr_type& a) { f##attr_name = a; } \
Florin Malita385e7442020-10-21 16:55:46 -0400164 void set##attr_name(attr_type&& a) { f##attr_name = std::move(a); }
165
Florin Malitab3418102020-10-15 18:10:29 -0400166#endif // SkSVGNode_DEFINED