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

Unified Diff: experimental/svg/model/SkSVGSVG.cpp

Issue 2222793002: [SVGDom] Add <svg> viewBox support (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: typo Created 4 years, 4 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 | « experimental/svg/model/SkSVGSVG.h ('k') | experimental/svg/model/SkSVGShape.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: experimental/svg/model/SkSVGSVG.cpp
diff --git a/experimental/svg/model/SkSVGSVG.cpp b/experimental/svg/model/SkSVGSVG.cpp
index 42bd280a5e1f7418fab5cb6de2bc6b164e1142ad..13f18043c0525ff41e20e915fec94966f3159098 100644
--- a/experimental/svg/model/SkSVGSVG.cpp
+++ b/experimental/svg/model/SkSVGSVG.cpp
@@ -5,11 +5,45 @@
* found in the LICENSE file.
*/
+#include "SkCanvas.h"
+#include "SkSVGRenderContext.h"
#include "SkSVGSVG.h"
#include "SkSVGValue.h"
SkSVGSVG::SkSVGSVG() : INHERITED(SkSVGTag::kSvg) { }
+bool SkSVGSVG::onPrepareToRender(SkSVGRenderContext* ctx) const {
+ auto viewPortRect = ctx->lengthContext().resolveRect(fX, fY, fWidth, fHeight);
+ auto contentMatrix = SkMatrix::MakeTrans(viewPortRect.x(), viewPortRect.y());
+ auto viewPort = SkSize::Make(viewPortRect.width(), viewPortRect.height());
+
+ if (fViewBox.isValid()) {
+ const SkRect& viewBox = *fViewBox.get();
+
+ // An empty viewbox disables rendering.
+ if (viewBox.isEmpty()) {
+ return false;
+ }
+
+ // A viewBox overrides the intrinsic viewport.
+ viewPort = SkSize::Make(viewBox.width(), viewBox.height());
+
+ contentMatrix.preConcat(
+ SkMatrix::MakeRectToRect(viewBox, viewPortRect, SkMatrix::kFill_ScaleToFit));
+ }
+
+ if (!contentMatrix.isIdentity()) {
+ ctx->canvas()->save();
+ ctx->canvas()->concat(contentMatrix);
+ }
+
+ if (viewPort != ctx->lengthContext().viewPort()) {
+ ctx->writableLengthContext()->setViewPort(viewPort);
+ }
+
+ return this->INHERITED::onPrepareToRender(ctx);
+}
+
void SkSVGSVG::setX(const SkSVGLength& x) {
fX = x;
}
@@ -26,6 +60,10 @@ void SkSVGSVG::setHeight(const SkSVGLength& h) {
fHeight = h;
}
+void SkSVGSVG::setViewBox(const SkSVGViewBoxType& vb) {
+ fViewBox.set(vb);
+}
+
void SkSVGSVG::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
switch (attr) {
case SkSVGAttribute::kX:
@@ -48,6 +86,11 @@ void SkSVGSVG::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
this->setHeight(*h);
}
break;
+ case SkSVGAttribute::kViewBox:
+ if (const auto* vb = v.as<SkSVGViewBoxValue>()) {
+ this->setViewBox(*vb);
+ }
+ break;
default:
this->INHERITED::onSetAttribute(attr, v);
}
« no previous file with comments | « experimental/svg/model/SkSVGSVG.h ('k') | experimental/svg/model/SkSVGShape.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698