SkSVGNode.cpp revision 6ceef3dd67617c5f4572ada98d5ee85777d2db99
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCanvas.h"
9#include "SkMatrix.h"
10#include "SkSVGNode.h"
11#include "SkSVGRenderContext.h"
12#include "SkSVGValue.h"
13#include "SkTLazy.h"
14
15SkSVGNode::SkSVGNode(SkSVGTag t) : fTag(t) { }
16
17SkSVGNode::~SkSVGNode() { }
18
19void SkSVGNode::render(SkCanvas* canvas) const {
20    this->render(canvas, SkSVGRenderContext());
21}
22
23void SkSVGNode::render(SkCanvas* canvas, const SkSVGRenderContext& ctx) const {
24    SkTCopyOnFirstWrite<SkSVGRenderContext> localContext(ctx);
25    fPresentationAttributes.applyTo(localContext);
26
27    SkAutoCanvasRestore acr(canvas, false);
28    const SkMatrix& m = this->onLocalMatrix();
29    if (!m.isIdentity()) {
30        canvas->save();
31        canvas->concat(m);
32    }
33
34    this->onRender(canvas, *localContext);
35}
36
37void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
38    this->onSetAttribute(attr, v);
39}
40
41void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
42    switch (attr) {
43    case SkSVGAttribute::fill:
44        if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) {
45            fPresentationAttributes.setFill(*color);
46        }
47        break;
48    case SkSVGAttribute::stroke:
49        if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) {
50            fPresentationAttributes.setStroke(*color);
51        }
52        break;
53    default:
54        break;
55    }
56}
57
58const SkMatrix& SkSVGNode::onLocalMatrix() const {
59    return SkMatrix::I();
60}
61