[SVGDom] Add viewBox support
The main feature is <svg> viewBox and proper viewport support, but the CL
touches a few other things:
* refactor SkSVGRenderContext to auto-restore canvas state, and split the
presentation bits into a separate CoW SkSVGPresentationContext
* introduce SkSVGNode::onPrepareToRender(), as a way for nodes to push their
custom state before the actual onRender() call (instead of relying on
non-virtual SkSVGNode to know about all possible state bits)
* add a "Type" suffix to SVG types, to disambiguate (e.g. SkSVGRectType vs.
SkSVGRect)
R=robertphillips@google.com,stephana@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2222793002
Review-Url: https://codereview.chromium.org/2222793002
diff --git a/experimental/svg/model/SkSVGRenderContext.cpp b/experimental/svg/model/SkSVGRenderContext.cpp
index 38498d3..e902d4e 100644
--- a/experimental/svg/model/SkSVGRenderContext.cpp
+++ b/experimental/svg/model/SkSVGRenderContext.cpp
@@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
+#include "SkCanvas.h"
#include "SkSVGRenderContext.h"
#include "SkSVGTypes.h"
@@ -42,10 +43,27 @@
return 0;
}
-SkSVGRenderContext::SkSVGRenderContext(const SkSize& initialViewport)
- : fLengthContext(initialViewport) {}
+SkRect SkSVGLengthContext::resolveRect(const SkSVGLength& x, const SkSVGLength& y,
+ const SkSVGLength& w, const SkSVGLength& h) const {
+ return SkRect::MakeXYWH(
+ this->resolve(x, SkSVGLengthContext::LengthType::kHorizontal),
+ this->resolve(y, SkSVGLengthContext::LengthType::kVertical),
+ this->resolve(w, SkSVGLengthContext::LengthType::kHorizontal),
+ this->resolve(h, SkSVGLengthContext::LengthType::kVertical));
+}
-SkSVGRenderContext& SkSVGRenderContext::operator=(const SkSVGRenderContext& other) {
+SkSVGPresentationContext::SkSVGPresentationContext() {}
+
+SkSVGPresentationContext::SkSVGPresentationContext(const SkSVGPresentationContext& o) {
+ this->initFrom(o);
+}
+
+SkSVGPresentationContext& SkSVGPresentationContext::operator=(const SkSVGPresentationContext& o) {
+ this->initFrom(o);
+ return *this;
+}
+
+void SkSVGPresentationContext::initFrom(const SkSVGPresentationContext& other) {
if (other.fFill.isValid()) {
fFill.set(*other.fFill.get());
} else {
@@ -57,11 +75,9 @@
} else {
fStroke.reset();
}
-
- return *this;
}
-SkPaint& SkSVGRenderContext::ensureFill() {
+SkPaint& SkSVGPresentationContext::ensureFill() {
if (!fFill.isValid()) {
fFill.init();
fFill.get()->setStyle(SkPaint::kFill_Style);
@@ -70,7 +86,7 @@
return *fFill.get();
}
-SkPaint& SkSVGRenderContext::ensureStroke() {
+SkPaint& SkSVGPresentationContext::ensureStroke() {
if (!fStroke.isValid()) {
fStroke.init();
fStroke.get()->setStyle(SkPaint::kStroke_Style);
@@ -79,10 +95,27 @@
return *fStroke.get();
}
-void SkSVGRenderContext::setFillColor(SkColor color) {
+void SkSVGPresentationContext::setFillColor(SkColor color) {
this->ensureFill().setColor(color);
}
-void SkSVGRenderContext::setStrokeColor(SkColor color) {
+void SkSVGPresentationContext::setStrokeColor(SkColor color) {
this->ensureStroke().setColor(color);
}
+
+SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas,
+ const SkSVGLengthContext& lctx,
+ const SkSVGPresentationContext& pctx)
+ : fLengthContext(lctx)
+ , fPresentationContext(pctx)
+ , fCanvas(canvas)
+ , fCanvasSaveCount(canvas->getSaveCount()) {}
+
+SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other)
+ : SkSVGRenderContext(other.canvas(),
+ other.lengthContext(),
+ other.presentationContext()) {}
+
+SkSVGRenderContext::~SkSVGRenderContext() {
+ fCanvas->restoreToCount(fCanvasSaveCount);
+}