Tyler Denniston | 8ca4626 | 2021-02-02 16:16:21 -0500 | [diff] [blame] | 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 | |
| 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 | |
| 16 | bool 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 Denniston | 1f4cd07 | 2021-02-05 09:08:33 -0500 | [diff] [blame] | 22 | this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", n, v)) || |
| 23 | this->setPreserveAspectRatio(SkSVGAttributeParser::parse<SkSVGPreserveAspectRatio>( |
| 24 | "preserveAspectRatio", n, v)); |
Tyler Denniston | 8ca4626 | 2021-02-02 16:16:21 -0500 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | bool 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 | |
| 34 | static 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 Denniston | 2992fb0 | 2021-05-04 14:17:27 -0400 | [diff] [blame^] | 57 | SkSVGImage::ImageInfo SkSVGImage::LoadImage(const sk_sp<skresources::ResourceProvider>& rp, |
| 58 | const SkSVGIRI& iri, |
| 59 | const SkRect& viewPort, |
| 60 | SkSVGPreserveAspectRatio par) { |
Tyler Denniston | 8ca4626 | 2021-02-02 16:16:21 -0500 | [diff] [blame] | 61 | SkASSERT(rp); |
| 62 | |
| 63 | // TODO: svg sources |
Tyler Denniston | 2992fb0 | 2021-05-04 14:17:27 -0400 | [diff] [blame^] | 64 | sk_sp<SkImage> image = ::LoadImage(rp, iri); |
Tyler Denniston | 8ca4626 | 2021-02-02 16:16:21 -0500 | [diff] [blame] | 65 | if (!image) { |
Tyler Denniston | 2992fb0 | 2021-05-04 14:17:27 -0400 | [diff] [blame^] | 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 | |
| 79 | void 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 Denniston | 8ca4626 | 2021-02-02 16:16:21 -0500 | [diff] [blame] | 86 | SkDebugf("can't render image: load image failed\n"); |
| 87 | return; |
| 88 | } |
| 89 | |
Tyler Denniston | 2992fb0 | 2021-05-04 14:17:27 -0400 | [diff] [blame^] | 90 | // TODO: image-rendering property |
| 91 | ctx.canvas()->drawImageRect( |
| 92 | imgInfo.fImage, imgInfo.fDst, SkSamplingOptions(SkFilterMode::kLinear)); |
Tyler Denniston | 8ca4626 | 2021-02-02 16:16:21 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | SkPath SkSVGImage::onAsPath(const SkSVGRenderContext&) const { return {}; } |
| 96 | |
| 97 | SkRect SkSVGImage::onObjectBoundingBox(const SkSVGRenderContext& ctx) const { |
| 98 | const SkSVGLengthContext& lctx = ctx.lengthContext(); |
| 99 | return lctx.resolveRect(fX, fY, fWidth, fHeight); |
| 100 | } |