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