blob: a1cc8f75f0a7c55d9fe64a1f5da95f01b1c89fe8 [file] [log] [blame]
fmalita6ceef3d2016-07-26 18:46:34 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "experimental/svg/model/SkSVGRenderContext.h"
9#include "experimental/svg/model/SkSVGSVG.h"
Florin Malitaf4403e72020-04-10 14:14:04 +000010#include "experimental/svg/model/SkSVGValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
fmalita6ceef3d2016-07-26 18:46:34 -070012
fmalita58649cc2016-07-29 08:52:03 -070013SkSVGSVG::SkSVGSVG() : INHERITED(SkSVGTag::kSvg) { }
fmalitabffc2562016-08-03 10:21:11 -070014
fmalita397a5172016-08-08 11:38:55 -070015bool SkSVGSVG::onPrepareToRender(SkSVGRenderContext* ctx) const {
16 auto viewPortRect = ctx->lengthContext().resolveRect(fX, fY, fWidth, fHeight);
Mike Reedc80ee452020-05-21 16:05:02 +000017 auto contentMatrix = SkMatrix::MakeTrans(viewPortRect.x(), viewPortRect.y());
fmalita397a5172016-08-08 11:38:55 -070018 auto viewPort = SkSize::Make(viewPortRect.width(), viewPortRect.height());
19
20 if (fViewBox.isValid()) {
21 const SkRect& viewBox = *fViewBox.get();
22
23 // An empty viewbox disables rendering.
24 if (viewBox.isEmpty()) {
25 return false;
26 }
27
28 // A viewBox overrides the intrinsic viewport.
29 viewPort = SkSize::Make(viewBox.width(), viewBox.height());
30
31 contentMatrix.preConcat(
32 SkMatrix::MakeRectToRect(viewBox, viewPortRect, SkMatrix::kFill_ScaleToFit));
33 }
34
35 if (!contentMatrix.isIdentity()) {
Florin Malitab36be142017-10-11 14:11:16 -040036 ctx->saveOnce();
fmalita397a5172016-08-08 11:38:55 -070037 ctx->canvas()->concat(contentMatrix);
38 }
39
40 if (viewPort != ctx->lengthContext().viewPort()) {
41 ctx->writableLengthContext()->setViewPort(viewPort);
42 }
43
44 return this->INHERITED::onPrepareToRender(ctx);
45}
46
fmalitabffc2562016-08-03 10:21:11 -070047void SkSVGSVG::setX(const SkSVGLength& x) {
48 fX = x;
49}
50
51void SkSVGSVG::setY(const SkSVGLength& y) {
52 fY = y;
53}
54
55void SkSVGSVG::setWidth(const SkSVGLength& w) {
56 fWidth = w;
57}
58
59void SkSVGSVG::setHeight(const SkSVGLength& h) {
60 fHeight = h;
61}
62
fmalita397a5172016-08-08 11:38:55 -070063void SkSVGSVG::setViewBox(const SkSVGViewBoxType& vb) {
64 fViewBox.set(vb);
65}
66
Florin Malitaf4403e72020-04-10 14:14:04 +000067void SkSVGSVG::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalitabffc2562016-08-03 10:21:11 -070068 switch (attr) {
69 case SkSVGAttribute::kX:
Florin Malitaf4403e72020-04-10 14:14:04 +000070 if (const auto* x = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070071 this->setX(*x);
72 }
73 break;
74 case SkSVGAttribute::kY:
Florin Malitaf4403e72020-04-10 14:14:04 +000075 if (const auto* y = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070076 this->setY(*y);
77 }
78 break;
79 case SkSVGAttribute::kWidth:
Florin Malitaf4403e72020-04-10 14:14:04 +000080 if (const auto* w = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070081 this->setWidth(*w);
82 }
83 break;
84 case SkSVGAttribute::kHeight:
Florin Malitaf4403e72020-04-10 14:14:04 +000085 if (const auto* h = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070086 this->setHeight(*h);
87 }
88 break;
fmalita397a5172016-08-08 11:38:55 -070089 case SkSVGAttribute::kViewBox:
Florin Malitaf4403e72020-04-10 14:14:04 +000090 if (const auto* vb = v.as<SkSVGViewBoxValue>()) {
fmalita397a5172016-08-08 11:38:55 -070091 this->setViewBox(*vb);
92 }
93 break;
fmalitabffc2562016-08-03 10:21:11 -070094 default:
95 this->INHERITED::onSetAttribute(attr, v);
96 }
97}
fmalitae1baa7c2016-09-14 12:04:30 -070098
Florin Malitaf005c252020-04-08 10:10:53 -040099// https://www.w3.org/TR/SVG11/coords.html#IntrinsicSizing
fmalitae1baa7c2016-09-14 12:04:30 -0700100SkSize SkSVGSVG::intrinsicSize(const SkSVGLengthContext& lctx) const {
101 // Percentage values do not provide an intrinsic size.
102 if (fWidth.unit() == SkSVGLength::Unit::kPercentage ||
103 fHeight.unit() == SkSVGLength::Unit::kPercentage) {
104 return SkSize::Make(0, 0);
105 }
106
107 return SkSize::Make(lctx.resolve(fWidth, SkSVGLengthContext::LengthType::kHorizontal),
108 lctx.resolve(fHeight, SkSVGLengthContext::LengthType::kVertical));
109}