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