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