Flutter Engine
The Flutter Engine
SkSVGImage.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 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
14#include "src/utils/SkOSPath.h"
15
16bool SkSVGImage::parseAndSetAttribute(const char* n, const char* v) {
18 this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) ||
19 this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) ||
20 this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) ||
21 this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) ||
22 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", n, v)) ||
23 this->setPreserveAspectRatio(SkSVGAttributeParser::parse<SkSVGPreserveAspectRatio>(
24 "preserveAspectRatio", n, v));
25}
26
28 // Width or height of 0 disables rendering per spec:
29 // https://www.w3.org/TR/SVG11/struct.html#ImageElement
30 return !fHref.iri().isEmpty() && fWidth.value() > 0 && fHeight.value() > 0 &&
32}
33
35 const SkSVGIRI& href) {
36 // TODO: It may be better to use the SVG 'id' attribute as the asset id, to allow
37 // clients to perform asset substitution based on element id.
39 switch (href.type()) {
41 imageAsset = rp->loadImageAsset("", href.iri().c_str(), "");
42 break;
44 const auto path = SkOSPath::Dirname(href.iri().c_str());
45 const auto name = SkOSPath::Basename(href.iri().c_str());
46 imageAsset = rp->loadImageAsset(path.c_str(), name.c_str(), /* id */ name.c_str());
47 break;
48 }
49 default:
50 SkDebugf("error loading image: unhandled iri type %d\n", (int)href.type());
51 return nullptr;
52 }
53
54 return imageAsset ? imageAsset->getFrameData(0).image : nullptr;
55}
56
58 const SkSVGIRI& iri,
59 const SkRect& viewPort,
61 SkASSERT(rp);
62
63 // TODO: svg sources
65 if (!image) {
66 return {};
67 }
68
69 // Per spec: raster content has implicit viewbox of '0 0 width height'.
70 const SkRect viewBox = SkRect::Make(image->bounds());
71
72 // Map and place at x, y specified by viewport
73 const SkMatrix m = ComputeViewboxMatrix(viewBox, viewPort, par);
74 const SkRect dst = m.mapRect(viewBox).makeOffset(viewPort.fLeft, viewPort.fTop);
75
76 return {std::move(image), dst};
77}
78
80 // Per spec: x, w, width, height attributes establish the new viewport.
81 const SkSVGLengthContext& lctx = ctx.lengthContext();
82 const SkRect viewPort = lctx.resolveRect(fX, fY, fWidth, fHeight);
83
84 const auto imgInfo = LoadImage(ctx.resourceProvider(), fHref, viewPort, fPreserveAspectRatio);
85 if (!imgInfo.fImage) {
86 SkDebugf("can't render image: load image failed\n");
87 return;
88 }
89
90 // TODO: image-rendering property
91 ctx.canvas()->drawImageRect(
92 imgInfo.fImage, imgInfo.fDst, SkSamplingOptions(SkFilterMode::kLinear));
93}
94
96
98 const SkSVGLengthContext& lctx = ctx.lengthContext();
99 return lctx.resolveRect(fX, fY, fWidth, fHeight);
100}
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static sk_sp< SkImage > LoadImage(const sk_sp< skresources::ResourceProvider > &rp, const SkSVGIRI &href)
Definition: SkSVGImage.cpp:34
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
SkIRect bounds() const
Definition: SkImage.h:303
static SkString Basename(const char *fullPath)
Definition: SkOSPath.cpp:23
static SkString Dirname(const char *fullPath)
Definition: SkOSPath.cpp:36
Definition: SkPath.h:59
const SkSVGStringType & iri() const
Definition: SkSVGTypes.h:164
Type type() const
Definition: SkSVGTypes.h:163
void onRender(const SkSVGRenderContext &) const override
Definition: SkSVGImage.cpp:79
bool parseAndSetAttribute(const char *, const char *) override
Definition: SkSVGImage.cpp:16
bool onPrepareToRender(SkSVGRenderContext *) const override
Definition: SkSVGImage.cpp:27
SkPath onAsPath(const SkSVGRenderContext &) const override
Definition: SkSVGImage.cpp:95
static ImageInfo LoadImage(const sk_sp< skresources::ResourceProvider > &, const SkSVGIRI &, const SkRect &, SkSVGPreserveAspectRatio)
Definition: SkSVGImage.cpp:57
SkRect onObjectBoundingBox(const SkSVGRenderContext &) const override
Definition: SkSVGImage.cpp:97
SkRect resolveRect(const SkSVGLength &x, const SkSVGLength &y, const SkSVGLength &w, const SkSVGLength &h) const
virtual bool parseAndSetAttribute(const char *name, const char *value)
Definition: SkSVGNode.cpp:90
static SkMatrix ComputeViewboxMatrix(const SkRect &, const SkRect &, SkSVGPreserveAspectRatio)
Definition: SkSVGNode.cpp:132
SkCanvas * canvas() const
const SkSVGLengthContext & lengthContext() const
const sk_sp< skresources::ResourceProvider > & resourceProvider() const
bool onPrepareToRender(SkSVGRenderContext *) const override
const char * c_str() const
Definition: SkString.h:133
virtual FrameData getFrameData(float t)
virtual sk_sp< ImageAsset > loadImageAsset(const char[], const char[], const char[]) const
Definition: SkResources.h:165
sk_sp< const SkImage > image
Definition: SkRecords.h:269
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
dst
Definition: cp.py:12
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15