diff options
author | 2016-09-17 07:26:26 -0700 | |
---|---|---|
committer | 2016-09-17 07:26:26 -0700 | |
commit | bdf3e5c0341884ead2c1fd97084637b37249418a (patch) | |
tree | 618833199fc3bd67e70f7910626548d9c7554df2 | |
parent | 67ac33e1f1a17a6b1380025ec5b7a455fb082bf4 (diff) |
[SVGDom] Improved DM sizing
Rather than always using a 1000x1000 canvas, observe the SVG intrinsic
size when available.
If the intrinsic size is < 128x128, scale uniformly.
R=robertphillips@google.com,stephana@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2342313003
Review-Url: https://codereview.chromium.org/2342313003
-rw-r--r-- | dm/DMSrcSink.cpp | 57 | ||||
-rw-r--r-- | dm/DMSrcSink.h | 12 |
2 files changed, 53 insertions, 16 deletions
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp index ae21a5becb..3c2ee324ff 100644 --- a/dm/DMSrcSink.cpp +++ b/dm/DMSrcSink.cpp @@ -1056,30 +1056,57 @@ Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #if defined(SK_XML) -// Should we try to use the SVG intrinsic size instead? -static const SkSize kSVGSize = SkSize::Make(1000, 1000); +// Used when the image doesn't have an intrinsic size. +static const SkSize kDefaultSVGSize = SkSize::Make(1000, 1000); -SVGSrc::SVGSrc(Path path) : fPath(path) {} +// Used to force-scale tiny fixed-size images. +static const SkSize kMinimumSVGSize = SkSize::Make(128, 128); -Error SVGSrc::draw(SkCanvas* canvas) const { - SkFILEStream stream(fPath.c_str()); - if (!stream.isValid()) { - return SkStringPrintf("Unable to open file: %s", fPath.c_str()); - } +SVGSrc::SVGSrc(Path path) : fPath(path), fScale(1) {} - sk_sp<SkSVGDOM> dom = SkSVGDOM::MakeFromStream(stream); - if (!dom) { - return SkStringPrintf("Unable to parse file: %s", fPath.c_str()); - } +Error SVGSrc::ensureDom() const { + if (!fDom) { + SkFILEStream stream(fPath.c_str()); + if (!stream.isValid()) { + return SkStringPrintf("Unable to open file: %s", fPath.c_str()); + } + fDom = SkSVGDOM::MakeFromStream(stream); + if (!fDom) { + return SkStringPrintf("Unable to parse file: %s", fPath.c_str()); + } - dom->setContainerSize(kSVGSize); - dom->render(canvas); + const SkSize& sz = fDom->containerSize(); + if (sz.isEmpty()) { + // no intrinsic size + fDom->setContainerSize(kDefaultSVGSize); + } else { + fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(), + kMinimumSVGSize.height() / sz.height())); + } + } return ""; } +Error SVGSrc::draw(SkCanvas* canvas) const { + Error err = this->ensureDom(); + if (err.isEmpty()) { + SkAutoCanvasRestore acr(canvas, true); + canvas->scale(fScale, fScale); + fDom->render(canvas); + } + + return err; +} + SkISize SVGSrc::size() const { - return kSVGSize.toRound(); + Error err = this->ensureDom(); + if (!err.isEmpty()) { + return SkISize::Make(0, 0); + } + + return SkSize::Make(fDom->containerSize().width() * fScale, + fDom->containerSize().height() * fScale).toRound(); } Name SVGSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } diff --git a/dm/DMSrcSink.h b/dm/DMSrcSink.h index dfc4955b5a..e8466aa50d 100644 --- a/dm/DMSrcSink.h +++ b/dm/DMSrcSink.h @@ -249,6 +249,12 @@ private: }; #if defined(SK_XML) +} // namespace DM + +class SkSVGDOM; + +namespace DM { + class SVGSrc : public Src { public: explicit SVGSrc(Path path); @@ -259,7 +265,11 @@ public: bool veto(SinkFlags) const override; private: - Path fPath; + Error ensureDom() const; + + Path fPath; + mutable sk_sp<SkSVGDOM> fDom; + mutable SkScalar fScale; typedef Src INHERITED; }; |