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 | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 29 | kFeTurbulence, |
Tyler Denniston | df208a3 | 2020-10-30 16:01:54 -0400 | [diff] [blame] | 30 | kFilter, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 31 | kG, |
| 32 | kLine, |
| 33 | kLinearGradient, |
| 34 | kPath, |
| 35 | kPattern, |
| 36 | kPolygon, |
| 37 | kPolyline, |
| 38 | kRadialGradient, |
| 39 | kRect, |
| 40 | kStop, |
| 41 | kSvg, |
| 42 | kText, |
| 43 | kUse |
| 44 | }; |
| 45 | |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame^] | 46 | #define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \ |
| 47 | private: \ |
| 48 | bool set##attr_name(SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \ |
| 49 | if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \ |
| 50 | return pr.isValid(); \ |
| 51 | } \ |
| 52 | public: \ |
| 53 | const attr_type* get##attr_name() const { \ |
| 54 | return fPresentationAttributes.f##attr_name.getMaybeNull(); \ |
| 55 | } \ |
| 56 | void set##attr_name(const attr_type& v) { \ |
| 57 | if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \ |
| 58 | fPresentationAttributes.f##attr_name.set(v); \ |
| 59 | } else { \ |
| 60 | /* kInherited values are semantically equivalent to \ |
| 61 | the absence of a local presentation attribute.*/ \ |
| 62 | fPresentationAttributes.f##attr_name.reset(); \ |
| 63 | } \ |
| 64 | } \ |
| 65 | void set##attr_name(attr_type&& v) { \ |
| 66 | if (!attr_inherited || v.type() != attr_type::Type::kInherit) { \ |
| 67 | fPresentationAttributes.f##attr_name.set(std::move(v)); \ |
| 68 | } else { \ |
| 69 | /* kInherited values are semantically equivalent to \ |
| 70 | the absence of a local presentation attribute.*/ \ |
| 71 | fPresentationAttributes.f##attr_name.reset(); \ |
| 72 | } \ |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 75 | class SkSVGNode : public SkRefCnt { |
| 76 | public: |
| 77 | ~SkSVGNode() override; |
| 78 | |
| 79 | SkSVGTag tag() const { return fTag; } |
| 80 | |
| 81 | virtual void appendChild(sk_sp<SkSVGNode>) = 0; |
| 82 | |
| 83 | void render(const SkSVGRenderContext&) const; |
| 84 | bool asPaint(const SkSVGRenderContext&, SkPaint*) const; |
| 85 | SkPath asPath(const SkSVGRenderContext&) const; |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 86 | SkRect objectBoundingBox(const SkSVGRenderContext&) const; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 87 | |
| 88 | void setAttribute(SkSVGAttribute, const SkSVGValue&); |
| 89 | bool setAttribute(const char* attributeName, const char* attributeValue); |
| 90 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 91 | // TODO: consolidate with existing setAttribute |
| 92 | virtual bool parseAndSetAttribute(const char* name, const char* value); |
| 93 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 94 | void setColor(const SkSVGColorType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 95 | void setFillOpacity(const SkSVGNumberType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 96 | void setOpacity(const SkSVGNumberType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 97 | void setStrokeDashOffset(const SkSVGLength&); |
| 98 | void setStrokeOpacity(const SkSVGNumberType&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 99 | void setStrokeMiterLimit(const SkSVGNumberType&); |
| 100 | void setStrokeWidth(const SkSVGLength&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 101 | |
Florin Malita | 8c42567 | 2020-11-06 13:49:37 -0500 | [diff] [blame^] | 102 | // inherited |
| 103 | SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true) |
| 104 | SVG_PRES_ATTR(FillRule , SkSVGFillRule , true) |
| 105 | SVG_PRES_ATTR(Fill , SkSVGPaint , 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(StrokeLineCap , SkSVGLineCap , true) |
| 113 | SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true) |
| 114 | SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true) |
| 115 | SVG_PRES_ATTR(Visibility , SkSVGVisibility, true) |
| 116 | |
| 117 | // not inherited |
| 118 | SVG_PRES_ATTR(ClipPath , SkSVGClip , false) |
| 119 | SVG_PRES_ATTR(Filter , SkSVGFilterType, false) |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 120 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 121 | protected: |
| 122 | SkSVGNode(SkSVGTag); |
| 123 | |
| 124 | // Called before onRender(), to apply local attributes to the context. Unlike onRender(), |
| 125 | // onPrepareToRender() bubbles up the inheritance chain: overriders should always call |
| 126 | // INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering |
| 127 | // (return false). |
| 128 | // Implementations are expected to return true if rendering is to continue, or false if |
| 129 | // the node/subtree rendering is disabled. |
| 130 | virtual bool onPrepareToRender(SkSVGRenderContext*) const; |
| 131 | |
| 132 | virtual void onRender(const SkSVGRenderContext&) const = 0; |
| 133 | |
| 134 | virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; } |
| 135 | |
| 136 | virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0; |
| 137 | |
| 138 | virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&); |
| 139 | |
| 140 | virtual bool hasChildren() const { return false; } |
| 141 | |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 142 | virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const { |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 143 | return SkRect::MakeEmpty(); |
| 144 | } |
| 145 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 146 | private: |
| 147 | SkSVGTag fTag; |
| 148 | |
| 149 | // FIXME: this should be sparse |
| 150 | SkSVGPresentationAttributes fPresentationAttributes; |
| 151 | |
| 152 | using INHERITED = SkRefCnt; |
| 153 | }; |
| 154 | |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 155 | #undef SVG_PRES_ATTR // presentation attributes are only defined for the base class |
| 156 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 157 | #define SVG_ATTR(attr_name, attr_type, attr_default) \ |
| 158 | private: \ |
| 159 | attr_type f##attr_name = attr_default; \ |
| 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 | const attr_type& get##attr_name() const { return f##attr_name; } \ |
| 172 | void set##attr_name(const attr_type& a) { f##attr_name = a; } \ |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 173 | void set##attr_name(attr_type&& a) { f##attr_name = std::move(a); } |
| 174 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 175 | #endif // SkSVGNode_DEFINED |