2020-10-15 22:10:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSVGNode_DEFINED
|
|
|
|
#define SkSVGNode_DEFINED
|
|
|
|
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "modules/svg/include/SkSVGAttribute.h"
|
2020-11-04 21:08:30 +00:00
|
|
|
#include "modules/svg/include/SkSVGAttributeParser.h"
|
2020-10-15 22:10:29 +00:00
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
class SkMatrix;
|
|
|
|
class SkPaint;
|
|
|
|
class SkPath;
|
2020-10-22 19:54:24 +00:00
|
|
|
class SkSVGLengthContext;
|
2020-10-15 22:10:29 +00:00
|
|
|
class SkSVGRenderContext;
|
|
|
|
class SkSVGValue;
|
|
|
|
|
|
|
|
enum class SkSVGTag {
|
|
|
|
kCircle,
|
|
|
|
kClipPath,
|
|
|
|
kDefs,
|
|
|
|
kEllipse,
|
2021-01-15 17:38:29 +00:00
|
|
|
kFeBlend,
|
2020-11-06 17:07:53 +00:00
|
|
|
kFeColorMatrix,
|
2020-11-09 17:46:02 +00:00
|
|
|
kFeComposite,
|
2021-04-29 18:54:25 +00:00
|
|
|
kFeDiffuseLighting,
|
2021-01-25 17:56:13 +00:00
|
|
|
kFeDisplacementMap,
|
2021-01-26 19:36:32 +00:00
|
|
|
kFeDistantLight,
|
2020-12-10 20:51:04 +00:00
|
|
|
kFeFlood,
|
2021-01-12 14:34:23 +00:00
|
|
|
kFeGaussianBlur,
|
2021-01-20 14:41:22 +00:00
|
|
|
kFeMorphology,
|
2021-01-15 14:17:55 +00:00
|
|
|
kFeOffset,
|
2021-01-26 19:36:32 +00:00
|
|
|
kFePointLight,
|
2021-01-27 14:18:06 +00:00
|
|
|
kFeSpecularLighting,
|
2021-01-26 19:36:32 +00:00
|
|
|
kFeSpotLight,
|
2020-11-03 15:04:25 +00:00
|
|
|
kFeTurbulence,
|
2020-10-30 20:01:54 +00:00
|
|
|
kFilter,
|
2020-10-15 22:10:29 +00:00
|
|
|
kG,
|
2021-02-02 21:16:21 +00:00
|
|
|
kImage,
|
2020-10-15 22:10:29 +00:00
|
|
|
kLine,
|
|
|
|
kLinearGradient,
|
2021-01-13 16:48:02 +00:00
|
|
|
kMask,
|
2020-10-15 22:10:29 +00:00
|
|
|
kPath,
|
|
|
|
kPattern,
|
|
|
|
kPolygon,
|
|
|
|
kPolyline,
|
|
|
|
kRadialGradient,
|
|
|
|
kRect,
|
|
|
|
kStop,
|
|
|
|
kSvg,
|
|
|
|
kText,
|
2020-12-06 16:50:52 +00:00
|
|
|
kTextLiteral,
|
2021-01-12 18:27:01 +00:00
|
|
|
kTextPath,
|
2020-12-06 16:50:52 +00:00
|
|
|
kTSpan,
|
2020-10-15 22:10:29 +00:00
|
|
|
kUse
|
|
|
|
};
|
|
|
|
|
2020-11-06 18:49:37 +00:00
|
|
|
#define SVG_PRES_ATTR(attr_name, attr_type, attr_inherited) \
|
|
|
|
private: \
|
2020-11-18 14:34:07 +00:00
|
|
|
bool set##attr_name(SkSVGAttributeParser::ParseResult< \
|
|
|
|
SkSVGProperty<attr_type, attr_inherited>>&& pr) {\
|
2020-11-06 18:49:37 +00:00
|
|
|
if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
|
|
|
|
return pr.isValid(); \
|
|
|
|
} \
|
2020-11-18 14:34:07 +00:00
|
|
|
\
|
2020-11-06 18:49:37 +00:00
|
|
|
public: \
|
2020-11-18 14:34:07 +00:00
|
|
|
const SkSVGProperty<attr_type, attr_inherited>& get##attr_name() const { \
|
|
|
|
return fPresentationAttributes.f##attr_name; \
|
2020-11-06 18:49:37 +00:00
|
|
|
} \
|
2020-11-18 14:34:07 +00:00
|
|
|
void set##attr_name(const SkSVGProperty<attr_type, attr_inherited>& v) { \
|
2020-11-17 17:26:25 +00:00
|
|
|
auto* dest = &fPresentationAttributes.f##attr_name; \
|
2020-11-18 14:34:07 +00:00
|
|
|
if (!dest->isInheritable() || v.isValue()) { \
|
|
|
|
/* TODO: If dest is not inheritable, handle v == "inherit" */ \
|
|
|
|
*dest = v; \
|
2020-11-06 18:49:37 +00:00
|
|
|
} else { \
|
2020-11-17 17:26:25 +00:00
|
|
|
dest->set(SkSVGPropertyState::kInherit); \
|
2020-11-06 18:49:37 +00:00
|
|
|
} \
|
|
|
|
} \
|
2020-11-18 14:34:07 +00:00
|
|
|
void set##attr_name(SkSVGProperty<attr_type, attr_inherited>&& v) { \
|
2020-11-17 17:26:25 +00:00
|
|
|
auto* dest = &fPresentationAttributes.f##attr_name; \
|
2020-11-18 14:34:07 +00:00
|
|
|
if (!dest->isInheritable() || v.isValue()) { \
|
|
|
|
/* TODO: If dest is not inheritable, handle v == "inherit" */ \
|
|
|
|
*dest = std::move(v); \
|
2020-11-06 18:49:37 +00:00
|
|
|
} else { \
|
2020-11-17 17:26:25 +00:00
|
|
|
dest->set(SkSVGPropertyState::kInherit); \
|
2020-11-06 18:49:37 +00:00
|
|
|
} \
|
2020-10-20 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 22:10:29 +00:00
|
|
|
class SkSVGNode : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
~SkSVGNode() override;
|
|
|
|
|
|
|
|
SkSVGTag tag() const { return fTag; }
|
|
|
|
|
|
|
|
virtual void appendChild(sk_sp<SkSVGNode>) = 0;
|
|
|
|
|
|
|
|
void render(const SkSVGRenderContext&) const;
|
|
|
|
bool asPaint(const SkSVGRenderContext&, SkPaint*) const;
|
|
|
|
SkPath asPath(const SkSVGRenderContext&) const;
|
2020-10-27 19:02:02 +00:00
|
|
|
SkRect objectBoundingBox(const SkSVGRenderContext&) const;
|
2020-10-15 22:10:29 +00:00
|
|
|
|
|
|
|
void setAttribute(SkSVGAttribute, const SkSVGValue&);
|
|
|
|
bool setAttribute(const char* attributeName, const char* attributeValue);
|
|
|
|
|
2020-11-04 21:08:30 +00:00
|
|
|
// TODO: consolidate with existing setAttribute
|
|
|
|
virtual bool parseAndSetAttribute(const char* name, const char* value);
|
|
|
|
|
2020-11-06 18:49:37 +00:00
|
|
|
// inherited
|
2021-01-13 17:08:04 +00:00
|
|
|
SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
|
|
|
|
SVG_PRES_ATTR(Color , SkSVGColorType , true)
|
2021-01-15 13:58:09 +00:00
|
|
|
SVG_PRES_ATTR(ColorInterpolation , SkSVGColorspace, true)
|
2021-01-13 17:08:04 +00:00
|
|
|
SVG_PRES_ATTR(ColorInterpolationFilters, SkSVGColorspace, true)
|
|
|
|
SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
|
|
|
|
SVG_PRES_ATTR(Fill , SkSVGPaint , true)
|
|
|
|
SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
|
|
|
|
SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
|
|
|
|
SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
|
|
|
|
SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
|
|
|
|
SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
|
|
|
|
SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
|
|
|
|
SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
|
|
|
|
SVG_PRES_ATTR(StrokeDashOffset , SkSVGLength , true)
|
|
|
|
SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
|
|
|
|
SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
|
|
|
|
SVG_PRES_ATTR(StrokeMiterLimit , SkSVGNumberType, true)
|
|
|
|
SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
|
|
|
|
SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
|
|
|
|
SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
|
|
|
|
SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
|
2020-11-06 18:49:37 +00:00
|
|
|
|
|
|
|
// not inherited
|
2021-01-13 17:08:04 +00:00
|
|
|
SVG_PRES_ATTR(ClipPath , SkSVGFuncIRI , false)
|
|
|
|
SVG_PRES_ATTR(Mask , SkSVGFuncIRI , false)
|
|
|
|
SVG_PRES_ATTR(Filter , SkSVGFuncIRI , false)
|
|
|
|
SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
|
|
|
|
SVG_PRES_ATTR(StopColor , SkSVGColor , false)
|
|
|
|
SVG_PRES_ATTR(StopOpacity , SkSVGNumberType, false)
|
|
|
|
SVG_PRES_ATTR(FloodColor , SkSVGColor , false)
|
|
|
|
SVG_PRES_ATTR(FloodOpacity , SkSVGNumberType, false)
|
2021-01-26 19:36:32 +00:00
|
|
|
SVG_PRES_ATTR(LightingColor , SkSVGColor , false)
|
2020-10-20 14:43:03 +00:00
|
|
|
|
2020-10-15 22:10:29 +00:00
|
|
|
protected:
|
|
|
|
SkSVGNode(SkSVGTag);
|
|
|
|
|
2021-02-05 14:08:33 +00:00
|
|
|
static SkMatrix ComputeViewboxMatrix(const SkRect&, const SkRect&, SkSVGPreserveAspectRatio);
|
|
|
|
|
2020-10-15 22:10:29 +00:00
|
|
|
// Called before onRender(), to apply local attributes to the context. Unlike onRender(),
|
|
|
|
// onPrepareToRender() bubbles up the inheritance chain: overriders should always call
|
|
|
|
// INHERITED::onPrepareToRender(), unless they intend to short-circuit rendering
|
|
|
|
// (return false).
|
|
|
|
// Implementations are expected to return true if rendering is to continue, or false if
|
|
|
|
// the node/subtree rendering is disabled.
|
|
|
|
virtual bool onPrepareToRender(SkSVGRenderContext*) const;
|
|
|
|
|
|
|
|
virtual void onRender(const SkSVGRenderContext&) const = 0;
|
|
|
|
|
|
|
|
virtual bool onAsPaint(const SkSVGRenderContext&, SkPaint*) const { return false; }
|
|
|
|
|
|
|
|
virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
|
|
|
|
|
2020-11-30 20:31:32 +00:00
|
|
|
virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
|
2020-10-15 22:10:29 +00:00
|
|
|
|
|
|
|
virtual bool hasChildren() const { return false; }
|
|
|
|
|
2020-10-27 19:02:02 +00:00
|
|
|
virtual SkRect onObjectBoundingBox(const SkSVGRenderContext&) const {
|
2020-10-22 19:54:24 +00:00
|
|
|
return SkRect::MakeEmpty();
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:10:29 +00:00
|
|
|
private:
|
|
|
|
SkSVGTag fTag;
|
|
|
|
|
|
|
|
// FIXME: this should be sparse
|
|
|
|
SkSVGPresentationAttributes fPresentationAttributes;
|
|
|
|
|
|
|
|
using INHERITED = SkRefCnt;
|
|
|
|
};
|
|
|
|
|
2020-10-21 20:55:46 +00:00
|
|
|
#undef SVG_PRES_ATTR // presentation attributes are only defined for the base class
|
|
|
|
|
2020-11-10 18:13:28 +00:00
|
|
|
#define _SVG_ATTR_SETTERS(attr_name, attr_type, attr_default, set_cp, set_mv) \
|
|
|
|
private: \
|
|
|
|
bool set##attr_name( \
|
|
|
|
const SkSVGAttributeParser::ParseResult<attr_type>& pr) { \
|
|
|
|
if (pr.isValid()) { this->set##attr_name(*pr); } \
|
|
|
|
return pr.isValid(); \
|
|
|
|
} \
|
|
|
|
bool set##attr_name( \
|
|
|
|
SkSVGAttributeParser::ParseResult<attr_type>&& pr) { \
|
|
|
|
if (pr.isValid()) { this->set##attr_name(std::move(*pr)); } \
|
|
|
|
return pr.isValid(); \
|
|
|
|
} \
|
|
|
|
public: \
|
|
|
|
void set##attr_name(const attr_type& a) { set_cp(a); } \
|
|
|
|
void set##attr_name(attr_type&& a) { set_mv(std::move(a)); }
|
|
|
|
|
2020-11-04 21:08:30 +00:00
|
|
|
#define SVG_ATTR(attr_name, attr_type, attr_default) \
|
|
|
|
private: \
|
|
|
|
attr_type f##attr_name = attr_default; \
|
|
|
|
public: \
|
|
|
|
const attr_type& get##attr_name() const { return f##attr_name; } \
|
2020-11-10 18:13:28 +00:00
|
|
|
_SVG_ATTR_SETTERS( \
|
|
|
|
attr_name, attr_type, attr_default, \
|
|
|
|
[this](const attr_type& a) { this->f##attr_name = a; }, \
|
|
|
|
[this](attr_type&& a) { this->f##attr_name = std::move(a); })
|
|
|
|
|
|
|
|
#define SVG_OPTIONAL_ATTR(attr_name, attr_type) \
|
|
|
|
private: \
|
|
|
|
SkTLazy<attr_type> f##attr_name; \
|
|
|
|
public: \
|
|
|
|
const SkTLazy<attr_type>& get##attr_name() const { return f##attr_name; } \
|
|
|
|
_SVG_ATTR_SETTERS( \
|
|
|
|
attr_name, attr_type, attr_default, \
|
|
|
|
[this](const attr_type& a) { this->f##attr_name.set(a); }, \
|
|
|
|
[this](attr_type&& a) { this->f##attr_name.set(std::move(a)); })
|
2020-10-21 20:55:46 +00:00
|
|
|
|
2020-10-15 22:10:29 +00:00
|
|
|
#endif // SkSVGNode_DEFINED
|