SkSVGNode.cpp revision 6fb0648c35aead138e65133c031d2b4fe5c8f0c7
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::setFill(const SkSVGPaint& svgPaint) {
37    fPresentationAttributes.fFill.set(svgPaint);
38}
39
40void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) {
41    fPresentationAttributes.fFillOpacity.set(
42        SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
43}
44
45void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) {
46    fPresentationAttributes.fOpacity.set(
47        SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
48}
49
50void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) {
51    fPresentationAttributes.fStroke.set(svgPaint);
52}
53
54void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
55    fPresentationAttributes.fStrokeOpacity.set(
56        SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
57}
58
59void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) {
60    fPresentationAttributes.fStrokeWidth.set(strokeWidth);
61}
62
63void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
64    switch (attr) {
65    case SkSVGAttribute::kFill:
66        if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
67            this->setFill(*paint);
68        }
69        break;
70    case SkSVGAttribute::kFillOpacity:
71        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
72            this->setFillOpacity(*opacity);
73        }
74        break;
75    case SkSVGAttribute::kOpacity:
76        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
77            this->setOpacity(*opacity);
78        }
79        break;
80    case SkSVGAttribute::kStroke:
81        if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
82            this->setStroke(*paint);
83        }
84        break;
85    case SkSVGAttribute::kStrokeOpacity:
86        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
87            this->setStrokeOpacity(*opacity);
88        }
89        break;
90    case SkSVGAttribute::kStrokeLineCap:
91        if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) {
92            fPresentationAttributes.fStrokeLineCap.set(*lineCap);
93        }
94        break;
95    case SkSVGAttribute::kStrokeLineJoin:
96        if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) {
97            fPresentationAttributes.fStrokeLineJoin.set(*lineJoin);
98        }
99        break;
100    case SkSVGAttribute::kStrokeWidth:
101        if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
102            this->setStrokeWidth(*strokeWidth);
103        }
104        break;
105    default:
106        SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
107        break;
108    }
109}
110