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 { |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 22 | SkSVGRenderContext localContext(ctx, this); |
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 | |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 51 | SkRect SkSVGNode::objectBoundingBox(const SkSVGRenderContext& ctx) const { |
| 52 | return this->onObjectBoundingBox(ctx); |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 53 | } |
| 54 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 55 | bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const { |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 56 | ctx->applyPresentationAttributes(fPresentationAttributes, |
| 57 | this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 58 | |
| 59 | // visibility:hidden disables rendering |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 60 | const auto visibility = ctx->presentationContext().fInherited.fVisibility->type(); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 61 | return visibility != SkSVGVisibility::Type::kHidden; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 64 | void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 65 | this->onSetAttribute(attr, v); |
| 66 | } |
| 67 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 68 | void SkSVGNode::setClipPath(const SkSVGClip& clip) { |
| 69 | fPresentationAttributes.fClipPath.set(clip); |
| 70 | } |
| 71 | |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 72 | template <typename T> |
| 73 | void 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 Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 83 | void SkSVGNode::setClipRule(const SkSVGFillRule& clipRule) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 84 | SetInheritedByDefault(fPresentationAttributes.fClipRule, clipRule); |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 87 | void SkSVGNode::setColor(const SkSVGColorType& color) { |
| 88 | // TODO: Color should be inherited by default |
| 89 | fPresentationAttributes.fColor.set(color); |
| 90 | } |
| 91 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 92 | void SkSVGNode::setFill(const SkSVGPaint& svgPaint) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 93 | SetInheritedByDefault(fPresentationAttributes.fFill, svgPaint); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 97 | fPresentationAttributes.fFillOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1))); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 100 | void SkSVGNode::setFillRule(const SkSVGFillRule& fillRule) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 101 | SetInheritedByDefault(fPresentationAttributes.fFillRule, fillRule); |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 102 | } |
| 103 | |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 104 | void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 105 | fPresentationAttributes.fOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1))); |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 106 | } |
| 107 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 108 | void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 109 | SetInheritedByDefault(fPresentationAttributes.fStroke, svgPaint); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 112 | void SkSVGNode::setStrokeDashArray(const SkSVGDashArray& dashArray) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 113 | SetInheritedByDefault(fPresentationAttributes.fStrokeDashArray, dashArray); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 116 | void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) { |
| 117 | fPresentationAttributes.fStrokeDashOffset.set(dashOffset); |
| 118 | } |
| 119 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 120 | void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 121 | fPresentationAttributes.fStrokeOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1))); |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 124 | void SkSVGNode::setStrokeLineCap(const SkSVGLineCap& lc) { |
| 125 | SetInheritedByDefault(fPresentationAttributes.fStrokeLineCap, lc); |
| 126 | } |
| 127 | |
| 128 | void SkSVGNode::setStrokeLineJoin(const SkSVGLineJoin& lj) { |
| 129 | SetInheritedByDefault(fPresentationAttributes.fStrokeLineJoin, lj); |
| 130 | } |
| 131 | |
| 132 | void SkSVGNode::setStrokeMiterLimit(const SkSVGNumberType& ml) { |
| 133 | fPresentationAttributes.fStrokeMiterLimit.set(ml); |
| 134 | } |
| 135 | |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 136 | void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) { |
| 137 | fPresentationAttributes.fStrokeWidth.set(strokeWidth); |
| 138 | } |
| 139 | |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 140 | void SkSVGNode::setVisibility(const SkSVGVisibility& visibility) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 141 | SetInheritedByDefault(fPresentationAttributes.fVisibility, visibility); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 144 | void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 145 | switch (attr) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 146 | case SkSVGAttribute::kClipPath: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 147 | if (const SkSVGClipValue* clip = v.as<SkSVGClipValue>()) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 148 | this->setClipPath(*clip); |
| 149 | } |
| 150 | break; |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 151 | case SkSVGAttribute::kClipRule: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 152 | if (const SkSVGFillRuleValue* clipRule = v.as<SkSVGFillRuleValue>()) { |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 153 | this->setClipRule(*clipRule); |
| 154 | } |
| 155 | break; |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 156 | case SkSVGAttribute::kColor: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 157 | if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) { |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 158 | this->setColor(*color); |
| 159 | } |
| 160 | break; |
fmalita | 58649cc | 2016-07-29 08:52:03 -0700 | [diff] [blame] | 161 | case SkSVGAttribute::kFill: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 162 | if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 163 | this->setFill(*paint); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 164 | } |
| 165 | break; |
| 166 | case SkSVGAttribute::kFillOpacity: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 167 | if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 168 | this->setFillOpacity(*opacity); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 169 | } |
| 170 | break; |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 171 | case SkSVGAttribute::kFillRule: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 172 | if (const SkSVGFillRuleValue* fillRule = v.as<SkSVGFillRuleValue>()) { |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 173 | this->setFillRule(*fillRule); |
| 174 | } |
| 175 | break; |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 176 | 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; |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 196 | case SkSVGAttribute::kOpacity: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 197 | if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) { |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 198 | this->setOpacity(*opacity); |
| 199 | } |
| 200 | break; |
fmalita | 58649cc | 2016-07-29 08:52:03 -0700 | [diff] [blame] | 201 | case SkSVGAttribute::kStroke: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 202 | if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 203 | this->setStroke(*paint); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 204 | } |
| 205 | break; |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 206 | case SkSVGAttribute::kStrokeDashArray: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 207 | if (const SkSVGDashArrayValue* dashArray = v.as<SkSVGDashArrayValue>()) { |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 208 | this->setStrokeDashArray(*dashArray); |
| 209 | } |
| 210 | break; |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 211 | case SkSVGAttribute::kStrokeDashOffset: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 212 | if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) { |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 213 | this->setStrokeDashOffset(*dashOffset); |
| 214 | } |
| 215 | break; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 216 | case SkSVGAttribute::kStrokeOpacity: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 217 | if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 218 | this->setStrokeOpacity(*opacity); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 219 | } |
| 220 | break; |
| 221 | case SkSVGAttribute::kStrokeLineCap: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 222 | if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 223 | this->setStrokeLineCap(*lineCap); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | case SkSVGAttribute::kStrokeLineJoin: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 227 | if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 228 | this->setStrokeLineJoin(*lineJoin); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 229 | } |
| 230 | break; |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 231 | case SkSVGAttribute::kStrokeMiterLimit: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 232 | if (const SkSVGNumberValue* miterLimit = v.as<SkSVGNumberValue>()) { |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 233 | this->setStrokeMiterLimit(*miterLimit); |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 234 | } |
| 235 | break; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 236 | case SkSVGAttribute::kStrokeWidth: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 237 | if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) { |
fmalita | ca39d71 | 2016-08-12 13:17:11 -0700 | [diff] [blame] | 238 | this->setStrokeWidth(*strokeWidth); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 239 | } |
| 240 | break; |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 241 | case SkSVGAttribute::kVisibility: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 242 | if (const SkSVGVisibilityValue* visibility = v.as<SkSVGVisibilityValue>()) { |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 243 | this->setVisibility(*visibility); |
| 244 | } |
| 245 | break; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 246 | default: |
Florin Malita | c9649ce | 2018-02-16 13:49:48 -0500 | [diff] [blame] | 247 | #if defined(SK_VERBOSE_SVG_PARSING) |
| 248 | SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag); |
| 249 | #endif |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | } |