blob: d87237837af1be6ad9c981451090c1090169c971 [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)) ||
22 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", n, v));
23}
24
25bool SkSVGImage::onPrepareToRender(SkSVGRenderContext* ctx) const {
26 // Width or height of 0 disables rendering per spec:
27 // https://www.w3.org/TR/SVG11/struct.html#ImageElement
28 return !fHref.iri().isEmpty() && fWidth.value() > 0 && fHeight.value() > 0 &&
29 INHERITED::onPrepareToRender(ctx);
30}
31
32static sk_sp<SkImage> LoadImage(const sk_sp<skresources::ResourceProvider>& rp,
33 const SkSVGIRI& href) {
34 // TODO: It may be better to use the SVG 'id' attribute as the asset id, to allow
35 // clients to perform asset substitution based on element id.
36 sk_sp<skresources::ImageAsset> imageAsset;
37 switch (href.type()) {
38 case SkSVGIRI::Type::kDataURI:
39 imageAsset = rp->loadImageAsset("", href.iri().c_str(), "");
40 break;
41 case SkSVGIRI::Type::kNonlocal: {
42 const auto path = SkOSPath::Dirname(href.iri().c_str());
43 const auto name = SkOSPath::Basename(href.iri().c_str());
44 imageAsset = rp->loadImageAsset(path.c_str(), name.c_str(), /* id */ name.c_str());
45 break;
46 }
47 default:
48 SkDebugf("error loading image: unhandled iri type %d\n", href.type());
49 return nullptr;
50 }
51
52 return imageAsset ? imageAsset->getFrameData(0).image : nullptr;
53}
54
55void SkSVGImage::onRender(const SkSVGRenderContext& ctx) const {
56 const auto& rp = ctx.resourceProvider();
57 SkASSERT(rp);
58
59 // TODO: svg sources
60 sk_sp<SkImage> image = LoadImage(rp, fHref);
61 if (!image) {
62 SkDebugf("can't render image: load image failed\n");
63 return;
64 }
65
66 // TODO: preserveAspectRatio support
67 const SkSVGLengthContext& lctx = ctx.lengthContext();
68 const SkRect rect = lctx.resolveRect(fX, fY, fWidth, fHeight);
69 ctx.canvas()->drawImageRect(image, rect, SkSamplingOptions(SkFilterMode::kLinear));
70}
71
72SkPath SkSVGImage::onAsPath(const SkSVGRenderContext&) const { return {}; }
73
74SkRect SkSVGImage::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
75 const SkSVGLengthContext& lctx = ctx.lengthContext();
76 return lctx.resolveRect(fX, fY, fWidth, fHeight);
77}