Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Unified Diff: dm/DMSrcSink.cpp

Issue 2342313003: [SVGDom] Improved DM sizing (Closed)
Patch Set: cleanup Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dm/DMSrcSink.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dm/DMSrcSink.cpp
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index e35bbad24e6c0981cbc9ab9a5c2c1f7937a2a7d8..00338028b09ffe4942fd67694f4ae047e27ee384 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -1032,30 +1032,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()); }
« no previous file with comments | « dm/DMSrcSink.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698