blob: 73e051bddfc12b184b4b3da768789c810f7c7fcb [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 Dennistondada9602020-11-03 10:04:25 -050028 kFeTurbulence,
Tyler Dennistondf208a32020-10-30 16:01:54 -040029 kFilter,
Florin Malitab3418102020-10-15 18:10:29 -040030 kG,
31 kLine,
32 kLinearGradient,
33 kPath,
34 kPattern,
35 kPolygon,
36 kPolyline,
37 kRadialGradient,
38 kRect,
39 kStop,
40 kSvg,
41 kText,
42 kUse
43};
44
Florin Malita39fe8c82020-10-20 10:43:03 -040045#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
46 const attr_type* get##attr_name() const { \
47 return fPresentationAttributes.f##attr_name.getMaybeNull(); \
48 } \
49 void set##attr_name(const attr_type& v) { \
50 if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \
51 fPresentationAttributes.f##attr_name.set(v); \
52 } else { \
53 /* kInherited values are semantically equivalent to \
54 the absence of a local presentation attribute.*/ \
55 fPresentationAttributes.f##attr_name.reset(); \
56 } \
57 } \
58 void set##attr_name(attr_type&& v) { \
59 if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \
60 fPresentationAttributes.f##attr_name.set(std::move(v)); \
61 } else { \
62 /* kInherited values are semantically equivalent to \
63 the absence of a local presentation attribute.*/ \
64 fPresentationAttributes.f##attr_name.reset(); \
65 } \
66 }
67
Florin Malitab3418102020-10-15 18:10:29 -040068class SkSVGNode : public SkRefCnt {
69public:
70 ~SkSVGNode() override;
71
72 SkSVGTag tag() const { return fTag; }
73
74 virtual void appendChild(sk_sp<SkSVGNode>) = 0;
75
76 void render(const SkSVGRenderContext&) const;
77 bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
78 SkPath asPath(const SkSVGRenderContext&) const;
Tyler Dennistonf548a022020-10-27 15:02:02 -040079 SkRect objectBoundingBox(const SkSVGRenderContext&) const;
Florin Malitab3418102020-10-15 18:10:29 -040080
81 void setAttribute(SkSVGAttribute, const SkSVGValue&);
82 bool setAttribute(const char* attributeName, const char* attributeValue);
83
Tyler Denniston57154992020-11-04 16:08:30 -050084 // TODO: consolidate with existing setAttribute
85 virtual bool parseAndSetAttribute(const char* name, const char* value);
86
Florin Malitab3418102020-10-15 18:10:29 -040087 void setClipPath(const SkSVGClip&);
88 void setClipRule(const SkSVGFillRule&);
89 void setColor(const SkSVGColorType&);
90 void setFill(const SkSVGPaint&);
91 void setFillOpacity(const SkSVGNumberType&);
92 void setFillRule(const SkSVGFillRule&);
93 void setOpacity(const SkSVGNumberType&);
94 void setStroke(const SkSVGPaint&);
95 void setStrokeDashArray(const SkSVGDashArray&);
96 void setStrokeDashOffset(const SkSVGLength&);
97 void setStrokeOpacity(const SkSVGNumberType&);
98 void setStrokeLineCap(const SkSVGLineCap&);
99 void setStrokeLineJoin(const SkSVGLineJoin&);
100 void setStrokeMiterLimit(const SkSVGNumberType&);
101 void setStrokeWidth(const SkSVGLength&);
102 void setVisibility(const SkSVGVisibility&);
103
Florin Malita39fe8c82020-10-20 10:43:03 -0400104 SVG_PRES_ATTR(FontFamily, SkSVGFontFamily, true)
105 SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
106 SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
107 SVG_PRES_ATTR(FontWeight, SkSVGFontWeight, true)
Florin Malita056385b2020-10-27 22:57:56 -0400108 SVG_PRES_ATTR(TextAnchor, SkSVGTextAnchor, true)
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -0400109 SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
Florin Malita39fe8c82020-10-20 10:43:03 -0400110
Florin Malitab3418102020-10-15 18:10:29 -0400111protected:
112 SkSVGNode(SkSVGTag);
113
114 // Called before onRender(), to apply local attributes to the context. Unlike onRender(),
115 // onPrepareToRender() bubbles up the inheritance chain: overriders should always call
116 // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
117 // (return false).
118 // Implementations are expected to return true if rendering is to continue, or false if
119 // the node/subtree rendering is disabled.
120 virtual bool onPrepareToRender(SkSVGRenderContext*) const;
121
122 virtual void onRender(const SkSVGRenderContext&) const = 0;
123
124 virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
125
126 virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
127
128 virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&);
129
130 virtual bool hasChildren() const { return false; }
131
Tyler Dennistonf548a022020-10-27 15:02:02 -0400132 virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
Tyler Denniston53281c72020-10-22 15:54:24 -0400133 return SkRect::MakeEmpty();
134 }
135
Florin Malitab3418102020-10-15 18:10:29 -0400136private:
137 SkSVGTag fTag;
138
139 // FIXME: this should be sparse
140 SkSVGPresentationAttributes fPresentationAttributes;
141
142 using INHERITED = SkRefCnt;
143};
144
Florin Malita385e7442020-10-21 16:55:46 -0400145#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
146
Tyler Denniston57154992020-11-04 16:08:30 -0500147#define SVG_ATTR(attr_name, attr_type, attr_default) \
148 private: \
149 attr_type f##attr_name = attr_default; \
150 bool set##attr_name( \
151 const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
152 if (pr.isValid()) { this->set##attr_name(*pr); } \
153 return pr.isValid(); \
154 } \
155 bool set##attr_name( \
156 SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
157 if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
158 return pr.isValid(); \
159 } \
160 public: \
161 const attr_type& get##attr_name() const { return f##attr_name; } \
162 void set##attr_name(const attr_type& a) { f##attr_name = a; } \
Florin Malita385e7442020-10-21 16:55:46 -0400163 void set##attr_name(attr_type&& a) { f##attr_name = std::move(a); }
164
Florin Malitab3418102020-10-15 18:10:29 -0400165#endif // SkSVGNode_DEFINED