blob: 74bce16ea828f59c4dad2ab1243e4f4a801146f8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkRect.h"
Florin Malitab3418102020-10-15 18:10:29 -040010#include "modules/svg/include/SkSVGRect.h"
11#include "modules/svg/include/SkSVGRenderContext.h"
12#include "modules/svg/include/SkSVGValue.h"
fmalitabffc2562016-08-03 10:21:11 -070013
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
Florin Malitaf4403e72020-04-10 14:14:04 +000040void SkSVGRect::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalitabffc2562016-08-03 10:21:11 -070041 switch (attr) {
42 case SkSVGAttribute::kX:
Florin Malitaf4403e72020-04-10 14:14:04 +000043 if (const auto* x = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070044 this->setX(*x);
45 }
46 break;
47 case SkSVGAttribute::kY:
Florin Malitaf4403e72020-04-10 14:14:04 +000048 if (const auto* y = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070049 this->setY(*y);
50 }
51 break;
52 case SkSVGAttribute::kWidth:
Florin Malitaf4403e72020-04-10 14:14:04 +000053 if (const auto* w = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070054 this->setWidth(*w);
55 }
56 break;
57 case SkSVGAttribute::kHeight:
Florin Malitaf4403e72020-04-10 14:14:04 +000058 if (const auto* h = v.as<SkSVGLengthValue>()) {
fmalitabffc2562016-08-03 10:21:11 -070059 this->setHeight(*h);
60 }
61 break;
fmalita286a8652016-08-10 17:11:29 -070062 case SkSVGAttribute::kRx:
Florin Malitaf4403e72020-04-10 14:14:04 +000063 if (const auto* rx = v.as<SkSVGLengthValue>()) {
fmalita286a8652016-08-10 17:11:29 -070064 this->setRx(*rx);
65 }
66 break;
67 case SkSVGAttribute::kRy:
Florin Malitaf4403e72020-04-10 14:14:04 +000068 if (const auto* ry = v.as<SkSVGLengthValue>()) {
fmalita286a8652016-08-10 17:11:29 -070069 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,
Mike Reed7d34dc72019-11-26 12:17:17 -050086 const SkPaint& paint, SkPathFillType) const {
Florin Malitace8840e2016-12-08 09:26:47 -050087 canvas->drawRRect(this->resolve(lctx), paint);
88}
89
90SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const {
Mike Reed58cc97a2020-08-24 15:15:01 -040091 SkPath path = SkPath::RRect(this->resolve(ctx.lengthContext()));
Florin Malitace8840e2016-12-08 09:26:47 -050092
93 this->mapToParent(&path);
94
95 return path;
fmalitabffc2562016-08-03 10:21:11 -070096}
Tyler Dennistonf548a022020-10-27 15:02:02 -040097
98SkRect SkSVGRect::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
99 return ctx.lengthContext().resolveRect(fX, fY, fWidth, fHeight);
100}