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 <tuple>
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkRect.h"
12 #include "modules/svg/include/SkSVGRect.h"
13 #include "modules/svg/include/SkSVGRenderContext.h"
14 #include "modules/svg/include/SkSVGValue.h"
15
SkSVGRect()16 SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {}
17
parseAndSetAttribute(const char * n,const char * v)18 bool 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));
26 }
27
resolve(const SkSVGLengthContext & lctx) const28 SkRRect SkSVGRect::resolve(const SkSVGLengthContext& lctx) const {
29 const auto rect = lctx.resolveRect(fX, fY, fWidth, fHeight);
30
31 // 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);
62 }
63
onDraw(SkCanvas * canvas,const SkSVGLengthContext & lctx,const SkPaint & paint,SkPathFillType) const64 void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
65 const SkPaint& paint, SkPathFillType) const {
66 canvas->drawRRect(this->resolve(lctx), paint);
67 }
68
onAsPath(const SkSVGRenderContext & ctx) const69 SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const {
70 SkPath path = SkPath::RRect(this->resolve(ctx.lengthContext()));
71
72 this->mapToParent(&path);
73
74 return path;
75 }
76
onObjectBoundingBox(const SkSVGRenderContext & ctx) const77 SkRect SkSVGRect::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
78 return ctx.lengthContext().resolveRect(fX, fY, fWidth, fHeight);
79 }
80