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 "experimental/svg/model/SkSVGRenderContext.h"
9 #include "experimental/svg/model/SkSVGTransformableNode.h"
10 #include "experimental/svg/model/SkSVGValue.h"
11 #include "include/core/SkCanvas.h"
12 
SkSVGTransformableNode(SkSVGTag tag)13 SkSVGTransformableNode::SkSVGTransformableNode(SkSVGTag tag)
14     : INHERITED(tag)
15     , fTransform(SkMatrix::I()) { }
16 
17 
onPrepareToRender(SkSVGRenderContext * ctx) const18 bool SkSVGTransformableNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
19     if (!fTransform.value().isIdentity()) {
20         ctx->saveOnce();
21         ctx->canvas()->concat(fTransform);
22     }
23 
24     return this->INHERITED::onPrepareToRender(ctx);
25 }
26 
onSetAttribute(SkSVGAttribute attr,const SkSVGValue & v)27 void SkSVGTransformableNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
28     switch (attr) {
29     case SkSVGAttribute::kTransform:
30         if (const auto* transform = v.as<SkSVGTransformValue>()) {
31             this->setTransform(*transform);
32         }
33         break;
34     default:
35         this->INHERITED::onSetAttribute(attr, v);
36         break;
37     }
38 }
39 
mapToParent(SkPath * path) const40 void SkSVGTransformableNode::mapToParent(SkPath* path) const {
41     // transforms the path to parent node coordinates.
42     path->transform(fTransform.value());
43 }
44