blob: 07f3a473a111f8eba1ca5220b843e4aa927f90e3 [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
17SkSVGNode::SkSVGNode(SkSVGTag t) : fTag(t) { }
18
19SkSVGNode::~SkSVGNode() { }
20
fmalita397a5172016-08-08 11:38:55 -070021void SkSVGNode::render(const SkSVGRenderContext& ctx) const {
Tyler Denniston53281c72020-10-22 15:54:24 -040022 SkSVGRenderContext localContext(ctx, this);
fmalita6ceef3d2016-07-26 18:46:34 -070023
fmalita397a5172016-08-08 11:38:55 -070024 if (this->onPrepareToRender(&localContext)) {
25 this->onRender(localContext);
fmalita6ceef3d2016-07-26 18:46:34 -070026 }
fmalita397a5172016-08-08 11:38:55 -070027}
fmalita6ceef3d2016-07-26 18:46:34 -070028
fmalita28d5b722016-09-12 17:06:47 -070029bool SkSVGNode::asPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
30 SkSVGRenderContext localContext(ctx);
31
32 return this->onPrepareToRender(&localContext) && this->onAsPaint(localContext, paint);
33}
34
Florin Malitace8840e2016-12-08 09:26:47 -050035SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const {
36 SkSVGRenderContext localContext(ctx);
Florin Malita7d529882016-12-08 16:04:24 -050037 if (!this->onPrepareToRender(&localContext)) {
38 return SkPath();
39 }
40
41 SkPath path = this->onAsPath(localContext);
42
43 if (const auto* clipPath = localContext.clipPath()) {
44 // There is a clip-path present on the current node.
45 Op(path, *clipPath, kIntersect_SkPathOp, &path);
46 }
47
48 return path;
Florin Malitace8840e2016-12-08 09:26:47 -050049}
50
Tyler Dennistonf548a022020-10-27 15:02:02 -040051SkRect SkSVGNode::objectBoundingBox(const SkSVGRenderContext& ctx) const {
52 return this->onObjectBoundingBox(ctx);
Tyler Denniston53281c72020-10-22 15:54:24 -040053}
54
fmalita397a5172016-08-08 11:38:55 -070055bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
fmalitabef51c22016-09-20 15:45:57 -070056 ctx->applyPresentationAttributes(fPresentationAttributes,
57 this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf);
Florin Malitaffe6ae42017-10-12 11:33:28 -040058
59 // visibility:hidden disables rendering
John Stilesa008b0f2020-08-16 08:48:02 -040060 const auto visibility = ctx->presentationContext().fInherited.fVisibility->type();
Florin Malitaffe6ae42017-10-12 11:33:28 -040061 return visibility != SkSVGVisibility::Type::kHidden;
fmalita6ceef3d2016-07-26 18:46:34 -070062}
63
Florin Malitaf4403e72020-04-10 14:14:04 +000064void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalita6ceef3d2016-07-26 18:46:34 -070065 this->onSetAttribute(attr, v);
66}
67
Florin Malitace8840e2016-12-08 09:26:47 -050068void SkSVGNode::setClipPath(const SkSVGClip& clip) {
69 fPresentationAttributes.fClipPath.set(clip);
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 Malita57a0edf2017-10-10 11:22:08 -040083void SkSVGNode::setClipRule(const SkSVGFillRule& clipRule) {
Florin Malita09294252020-04-09 08:54:29 -040084 SetInheritedByDefault(fPresentationAttributes.fClipRule, clipRule);
Florin Malita57a0edf2017-10-10 11:22:08 -040085}
86
Tyler Denniston6c8314e2020-04-09 14:14:10 -040087void SkSVGNode::setColor(const SkSVGColorType& color) {
88 // TODO: Color should be inherited by default
89 fPresentationAttributes.fColor.set(color);
90}
91
fmalitaca39d712016-08-12 13:17:11 -070092void SkSVGNode::setFill(const SkSVGPaint& svgPaint) {
Florin Malita09294252020-04-09 08:54:29 -040093 SetInheritedByDefault(fPresentationAttributes.fFill, svgPaint);
fmalitaca39d712016-08-12 13:17:11 -070094}
95
96void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) {
Florin Malitaa3626692020-04-09 14:36:45 -040097 fPresentationAttributes.fFillOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
fmalitaca39d712016-08-12 13:17:11 -070098}
99
Florin Malitae932d4b2016-12-01 13:35:11 -0500100void SkSVGNode::setFillRule(const SkSVGFillRule& fillRule) {
Florin Malita09294252020-04-09 08:54:29 -0400101 SetInheritedByDefault(fPresentationAttributes.fFillRule, fillRule);
Florin Malitae932d4b2016-12-01 13:35:11 -0500102}
103
fmalita6fb06482016-08-15 12:45:11 -0700104void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) {
Florin Malitaa3626692020-04-09 14:36:45 -0400105 fPresentationAttributes.fOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
fmalita6fb06482016-08-15 12:45:11 -0700106}
107
fmalitaca39d712016-08-12 13:17:11 -0700108void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) {
Florin Malita09294252020-04-09 08:54:29 -0400109 SetInheritedByDefault(fPresentationAttributes.fStroke, svgPaint);
fmalitaca39d712016-08-12 13:17:11 -0700110}
111
Florin Malitaf543a602017-10-13 14:07:44 -0400112void SkSVGNode::setStrokeDashArray(const SkSVGDashArray& dashArray) {
Florin Malita09294252020-04-09 08:54:29 -0400113 SetInheritedByDefault(fPresentationAttributes.fStrokeDashArray, dashArray);
Florin Malitaf543a602017-10-13 14:07:44 -0400114}
115
Florin Malitae1dadd72017-10-13 18:18:32 -0400116void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) {
117 fPresentationAttributes.fStrokeDashOffset.set(dashOffset);
118}
119
fmalitaca39d712016-08-12 13:17:11 -0700120void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
Florin Malitaa3626692020-04-09 14:36:45 -0400121 fPresentationAttributes.fStrokeOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
fmalitaca39d712016-08-12 13:17:11 -0700122}
123
Florin Malita09294252020-04-09 08:54:29 -0400124void SkSVGNode::setStrokeLineCap(const SkSVGLineCap& lc) {
125 SetInheritedByDefault(fPresentationAttributes.fStrokeLineCap, lc);
126}
127
128void SkSVGNode::setStrokeLineJoin(const SkSVGLineJoin& lj) {
129 SetInheritedByDefault(fPresentationAttributes.fStrokeLineJoin, lj);
130}
131
132void SkSVGNode::setStrokeMiterLimit(const SkSVGNumberType& ml) {
133 fPresentationAttributes.fStrokeMiterLimit.set(ml);
134}
135
fmalitaca39d712016-08-12 13:17:11 -0700136void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) {
137 fPresentationAttributes.fStrokeWidth.set(strokeWidth);
138}
139
Florin Malitaffe6ae42017-10-12 11:33:28 -0400140void SkSVGNode::setVisibility(const SkSVGVisibility& visibility) {
Florin Malita09294252020-04-09 08:54:29 -0400141 SetInheritedByDefault(fPresentationAttributes.fVisibility, visibility);
Florin Malitaffe6ae42017-10-12 11:33:28 -0400142}
143
Florin Malitaf4403e72020-04-10 14:14:04 +0000144void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalita6ceef3d2016-07-26 18:46:34 -0700145 switch (attr) {
Florin Malitace8840e2016-12-08 09:26:47 -0500146 case SkSVGAttribute::kClipPath:
Florin Malitaf4403e72020-04-10 14:14:04 +0000147 if (const SkSVGClipValue* clip = v.as<SkSVGClipValue>()) {
Florin Malitace8840e2016-12-08 09:26:47 -0500148 this->setClipPath(*clip);
149 }
150 break;
Florin Malita57a0edf2017-10-10 11:22:08 -0400151 case SkSVGAttribute::kClipRule:
Florin Malitaf4403e72020-04-10 14:14:04 +0000152 if (const SkSVGFillRuleValue* clipRule = v.as<SkSVGFillRuleValue>()) {
Florin Malita57a0edf2017-10-10 11:22:08 -0400153 this->setClipRule(*clipRule);
154 }
155 break;
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400156 case SkSVGAttribute::kColor:
Florin Malitaf4403e72020-04-10 14:14:04 +0000157 if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) {
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400158 this->setColor(*color);
159 }
160 break;
fmalita58649cc2016-07-29 08:52:03 -0700161 case SkSVGAttribute::kFill:
Florin Malitaf4403e72020-04-10 14:14:04 +0000162 if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700163 this->setFill(*paint);
fmalita2d961e02016-08-11 09:16:29 -0700164 }
165 break;
166 case SkSVGAttribute::kFillOpacity:
Florin Malitaf4403e72020-04-10 14:14:04 +0000167 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700168 this->setFillOpacity(*opacity);
fmalita6ceef3d2016-07-26 18:46:34 -0700169 }
170 break;
Florin Malitae932d4b2016-12-01 13:35:11 -0500171 case SkSVGAttribute::kFillRule:
Florin Malitaf4403e72020-04-10 14:14:04 +0000172 if (const SkSVGFillRuleValue* fillRule = v.as<SkSVGFillRuleValue>()) {
Florin Malitae932d4b2016-12-01 13:35:11 -0500173 this->setFillRule(*fillRule);
174 }
175 break;
Florin Malita39fe8c82020-10-20 10:43:03 -0400176 case SkSVGAttribute::kFontFamily:
177 if (const SkSVGFontFamilyValue* family = v.as<SkSVGFontFamilyValue>()) {
178 this->setFontFamily(*family);
179 }
180 break;
181 case SkSVGAttribute::kFontSize:
182 if (const SkSVGFontSizeValue* size = v.as<SkSVGFontSizeValue>()) {
183 this->setFontSize(*size);
184 }
185 break;
186 case SkSVGAttribute::kFontStyle:
187 if (const SkSVGFontStyleValue* style = v.as<SkSVGFontStyleValue>()) {
188 this->setFontStyle(*style);
189 }
190 break;
191 case SkSVGAttribute::kFontWeight:
192 if (const SkSVGFontWeightValue* style = v.as<SkSVGFontWeightValue>()) {
193 this->setFontWeight(*style);
194 }
195 break;
fmalita6fb06482016-08-15 12:45:11 -0700196 case SkSVGAttribute::kOpacity:
Florin Malitaf4403e72020-04-10 14:14:04 +0000197 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalita6fb06482016-08-15 12:45:11 -0700198 this->setOpacity(*opacity);
199 }
200 break;
fmalita58649cc2016-07-29 08:52:03 -0700201 case SkSVGAttribute::kStroke:
Florin Malitaf4403e72020-04-10 14:14:04 +0000202 if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700203 this->setStroke(*paint);
fmalita2d961e02016-08-11 09:16:29 -0700204 }
205 break;
Florin Malitaf543a602017-10-13 14:07:44 -0400206 case SkSVGAttribute::kStrokeDashArray:
Florin Malitaf4403e72020-04-10 14:14:04 +0000207 if (const SkSVGDashArrayValue* dashArray = v.as<SkSVGDashArrayValue>()) {
Florin Malitaf543a602017-10-13 14:07:44 -0400208 this->setStrokeDashArray(*dashArray);
209 }
210 break;
Florin Malitae1dadd72017-10-13 18:18:32 -0400211 case SkSVGAttribute::kStrokeDashOffset:
Florin Malitaf4403e72020-04-10 14:14:04 +0000212 if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) {
Florin Malitae1dadd72017-10-13 18:18:32 -0400213 this->setStrokeDashOffset(*dashOffset);
214 }
215 break;
fmalita2d961e02016-08-11 09:16:29 -0700216 case SkSVGAttribute::kStrokeOpacity:
Florin Malitaf4403e72020-04-10 14:14:04 +0000217 if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700218 this->setStrokeOpacity(*opacity);
fmalita2d961e02016-08-11 09:16:29 -0700219 }
220 break;
221 case SkSVGAttribute::kStrokeLineCap:
Florin Malitaf4403e72020-04-10 14:14:04 +0000222 if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400223 this->setStrokeLineCap(*lineCap);
fmalita2d961e02016-08-11 09:16:29 -0700224 }
225 break;
226 case SkSVGAttribute::kStrokeLineJoin:
Florin Malitaf4403e72020-04-10 14:14:04 +0000227 if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400228 this->setStrokeLineJoin(*lineJoin);
fmalita2d961e02016-08-11 09:16:29 -0700229 }
230 break;
Florin Malita4de426b2017-10-09 12:57:41 -0400231 case SkSVGAttribute::kStrokeMiterLimit:
Florin Malitaf4403e72020-04-10 14:14:04 +0000232 if (const SkSVGNumberValue* miterLimit = v.as<SkSVGNumberValue>()) {
Florin Malita09294252020-04-09 08:54:29 -0400233 this->setStrokeMiterLimit(*miterLimit);
Florin Malita4de426b2017-10-09 12:57:41 -0400234 }
235 break;
fmalita2d961e02016-08-11 09:16:29 -0700236 case SkSVGAttribute::kStrokeWidth:
Florin Malitaf4403e72020-04-10 14:14:04 +0000237 if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
fmalitaca39d712016-08-12 13:17:11 -0700238 this->setStrokeWidth(*strokeWidth);
fmalita6ceef3d2016-07-26 18:46:34 -0700239 }
240 break;
Florin Malitaffe6ae42017-10-12 11:33:28 -0400241 case SkSVGAttribute::kVisibility:
Florin Malitaf4403e72020-04-10 14:14:04 +0000242 if (const SkSVGVisibilityValue* visibility = v.as<SkSVGVisibilityValue>()) {
Florin Malitaffe6ae42017-10-12 11:33:28 -0400243 this->setVisibility(*visibility);
244 }
245 break;
fmalita6ceef3d2016-07-26 18:46:34 -0700246 default:
Florin Malitac9649ce2018-02-16 13:49:48 -0500247#if defined(SK_VERBOSE_SVG_PARSING)
248 SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
249#endif
fmalita6ceef3d2016-07-26 18:46:34 -0700250 break;
251 }
252}