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