fmalita | bffc256 | 2016-08-03 10:21:11 -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 | |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkRect.h" |
| 10 | #include "SkSVGRect.h" |
| 11 | #include "SkSVGRenderContext.h" |
| 12 | #include "SkSVGValue.h" |
| 13 | |
| 14 | SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {} |
| 15 | |
| 16 | void SkSVGRect::setX(const SkSVGLength& x) { |
| 17 | fX = x; |
| 18 | } |
| 19 | |
| 20 | void SkSVGRect::setY(const SkSVGLength& y) { |
| 21 | fY = y; |
| 22 | } |
| 23 | |
| 24 | void SkSVGRect::setWidth(const SkSVGLength& w) { |
| 25 | fWidth = w; |
| 26 | } |
| 27 | |
| 28 | void SkSVGRect::setHeight(const SkSVGLength& h) { |
| 29 | fHeight = h; |
| 30 | } |
| 31 | |
fmalita | 286a865 | 2016-08-10 17:11:29 -0700 | [diff] [blame] | 32 | void SkSVGRect::setRx(const SkSVGLength& rx) { |
| 33 | fRx = rx; |
| 34 | } |
| 35 | |
| 36 | void SkSVGRect::setRy(const SkSVGLength& ry) { |
| 37 | fRy = ry; |
| 38 | } |
| 39 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 40 | void SkSVGRect::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
| 41 | switch (attr) { |
| 42 | case SkSVGAttribute::kX: |
| 43 | if (const auto* x = v.as<SkSVGLengthValue>()) { |
| 44 | this->setX(*x); |
| 45 | } |
| 46 | break; |
| 47 | case SkSVGAttribute::kY: |
| 48 | if (const auto* y = v.as<SkSVGLengthValue>()) { |
| 49 | this->setY(*y); |
| 50 | } |
| 51 | break; |
| 52 | case SkSVGAttribute::kWidth: |
| 53 | if (const auto* w = v.as<SkSVGLengthValue>()) { |
| 54 | this->setWidth(*w); |
| 55 | } |
| 56 | break; |
| 57 | case SkSVGAttribute::kHeight: |
| 58 | if (const auto* h = v.as<SkSVGLengthValue>()) { |
| 59 | this->setHeight(*h); |
| 60 | } |
| 61 | break; |
fmalita | 286a865 | 2016-08-10 17:11:29 -0700 | [diff] [blame] | 62 | case SkSVGAttribute::kRx: |
| 63 | if (const auto* rx = v.as<SkSVGLengthValue>()) { |
| 64 | this->setRx(*rx); |
| 65 | } |
| 66 | break; |
| 67 | case SkSVGAttribute::kRy: |
| 68 | if (const auto* ry = v.as<SkSVGLengthValue>()) { |
| 69 | this->setRy(*ry); |
| 70 | } |
| 71 | break; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 72 | default: |
| 73 | this->INHERITED::onSetAttribute(attr, v); |
| 74 | } |
| 75 | } |
| 76 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 77 | SkRRect SkSVGRect::resolve(const SkSVGLengthContext& lctx) const { |
fmalita | 286a865 | 2016-08-10 17:11:29 -0700 | [diff] [blame] | 78 | const SkRect rect = lctx.resolveRect(fX, fY, fWidth, fHeight); |
| 79 | const SkScalar rx = lctx.resolve(fRx, SkSVGLengthContext::LengthType::kHorizontal); |
| 80 | const SkScalar ry = lctx.resolve(fRy, SkSVGLengthContext::LengthType::kVertical); |
| 81 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 82 | return SkRRect::MakeRectXY(rect, rx ,ry); |
| 83 | } |
| 84 | |
| 85 | void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx, |
| 86 | const SkPaint& paint, SkPath::FillType) const { |
| 87 | canvas->drawRRect(this->resolve(lctx), paint); |
| 88 | } |
| 89 | |
| 90 | SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const { |
| 91 | SkPath path; |
| 92 | path.addRRect(this->resolve(ctx.lengthContext())); |
| 93 | |
| 94 | this->mapToParent(&path); |
| 95 | |
| 96 | return path; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 97 | } |