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 | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 13 | bool SkSVGSVG::onPrepareToRender(SkSVGRenderContext* ctx) const { |
Florin Malita | cdeabca | 2021-01-20 13:21:20 -0500 | [diff] [blame] | 14 | // x/y are ignored for outermost svg elements |
| 15 | const auto x = fType == Type::kInner ? fX : SkSVGLength(0); |
| 16 | const auto y = fType == Type::kInner ? fY : SkSVGLength(0); |
| 17 | |
| 18 | auto viewPortRect = ctx->lengthContext().resolveRect(x, y, fWidth, fHeight); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 19 | auto contentMatrix = SkMatrix::Translate(viewPortRect.x(), viewPortRect.y()); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 20 | auto viewPort = SkSize::Make(viewPortRect.width(), viewPortRect.height()); |
| 21 | |
| 22 | if (fViewBox.isValid()) { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 23 | const SkRect& viewBox = *fViewBox; |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 24 | |
| 25 | // An empty viewbox disables rendering. |
| 26 | if (viewBox.isEmpty()) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | // A viewBox overrides the intrinsic viewport. |
| 31 | viewPort = SkSize::Make(viewBox.width(), viewBox.height()); |
| 32 | |
Tyler Denniston | 1f4cd07 | 2021-02-05 09:08:33 -0500 | [diff] [blame] | 33 | contentMatrix.preConcat(ComputeViewboxMatrix(viewBox, viewPortRect, fPreserveAspectRatio)); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | if (!contentMatrix.isIdentity()) { |
Florin Malita | b36be14 | 2017-10-11 14:11:16 -0400 | [diff] [blame] | 37 | ctx->saveOnce(); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 38 | ctx->canvas()->concat(contentMatrix); |
| 39 | } |
| 40 | |
| 41 | if (viewPort != ctx->lengthContext().viewPort()) { |
| 42 | ctx->writableLengthContext()->setViewPort(viewPort); |
| 43 | } |
| 44 | |
| 45 | return this->INHERITED::onPrepareToRender(ctx); |
| 46 | } |
| 47 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 48 | void SkSVGSVG::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 49 | switch (attr) { |
| 50 | case SkSVGAttribute::kX: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 51 | if (const auto* x = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 52 | this->setX(*x); |
| 53 | } |
| 54 | break; |
| 55 | case SkSVGAttribute::kY: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 56 | if (const auto* y = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 57 | this->setY(*y); |
| 58 | } |
| 59 | break; |
| 60 | case SkSVGAttribute::kWidth: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 61 | if (const auto* w = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 62 | this->setWidth(*w); |
| 63 | } |
| 64 | break; |
| 65 | case SkSVGAttribute::kHeight: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 66 | if (const auto* h = v.as<SkSVGLengthValue>()) { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 67 | this->setHeight(*h); |
| 68 | } |
| 69 | break; |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 70 | case SkSVGAttribute::kViewBox: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 71 | if (const auto* vb = v.as<SkSVGViewBoxValue>()) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 72 | this->setViewBox(*vb); |
| 73 | } |
| 74 | break; |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 75 | case SkSVGAttribute::kPreserveAspectRatio: |
| 76 | if (const auto* par = v.as<SkSVGPreserveAspectRatioValue>()) { |
| 77 | this->setPreserveAspectRatio(*par); |
| 78 | } |
| 79 | break; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 80 | default: |
| 81 | this->INHERITED::onSetAttribute(attr, v); |
| 82 | } |
| 83 | } |
fmalita | e1baa7c | 2016-09-14 12:04:30 -0700 | [diff] [blame] | 84 | |
Florin Malita | f005c25 | 2020-04-08 10:10:53 -0400 | [diff] [blame] | 85 | // https://www.w3.org/TR/SVG11/coords.html#IntrinsicSizing |
fmalita | e1baa7c | 2016-09-14 12:04:30 -0700 | [diff] [blame] | 86 | SkSize SkSVGSVG::intrinsicSize(const SkSVGLengthContext& lctx) const { |
| 87 | // Percentage values do not provide an intrinsic size. |
| 88 | if (fWidth.unit() == SkSVGLength::Unit::kPercentage || |
| 89 | fHeight.unit() == SkSVGLength::Unit::kPercentage) { |
| 90 | return SkSize::Make(0, 0); |
| 91 | } |
| 92 | |
| 93 | return SkSize::Make(lctx.resolve(fWidth, SkSVGLengthContext::LengthType::kHorizontal), |
| 94 | lctx.resolve(fHeight, SkSVGLengthContext::LengthType::kVertical)); |
| 95 | } |