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