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); |
} |