xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGRect.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkCanvas.h"
9 #include "include/core/SkRRect.h"
10 #include "include/core/SkRect.h"
11 #include "modules/svg/include/SkSVGAttributeParser.h"
12 #include "modules/svg/include/SkSVGRect.h"
13 #include "modules/svg/include/SkSVGRenderContext.h"
14 #include "modules/svg/src/SkSVGRectPriv.h"
15 
16 #include <algorithm>
17 #include <tuple>
18 
19 class SkPaint;
20 enum class SkPathFillType;
21 
ResolveOptionalRadii(const SkTLazy<SkSVGLength> & opt_rx,const SkTLazy<SkSVGLength> & opt_ry,const SkSVGLengthContext & lctx)22 std::tuple<float, float> ResolveOptionalRadii(const SkTLazy<SkSVGLength>& opt_rx,
23                                               const SkTLazy<SkSVGLength>& opt_ry,
24                                               const SkSVGLengthContext& lctx) {
25     // https://www.w3.org/TR/SVG2/shapes.html#RectElement
26     //
27     // The used values for rx and ry are determined from the computed values by following these
28     // steps in order:
29     //
30     // 1. If both rx and ry have a computed value of auto (since auto is the initial value for both
31     //    properties, this will also occur if neither are specified by the author or if all
32     //    author-supplied values are invalid), then the used value of both rx and ry is 0.
33     //    (This will result in square corners.)
34     // 2. Otherwise, convert specified values to absolute values as follows:
35     //     1. If rx is set to a length value or a percentage, but ry is auto, calculate an absolute
36     //        length equivalent for rx, resolving percentages against the used width of the
37     //        rectangle; the absolute value for ry is the same.
38     //     2. If ry is set to a length value or a percentage, but rx is auto, calculate the absolute
39     //        length equivalent for ry, resolving percentages against the used height of the
40     //        rectangle; the absolute value for rx is the same.
41     //     3. If both rx and ry were set to lengths or percentages, absolute values are generated
42     //        individually, resolving rx percentages against the used width, and resolving ry
43     //        percentages against the used height.
44     const float rx = opt_rx.isValid()
45         ? lctx.resolve(*opt_rx, SkSVGLengthContext::LengthType::kHorizontal)
46         : 0;
47     const float ry = opt_ry.isValid()
48         ? lctx.resolve(*opt_ry, SkSVGLengthContext::LengthType::kVertical)
49         : 0;
50 
51     return std::make_tuple(opt_rx.isValid() ? rx : ry,
52                            opt_ry.isValid() ? ry : rx);
53 }
54 
SkSVGRect()55 SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {}
56 
parseAndSetAttribute(const char * n,const char * v)57 bool SkSVGRect::parseAndSetAttribute(const char* n, const char* v) {
58     return INHERITED::parseAndSetAttribute(n, v) ||
59            this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) ||
60            this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) ||
61            this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) ||
62            this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) ||
63            this->setRx(SkSVGAttributeParser::parse<SkSVGLength>("rx", n, v)) ||
64            this->setRy(SkSVGAttributeParser::parse<SkSVGLength>("ry", n, v));
65 }
66 
resolve(const SkSVGLengthContext & lctx) const67 SkRRect SkSVGRect::resolve(const SkSVGLengthContext& lctx) const {
68     const auto rect = lctx.resolveRect(fX, fY, fWidth, fHeight);
69     const auto [ rx, ry ] = ResolveOptionalRadii(fRx, fRy, lctx);
70 
71     // https://www.w3.org/TR/SVG2/shapes.html#RectElement
72     // ...
73     // 3. Finally, apply clamping to generate the used values:
74     //     1. If the absolute rx (after the above steps) is greater than half of the used width,
75     //        then the used value of rx is half of the used width.
76     //     2. If the absolute ry (after the above steps) is greater than half of the used height,
77     //        then the used value of ry is half of the used height.
78     //     3. Otherwise, the used values of rx and ry are the absolute values computed previously.
79 
80     return SkRRect::MakeRectXY(rect,
81                                std::min(rx, rect.width() / 2),
82                                std::min(ry, rect.height() / 2));
83 }
84 
onDraw(SkCanvas * canvas,const SkSVGLengthContext & lctx,const SkPaint & paint,SkPathFillType) const85 void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
86                        const SkPaint& paint, SkPathFillType) const {
87     canvas->drawRRect(this->resolve(lctx), paint);
88 }
89 
onAsPath(const SkSVGRenderContext & ctx) const90 SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const {
91     SkPath path = SkPath::RRect(this->resolve(ctx.lengthContext()));
92 
93     this->mapToParent(&path);
94 
95     return path;
96 }
97 
onObjectBoundingBox(const SkSVGRenderContext & ctx) const98 SkRect SkSVGRect::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
99     return ctx.lengthContext().resolveRect(fX, fY, fWidth, fHeight);
100 }
101