blob: 0131e1e4f9eeddc26115b71b11a8df33ca1f6773 [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
Florin Malitac49c7f52021-03-29 13:38:38 -04008#include <tuple>
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkRect.h"
Florin Malitab3418102020-10-15 18:10:29 -040012#include "modules/svg/include/SkSVGRect.h"
13#include "modules/svg/include/SkSVGRenderContext.h"
14#include "modules/svg/include/SkSVGValue.h"
fmalitabffc2562016-08-03 10:21:11 -070015
16SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {}
17
Tyler Denniston52d94752021-02-05 10:23:34 -050018bool SkSVGRect::parseAndSetAttribute(const char* n, const char* v) {
19 return INHERITED::parseAndSetAttribute(n, v) ||
20 this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) ||
21 this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) ||
22 this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) ||
23 this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) ||
24 this->setRx(SkSVGAttributeParser::parse<SkSVGLength>("rx", n, v)) ||
25 this->setRy(SkSVGAttributeParser::parse<SkSVGLength>("ry", n, v));
fmalitabffc2562016-08-03 10:21:11 -070026}
27
Florin Malitace8840e2016-12-08 09:26:47 -050028SkRRect SkSVGRect::resolve(const SkSVGLengthContext& lctx) const {
Florin Malitac49c7f52021-03-29 13:38:38 -040029 const auto rect = lctx.resolveRect(fX, fY, fWidth, fHeight);
fmalita286a8652016-08-10 17:11:29 -070030
Florin Malitac49c7f52021-03-29 13:38:38 -040031 // https://www.w3.org/TR/SVG11/shapes.html#RectElementRXAttribute:
32 //
33 // - Let rx and ry be length values.
34 // - If neither ‘rx’ nor ‘ry’ are properly specified, then set both rx and ry to 0.
35 // - Otherwise, if a properly specified value is provided for ‘rx’, but not for ‘ry’,
36 // then set both rx and ry to the value of ‘rx’.
37 // - Otherwise, if a properly specified value is provided for ‘ry’, but not for ‘rx’,
38 // then set both rx and ry to the value of ‘ry’.
39 // - Otherwise, both ‘rx’ and ‘ry’ were specified properly. Set rx to the value of ‘rx’
40 // and ry to the value of ‘ry’.
41 // - If rx is greater than half of ‘width’, then set rx to half of ‘width’.
42 // - If ry is greater than half of ‘height’, then set ry to half of ‘height’.
43 // - The effective values of ‘rx’ and ‘ry’ are rx and ry, respectively.
44 //
45 auto radii = [this]() {
46 return fRx.isValid()
47 ? fRy.isValid()
48 ? std::make_tuple(*fRx, *fRy)
49 : std::make_tuple(*fRx, *fRx)
50 : fRy.isValid()
51 ? std::make_tuple(*fRy, *fRy)
52 : std::make_tuple(SkSVGLength(0), SkSVGLength(0));
53 };
54
55 const auto [ rxlen, rylen ] = radii();
56 const auto rx = std::min(lctx.resolve(rxlen, SkSVGLengthContext::LengthType::kHorizontal),
57 rect.width() / 2),
58 ry = std::min(lctx.resolve(rylen, SkSVGLengthContext::LengthType::kVertical),
59 rect.height() / 2);
60
61 return SkRRect::MakeRectXY(rect, rx, ry);
Florin Malitace8840e2016-12-08 09:26:47 -050062}
63
64void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
Mike Reed7d34dc72019-11-26 12:17:17 -050065 const SkPaint& paint, SkPathFillType) const {
Florin Malitace8840e2016-12-08 09:26:47 -050066 canvas->drawRRect(this->resolve(lctx), paint);
67}
68
69SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const {
Mike Reed58cc97a2020-08-24 15:15:01 -040070 SkPath path = SkPath::RRect(this->resolve(ctx.lengthContext()));
Florin Malitace8840e2016-12-08 09:26:47 -050071
72 this->mapToParent(&path);
73
74 return path;
fmalitabffc2562016-08-03 10:21:11 -070075}
Tyler Dennistonf548a022020-10-27 15:02:02 -040076
77SkRect SkSVGRect::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
78 return ctx.lengthContext().resolveRect(fX, fY, fWidth, fHeight);
79}