blob: fe68d622979ff630a8d38e222d9c4ec901385e76 [file] [log] [blame]
fmalitabffc2562016-08-03 10:21:11 -07001/*
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
14SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {}
15
16void SkSVGRect::setX(const SkSVGLength& x) {
17 fX = x;
18}
19
20void SkSVGRect::setY(const SkSVGLength& y) {
21 fY = y;
22}
23
24void SkSVGRect::setWidth(const SkSVGLength& w) {
25 fWidth = w;
26}
27
28void SkSVGRect::setHeight(const SkSVGLength& h) {
29 fHeight = h;
30}
31
fmalita286a8652016-08-10 17:11:29 -070032void SkSVGRect::setRx(const SkSVGLength& rx) {
33 fRx = rx;
34}
35
36void SkSVGRect::setRy(const SkSVGLength& ry) {
37 fRy = ry;
38}
39
fmalitabffc2562016-08-03 10:21:11 -070040void 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;
fmalita286a8652016-08-10 17:11:29 -070062 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;
fmalitabffc2562016-08-03 10:21:11 -070072 default:
73 this->INHERITED::onSetAttribute(attr, v);
74 }
75}
76
Florin Malitace8840e2016-12-08 09:26:47 -050077SkRRect SkSVGRect::resolve(const SkSVGLengthContext& lctx) const {
fmalita286a8652016-08-10 17:11:29 -070078 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 Malitace8840e2016-12-08 09:26:47 -050082 return SkRRect::MakeRectXY(rect, rx ,ry);
83}
84
85void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
86 const SkPaint& paint, SkPath::FillType) const {
87 canvas->drawRRect(this->resolve(lctx), paint);
88}
89
90SkPath 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;
fmalitabffc2562016-08-03 10:21:11 -070097}