SkSVGNode.cpp revision 2d961e086bb40b371b1a667536fa089794847368
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(const SkSVGRenderContext& ctx) const {
20    SkSVGRenderContext localContext(ctx);
21
22    if (this->onPrepareToRender(&localContext)) {
23        this->onRender(localContext);
24    }
25}
26
27bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
28    ctx->applyPresentationAttributes(fPresentationAttributes);
29    return true;
30}
31
32void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
33    this->onSetAttribute(attr, v);
34}
35
36void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
37    switch (attr) {
38    case SkSVGAttribute::kFill:
39        if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
40            fPresentationAttributes.fFill.set(*paint);
41        }
42        break;
43    case SkSVGAttribute::kFillOpacity:
44        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
45            fPresentationAttributes.fFillOpacity.set(
46                SkSVGNumberType(SkTPin<SkScalar>((*opacity)->value(), 0, 1)));
47        }
48        break;
49    case SkSVGAttribute::kStroke:
50        if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
51            fPresentationAttributes.fStroke.set(*paint);
52        }
53        break;
54    case SkSVGAttribute::kStrokeOpacity:
55        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
56            fPresentationAttributes.fStrokeOpacity.set(
57                SkSVGNumberType(SkTPin<SkScalar>((*opacity)->value(), 0, 1)));
58        }
59        break;
60    case SkSVGAttribute::kStrokeLineCap:
61        if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) {
62            fPresentationAttributes.fStrokeLineCap.set(*lineCap);
63        }
64        break;
65    case SkSVGAttribute::kStrokeLineJoin:
66        if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) {
67            fPresentationAttributes.fStrokeLineJoin.set(*lineJoin);
68        }
69        break;
70    case SkSVGAttribute::kStrokeWidth:
71        if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
72            fPresentationAttributes.fStrokeWidth.set(*strokeWidth);
73        }
74        break;
75    default:
76        SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
77        break;
78    }
79}
80