blob: eeccd2e3daaac58fee682a53c6f54f0f12eaeb02 [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 "experimental/svg/model/SkSVGNode.h"
9#include "experimental/svg/model/SkSVGRenderContext.h"
Florin Malitaf4403e72020-04-10 14:14:04 +000010#include "experimental/svg/model/SkSVGValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "include/core/SkMatrix.h"
13#include "include/pathops/SkPathOps.h"
14#include "src/core/SkTLazy.h"
fmalita6ceef3d2016-07-26 18:46:34 -070015
16SkSVGNode::SkSVGNode(SkSVGTag t) : fTag(t) { }
17
18SkSVGNode::~SkSVGNode() { }
19
fmalita397a5172016-08-08 11:38:55 -070020void SkSVGNode::render(const SkSVGRenderContext& ctx) const {
21 SkSVGRenderContext localContext(ctx);
fmalita6ceef3d2016-07-26 18:46:34 -070022
fmalita397a5172016-08-08 11:38:55 -070023 if (this->onPrepareToRender(&localContext)) {
24 this->onRender(localContext);
fmalita6ceef3d2016-07-26 18:46:34 -070025 }
fmalita397a5172016-08-08 11:38:55 -070026}
fmalita6ceef3d2016-07-26 18:46:34 -070027
fmalita28d5b722016-09-12 17:06:47 -070028bool SkSVGNode::asPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
29 SkSVGRenderContext localContext(ctx);
30
31 return this->onPrepareToRender(&localContext) && this->onAsPaint(localContext, paint);
32}
33
Florin Malitace8840e2016-12-08 09:26:47 -050034SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const {
35 SkSVGRenderContext localContext(ctx);
Florin Malita7d529882016-12-08 16:04:24 -050036 if (!this->onPrepareToRender(&localContext)) {
37 return SkPath();
38 }
39
40 SkPath path = this->onAsPath(localContext);
41
42 if (const auto* clipPath = localContext.clipPath()) {
43 // There is a clip-path present on the current node.
44 Op(path, *clipPath, kIntersect_SkPathOp, &path);
45 }
46
47 return path;
Florin Malitace8840e2016-12-08 09:26:47 -050048}
49
fmalita397a5172016-08-08 11:38:55 -070050bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
fmalitabef51c22016-09-20 15:45:57 -070051 ctx->applyPresentationAttributes(fPresentationAttributes,
52 this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf);
Florin Malitaffe6ae42017-10-12 11:33:28 -040053
54 // visibility:hidden disables rendering
John Stilesa008b0f2020-08-16 08:48:02 -040055 const auto visibility = ctx->presentationContext().fInherited.fVisibility->type();
Florin Malitaffe6ae42017-10-12 11:33:28 -040056 return visibility != SkSVGVisibility::Type::kHidden;
fmalita6ceef3d2016-07-26 18:46:34 -070057}
58
Florin Malitaf4403e72020-04-10 14:14:04 +000059void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalita6ceef3d2016-07-26 18:46:34 -070060 this->onSetAttribute(attr, v);
61}
62
Florin Malitace8840e2016-12-08 09:26:47 -050063void SkSVGNode::setClipPath(const SkSVGClip& clip) {
64 fPresentationAttributes.fClipPath.set(clip);
65}
66
Florin Malita09294252020-04-09 08:54:29 -040067template <typename T>
68void SetInheritedByDefault(SkTLazy<T>& presentation_attribute, const T& value) {
69 if (value.type() != T::Type::kInherit) {
70 presentation_attribute.set(value);
71 } else {
72 // kInherited values are semantically equivalent to
73 // the absence of a local presentation attribute.
74 presentation_attribute.reset();
75 }
76}
77
Florin Malita57a0edf2017-10-10 11:22:08 -040078void SkSVGNode::setClipRule(const SkSVGFillRule& clipRule) {
Florin Malita09294252020-04-09 08:54:29 -040079 SetInheritedByDefault(fPresentationAttributes.fClipRule, clipRule);
Florin Malita57a0edf2017-10-10 11:22:08 -040080}
81
Tyler Denniston6c8314e2020-04-09 14:14:10 -040082void SkSVGNode::setColor(const SkSVGColorType& color) {
83 // TODO: Color should be inherited by default
84 fPresentationAttributes.fColor.set(color);
85}
86
fmalitaca39d712016-08-12 13:17:11 -070087void SkSVGNode::setFill(const SkSVGPaint& svgPaint) {
Florin Malita09294252020-04-09 08:54:29 -040088 SetInheritedByDefault(fPresentationAttributes.fFill, svgPaint);
fmalitaca39d712016-08-12 13:17:11 -070089}
90
91void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) {
Florin Malitaa3626692020-04-09 14:36:45 -040092 fPresentationAttributes.fFillOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
fmalitaca39d712016-08-12 13:17:11 -070093}
94
Florin Malitae932d4b2016-12-01 13:35:11 -050095void SkSVGNode::setFillRule(const SkSVGFillRule& fillRule) {
Florin Malita09294252020-04-09 08:54:29 -040096 SetInheritedByDefault(fPresentationAttributes.fFillRule, fillRule);
Florin Malitae932d4b2016-12-01 13:35:11 -050097}
98
fmalita6fb06482016-08-15 12:45:11 -070099void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) {
Florin Malitaa3626692020-04-09 14:36:45 -0400100 fPresentationAttributes.fOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
fmalita6fb06482016-08-15 12:45:11 -0700101}
102
fmalitaca39d712016-08-12 13:17:11 -0700103void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) {
Florin Malita09294252020-04-09 08:54:29 -0400104 SetInheritedByDefault(fPresentationAttributes.fStroke, svgPaint);
fmalitaca39d712016-08-12 13:17:11 -0700105}
106
Florin Malitaf543a602017-10-13 14:07:44 -0400107void SkSVGNode::setStrokeDashArray(const SkSVGDashArray& dashArray) {
Florin Malita09294252020-04-09 08:54:29 -0400108 SetInheritedByDefault(fPresentationAttributes.fStrokeDashArray, dashArray);
Florin Malitaf543a602017-10-13 14:07:44 -0400109}
110
Florin Malitae1dadd72017-10-13 18:18:32 -0400111void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) {
112 fPresentationAttributes.fStrokeDashOffset.set(dashOffset);
113}
114
fmalitaca39d712016-08-12 13:17:11 -0700115void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
Florin Malitaa3626692020-04-09 14:36:45 -0400116 fPresentationAttributes.fStrokeOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
fmalitaca39d712016-08-12 13:17:11 -0700117}
118
Florin Malita09294252020-04-09 08:54:29 -0400119void SkSVGNode::setStrokeLineCap(const SkSVGLineCap& lc) {
120 SetInheritedByDefault(fPresentationAttributes.fStrokeLineCap, lc);
121}
122
123void SkSVGNode::setStrokeLineJoin(const SkSVGLineJoin& lj) {
124 SetInheritedByDefault(fPresentationAttributes.fStrokeLineJoin, lj);
125}
126
127void SkSVGNode::setStrokeMiterLimit(const SkSVGNumberType& ml) {
128 fPresentationAttributes.fStrokeMiterLimit.set(ml);
129}
130
fmalitaca39d712016-08-12 13:17:11 -0700131void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) {
132 fPresentationAttributes.fStrokeWidth.set(strokeWidth);
133}
134
Florin Malitaffe6ae42017-10-12 11:33:28 -0400135void SkSVGNode::setVisibility(const SkSVGVisibility& visibility) {
Florin Malita09294252020-04-09 08:54:29 -0400136 SetInheritedByDefault(fPresentationAttributes.fVisibility, visibility);
Florin Malitaffe6ae42017-10-12 11:33:28 -0400137}
138
Florin Malitaf4403e72020-04-10 14:14:04 +0000139void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalita6ceef3d2016-07-26 18:46:34 -0700140 switch (attr) {
Florin Malitace8840e2016-12-08 09:26:47 -0500141 case SkSVGAttribute::kClipPath:
Florin Malitaf4403e72020-04-10 14:14:04 +0000142 if (const SkSVGClipValue* clip = v.as<SkSVGClipValue>()) {
Florin Malitace8840e2016-12-08 09:26:47 -0500143 this->setClipPath(*clip);
144 }
145 break;
Florin Malita57a0edf2017-10-10 11:22:08 -0400146 case SkSVGAttribute::kClipRule:
Florin Malitaf4403e72020-04-10 14:14:04 +0000147 if (const SkSVGFillRuleValue* clipRule = v.as<SkSVGFillRuleValue>()) {
Florin Malita57a0edf2017-10-10 11:22:08 -0400148 this->setClipRule(*clipRule);
149 }
150 break;
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400151 case SkSVGAttribute::kColor:
Florin Malitaf4403e72020-04-10 14:14:04 +0000152 if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) {
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400153 this->setColor(*color);
154 }
155 break;
fmalita58649cc2016-07-29 08:52:03 -0700156 case SkSVGAttribute::kFill:
Florin Malitaf4403e72020-04-10 14:14:04 +0000157 if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700158 this->setFill(*paint);
fmalita2d961e02016-08-11 09:16:29 -0700159 }
160 break;
161 case SkSVGAttribute::kFillOpacity:
Florin Malitaf4403e72020-04-10 14:14:04 +0000162 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700163 this->setFillOpacity(*opacity);
fmalita6ceef3d2016-07-26 18:46:34 -0700164 }
165 break;
Florin Malitae932d4b2016-12-01 13:35:11 -0500166 case SkSVGAttribute::kFillRule:
Florin Malitaf4403e72020-04-10 14:14:04 +0000167 if (const SkSVGFillRuleValue* fillRule = v.as<SkSVGFillRuleValue>()) {
Florin Malitae932d4b2016-12-01 13:35:11 -0500168 this->setFillRule(*fillRule);
169 }
170 break;
fmalita6fb06482016-08-15 12:45:11 -0700171 case SkSVGAttribute::kOpacity:
Florin Malitaf4403e72020-04-10 14:14:04 +0000172 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalita6fb06482016-08-15 12:45:11 -0700173 this->setOpacity(*opacity);
174 }
175 break;
fmalita58649cc2016-07-29 08:52:03 -0700176 case SkSVGAttribute::kStroke:
Florin Malitaf4403e72020-04-10 14:14:04 +0000177 if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700178 this->setStroke(*paint);
fmalita2d961e02016-08-11 09:16:29 -0700179 }
180 break;
Florin Malitaf543a602017-10-13 14:07:44 -0400181 case SkSVGAttribute::kStrokeDashArray:
Florin Malitaf4403e72020-04-10 14:14:04 +0000182 if (const SkSVGDashArrayValue* dashArray = v.as<SkSVGDashArrayValue>()) {
Florin Malitaf543a602017-10-13 14:07:44 -0400183 this->setStrokeDashArray(*dashArray);
184 }
185 break;
Florin Malitae1dadd72017-10-13 18:18:32 -0400186 case SkSVGAttribute::kStrokeDashOffset:
Florin Malitaf4403e72020-04-10 14:14:04 +0000187 if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) {
Florin Malitae1dadd72017-10-13 18:18:32 -0400188 this->setStrokeDashOffset(*dashOffset);
189 }
190 break;
fmalita2d961e02016-08-11 09:16:29 -0700191 case SkSVGAttribute::kStrokeOpacity:
Florin Malitaf4403e72020-04-10 14:14:04 +0000192 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700193 this->setStrokeOpacity(*opacity);
fmalita2d961e02016-08-11 09:16:29 -0700194 }
195 break;
196 case SkSVGAttribute::kStrokeLineCap:
Florin Malitaf4403e72020-04-10 14:14:04 +0000197 if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400198 this->setStrokeLineCap(*lineCap);
fmalita2d961e02016-08-11 09:16:29 -0700199 }
200 break;
201 case SkSVGAttribute::kStrokeLineJoin:
Florin Malitaf4403e72020-04-10 14:14:04 +0000202 if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400203 this->setStrokeLineJoin(*lineJoin);
fmalita2d961e02016-08-11 09:16:29 -0700204 }
205 break;
Florin Malita4de426b2017-10-09 12:57:41 -0400206 case SkSVGAttribute::kStrokeMiterLimit:
Florin Malitaf4403e72020-04-10 14:14:04 +0000207 if (const SkSVGNumberValue* miterLimit = v.as<SkSVGNumberValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400208 this->setStrokeMiterLimit(*miterLimit);
Florin Malita4de426b2017-10-09 12:57:41 -0400209 }
210 break;
fmalita2d961e02016-08-11 09:16:29 -0700211 case SkSVGAttribute::kStrokeWidth:
Florin Malitaf4403e72020-04-10 14:14:04 +0000212 if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700213 this->setStrokeWidth(*strokeWidth);
fmalita6ceef3d2016-07-26 18:46:34 -0700214 }
215 break;
Florin Malitaffe6ae42017-10-12 11:33:28 -0400216 case SkSVGAttribute::kVisibility:
Florin Malitaf4403e72020-04-10 14:14:04 +0000217 if (const SkSVGVisibilityValue* visibility = v.as<SkSVGVisibilityValue>()) {
Florin Malitaffe6ae42017-10-12 11:33:28 -0400218 this->setVisibility(*visibility);
219 }
220 break;
fmalita6ceef3d2016-07-26 18:46:34 -0700221 default:
Florin Malitac9649ce2018-02-16 13:49:48 -0500222#if defined(SK_VERBOSE_SVG_PARSING)
223 SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
224#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700225 break;
226 }
227}