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