xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGTransformableNode.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "modules/svg/include/SkSVGTransformableNode.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkRect.h"
12 #include "modules/svg/include/SkSVGAttribute.h"
13 #include "modules/svg/include/SkSVGRenderContext.h"
14 #include "modules/svg/include/SkSVGValue.h"
15 
SkSVGTransformableNode(SkSVGTag tag)16 SkSVGTransformableNode::SkSVGTransformableNode(SkSVGTag tag)
17     : INHERITED(tag)
18     , fTransform(SkMatrix::I()) { }
19 
20 
onPrepareToRender(SkSVGRenderContext * ctx) const21 bool SkSVGTransformableNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
22     if (!fTransform.isIdentity()) {
23         ctx->saveOnce();
24         ctx->canvas()->concat(fTransform);
25     }
26 
27     return this->INHERITED::onPrepareToRender(ctx);
28 }
29 
onSetAttribute(SkSVGAttribute attr,const SkSVGValue & v)30 void SkSVGTransformableNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
31     switch (attr) {
32     case SkSVGAttribute::kTransform:
33         if (const auto* transform = v.as<SkSVGTransformValue>()) {
34             this->setTransform(*transform);
35         }
36         break;
37     default:
38         this->INHERITED::onSetAttribute(attr, v);
39         break;
40     }
41 }
42 
mapToParent(SkPath * path) const43 void SkSVGTransformableNode::mapToParent(SkPath* path) const {
44     // transforms the path to parent node coordinates.
45     path->transform(fTransform);
46 }
47 
mapToParent(SkRect * rect) const48 void SkSVGTransformableNode::mapToParent(SkRect* rect) const {
49     *rect = fTransform.mapRect(*rect);
50 }
51