blob: 21c237c388d1003498d9971cab58bedc267bddb7 [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"
10#include "experimental/svg/model/SkSVGValue.h"
11#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
55 const auto visibility = ctx->presentationContext().fInherited.fVisibility.get()->type();
56 return visibility != SkSVGVisibility::Type::kHidden;
fmalita6ceef3d2016-07-26 18:46:34 -070057}
58
59void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
60 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
fmalitaca39d712016-08-12 13:17:11 -070082void SkSVGNode::setFill(const SkSVGPaint& svgPaint) {
Florin Malita09294252020-04-09 08:54:29 -040083 SetInheritedByDefault(fPresentationAttributes.fFill, svgPaint);
fmalitaca39d712016-08-12 13:17:11 -070084}
85
86void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) {
87 fPresentationAttributes.fFillOpacity.set(
88 SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
89}
90
Florin Malitae932d4b2016-12-01 13:35:11 -050091void SkSVGNode::setFillRule(const SkSVGFillRule& fillRule) {
Florin Malita09294252020-04-09 08:54:29 -040092 SetInheritedByDefault(fPresentationAttributes.fFillRule, fillRule);
Florin Malitae932d4b2016-12-01 13:35:11 -050093}
94
fmalita6fb06482016-08-15 12:45:11 -070095void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) {
96 fPresentationAttributes.fOpacity.set(
97 SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
98}
99
fmalitaca39d712016-08-12 13:17:11 -0700100void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) {
Florin Malita09294252020-04-09 08:54:29 -0400101 SetInheritedByDefault(fPresentationAttributes.fStroke, svgPaint);
fmalitaca39d712016-08-12 13:17:11 -0700102}
103
Florin Malitaf543a602017-10-13 14:07:44 -0400104void SkSVGNode::setStrokeDashArray(const SkSVGDashArray& dashArray) {
Florin Malita09294252020-04-09 08:54:29 -0400105 SetInheritedByDefault(fPresentationAttributes.fStrokeDashArray, dashArray);
Florin Malitaf543a602017-10-13 14:07:44 -0400106}
107
Florin Malitae1dadd72017-10-13 18:18:32 -0400108void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) {
109 fPresentationAttributes.fStrokeDashOffset.set(dashOffset);
110}
111
fmalitaca39d712016-08-12 13:17:11 -0700112void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
113 fPresentationAttributes.fStrokeOpacity.set(
114 SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
115}
116
Florin Malita09294252020-04-09 08:54:29 -0400117void SkSVGNode::setStrokeLineCap(const SkSVGLineCap& lc) {
118 SetInheritedByDefault(fPresentationAttributes.fStrokeLineCap, lc);
119}
120
121void SkSVGNode::setStrokeLineJoin(const SkSVGLineJoin& lj) {
122 SetInheritedByDefault(fPresentationAttributes.fStrokeLineJoin, lj);
123}
124
125void SkSVGNode::setStrokeMiterLimit(const SkSVGNumberType& ml) {
126 fPresentationAttributes.fStrokeMiterLimit.set(ml);
127}
128
fmalitaca39d712016-08-12 13:17:11 -0700129void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) {
130 fPresentationAttributes.fStrokeWidth.set(strokeWidth);
131}
132
Florin Malitaffe6ae42017-10-12 11:33:28 -0400133void SkSVGNode::setVisibility(const SkSVGVisibility& visibility) {
Florin Malita09294252020-04-09 08:54:29 -0400134 SetInheritedByDefault(fPresentationAttributes.fVisibility, visibility);
Florin Malitaffe6ae42017-10-12 11:33:28 -0400135}
136
fmalita6ceef3d2016-07-26 18:46:34 -0700137void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
138 switch (attr) {
Florin Malitace8840e2016-12-08 09:26:47 -0500139 case SkSVGAttribute::kClipPath:
140 if (const SkSVGClipValue* clip = v.as<SkSVGClipValue>()) {
141 this->setClipPath(*clip);
142 }
143 break;
Florin Malita57a0edf2017-10-10 11:22:08 -0400144 case SkSVGAttribute::kClipRule:
145 if (const SkSVGFillRuleValue* clipRule = v.as<SkSVGFillRuleValue>()) {
146 this->setClipRule(*clipRule);
147 }
148 break;
fmalita58649cc2016-07-29 08:52:03 -0700149 case SkSVGAttribute::kFill:
fmalita2d961e02016-08-11 09:16:29 -0700150 if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700151 this->setFill(*paint);
fmalita2d961e02016-08-11 09:16:29 -0700152 }
153 break;
154 case SkSVGAttribute::kFillOpacity:
155 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700156 this->setFillOpacity(*opacity);
fmalita6ceef3d2016-07-26 18:46:34 -0700157 }
158 break;
Florin Malitae932d4b2016-12-01 13:35:11 -0500159 case SkSVGAttribute::kFillRule:
160 if (const SkSVGFillRuleValue* fillRule = v.as<SkSVGFillRuleValue>()) {
161 this->setFillRule(*fillRule);
162 }
163 break;
fmalita6fb06482016-08-15 12:45:11 -0700164 case SkSVGAttribute::kOpacity:
165 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
166 this->setOpacity(*opacity);
167 }
168 break;
fmalita58649cc2016-07-29 08:52:03 -0700169 case SkSVGAttribute::kStroke:
fmalita2d961e02016-08-11 09:16:29 -0700170 if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700171 this->setStroke(*paint);
fmalita2d961e02016-08-11 09:16:29 -0700172 }
173 break;
Florin Malitaf543a602017-10-13 14:07:44 -0400174 case SkSVGAttribute::kStrokeDashArray:
175 if (const SkSVGDashArrayValue* dashArray = v.as<SkSVGDashArrayValue>()) {
176 this->setStrokeDashArray(*dashArray);
177 }
178 break;
Florin Malitae1dadd72017-10-13 18:18:32 -0400179 case SkSVGAttribute::kStrokeDashOffset:
180 if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) {
181 this->setStrokeDashOffset(*dashOffset);
182 }
183 break;
fmalita2d961e02016-08-11 09:16:29 -0700184 case SkSVGAttribute::kStrokeOpacity:
185 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700186 this->setStrokeOpacity(*opacity);
fmalita2d961e02016-08-11 09:16:29 -0700187 }
188 break;
189 case SkSVGAttribute::kStrokeLineCap:
190 if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400191 this->setStrokeLineCap(*lineCap);
fmalita2d961e02016-08-11 09:16:29 -0700192 }
193 break;
194 case SkSVGAttribute::kStrokeLineJoin:
195 if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400196 this->setStrokeLineJoin(*lineJoin);
fmalita2d961e02016-08-11 09:16:29 -0700197 }
198 break;
Florin Malita4de426b2017-10-09 12:57:41 -0400199 case SkSVGAttribute::kStrokeMiterLimit:
200 if (const SkSVGNumberValue* miterLimit = v.as<SkSVGNumberValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400201 this->setStrokeMiterLimit(*miterLimit);
Florin Malita4de426b2017-10-09 12:57:41 -0400202 }
203 break;
fmalita2d961e02016-08-11 09:16:29 -0700204 case SkSVGAttribute::kStrokeWidth:
205 if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700206 this->setStrokeWidth(*strokeWidth);
fmalita6ceef3d2016-07-26 18:46:34 -0700207 }
208 break;
Florin Malitaffe6ae42017-10-12 11:33:28 -0400209 case SkSVGAttribute::kVisibility:
210 if (const SkSVGVisibilityValue* visibility = v.as<SkSVGVisibilityValue>()) {
211 this->setVisibility(*visibility);
212 }
213 break;
fmalita6ceef3d2016-07-26 18:46:34 -0700214 default:
Florin Malitac9649ce2018-02-16 13:49:48 -0500215#if defined(SK_VERBOSE_SVG_PARSING)
216 SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
217#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700218 break;
219 }
220}