SkSVGNode.cpp revision ce8840e3842f3a702c5d7bf440ff730bdfaf8e70
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::asPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
28    SkSVGRenderContext localContext(ctx);
29
30    return this->onPrepareToRender(&localContext) && this->onAsPaint(localContext, paint);
31}
32
33SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const {
34    SkSVGRenderContext localContext(ctx);
35    return this->onPrepareToRender(&localContext) ? this->onAsPath(localContext) : SkPath();
36}
37
38bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
39    ctx->applyPresentationAttributes(fPresentationAttributes,
40                                     this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf);
41    return true;
42}
43
44void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
45    this->onSetAttribute(attr, v);
46}
47
48void SkSVGNode::setClipPath(const SkSVGClip& clip) {
49    fPresentationAttributes.fClipPath.set(clip);
50}
51
52void SkSVGNode::setFill(const SkSVGPaint& svgPaint) {
53    fPresentationAttributes.fFill.set(svgPaint);
54}
55
56void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) {
57    fPresentationAttributes.fFillOpacity.set(
58        SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
59}
60
61void SkSVGNode::setFillRule(const SkSVGFillRule& fillRule) {
62    fPresentationAttributes.fFillRule.set(fillRule);
63}
64
65void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) {
66    fPresentationAttributes.fOpacity.set(
67        SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
68}
69
70void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) {
71    fPresentationAttributes.fStroke.set(svgPaint);
72}
73
74void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
75    fPresentationAttributes.fStrokeOpacity.set(
76        SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
77}
78
79void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) {
80    fPresentationAttributes.fStrokeWidth.set(strokeWidth);
81}
82
83void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
84    switch (attr) {
85    case SkSVGAttribute::kClipPath:
86        if (const SkSVGClipValue* clip = v.as<SkSVGClipValue>()) {
87            this->setClipPath(*clip);
88        }
89        break;
90    case SkSVGAttribute::kFill:
91        if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
92            this->setFill(*paint);
93        }
94        break;
95    case SkSVGAttribute::kFillOpacity:
96        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
97            this->setFillOpacity(*opacity);
98        }
99        break;
100    case SkSVGAttribute::kFillRule:
101        if (const SkSVGFillRuleValue* fillRule = v.as<SkSVGFillRuleValue>()) {
102            this->setFillRule(*fillRule);
103        }
104        break;
105    case SkSVGAttribute::kOpacity:
106        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
107            this->setOpacity(*opacity);
108        }
109        break;
110    case SkSVGAttribute::kStroke:
111        if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
112            this->setStroke(*paint);
113        }
114        break;
115    case SkSVGAttribute::kStrokeOpacity:
116        if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
117            this->setStrokeOpacity(*opacity);
118        }
119        break;
120    case SkSVGAttribute::kStrokeLineCap:
121        if (const SkSVGLineCapValue* lineCap = v.as<SkSVGLineCapValue>()) {
122            fPresentationAttributes.fStrokeLineCap.set(*lineCap);
123        }
124        break;
125    case SkSVGAttribute::kStrokeLineJoin:
126        if (const SkSVGLineJoinValue* lineJoin = v.as<SkSVGLineJoinValue>()) {
127            fPresentationAttributes.fStrokeLineJoin.set(*lineJoin);
128        }
129        break;
130    case SkSVGAttribute::kStrokeWidth:
131        if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
132            this->setStrokeWidth(*strokeWidth);
133        }
134        break;
135    default:
136        SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
137        break;
138    }
139}
140