blob: acf8f8f49d47349294f4228ecbbeb81cba4244a7 [file] [log] [blame]
fmalita6ceef3d2016-07-26 18:46:34 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkMatrix.h"
10#include "include/pathops/SkPathOps.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050011#include "include/private/SkTPin.h"
Florin Malitab3418102020-10-15 18:10:29 -040012#include "modules/svg/include/SkSVGNode.h"
13#include "modules/svg/include/SkSVGRenderContext.h"
14#include "modules/svg/include/SkSVGValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTLazy.h"
fmalita6ceef3d2016-07-26 18:46:34 -070016
Tyler Denniston04e03bc2020-12-09 14:16:25 -050017SkSVGNode::SkSVGNode(SkSVGTag t) : fTag(t) {
18 // Uninherited presentation attributes need a non-null default value.
19 fPresentationAttributes.fStopColor.set(SkSVGColor(SK_ColorBLACK));
20 fPresentationAttributes.fStopOpacity.set(SkSVGNumberType(1.0f));
21}
fmalita6ceef3d2016-07-26 18:46:34 -070022
23SkSVGNode::~SkSVGNode() { }
24
fmalita397a5172016-08-08 11:38:55 -070025void SkSVGNode::render(const SkSVGRenderContext& ctx) const {
Tyler Denniston53281c72020-10-22 15:54:24 -040026 SkSVGRenderContext localContext(ctx, this);
fmalita6ceef3d2016-07-26 18:46:34 -070027
fmalita397a5172016-08-08 11:38:55 -070028 if (this->onPrepareToRender(&localContext)) {
29 this->onRender(localContext);
fmalita6ceef3d2016-07-26 18:46:34 -070030 }
fmalita397a5172016-08-08 11:38:55 -070031}
fmalita6ceef3d2016-07-26 18:46:34 -070032
fmalita28d5b722016-09-12 17:06:47 -070033bool SkSVGNode::asPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
34 SkSVGRenderContext localContext(ctx);
35
36 return this->onPrepareToRender(&localContext) && this->onAsPaint(localContext, paint);
37}
38
Florin Malitace8840e2016-12-08 09:26:47 -050039SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const {
40 SkSVGRenderContext localContext(ctx);
Florin Malita7d529882016-12-08 16:04:24 -050041 if (!this->onPrepareToRender(&localContext)) {
42 return SkPath();
43 }
44
45 SkPath path = this->onAsPath(localContext);
46
47 if (const auto* clipPath = localContext.clipPath()) {
48 // There is a clip-path present on the current node.
49 Op(path, *clipPath, kIntersect_SkPathOp, &path);
50 }
51
52 return path;
Florin Malitace8840e2016-12-08 09:26:47 -050053}
54
Tyler Dennistonf548a022020-10-27 15:02:02 -040055SkRect SkSVGNode::objectBoundingBox(const SkSVGRenderContext& ctx) const {
56 return this->onObjectBoundingBox(ctx);
Tyler Denniston53281c72020-10-22 15:54:24 -040057}
58
fmalita397a5172016-08-08 11:38:55 -070059bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
fmalitabef51c22016-09-20 15:45:57 -070060 ctx->applyPresentationAttributes(fPresentationAttributes,
61 this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf);
Florin Malitaffe6ae42017-10-12 11:33:28 -040062
63 // visibility:hidden disables rendering
John Stilesa008b0f2020-08-16 08:48:02 -040064 const auto visibility = ctx->presentationContext().fInherited.fVisibility->type();
Florin Malitaffe6ae42017-10-12 11:33:28 -040065 return visibility != SkSVGVisibility::Type::kHidden;
fmalita6ceef3d2016-07-26 18:46:34 -070066}
67
Florin Malitaf4403e72020-04-10 14:14:04 +000068void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalita6ceef3d2016-07-26 18:46:34 -070069 this->onSetAttribute(attr, v);
70}
71
Florin Malita09294252020-04-09 08:54:29 -040072template <typename T>
73void SetInheritedByDefault(SkTLazy<T>& presentation_attribute, const T& value) {
74 if (value.type() != T::Type::kInherit) {
75 presentation_attribute.set(value);
76 } else {
77 // kInherited values are semantically equivalent to
78 // the absence of a local presentation attribute.
79 presentation_attribute.reset();
80 }
81}
82
Florin Malita8c425672020-11-06 13:49:37 -050083bool SkSVGNode::parseAndSetAttribute(const char* n, const char* v) {
Tyler Denniston79832e32020-11-18 09:34:07 -050084#define PARSE_AND_SET(svgName, attrName) \
85 this->set##attrName( \
86 SkSVGAttributeParser::parseProperty<decltype(fPresentationAttributes.f##attrName)>( \
87 svgName, n, v))
88
Tyler Denniston4c6f57a2020-11-30 15:31:32 -050089 return PARSE_AND_SET( "clip-path" , ClipPath)
90 || PARSE_AND_SET("clip-rule" , ClipRule)
91 || PARSE_AND_SET("color" , Color)
92 || PARSE_AND_SET("fill" , Fill)
93 || PARSE_AND_SET("fill-opacity" , FillOpacity)
94 || PARSE_AND_SET("fill-rule" , FillRule)
95 || PARSE_AND_SET("filter" , Filter)
96 || PARSE_AND_SET("font-family" , FontFamily)
97 || PARSE_AND_SET("font-size" , FontSize)
98 || PARSE_AND_SET("font-style" , FontStyle)
99 || PARSE_AND_SET("font-weight" , FontWeight)
100 || PARSE_AND_SET("opacity" , Opacity)
Tyler Denniston04e03bc2020-12-09 14:16:25 -0500101 || PARSE_AND_SET("stop-color" , StopColor)
102 || PARSE_AND_SET("stop-opacity" , StopOpacity)
Tyler Denniston4c6f57a2020-11-30 15:31:32 -0500103 || PARSE_AND_SET("stroke" , Stroke)
104 || PARSE_AND_SET("stroke-dasharray" , StrokeDashArray)
105 || PARSE_AND_SET("stroke-dashoffset", StrokeDashOffset)
106 || PARSE_AND_SET("stroke-linecap" , StrokeLineCap)
107 || PARSE_AND_SET("stroke-linejoin" , StrokeLineJoin)
108 || PARSE_AND_SET("stroke-miterlimit", StrokeMiterLimit)
109 || PARSE_AND_SET("stroke-opacity" , StrokeOpacity)
110 || PARSE_AND_SET("stroke-width" , StrokeWidth)
111 || PARSE_AND_SET("text-anchor" , TextAnchor)
112 || PARSE_AND_SET("visibility" , Visibility);
Tyler Denniston79832e32020-11-18 09:34:07 -0500113
114#undef PARSE_AND_SET
Tyler Denniston57154992020-11-04 16:08:30 -0500115}