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 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 8 | #include "modules/svg/include/SkSVGRenderContext.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/effects/SkDashPathEffect.h" |
| 13 | #include "include/private/SkTo.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 14 | #include "modules/svg/include/SkSVGAttribute.h" |
| 15 | #include "modules/svg/include/SkSVGNode.h" |
| 16 | #include "modules/svg/include/SkSVGTypes.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 17 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | SkScalar length_size_for_type(const SkSize& viewport, SkSVGLengthContext::LengthType t) { |
| 21 | switch (t) { |
| 22 | case SkSVGLengthContext::LengthType::kHorizontal: |
| 23 | return viewport.width(); |
| 24 | case SkSVGLengthContext::LengthType::kVertical: |
| 25 | return viewport.height(); |
| 26 | case SkSVGLengthContext::LengthType::kOther: |
| 27 | return SkScalarSqrt(viewport.width() * viewport.height()); |
| 28 | } |
| 29 | |
| 30 | SkASSERT(false); // Not reached. |
| 31 | return 0; |
| 32 | } |
| 33 | |
fmalita | 280e282 | 2016-08-17 14:51:03 -0700 | [diff] [blame] | 34 | // Multipliers for DPI-relative units. |
| 35 | constexpr SkScalar kINMultiplier = 1.00f; |
| 36 | constexpr SkScalar kPTMultiplier = kINMultiplier / 72.272f; |
| 37 | constexpr SkScalar kPCMultiplier = kPTMultiplier * 12; |
| 38 | constexpr SkScalar kMMMultiplier = kINMultiplier / 25.4f; |
| 39 | constexpr SkScalar kCMMultiplier = kMMMultiplier * 10; |
| 40 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 41 | } // namespace |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 42 | |
| 43 | SkScalar SkSVGLengthContext::resolve(const SkSVGLength& l, LengthType t) const { |
| 44 | switch (l.unit()) { |
| 45 | case SkSVGLength::Unit::kNumber: |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 46 | // Fall through. |
| 47 | case SkSVGLength::Unit::kPX: |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 48 | return l.value(); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 49 | case SkSVGLength::Unit::kPercentage: |
| 50 | return l.value() * length_size_for_type(fViewport, t) / 100; |
fmalita | 280e282 | 2016-08-17 14:51:03 -0700 | [diff] [blame] | 51 | case SkSVGLength::Unit::kCM: |
| 52 | return l.value() * fDPI * kCMMultiplier; |
| 53 | case SkSVGLength::Unit::kMM: |
| 54 | return l.value() * fDPI * kMMMultiplier; |
| 55 | case SkSVGLength::Unit::kIN: |
| 56 | return l.value() * fDPI * kINMultiplier; |
| 57 | case SkSVGLength::Unit::kPT: |
| 58 | return l.value() * fDPI * kPTMultiplier; |
| 59 | case SkSVGLength::Unit::kPC: |
| 60 | return l.value() * fDPI * kPCMultiplier; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 61 | default: |
| 62 | SkDebugf("unsupported unit type: <%d>\n", l.unit()); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 63 | return 0; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 64 | } |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 65 | } |
| 66 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 67 | SkRect SkSVGLengthContext::resolveRect(const SkSVGLength& x, const SkSVGLength& y, |
| 68 | const SkSVGLength& w, const SkSVGLength& h) const { |
| 69 | return SkRect::MakeXYWH( |
| 70 | this->resolve(x, SkSVGLengthContext::LengthType::kHorizontal), |
| 71 | this->resolve(y, SkSVGLengthContext::LengthType::kVertical), |
| 72 | this->resolve(w, SkSVGLengthContext::LengthType::kHorizontal), |
| 73 | this->resolve(h, SkSVGLengthContext::LengthType::kVertical)); |
| 74 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 75 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 76 | namespace { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 77 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 78 | SkPaint::Cap toSkCap(const SkSVGLineCap& cap) { |
| 79 | switch (cap.type()) { |
| 80 | case SkSVGLineCap::Type::kButt: |
| 81 | return SkPaint::kButt_Cap; |
| 82 | case SkSVGLineCap::Type::kRound: |
| 83 | return SkPaint::kRound_Cap; |
| 84 | case SkSVGLineCap::Type::kSquare: |
| 85 | return SkPaint::kSquare_Cap; |
| 86 | default: |
| 87 | SkASSERT(false); |
| 88 | return SkPaint::kButt_Cap; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 89 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 90 | } |
| 91 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 92 | SkPaint::Join toSkJoin(const SkSVGLineJoin& join) { |
| 93 | switch (join.type()) { |
| 94 | case SkSVGLineJoin::Type::kMiter: |
| 95 | return SkPaint::kMiter_Join; |
| 96 | case SkSVGLineJoin::Type::kRound: |
| 97 | return SkPaint::kRound_Join; |
| 98 | case SkSVGLineJoin::Type::kBevel: |
| 99 | return SkPaint::kBevel_Join; |
| 100 | default: |
| 101 | SkASSERT(false); |
| 102 | return SkPaint::kMiter_Join; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 103 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 104 | } |
| 105 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 106 | void applySvgPaint(const SkSVGRenderContext& ctx, const SkSVGPaint& svgPaint, SkPaint* p) { |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 107 | switch (svgPaint.type()) { |
| 108 | case SkSVGPaint::Type::kColor: |
| 109 | p->setColor(SkColorSetA(svgPaint.color(), p->getAlpha())); |
| 110 | break; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 111 | case SkSVGPaint::Type::kIRI: { |
Florin Malita | 27aeb12 | 2020-04-08 15:02:02 -0400 | [diff] [blame] | 112 | const auto node = ctx.findNodeById(svgPaint.iri()); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 113 | if (!node || !node->asPaint(ctx, p)) { |
| 114 | p->setColor(SK_ColorTRANSPARENT); |
| 115 | } |
| 116 | break; |
| 117 | } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 118 | case SkSVGPaint::Type::kCurrentColor: |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 119 | p->setColor(*ctx.presentationContext().fInherited.fColor); |
| 120 | break; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 121 | case SkSVGPaint::Type::kNone: |
| 122 | // Fall through. |
| 123 | case SkSVGPaint::Type::kInherit: |
| 124 | break; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 125 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 126 | } |
| 127 | |
fmalita | a26cab0 | 2016-08-29 05:54:42 -0700 | [diff] [blame] | 128 | inline uint8_t opacity_to_alpha(SkScalar o) { |
| 129 | return SkTo<uint8_t>(SkScalarRoundToInt(o * 255)); |
| 130 | } |
| 131 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 132 | // Commit the selected attribute to the paint cache. |
| 133 | template <SkSVGAttribute> |
| 134 | void commitToPaint(const SkSVGPresentationAttributes&, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 135 | const SkSVGRenderContext&, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 136 | SkSVGPresentationContext*); |
| 137 | |
| 138 | template <> |
| 139 | void commitToPaint<SkSVGAttribute::kFill>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 140 | const SkSVGRenderContext& ctx, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 141 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 142 | const auto& fill = *attrs.fFill; |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 143 | SkASSERT(fill.type() != SkSVGPaint::Type::kInherit); |
| 144 | |
| 145 | applySvgPaint(ctx, fill, &pctx->fFillPaint); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 146 | } |
| 147 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 148 | template <> |
| 149 | void commitToPaint<SkSVGAttribute::kStroke>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 150 | const SkSVGRenderContext& ctx, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 151 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 152 | const auto& stroke = *attrs.fStroke; |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 153 | SkASSERT(stroke.type() != SkSVGPaint::Type::kInherit); |
| 154 | |
| 155 | applySvgPaint(ctx, stroke, &pctx->fStrokePaint); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <> |
| 159 | void commitToPaint<SkSVGAttribute::kFillOpacity>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 160 | const SkSVGRenderContext&, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 161 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 162 | pctx->fFillPaint.setAlpha(opacity_to_alpha(*attrs.fFillOpacity)); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | template <> |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 166 | void commitToPaint<SkSVGAttribute::kStrokeDashArray>(const SkSVGPresentationAttributes& attrs, |
| 167 | const SkSVGRenderContext& ctx, |
| 168 | SkSVGPresentationContext* pctx) { |
| 169 | const auto& dashArray = attrs.fStrokeDashArray.get(); |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 170 | SkASSERT(dashArray->type() != SkSVGDashArray::Type::kInherit); |
| 171 | |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 172 | if (dashArray->type() != SkSVGDashArray::Type::kDashArray) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | const auto count = dashArray->dashArray().count(); |
| 177 | SkSTArray<128, SkScalar, true> intervals(count); |
| 178 | for (const auto& dash : dashArray->dashArray()) { |
| 179 | intervals.push_back(ctx.lengthContext().resolve(dash, |
| 180 | SkSVGLengthContext::LengthType::kOther)); |
| 181 | } |
| 182 | |
| 183 | if (count & 1) { |
| 184 | // If an odd number of values is provided, then the list of values |
| 185 | // is repeated to yield an even number of values. |
| 186 | intervals.push_back_n(count); |
Florin Malita | 5076bd2 | 2020-04-09 07:41:52 -0400 | [diff] [blame] | 187 | memcpy(intervals.begin() + count, intervals.begin(), count * sizeof(SkScalar)); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | SkASSERT((intervals.count() & 1) == 0); |
| 191 | |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 192 | const SkScalar phase = ctx.lengthContext().resolve(*pctx->fInherited.fStrokeDashOffset, |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 193 | SkSVGLengthContext::LengthType::kOther); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 194 | pctx->fStrokePaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(), |
| 195 | intervals.count(), |
| 196 | phase)); |
| 197 | } |
| 198 | |
| 199 | template <> |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 200 | void commitToPaint<SkSVGAttribute::kStrokeDashOffset>(const SkSVGPresentationAttributes&, |
| 201 | const SkSVGRenderContext&, |
| 202 | SkSVGPresentationContext*) { |
| 203 | // Applied via kStrokeDashArray. |
| 204 | } |
| 205 | |
| 206 | template <> |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 207 | void commitToPaint<SkSVGAttribute::kStrokeLineCap>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 208 | const SkSVGRenderContext&, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 209 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 210 | const auto& cap = *attrs.fStrokeLineCap; |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 211 | SkASSERT(cap.type() != SkSVGLineCap::Type::kInherit); |
| 212 | |
| 213 | pctx->fStrokePaint.setStrokeCap(toSkCap(cap)); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | template <> |
| 217 | void commitToPaint<SkSVGAttribute::kStrokeLineJoin>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 218 | const SkSVGRenderContext&, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 219 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 220 | const auto& join = *attrs.fStrokeLineJoin; |
Florin Malita | 0929425 | 2020-04-09 08:54:29 -0400 | [diff] [blame] | 221 | SkASSERT(join.type() != SkSVGLineJoin::Type::kInherit); |
| 222 | |
| 223 | pctx->fStrokePaint.setStrokeJoin(toSkJoin(join)); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | template <> |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 227 | void commitToPaint<SkSVGAttribute::kStrokeMiterLimit>(const SkSVGPresentationAttributes& attrs, |
| 228 | const SkSVGRenderContext&, |
| 229 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 230 | pctx->fStrokePaint.setStrokeMiter(*attrs.fStrokeMiterLimit); |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | template <> |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 234 | void commitToPaint<SkSVGAttribute::kStrokeOpacity>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 235 | const SkSVGRenderContext&, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 236 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 237 | pctx->fStrokePaint.setAlpha(opacity_to_alpha(*attrs.fStrokeOpacity)); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | template <> |
| 241 | void commitToPaint<SkSVGAttribute::kStrokeWidth>(const SkSVGPresentationAttributes& attrs, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 242 | const SkSVGRenderContext& ctx, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 243 | SkSVGPresentationContext* pctx) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 244 | auto strokeWidth = ctx.lengthContext().resolve(*attrs.fStrokeWidth, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 245 | SkSVGLengthContext::LengthType::kOther); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 246 | pctx->fStrokePaint.setStrokeWidth(strokeWidth); |
| 247 | } |
| 248 | |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 249 | template <> |
| 250 | void commitToPaint<SkSVGAttribute::kFillRule>(const SkSVGPresentationAttributes&, |
| 251 | const SkSVGRenderContext&, |
| 252 | SkSVGPresentationContext*) { |
| 253 | // Not part of the SkPaint state; applied to the path at render time. |
| 254 | } |
| 255 | |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 256 | template <> |
| 257 | void commitToPaint<SkSVGAttribute::kClipRule>(const SkSVGPresentationAttributes&, |
| 258 | const SkSVGRenderContext&, |
| 259 | SkSVGPresentationContext*) { |
| 260 | // Not part of the SkPaint state; applied to the path at clip time. |
| 261 | } |
| 262 | |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 263 | template <> |
| 264 | void commitToPaint<SkSVGAttribute::kVisibility>(const SkSVGPresentationAttributes&, |
| 265 | const SkSVGRenderContext&, |
| 266 | SkSVGPresentationContext*) { |
| 267 | // Not part of the SkPaint state; queried to veto rendering. |
| 268 | } |
| 269 | |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 270 | template <> |
| 271 | void commitToPaint<SkSVGAttribute::kColor>(const SkSVGPresentationAttributes&, |
| 272 | const SkSVGRenderContext&, |
| 273 | SkSVGPresentationContext*) { |
| 274 | // Not part of the SkPaint state; applied via 'currentColor' color value |
| 275 | } |
| 276 | |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 277 | template <> |
| 278 | void commitToPaint<SkSVGAttribute::kFontFamily>(const SkSVGPresentationAttributes&, |
| 279 | const SkSVGRenderContext&, |
| 280 | SkSVGPresentationContext*) { |
| 281 | // Not part of the SkPaint state; applied at render time. |
| 282 | } |
| 283 | |
| 284 | template <> |
| 285 | void commitToPaint<SkSVGAttribute::kFontSize>(const SkSVGPresentationAttributes&, |
| 286 | const SkSVGRenderContext&, |
| 287 | SkSVGPresentationContext*) { |
| 288 | // Not part of the SkPaint state; applied at render time. |
| 289 | } |
| 290 | |
| 291 | template <> |
| 292 | void commitToPaint<SkSVGAttribute::kFontStyle>(const SkSVGPresentationAttributes&, |
| 293 | const SkSVGRenderContext&, |
| 294 | SkSVGPresentationContext*) { |
| 295 | // Not part of the SkPaint state; applied at render time. |
| 296 | } |
| 297 | |
| 298 | template <> |
| 299 | void commitToPaint<SkSVGAttribute::kFontWeight>(const SkSVGPresentationAttributes&, |
| 300 | const SkSVGRenderContext&, |
| 301 | SkSVGPresentationContext*) { |
| 302 | // Not part of the SkPaint state; applied at render time. |
| 303 | } |
| 304 | |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame^] | 305 | template <> |
| 306 | void commitToPaint<SkSVGAttribute::kTextAnchor>(const SkSVGPresentationAttributes&, |
| 307 | const SkSVGRenderContext&, |
| 308 | SkSVGPresentationContext*) { |
| 309 | // Not part of the SkPaint state; applied at render time. |
| 310 | } |
| 311 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 312 | } // namespace |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 313 | |
| 314 | SkSVGPresentationContext::SkSVGPresentationContext() |
| 315 | : fInherited(SkSVGPresentationAttributes::MakeInitial()) { |
| 316 | |
| 317 | fFillPaint.setStyle(SkPaint::kFill_Style); |
| 318 | fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 319 | |
| 320 | // TODO: drive AA off presentation attrs also (shape-rendering?) |
| 321 | fFillPaint.setAntiAlias(true); |
| 322 | fStrokePaint.setAntiAlias(true); |
| 323 | |
| 324 | // Commit initial values to the paint cache. |
Leon Scroggins III | 577536a | 2020-07-31 13:47:50 -0400 | [diff] [blame] | 325 | SkCanvas fakeCanvas(0, 0); |
| 326 | SkSVGRenderContext fake(&fakeCanvas, SkSVGIDMapper(), SkSVGLengthContext(SkSize::Make(0, 0)), |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 327 | *this, nullptr); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 328 | |
Leon Scroggins III | 577536a | 2020-07-31 13:47:50 -0400 | [diff] [blame] | 329 | commitToPaint<SkSVGAttribute::kFill>(fInherited, fake, this); |
| 330 | commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, fake, this); |
| 331 | commitToPaint<SkSVGAttribute::kStroke>(fInherited, fake, this); |
| 332 | commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, fake, this); |
| 333 | commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, fake, this); |
| 334 | commitToPaint<SkSVGAttribute::kStrokeMiterLimit>(fInherited, fake, this); |
| 335 | commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, fake, this); |
| 336 | commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, fake, this); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 337 | } |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 338 | |
| 339 | SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 340 | const SkSVGIDMapper& mapper, |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 341 | const SkSVGLengthContext& lctx, |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 342 | const SkSVGPresentationContext& pctx, |
| 343 | const SkSVGNode* node) |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 344 | : fIDMapper(mapper) |
| 345 | , fLengthContext(lctx) |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 346 | , fPresentationContext(pctx) |
| 347 | , fCanvas(canvas) |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 348 | , fCanvasSaveCount(canvas->getSaveCount()) |
| 349 | , fNode(node) {} |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 350 | |
| 351 | SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other) |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 352 | : SkSVGRenderContext(other.fCanvas, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 353 | other.fIDMapper, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 354 | *other.fLengthContext, |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 355 | *other.fPresentationContext, |
| 356 | other.fNode) {} |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 357 | |
Florin Malita | 1aa1bb6 | 2017-10-11 14:34:33 -0400 | [diff] [blame] | 358 | SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other, SkCanvas* canvas) |
| 359 | : SkSVGRenderContext(canvas, |
| 360 | other.fIDMapper, |
| 361 | *other.fLengthContext, |
Tyler Denniston | 53281c7 | 2020-10-22 15:54:24 -0400 | [diff] [blame] | 362 | *other.fPresentationContext, |
| 363 | other.fNode) {} |
| 364 | |
| 365 | SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other, const SkSVGNode* node) |
| 366 | : SkSVGRenderContext(other.fCanvas, |
| 367 | other.fIDMapper, |
| 368 | *other.fLengthContext, |
| 369 | *other.fPresentationContext, |
| 370 | node) {} |
Florin Malita | 1aa1bb6 | 2017-10-11 14:34:33 -0400 | [diff] [blame] | 371 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 372 | SkSVGRenderContext::~SkSVGRenderContext() { |
| 373 | fCanvas->restoreToCount(fCanvasSaveCount); |
| 374 | } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 375 | |
Florin Malita | 27aeb12 | 2020-04-08 15:02:02 -0400 | [diff] [blame] | 376 | SkSVGRenderContext::BorrowedNode SkSVGRenderContext::findNodeById(const SkString& id) const { |
| 377 | return BorrowedNode(fIDMapper.find(id)); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 378 | } |
| 379 | |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 380 | void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs, |
| 381 | uint32_t flags) { |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 382 | |
| 383 | #define ApplyLazyInheritedAttribute(ATTR) \ |
| 384 | do { \ |
| 385 | /* All attributes should be defined on the inherited context. */ \ |
| 386 | SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \ |
| 387 | const auto* value = attrs.f ## ATTR.getMaybeNull(); \ |
| 388 | if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()) { \ |
| 389 | /* Update the local attribute value */ \ |
| 390 | fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \ |
| 391 | /* Update the cached paints */ \ |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 392 | commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \ |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 393 | fPresentationContext.writable()); \ |
| 394 | } \ |
| 395 | } while (false) |
| 396 | |
| 397 | ApplyLazyInheritedAttribute(Fill); |
| 398 | ApplyLazyInheritedAttribute(FillOpacity); |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 399 | ApplyLazyInheritedAttribute(FillRule); |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 400 | ApplyLazyInheritedAttribute(FontFamily); |
| 401 | ApplyLazyInheritedAttribute(FontSize); |
| 402 | ApplyLazyInheritedAttribute(FontStyle); |
| 403 | ApplyLazyInheritedAttribute(FontWeight); |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 404 | ApplyLazyInheritedAttribute(ClipRule); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 405 | ApplyLazyInheritedAttribute(Stroke); |
Florin Malita | e1dadd7 | 2017-10-13 18:18:32 -0400 | [diff] [blame] | 406 | ApplyLazyInheritedAttribute(StrokeDashOffset); |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 407 | ApplyLazyInheritedAttribute(StrokeDashArray); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 408 | ApplyLazyInheritedAttribute(StrokeLineCap); |
| 409 | ApplyLazyInheritedAttribute(StrokeLineJoin); |
Florin Malita | 4de426b | 2017-10-09 12:57:41 -0400 | [diff] [blame] | 410 | ApplyLazyInheritedAttribute(StrokeMiterLimit); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 411 | ApplyLazyInheritedAttribute(StrokeOpacity); |
| 412 | ApplyLazyInheritedAttribute(StrokeWidth); |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame^] | 413 | ApplyLazyInheritedAttribute(TextAnchor); |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 414 | ApplyLazyInheritedAttribute(Visibility); |
Tyler Denniston | 6c8314e | 2020-04-09 14:14:10 -0400 | [diff] [blame] | 415 | ApplyLazyInheritedAttribute(Color); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 416 | |
Tyler Denniston | df1b014 | 2020-04-20 10:52:21 -0400 | [diff] [blame] | 417 | // Local 'color' attribute: update paints for attributes that are set to 'currentColor'. |
| 418 | if (attrs.fColor.isValid()) { |
| 419 | updatePaintsWithCurrentColor(attrs); |
| 420 | } |
| 421 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 422 | #undef ApplyLazyInheritedAttribute |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 423 | |
| 424 | // Uninherited attributes. Only apply to the current context. |
| 425 | |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 426 | if (auto* opacity = attrs.fOpacity.getMaybeNull()) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 427 | this->applyOpacity(*opacity, flags); |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 428 | } |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 429 | |
| 430 | if (auto* clip = attrs.fClipPath.getMaybeNull()) { |
| 431 | this->applyClip(*clip); |
| 432 | } |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void SkSVGRenderContext::applyOpacity(SkScalar opacity, uint32_t flags) { |
| 436 | if (opacity >= 1) { |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | const bool hasFill = SkToBool(this->fillPaint()); |
| 441 | const bool hasStroke = SkToBool(this->strokePaint()); |
| 442 | |
| 443 | // We can apply the opacity as paint alpha iif it only affects one atomic draw. |
| 444 | // For now, this means a) the target node doesn't have any descendants, and |
| 445 | // b) it only has a stroke or a fill (but not both). Going forward, we may need |
| 446 | // to refine this heuristic (e.g. to accommodate markers). |
| 447 | if ((flags & kLeaf) && (hasFill ^ hasStroke)) { |
| 448 | auto* pctx = fPresentationContext.writable(); |
| 449 | if (hasFill) { |
| 450 | pctx->fFillPaint.setAlpha( |
| 451 | SkScalarRoundToInt(opacity * pctx->fFillPaint.getAlpha())); |
| 452 | } else { |
| 453 | pctx->fStrokePaint.setAlpha( |
| 454 | SkScalarRoundToInt(opacity * pctx->fStrokePaint.getAlpha())); |
| 455 | } |
| 456 | } else { |
| 457 | // Expensive, layer-based fall back. |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 458 | SkPaint opacityPaint; |
fmalita | bef51c2 | 2016-09-20 15:45:57 -0700 | [diff] [blame] | 459 | opacityPaint.setAlpha(opacity_to_alpha(opacity)); |
fmalita | 6fb0648 | 2016-08-15 12:45:11 -0700 | [diff] [blame] | 460 | // Balanced in the destructor, via restoreToCount(). |
| 461 | fCanvas->saveLayer(nullptr, &opacityPaint); |
| 462 | } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Florin Malita | b36be14 | 2017-10-11 14:11:16 -0400 | [diff] [blame] | 465 | void SkSVGRenderContext::saveOnce() { |
| 466 | // The canvas only needs to be saved once, per local SkSVGRenderContext. |
| 467 | if (fCanvas->getSaveCount() == fCanvasSaveCount) { |
| 468 | fCanvas->save(); |
| 469 | } |
| 470 | |
| 471 | SkASSERT(fCanvas->getSaveCount() > fCanvasSaveCount); |
| 472 | } |
| 473 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 474 | void SkSVGRenderContext::applyClip(const SkSVGClip& clip) { |
| 475 | if (clip.type() != SkSVGClip::Type::kIRI) { |
| 476 | return; |
| 477 | } |
| 478 | |
Florin Malita | 27aeb12 | 2020-04-08 15:02:02 -0400 | [diff] [blame] | 479 | const auto clipNode = this->findNodeById(clip.iri()); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 480 | if (!clipNode || clipNode->tag() != SkSVGTag::kClipPath) { |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | const SkPath clipPath = clipNode->asPath(*this); |
| 485 | |
Florin Malita | 7d52988 | 2016-12-08 16:04:24 -0500 | [diff] [blame] | 486 | // We use the computed clip path in two ways: |
| 487 | // |
| 488 | // - apply to the current canvas, for drawing |
| 489 | // - track in the presentation context, for asPath() composition |
| 490 | // |
| 491 | // TODO: the two uses are exclusive, avoid canvas churn when non needed. |
| 492 | |
Florin Malita | b36be14 | 2017-10-11 14:11:16 -0400 | [diff] [blame] | 493 | this->saveOnce(); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 494 | |
| 495 | fCanvas->clipPath(clipPath, true); |
Florin Malita | 7d52988 | 2016-12-08 16:04:24 -0500 | [diff] [blame] | 496 | fClipPath.set(clipPath); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Tyler Denniston | df1b014 | 2020-04-20 10:52:21 -0400 | [diff] [blame] | 499 | void SkSVGRenderContext::updatePaintsWithCurrentColor(const SkSVGPresentationAttributes& attrs) { |
| 500 | // Of the attributes that can use currentColor: |
| 501 | // https://www.w3.org/TR/SVG11/color.html#ColorProperty |
| 502 | // Only fill and stroke require paint updates. The others are resolved at render time. |
| 503 | |
| 504 | if (fPresentationContext->fInherited.fFill->type() == SkSVGPaint::Type::kCurrentColor) { |
| 505 | applySvgPaint(*this, *fPresentationContext->fInherited.fFill, |
| 506 | &fPresentationContext.writable()->fFillPaint); |
| 507 | } |
| 508 | |
| 509 | if (fPresentationContext->fInherited.fStroke->type() == SkSVGPaint::Type::kCurrentColor) { |
| 510 | applySvgPaint(*this, *fPresentationContext->fInherited.fStroke, |
| 511 | &fPresentationContext.writable()->fStrokePaint); |
| 512 | } |
| 513 | } |
| 514 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 515 | const SkPaint* SkSVGRenderContext::fillPaint() const { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 516 | const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill->type(); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 517 | return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPaint : nullptr; |
| 518 | } |
| 519 | |
| 520 | const SkPaint* SkSVGRenderContext::strokePaint() const { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 521 | const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke->type(); |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 522 | return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStrokePaint : nullptr; |
| 523 | } |