fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkMatrix.h" |
| 10 | #include "include/pathops/SkPathOps.h" |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame^] | 11 | #include "include/private/SkTPin.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 12 | #include "modules/svg/include/SkSVGNode.h" |
| 13 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 14 | #include "modules/svg/include/SkSVGValue.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/core/SkTLazy.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 16 | |
| 17 | SkSVGNode::SkSVGNode(SkSVGTag t) : fTag(t) { } |
| 18 | |
| 19 | SkSVGNode::~SkSVGNode() { } |
| 20 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 21 | void SkSVGNode::render(const SkSVGRenderContext& ctx) const { |
| 22 | SkSVGRenderContext localContext(ctx); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 23 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 24 | if (this->onPrepareToRender(&localContext)) { |
| 25 | this->onRender(localContext); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 26 | } |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 27 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 28 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 29 | bool 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 Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 35 | SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const { |
| 36 | SkSVGRenderContext localContext(ctx); |
Florin Malita | 7d52988 | 2016-12-08 16:04:24 -0500 | [diff] [blame] | 37 | 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 Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 49 | } |
| 50 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 51 | bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const { |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 52 | ctx->applyPresentationAttributes(fPresentationAttributes, |
| 53 | this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 54 | |
| 55 | // visibility:hidden disables rendering |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 56 | const auto visibility = ctx->presentationContext().fInherited.fVisibility->type(); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 57 | return visibility != SkSVGVisibility::Type::kHidden; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 60 | void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 61 | this->onSetAttribute(attr, v); |
| 62 | } |
| 63 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 64 | void SkSVGNode::setClipPath(const SkSVGClip& clip) { |
| 65 | fPresentationAttributes.fClipPath.set(clip); |
| 66 | } |
| 67 | |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 68 | template <typename T> |
| 69 | void SetInheritedByDefault(SkTLazy<T>& presentation_attribute, const T& value) { |
| 70 | if (value.type() != T::Type::kInherit) { |
| 71 | presentation_attribute.set(value); |
| 72 | } else { |
| 73 | // kInherited values are semantically equivalent to |
| 74 | // the absence of a local presentation attribute. |
| 75 | presentation_attribute.reset(); |
| 76 | } |
| 77 | } |
| 78 | |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 79 | void SkSVGNode::setClipRule(const SkSVGFillRule& clipRule) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 80 | SetInheritedByDefault(fPresentationAttributes.fClipRule, clipRule); |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 83 | void SkSVGNode::setColor(const SkSVGColorType& color) { |
| 84 | // TODO: Color should be inherited by default |
| 85 | fPresentationAttributes.fColor.set(color); |
| 86 | } |
| 87 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 88 | void SkSVGNode::setFill(const SkSVGPaint& svgPaint) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 89 | SetInheritedByDefault(fPresentationAttributes.fFill, svgPaint); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 93 | fPresentationAttributes.fFillOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1))); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 96 | void SkSVGNode::setFillRule(const SkSVGFillRule& fillRule) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 97 | SetInheritedByDefault(fPresentationAttributes.fFillRule, fillRule); |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 98 | } |
| 99 | |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 100 | void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 101 | fPresentationAttributes.fOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1))); |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 102 | } |
| 103 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 104 | void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 105 | SetInheritedByDefault(fPresentationAttributes.fStroke, svgPaint); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 108 | void SkSVGNode::setStrokeDashArray(const SkSVGDashArray& dashArray) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 109 | SetInheritedByDefault(fPresentationAttributes.fStrokeDashArray, dashArray); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 112 | void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) { |
| 113 | fPresentationAttributes.fStrokeDashOffset.set(dashOffset); |
| 114 | } |
| 115 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 116 | void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 117 | fPresentationAttributes.fStrokeOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1))); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 120 | void SkSVGNode::setStrokeLineCap(const SkSVGLineCap& lc) { |
| 121 | SetInheritedByDefault(fPresentationAttributes.fStrokeLineCap, lc); |
| 122 | } |
| 123 | |
| 124 | void SkSVGNode::setStrokeLineJoin(const SkSVGLineJoin& lj) { |
| 125 | SetInheritedByDefault(fPresentationAttributes.fStrokeLineJoin, lj); |
| 126 | } |
| 127 | |
| 128 | void SkSVGNode::setStrokeMiterLimit(const SkSVGNumberType& ml) { |
| 129 | fPresentationAttributes.fStrokeMiterLimit.set(ml); |
| 130 | } |
| 131 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 132 | void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) { |
| 133 | fPresentationAttributes.fStrokeWidth.set(strokeWidth); |
| 134 | } |
| 135 | |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 136 | void SkSVGNode::setVisibility(const SkSVGVisibility& visibility) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 137 | SetInheritedByDefault(fPresentationAttributes.fVisibility, visibility); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 140 | void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 141 | switch (attr) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 142 | case SkSVGAttribute::kClipPath: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 143 | if (const SkSVGClipValue* clip = v.as<SkSVGClipValue>()) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 144 | this->setClipPath(*clip); |
| 145 | } |
| 146 | break; |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 147 | case SkSVGAttribute::kClipRule: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 148 | if (const SkSVGFillRuleValue* clipRule = v.as<SkSVGFillRuleValue>()) { |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 149 | this->setClipRule(*clipRule); |
| 150 | } |
| 151 | break; |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 152 | case SkSVGAttribute::kColor: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 153 | if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) { |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 154 | this->setColor(*color); |
| 155 | } |
| 156 | break; |
fmalita | 58649cc | 2016-07-29 08:52:03 -0700 | [diff] [blame] | 157 | case SkSVGAttribute::kFill: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 158 | if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 159 | this->setFill(*paint); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 160 | } |
| 161 | break; |
| 162 | case SkSVGAttribute::kFillOpacity: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 163 | if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 164 | this->setFillOpacity(*opacity); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 165 | } |
| 166 | break; |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 167 | case SkSVGAttribute::kFillRule: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 168 | if (const SkSVGFillRuleValue* fillRule = v.as<SkSVGFillRuleValue>()) { |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 169 | this->setFillRule(*fillRule); |
| 170 | } |
| 171 | break; |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 172 | case SkSVGAttribute::kOpacity: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 173 | if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) { |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 174 | this->setOpacity(*opacity); |
| 175 | } |
| 176 | break; |
fmalita | 58649cc | 2016-07-29 08:52:03 -0700 | [diff] [blame] | 177 | case SkSVGAttribute::kStroke: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 178 | if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 179 | this->setStroke(*paint); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 180 | } |
| 181 | break; |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 182 | case SkSVGAttribute::kStrokeDashArray: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 183 | if (const SkSVGDashArrayValue* dashArray = v.as<SkSVGDashArrayValue>()) { |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 184 | this->setStrokeDashArray(*dashArray); |
| 185 | } |
| 186 | break; |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 187 | case SkSVGAttribute::kStrokeDashOffset: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 188 | if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) { |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 189 | this->setStrokeDashOffset(*dashOffset); |
| 190 | } |
| 191 | break; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 192 | case SkSVGAttribute::kStrokeOpacity: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 193 | if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 194 | this->setStrokeOpacity(*opacity); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 195 | } |
| 196 | break; |
| 197 | case SkSVGAttribute::kStrokeLineCap: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 198 | if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 199 | this->setStrokeLineCap(*lineCap); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 200 | } |
| 201 | break; |
| 202 | case SkSVGAttribute::kStrokeLineJoin: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 203 | if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 204 | this->setStrokeLineJoin(*lineJoin); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 205 | } |
| 206 | break; |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 207 | case SkSVGAttribute::kStrokeMiterLimit: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 208 | if (const SkSVGNumberValue* miterLimit = v.as<SkSVGNumberValue>()) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 209 | this->setStrokeMiterLimit(*miterLimit); |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 210 | } |
| 211 | break; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 212 | case SkSVGAttribute::kStrokeWidth: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 213 | if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 214 | this->setStrokeWidth(*strokeWidth); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 215 | } |
| 216 | break; |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 217 | case SkSVGAttribute::kVisibility: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 218 | if (const SkSVGVisibilityValue* visibility = v.as<SkSVGVisibilityValue>()) { |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 219 | this->setVisibility(*visibility); |
| 220 | } |
| 221 | break; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 222 | default: |
Florin Malita | c9649ce | 2018-02-16 13:49:48 -0500 | [diff] [blame] | 223 | #if defined(SK_VERBOSE_SVG_PARSING) |
| 224 | SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag); |
| 225 | #endif |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | } |