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" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 9 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 10 | #include "modules/svg/include/SkSVGSVG.h" |
| 11 | #include "modules/svg/include/SkSVGValue.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 12 | |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 13 | static SkMatrix ViewboxMatrix(const SkRect& view_box, const SkRect& view_port, |
| 14 | const SkSVGPreserveAspectRatio& par) { |
| 15 | SkASSERT(!view_box.isEmpty()); |
| 16 | SkASSERT(!view_port.isEmpty()); |
| 17 | |
| 18 | auto compute_scale = [&]() -> SkV2 { |
| 19 | const auto sx = view_port.width() / view_box.width(), |
| 20 | sy = view_port.height() / view_box.height(); |
| 21 | |
| 22 | if (par.fAlign == SkSVGPreserveAspectRatio::kNone) { |
| 23 | // none -> anisotropic scaling, regardless of fScale |
| 24 | return {sx,sy}; |
| 25 | } |
| 26 | |
| 27 | // isotropic scaling |
| 28 | const auto s = par.fScale == SkSVGPreserveAspectRatio::kMeet |
| 29 | ? std::min(sx, sy) |
| 30 | : std::max(sx, sy); |
| 31 | return {s,s}; |
| 32 | }; |
| 33 | |
| 34 | auto compute_trans = [&](const SkV2& scale) -> SkV2 { |
| 35 | static constexpr float gAlignCoeffs[] = { |
| 36 | 0.0f, // Min |
| 37 | 0.5f, // Mid |
| 38 | 1.0f // Max |
| 39 | }; |
| 40 | |
| 41 | const size_t x_coeff = par.fAlign>>0 & 0x03, |
| 42 | y_coeff = par.fAlign>>2 & 0x03; |
| 43 | |
| 44 | SkASSERT(x_coeff < SK_ARRAY_COUNT(gAlignCoeffs) && |
| 45 | y_coeff < SK_ARRAY_COUNT(gAlignCoeffs)); |
| 46 | |
| 47 | const auto tx = -view_box.x() * scale.x, |
| 48 | ty = -view_box.y() * scale.y, |
| 49 | dx = view_port.width() - view_box.width() * scale.x, |
| 50 | dy = view_port.height() - view_box.height()* scale.y; |
| 51 | |
| 52 | return { |
| 53 | tx + dx * gAlignCoeffs[x_coeff], |
| 54 | ty + dy * gAlignCoeffs[y_coeff] |
| 55 | }; |
| 56 | }; |
| 57 | |
| 58 | const auto s = compute_scale(), |
| 59 | t = compute_trans(s); |
| 60 | |
| 61 | return SkMatrix::Translate(t.x, t.y) * |
| 62 | SkMatrix::Scale(s.x, s.y); |
| 63 | } |
| 64 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 65 | bool SkSVGSVG::onPrepareToRender(SkSVGRenderContext* ctx) const { |
Florin Malita | cdeabca | 2021-01-20 13:21:20 -0500 | [diff] [blame^] | 66 | // x/y are ignored for outermost svg elements |
| 67 | const auto x = fType == Type::kInner ? fX : SkSVGLength(0); |
| 68 | const auto y = fType == Type::kInner ? fY : SkSVGLength(0); |
| 69 | |
| 70 | auto viewPortRect = ctx->lengthContext().resolveRect(x, y, fWidth, fHeight); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 71 | auto contentMatrix = SkMatrix::Translate(viewPortRect.x(), viewPortRect.y()); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 72 | auto viewPort = SkSize::Make(viewPortRect.width(), viewPortRect.height()); |
| 73 | |
| 74 | if (fViewBox.isValid()) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 75 | const SkRect& viewBox = *fViewBox; |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 76 | |
| 77 | // An empty viewbox disables rendering. |
| 78 | if (viewBox.isEmpty()) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // A viewBox overrides the intrinsic viewport. |
| 83 | viewPort = SkSize::Make(viewBox.width(), viewBox.height()); |
| 84 | |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 85 | contentMatrix.preConcat(ViewboxMatrix(viewBox, viewPortRect, fPreserveAspectRatio)); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (!contentMatrix.isIdentity()) { |
Florin Malita | b36be14 | 2017-10-11 14:11:16 -0400 | [diff] [blame] | 89 | ctx->saveOnce(); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 90 | ctx->canvas()->concat(contentMatrix); |
| 91 | } |
| 92 | |
| 93 | if (viewPort != ctx->lengthContext().viewPort()) { |
| 94 | ctx->writableLengthContext()->setViewPort(viewPort); |
| 95 | } |
| 96 | |
| 97 | return this->INHERITED::onPrepareToRender(ctx); |
| 98 | } |
| 99 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 100 | void SkSVGSVG::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 101 | switch (attr) { |
| 102 | case SkSVGAttribute::kX: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 103 | if (const auto* x = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 104 | this->setX(*x); |
| 105 | } |
| 106 | break; |
| 107 | case SkSVGAttribute::kY: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 108 | if (const auto* y = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 109 | this->setY(*y); |
| 110 | } |
| 111 | break; |
| 112 | case SkSVGAttribute::kWidth: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 113 | if (const auto* w = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 114 | this->setWidth(*w); |
| 115 | } |
| 116 | break; |
| 117 | case SkSVGAttribute::kHeight: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 118 | if (const auto* h = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 119 | this->setHeight(*h); |
| 120 | } |
| 121 | break; |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 122 | case SkSVGAttribute::kViewBox: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 123 | if (const auto* vb = v.as<SkSVGViewBoxValue>()) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 124 | this->setViewBox(*vb); |
| 125 | } |
| 126 | break; |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 127 | case SkSVGAttribute::kPreserveAspectRatio: |
| 128 | if (const auto* par = v.as<SkSVGPreserveAspectRatioValue>()) { |
| 129 | this->setPreserveAspectRatio(*par); |
| 130 | } |
| 131 | break; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 132 | default: |
| 133 | this->INHERITED::onSetAttribute(attr, v); |
| 134 | } |
| 135 | } |
fmalita | e1baa7c | 2016-09-14 12:04:30 -0700 | [diff] [blame] | 136 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 137 | // https://www.w3.org/TR/SVG11/coords.html#IntrinsicSizing |
fmalita | e1baa7c | 2016-09-14 12:04:30 -0700 | [diff] [blame] | 138 | SkSize SkSVGSVG::intrinsicSize(const SkSVGLengthContext& lctx) const { |
| 139 | // Percentage values do not provide an intrinsic size. |
| 140 | if (fWidth.unit() == SkSVGLength::Unit::kPercentage || |
| 141 | fHeight.unit() == SkSVGLength::Unit::kPercentage) { |
| 142 | return SkSize::Make(0, 0); |
| 143 | } |
| 144 | |
| 145 | return SkSize::Make(lctx.resolve(fWidth, SkSVGLengthContext::LengthType::kHorizontal), |
| 146 | lctx.resolve(fHeight, SkSVGLengthContext::LengthType::kVertical)); |
| 147 | } |