fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 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 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
| 9 | #include "SkSVGRenderContext.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 10 | #include "SkSVGTransformableNode.h" |
| 11 | #include "SkSVGValue.h" |
| 12 | |
| 13 | SkSVGTransformableNode::SkSVGTransformableNode(SkSVGTag tag) |
| 14 | : INHERITED(tag) |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 15 | , fTransform(SkMatrix::I()) { } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 16 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 17 | |
| 18 | bool SkSVGTransformableNode::onPrepareToRender(SkSVGRenderContext* ctx) const { |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 19 | if (!fTransform.value().isIdentity()) { |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 20 | ctx->canvas()->save(); |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 21 | ctx->canvas()->concat(fTransform); |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | return this->INHERITED::onPrepareToRender(ctx); |
| 25 | } |
| 26 | |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 27 | void SkSVGTransformableNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
| 28 | switch (attr) { |
fmalita | 58649cc | 2016-07-29 08:52:03 -0700 | [diff] [blame] | 29 | case SkSVGAttribute::kTransform: |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 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 | } |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 39 | |
| 40 | void SkSVGTransformableNode::mapToParent(SkPath* path) const { |
| 41 | if (fTransform.value().isIdentity()) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | SkMatrix inv; |
| 46 | if (!fTransform.value().invert(&inv)) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | path->transform(inv); |
| 51 | } |