blob: ce6ede154657b490e174c877cbb9b7e4364b6523 [file] [log] [blame]
Tyler Denniston8ca46262021-02-02 16:16:21 -05001/*
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
8#include "include/core/SkCanvas.h"
9#include "include/core/SkImage.h"
10#include "modules/svg/include/SkSVGAttributeParser.h"
11#include "modules/svg/include/SkSVGImage.h"
12#include "modules/svg/include/SkSVGRenderContext.h"
13#include "modules/svg/include/SkSVGValue.h"
14#include "src/utils/SkOSPath.h"
15
16bool SkSVGImage::parseAndSetAttribute(const char* n, const char* v) {
17 return INHERITED::parseAndSetAttribute(n, 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)) ||
Tyler Denniston1f4cd072021-02-05 09:08:33 -050022 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", n, v)) ||
23 this->setPreserveAspectRatio(SkSVGAttributeParser::parse<SkSVGPreserveAspectRatio>(
24 "preserveAspectRatio", n, v));
Tyler Denniston8ca46262021-02-02 16:16:21 -050025}
26
27bool SkSVGImage::onPrepareToRender(SkSVGRenderContext* ctx) const {
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 &&
31 INHERITED::onPrepareToRender(ctx);
32}
33
34static sk_sp<SkImage> LoadImage(const sk_sp<skresources::ResourceProvider>& rp,
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.
38 sk_sp<skresources::ImageAsset> imageAsset;
39 switch (href.type()) {
40 case SkSVGIRI::Type::kDataURI:
41 imageAsset = rp->loadImageAsset("", href.iri().c_str(), "");
42 break;
43 case SkSVGIRI::Type::kNonlocal: {
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", href.type());
51 return nullptr;
52 }
53
54 return imageAsset ? imageAsset->getFrameData(0).image : nullptr;
55}
56
Tyler Denniston2992fb02021-05-04 14:17:27 -040057SkSVGImage::ImageInfo SkSVGImage::LoadImage(const sk_sp<skresources::ResourceProvider>& rp,
58 const SkSVGIRI& iri,
59 const SkRect& viewPort,
60 SkSVGPreserveAspectRatio par) {
Tyler Denniston8ca46262021-02-02 16:16:21 -050061 SkASSERT(rp);
62
63 // TODO: svg sources
Tyler Denniston2992fb02021-05-04 14:17:27 -040064 sk_sp<SkImage> image = ::LoadImage(rp, iri);
Tyler Denniston8ca46262021-02-02 16:16:21 -050065 if (!image) {
Tyler Denniston2992fb02021-05-04 14:17:27 -040066 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
79void SkSVGImage::onRender(const SkSVGRenderContext& ctx) const {
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) {
Tyler Denniston8ca46262021-02-02 16:16:21 -050086 SkDebugf("can't render image: load image failed\n");
87 return;
88 }
89
Tyler Denniston2992fb02021-05-04 14:17:27 -040090 // TODO: image-rendering property
91 ctx.canvas()->drawImageRect(
92 imgInfo.fImage, imgInfo.fDst, SkSamplingOptions(SkFilterMode::kLinear));
Tyler Denniston8ca46262021-02-02 16:16:21 -050093}
94
95SkPath SkSVGImage::onAsPath(const SkSVGRenderContext&) const { return {}; }
96
97SkRect SkSVGImage::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
98 const SkSVGLengthContext& lctx = ctx.lengthContext();
99 return lctx.resolveRect(fX, fY, fWidth, fHeight);
100}