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