| 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 "modules/svg/include/SkSVGImage.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkSamplingOptions.h" |
| 14 |
#include "include/core/SkString.h" |
| 15 |
#include "include/private/base/SkAssert.h" |
| 16 |
#include "modules/skresources/include/SkResources.h" |
| 17 |
#include "modules/svg/include/SkSVGAttributeParser.h" |
| 18 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 19 |
#include "src/utils/SkOSPath.h" |
| 20 |
|
| 21 |
#include <utility> |
| 22 |
|
| 23 |
bool SkSVGImage::parseAndSetAttribute(const char* n, const char* v) { |
| 24 |
return INHERITED::parseAndSetAttribute(n, v) || |
| 25 |
this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) || |
| 26 |
this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) || |
| 27 |
this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) || |
| 28 |
this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) || |
| 29 |
this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", n, v)) || |
| 30 |
this->setPreserveAspectRatio(SkSVGAttributeParser::parse<SkSVGPreserveAspectRatio>( |
| 31 |
"preserveAspectRatio", n, v)); |
| 32 |
} |
| 33 |
|
| 34 |
bool SkSVGImage::onPrepareToRender(SkSVGRenderContext* ctx) const { |
| 35 |
// Width or height of 0 disables rendering per spec: |
| 36 |
// https://www.w3.org/TR/SVG11/struct.html#ImageElement |
| 37 |
return !fHref.iri().isEmpty() && fWidth.value() > 0 && fHeight.value() > 0 && |
| 38 |
INHERITED::onPrepareToRender(ctx); |
| 39 |
} |
| 40 |
|
| 41 |
static sk_sp<SkImage> LoadImage(const sk_sp<skresources::ResourceProvider>& rp, |
| 42 |
const SkSVGIRI& href) { |
| 43 |
// TODO: It may be better to use the SVG 'id' attribute as the asset id, to allow |
| 44 |
// clients to perform asset substitution based on element id. |
| 45 |
sk_sp<skresources::ImageAsset> imageAsset; |
| 46 |
switch (href.type()) { |
| 47 |
case SkSVGIRI::Type::kDataURI: |
| 48 |
imageAsset = rp->loadImageAsset("", href.iri().c_str(), ""); |
| 49 |
break; |
| 50 |
case SkSVGIRI::Type::kNonlocal: { |
| 51 |
const auto path = SkOSPath::Dirname(href.iri().c_str()); |
| 52 |
const auto name = SkOSPath::Basename(href.iri().c_str()); |
| 53 |
imageAsset = rp->loadImageAsset(path.c_str(), name.c_str(), /* id */ name.c_str()); |
| 54 |
break; |
| 55 |
} |
| 56 |
default: |
| 57 |
SkDEBUGF("error loading image: unhandled iri type %d\n", (int)href.type()); |
| 58 |
return nullptr; |
| 59 |
} |
| 60 |
|
| 61 |
return imageAsset ? imageAsset->getFrameData(0).image : nullptr; |
| 62 |
} |
| 63 |
|
| 64 |
SkSVGImage::ImageInfo SkSVGImage::LoadImage(const sk_sp<skresources::ResourceProvider>& rp, |
| 65 |
const SkSVGIRI& iri, |
| 66 |
const SkRect& viewPort, |
| 67 |
SkSVGPreserveAspectRatio par) { |
| 68 |
SkASSERT(rp); |
| 69 |
|
| 70 |
// TODO: svg sources |
| 71 |
sk_sp<SkImage> image = ::LoadImage(rp, iri); |
| 72 |
if (!image) { |
| 73 |
return {}; |
| 74 |
} |
| 75 |
|
| 76 |
// Per spec: raster content has implicit viewbox of '0 0 width height'. |
| 77 |
const SkRect viewBox = SkRect::Make(image->bounds()); |
| 78 |
|
| 79 |
// Map and place at x, y specified by viewport |
| 80 |
const SkMatrix m = ComputeViewboxMatrix(viewBox, viewPort, par); |
| 81 |
const SkRect dst = m.mapRect(viewBox).makeOffset(viewPort.fLeft, viewPort.fTop); |
| 82 |
|
| 83 |
return {std::move(image), dst}; |
| 84 |
} |
| 85 |
|
| 86 |
void SkSVGImage::onRender(const SkSVGRenderContext& ctx) const { |
| 87 |
// Per spec: x, w, width, height attributes establish the new viewport. |
| 88 |
const SkSVGLengthContext& lctx = ctx.lengthContext(); |
| 89 |
const SkRect viewPort = lctx.resolveRect(fX, fY, fWidth, fHeight); |
| 90 |
|
| 91 |
const auto imgInfo = LoadImage(ctx.resourceProvider(), fHref, viewPort, fPreserveAspectRatio); |
| 92 |
if (!imgInfo.fImage) { |
| 93 |
SkDEBUGF("can't render image: load image failed\n"); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// TODO: image-rendering property |
| 98 |
ctx.canvas()->drawImageRect( |
| 99 |
imgInfo.fImage, imgInfo.fDst, SkSamplingOptions(SkFilterMode::kLinear)); |
| 100 |
} |
| 101 |
|
| 102 |
SkPath SkSVGImage::onAsPath(const SkSVGRenderContext&) const { return {}; } |
| 103 |
|
| 104 |
SkRect SkSVGImage::onTransformableObjectBoundingBox(const SkSVGRenderContext& ctx) const { |
| 105 |
const SkSVGLengthContext& lctx = ctx.lengthContext(); |
| 106 |
return lctx.resolveRect(fX, fY, fWidth, fHeight); |
| 107 |
} |