Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 1 | /* |
| 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 Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 13 | #include "modules/svg/include/SkSVGAttributeParser.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 14 | |
| 15 | class SkCanvas; |
| 16 | class SkMatrix; |
| 17 | class SkPaint; |
| 18 | class SkPath; |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 19 | class SkSVGLengthContext; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 20 | class SkSVGRenderContext; |
| 21 | class SkSVGValue; |
| 22 | |
| 23 | enum class SkSVGTag { |
| 24 | kCircle, |
| 25 | kClipPath, |
| 26 | kDefs, |
| 27 | kEllipse, |
Tyler Denniston | 70bb18d | 2020-11-06 12:07:53 -0500 | [diff] [blame] | 28 | kFeColorMatrix, |
Tyler Denniston | b25caae | 2020-11-09 12:46:02 -0500 | [diff] [blame] | 29 | kFeComposite, |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 30 | kFeTurbulence, |
Tyler Denniston | df208a3 | 2020-10-30 16:01:54 -0400 | [diff] [blame] | 31 | kFilter, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 32 | 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 Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 47 | #define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \ |
| 48 | private: \ |
| 49 | bool set##attr_name(SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \ |
| 50 | if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \ |
| 51 | return pr.isValid(); \ |
| 52 | } \ |
| 53 | public: \ |
| 54 | const attr_type* get##attr_name() const { \ |
| 55 | return fPresentationAttributes.f##attr_name.getMaybeNull(); \ |
| 56 | } \ |
| 57 | void set##attr_name(const attr_type& v) { \ |
| 58 | if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \ |
| 59 | fPresentationAttributes.f##attr_name.set(v); \ |
| 60 | } else { \ |
| 61 | /* kInherited values are semantically equivalent to \ |
| 62 | the absence of a local presentation attribute.*/ \ |
| 63 | fPresentationAttributes.f##attr_name.reset(); \ |
| 64 | } \ |
| 65 | } \ |
| 66 | void set##attr_name(attr_type&& v) { \ |
| 67 | if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \ |
| 68 | fPresentationAttributes.f##attr_name.set(std::move(v)); \ |
| 69 | } else { \ |
| 70 | /* kInherited values are semantically equivalent to \ |
| 71 | the absence of a local presentation attribute.*/ \ |
| 72 | fPresentationAttributes.f##attr_name.reset(); \ |
| 73 | } \ |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 76 | class SkSVGNode : public SkRefCnt { |
| 77 | public: |
| 78 | ~SkSVGNode() override; |
| 79 | |
| 80 | SkSVGTag tag() const { return fTag; } |
| 81 | |
| 82 | virtual void appendChild(sk_sp<SkSVGNode>) = 0; |
| 83 | |
| 84 | void render(const SkSVGRenderContext&) const; |
| 85 | bool asPaint(const SkSVGRenderContext&, SkPaint*) const; |
| 86 | SkPath asPath(const SkSVGRenderContext&) const; |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 87 | SkRect objectBoundingBox(const SkSVGRenderContext&) const; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 88 | |
| 89 | void setAttribute(SkSVGAttribute, const SkSVGValue&); |
| 90 | bool setAttribute(const char* attributeName, const char* attributeValue); |
| 91 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 92 | // TODO: consolidate with existing setAttribute |
| 93 | virtual bool parseAndSetAttribute(const char* name, const char* value); |
| 94 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 95 | void setColor(const SkSVGColorType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 96 | void setFillOpacity(const SkSVGNumberType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 97 | void setOpacity(const SkSVGNumberType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 98 | void setStrokeDashOffset(const SkSVGLength&); |
| 99 | void setStrokeOpacity(const SkSVGNumberType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 100 | void setStrokeMiterLimit(const SkSVGNumberType&); |
| 101 | void setStrokeWidth(const SkSVGLength&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 102 | |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 103 | // inherited |
| 104 | SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true) |
| 105 | SVG_PRES_ATTR(FillRule , SkSVGFillRule , true) |
| 106 | SVG_PRES_ATTR(Fill , SkSVGPaint , true) |
| 107 | SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true) |
| 108 | SVG_PRES_ATTR(FontSize , SkSVGFontSize , true) |
| 109 | SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true) |
| 110 | SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true) |
| 111 | SVG_PRES_ATTR(Stroke , SkSVGPaint , true) |
| 112 | SVG_PRES_ATTR(StrokeDashArray, SkSVGDashArray , true) |
| 113 | SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true) |
| 114 | SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true) |
| 115 | SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true) |
| 116 | SVG_PRES_ATTR(Visibility , SkSVGVisibility, true) |
| 117 | |
| 118 | // not inherited |
| 119 | SVG_PRES_ATTR(ClipPath , SkSVGClip , false) |
| 120 | SVG_PRES_ATTR(Filter , SkSVGFilterType, false) |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 121 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 122 | protected: |
| 123 | SkSVGNode(SkSVGTag); |
| 124 | |
| 125 | // Called before onRender(), to apply local attributes to the context. Unlike onRender(), |
| 126 | // onPrepareToRender() bubbles up the inheritance chain: overriders should always call |
| 127 | // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering |
| 128 | // (return false). |
| 129 | // Implementations are expected to return true if rendering is to continue, or false if |
| 130 | // the node/subtree rendering is disabled. |
| 131 | virtual bool onPrepareToRender(SkSVGRenderContext*) const; |
| 132 | |
| 133 | virtual void onRender(const SkSVGRenderContext&) const = 0; |
| 134 | |
| 135 | virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; } |
| 136 | |
| 137 | virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0; |
| 138 | |
| 139 | virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&); |
| 140 | |
| 141 | virtual bool hasChildren() const { return false; } |
| 142 | |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 143 | virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const { |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 144 | return SkRect::MakeEmpty(); |
| 145 | } |
| 146 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 147 | private: |
| 148 | SkSVGTag fTag; |
| 149 | |
| 150 | // FIXME: this should be sparse |
| 151 | SkSVGPresentationAttributes fPresentationAttributes; |
| 152 | |
| 153 | using INHERITED = SkRefCnt; |
| 154 | }; |
| 155 | |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 156 | #undef SVG_PRES_ATTR // presentation attributes are only defined for the base class |
| 157 | |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame^] | 158 | #define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \ |
| 159 | private: \ |
| 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 | void set##attr_name(const attr_type& a) { set_cp(a); } \ |
| 172 | void set##attr_name(attr_type&& a) { set_mv(std::move(a)); } |
| 173 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 174 | #define SVG_ATTR(attr_name, attr_type, attr_default) \ |
| 175 | private: \ |
| 176 | attr_type f##attr_name = attr_default; \ |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 177 | public: \ |
| 178 | const attr_type& get##attr_name() const { return f##attr_name; } \ |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame^] | 179 | _SVG_ATTR_SETTERS( \ |
| 180 | attr_name, attr_type, attr_default, \ |
| 181 | [this](const attr_type& a) { this->f##attr_name = a; }, \ |
| 182 | [this](attr_type&& a) { this->f##attr_name = std::move(a); }) |
| 183 | |
| 184 | #define SVG_OPTIONAL_ATTR(attr_name, attr_type) \ |
| 185 | private: \ |
| 186 | SkTLazy<attr_type> f##attr_name; \ |
| 187 | public: \ |
| 188 | const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \ |
| 189 | _SVG_ATTR_SETTERS( \ |
| 190 | attr_name, attr_type, attr_default, \ |
| 191 | [this](const attr_type& a) { this->f##attr_name.set(a); }, \ |
| 192 | [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); }) |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 193 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 194 | #endif // SkSVGNode_DEFINED |