Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSVGSVG.cpp
Go to the documentation of this file.
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
12
13void SkSVGSVG::renderNode(const SkSVGRenderContext& ctx, const SkSVGIRI& iri) const {
14 SkSVGRenderContext localContext(ctx, this);
15 SkSVGRenderContext::BorrowedNode node = localContext.findNodeById(iri);
16 if (!node) {
17 return;
18 }
19
20 if (this->onPrepareToRender(&localContext)) {
21 if (this == node.get()) {
22 this->onRender(ctx);
23 } else {
24 node->render(localContext);
25 }
26 }
27}
28
30 // x/y are ignored for outermost svg elements
31 const auto x = fType == Type::kInner ? fX : SkSVGLength(0);
32 const auto y = fType == Type::kInner ? fY : SkSVGLength(0);
33
34 auto viewPortRect = ctx->lengthContext().resolveRect(x, y, fWidth, fHeight);
35 auto contentMatrix = SkMatrix::Translate(viewPortRect.x(), viewPortRect.y());
36 auto viewPort = SkSize::Make(viewPortRect.width(), viewPortRect.height());
37
38 if (fViewBox.isValid()) {
39 const SkRect& viewBox = *fViewBox;
40
41 // An empty viewbox disables rendering.
42 if (viewBox.isEmpty()) {
43 return false;
44 }
45
46 // A viewBox overrides the intrinsic viewport.
47 viewPort = SkSize::Make(viewBox.width(), viewBox.height());
48
49 contentMatrix.preConcat(ComputeViewboxMatrix(viewBox, viewPortRect, fPreserveAspectRatio));
50 }
51
52 if (!contentMatrix.isIdentity()) {
53 ctx->saveOnce();
54 ctx->canvas()->concat(contentMatrix);
55 }
56
57 if (viewPort != ctx->lengthContext().viewPort()) {
58 ctx->writableLengthContext()->setViewPort(viewPort);
59 }
60
61 return this->INHERITED::onPrepareToRender(ctx);
62}
63
65 switch (attr) {
67 if (const auto* x = v.as<SkSVGLengthValue>()) {
68 this->setX(*x);
69 }
70 break;
72 if (const auto* y = v.as<SkSVGLengthValue>()) {
73 this->setY(*y);
74 }
75 break;
77 if (const auto* w = v.as<SkSVGLengthValue>()) {
78 this->setWidth(*w);
79 }
80 break;
82 if (const auto* h = v.as<SkSVGLengthValue>()) {
83 this->setHeight(*h);
84 }
85 break;
87 if (const auto* vb = v.as<SkSVGViewBoxValue>()) {
88 this->setViewBox(*vb);
89 }
90 break;
92 if (const auto* par = v.as<SkSVGPreserveAspectRatioValue>()) {
93 this->setPreserveAspectRatio(*par);
94 }
95 break;
96 default:
97 this->INHERITED::onSetAttribute(attr, v);
98 }
99}
100
101// https://www.w3.org/TR/SVG11/coords.html#IntrinsicSizing
103 // Percentage values do not provide an intrinsic size.
104 if (fWidth.unit() == SkSVGLength::Unit::kPercentage ||
105 fHeight.unit() == SkSVGLength::Unit::kPercentage) {
106 return SkSize::Make(0, 0);
107 }
108
111}
SkSVGAttribute
void concat(const SkMatrix &matrix)
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
void onRender(const SkSVGRenderContext &) const override
void setViewPort(const SkSize &viewport)
SkRect resolveRect(const SkSVGLength &x, const SkSVGLength &y, const SkSVGLength &w, const SkSVGLength &h) const
const SkSize & viewPort() const
SkScalar resolve(const SkSVGLength &, LengthType) const
void render(const SkSVGRenderContext &) const
Definition SkSVGNode.cpp:28
static SkMatrix ComputeViewboxMatrix(const SkRect &, const SkRect &, SkSVGPreserveAspectRatio)
virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue &)
Definition SkSVGNode.h:168
virtual bool onPrepareToRender(SkSVGRenderContext *) const
Definition SkSVGNode.cpp:62
SkSVGLengthContext * writableLengthContext()
BorrowedNode findNodeById(const SkSVGIRI &) const
SkCanvas * canvas() const
const SkSVGLengthContext & lengthContext() const
void onSetAttribute(SkSVGAttribute, const SkSVGValue &) override
Definition SkSVGSVG.cpp:64
SkSize intrinsicSize(const SkSVGLengthContext &) const
Definition SkSVGSVG.cpp:102
void renderNode(const SkSVGRenderContext &, const SkSVGIRI &iri) const
Definition SkSVGSVG.cpp:13
bool onPrepareToRender(SkSVGRenderContext *) const override
Definition SkSVGSVG.cpp:29
const T * as() const
Definition SkSVGValue.h:36
double y
double x
SkScalar w
SkScalar h
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
bool isEmpty() const
Definition SkRect.h:693
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition SkSize.h:56