skia2/experimental/svg/model/SkSVGSVG.cpp
fmalita 397a517d1a [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
2016-08-08 11:38:55 -07:00

98 lines
2.5 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* 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;
}
void SkSVGSVG::setY(const SkSVGLength& y) {
fY = y;
}
void SkSVGSVG::setWidth(const SkSVGLength& w) {
fWidth = w;
}
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:
if (const auto* x = v.as<SkSVGLengthValue>()) {
this->setX(*x);
}
break;
case SkSVGAttribute::kY:
if (const auto* y = v.as<SkSVGLengthValue>()) {
this->setY(*y);
}
break;
case SkSVGAttribute::kWidth:
if (const auto* w = v.as<SkSVGLengthValue>()) {
this->setWidth(*w);
}
break;
case SkSVGAttribute::kHeight:
if (const auto* h = v.as<SkSVGLengthValue>()) {
this->setHeight(*h);
}
break;
case SkSVGAttribute::kViewBox:
if (const auto* vb = v.as<SkSVGViewBoxValue>()) {
this->setViewBox(*vb);
}
break;
default:
this->INHERITED::onSetAttribute(attr, v);
}
}