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