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 | 8ed0443 | 2020-12-10 15:51:04 -0500 | [diff] [blame] | 30 | kFeFlood, |
Tyler Denniston | 187d811 | 2021-01-12 09:34:23 -0500 | [diff] [blame] | 31 | kFeGaussianBlur, |
Tyler Denniston | 5878ece | 2021-01-15 09:17:55 -0500 | [diff] [blame^] | 32 | kFeOffset, |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 33 | kFeTurbulence, |
Tyler Denniston | df208a3 | 2020-10-30 16:01:54 -0400 | [diff] [blame] | 34 | kFilter, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 35 | kG, |
| 36 | kLine, |
| 37 | kLinearGradient, |
Florin Malita | 836c2ca | 2021-01-13 11:48:02 -0500 | [diff] [blame] | 38 | kMask, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 39 | kPath, |
| 40 | kPattern, |
| 41 | kPolygon, |
| 42 | kPolyline, |
| 43 | kRadialGradient, |
| 44 | kRect, |
| 45 | kStop, |
| 46 | kSvg, |
| 47 | kText, |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 48 | kTextLiteral, |
Florin Malita | fc0ea0a | 2021-01-12 13:27:01 -0500 | [diff] [blame] | 49 | kTextPath, |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 50 | kTSpan, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 51 | kUse |
| 52 | }; |
| 53 | |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 54 | #define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \ |
| 55 | private: \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 56 | bool set##attr_name(SkSVGAttributeParser::ParseResult< \ |
| 57 | SkSVGProperty<attr_type, attr_inherited>>&& pr) {\ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 58 | if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \ |
| 59 | return pr.isValid(); \ |
| 60 | } \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 61 | \ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 62 | public: \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 63 | const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \ |
| 64 | return fPresentationAttributes.f##attr_name; \ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 65 | } \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 66 | void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \ |
Tyler Denniston | 75c38f9 | 2020-11-17 12:26:25 -0500 | [diff] [blame] | 67 | auto* dest = &fPresentationAttributes.f##attr_name; \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 68 | if (!dest->isInheritable() || v.isValue()) { \ |
| 69 | /* TODO: If dest is not inheritable, handle v == "inherit" */ \ |
| 70 | *dest = v; \ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 71 | } else { \ |
Tyler Denniston | 75c38f9 | 2020-11-17 12:26:25 -0500 | [diff] [blame] | 72 | dest->set(SkSVGPropertyState::kInherit); \ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 73 | } \ |
| 74 | } \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 75 | void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \ |
Tyler Denniston | 75c38f9 | 2020-11-17 12:26:25 -0500 | [diff] [blame] | 76 | auto* dest = &fPresentationAttributes.f##attr_name; \ |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 77 | if (!dest->isInheritable() || v.isValue()) { \ |
| 78 | /* TODO: If dest is not inheritable, handle v == "inherit" */ \ |
| 79 | *dest = std::move(v); \ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 80 | } else { \ |
Tyler Denniston | 75c38f9 | 2020-11-17 12:26:25 -0500 | [diff] [blame] | 81 | dest->set(SkSVGPropertyState::kInherit); \ |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 82 | } \ |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 85 | class SkSVGNode : public SkRefCnt { |
| 86 | public: |
| 87 | ~SkSVGNode() override; |
| 88 | |
| 89 | SkSVGTag tag() const { return fTag; } |
| 90 | |
| 91 | virtual void appendChild(sk_sp<SkSVGNode>) = 0; |
| 92 | |
| 93 | void render(const SkSVGRenderContext&) const; |
| 94 | bool asPaint(const SkSVGRenderContext&, SkPaint*) const; |
| 95 | SkPath asPath(const SkSVGRenderContext&) const; |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 96 | SkRect objectBoundingBox(const SkSVGRenderContext&) const; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 97 | |
| 98 | void setAttribute(SkSVGAttribute, const SkSVGValue&); |
| 99 | bool setAttribute(const char* attributeName, const char* attributeValue); |
| 100 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 101 | // TODO: consolidate with existing setAttribute |
| 102 | virtual bool parseAndSetAttribute(const char* name, const char* value); |
| 103 | |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 104 | // inherited |
Tyler Denniston | 7bb85db | 2021-01-13 12:08:04 -0500 | [diff] [blame] | 105 | SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true) |
| 106 | SVG_PRES_ATTR(Color , SkSVGColorType , true) |
| 107 | SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true) |
| 108 | SVG_PRES_ATTR(FillRule , SkSVGFillRule , true) |
| 109 | SVG_PRES_ATTR(Fill , SkSVGPaint , true) |
| 110 | SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true) |
| 111 | SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true) |
| 112 | SVG_PRES_ATTR(FontSize , SkSVGFontSize , true) |
| 113 | SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true) |
| 114 | SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true) |
| 115 | SVG_PRES_ATTR(Stroke , SkSVGPaint , true) |
| 116 | SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true) |
| 117 | SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true) |
| 118 | SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true) |
| 119 | SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true) |
| 120 | SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true) |
| 121 | SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true) |
| 122 | SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true) |
| 123 | SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true) |
| 124 | SVG_PRES_ATTR(Visibility , SkSVGVisibility, true) |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame] | 125 | |
| 126 | // not inherited |
Tyler Denniston | 7bb85db | 2021-01-13 12:08:04 -0500 | [diff] [blame] | 127 | SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false) |
| 128 | SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false) |
| 129 | SVG_PRES_ATTR(Filter , SkSVGFuncIRI , false) |
| 130 | SVG_PRES_ATTR(Opacity , SkSVGNumberType, false) |
| 131 | SVG_PRES_ATTR(StopColor , SkSVGColor , false) |
| 132 | SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false) |
| 133 | SVG_PRES_ATTR(FloodColor , SkSVGColor , false) |
| 134 | SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false) |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 135 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 136 | protected: |
| 137 | SkSVGNode(SkSVGTag); |
| 138 | |
| 139 | // Called before onRender(), to apply local attributes to the context. Unlike onRender(), |
| 140 | // onPrepareToRender() bubbles up the inheritance chain: overriders should always call |
| 141 | // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering |
| 142 | // (return false). |
| 143 | // Implementations are expected to return true if rendering is to continue, or false if |
| 144 | // the node/subtree rendering is disabled. |
| 145 | virtual bool onPrepareToRender(SkSVGRenderContext*) const; |
| 146 | |
| 147 | virtual void onRender(const SkSVGRenderContext&) const = 0; |
| 148 | |
| 149 | virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; } |
| 150 | |
| 151 | virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0; |
| 152 | |
Tyler Denniston | 4c6f57a | 2020-11-30 15:31:32 -0500 | [diff] [blame] | 153 | virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {} |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 154 | |
| 155 | virtual bool hasChildren() const { return false; } |
| 156 | |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 157 | virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const { |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 158 | return SkRect::MakeEmpty(); |
| 159 | } |
| 160 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 161 | private: |
| 162 | SkSVGTag fTag; |
| 163 | |
| 164 | // FIXME: this should be sparse |
| 165 | SkSVGPresentationAttributes fPresentationAttributes; |
| 166 | |
| 167 | using INHERITED = SkRefCnt; |
| 168 | }; |
| 169 | |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 170 | #undef SVG_PRES_ATTR // presentation attributes are only defined for the base class |
| 171 | |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 172 | #define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \ |
| 173 | private: \ |
| 174 | bool set##attr_name( \ |
| 175 | const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \ |
| 176 | if (pr.isValid()) { this->set##attr_name(*pr); } \ |
| 177 | return pr.isValid(); \ |
| 178 | } \ |
| 179 | bool set##attr_name( \ |
| 180 | SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \ |
| 181 | if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \ |
| 182 | return pr.isValid(); \ |
| 183 | } \ |
| 184 | public: \ |
| 185 | void set##attr_name(const attr_type& a) { set_cp(a); } \ |
| 186 | void set##attr_name(attr_type&& a) { set_mv(std::move(a)); } |
| 187 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 188 | #define SVG_ATTR(attr_name, attr_type, attr_default) \ |
| 189 | private: \ |
| 190 | attr_type f##attr_name = attr_default; \ |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 191 | public: \ |
| 192 | const attr_type& get##attr_name() const { return f##attr_name; } \ |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 193 | _SVG_ATTR_SETTERS( \ |
| 194 | attr_name, attr_type, attr_default, \ |
| 195 | [this](const attr_type& a) { this->f##attr_name = a; }, \ |
| 196 | [this](attr_type&& a) { this->f##attr_name = std::move(a); }) |
| 197 | |
| 198 | #define SVG_OPTIONAL_ATTR(attr_name, attr_type) \ |
| 199 | private: \ |
| 200 | SkTLazy<attr_type> f##attr_name; \ |
| 201 | public: \ |
| 202 | const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \ |
| 203 | _SVG_ATTR_SETTERS( \ |
| 204 | attr_name, attr_type, attr_default, \ |
| 205 | [this](const attr_type& a) { this->f##attr_name.set(a); }, \ |
| 206 | [this](attr_type&& a) { this->f##attr_name.set(std::move(a)); }) |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 207 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 208 | #endif // SkSVGNode_DEFINED |