blob: 6dec0e8978b0d285fcee0f199781490cbc0515a2 [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 Denniston1f4cd072021-02-05 09:08:33 -050057SkRect SkSVGImage::resolveImageRect(const SkRect& viewBox, const SkRect& viewPort) const {
58 const SkMatrix m = ComputeViewboxMatrix(viewBox, viewPort, fPreserveAspectRatio);
59 // Map and place at x, y specified by image attributes
60 return m.mapRect(viewBox).makeOffset(viewPort.fLeft, viewPort.fTop);
61}
62
Tyler Denniston8ca46262021-02-02 16:16:21 -050063void SkSVGImage::onRender(const SkSVGRenderContext& ctx) const {
64 const auto& rp = ctx.resourceProvider();
65 SkASSERT(rp);
66
67 // TODO: svg sources
68 sk_sp<SkImage> image = LoadImage(rp, fHref);
69 if (!image) {
70 SkDebugf("can't render image: load image failed\n");
71 return;
72 }
73
Tyler Denniston1f4cd072021-02-05 09:08:33 -050074 // Per spec: x, w, width, height attributes establish the new viewport.
Tyler Denniston8ca46262021-02-02 16:16:21 -050075 const SkSVGLengthContext& lctx = ctx.lengthContext();
Tyler Denniston1f4cd072021-02-05 09:08:33 -050076 const SkRect viewPort = lctx.resolveRect(fX, fY, fWidth, fHeight);
77 // Per spec: raster content has implicit viewbox of '0 0 width height'.
78 const SkRect viewBox = SkRect::Make(image->bounds());
79
80 ctx.canvas()->drawImageRect(image,
81 this->resolveImageRect(viewBox, viewPort),
82 SkSamplingOptions(SkFilterMode::kLinear));
Tyler Denniston8ca46262021-02-02 16:16:21 -050083}
84
85SkPath SkSVGImage::onAsPath(const SkSVGRenderContext&) const { return {}; }
86
87SkRect SkSVGImage::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
88 const SkSVGLengthContext& lctx = ctx.lengthContext();
89 return lctx.resolveRect(fX, fY, fWidth, fHeight);
90}