Debian Codegraph @

modules/svg/src/SkSVGRect.cpp

raw
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/SkPathTypes.h"
10 #include "include/core/SkRRect.h"
11 #include "include/core/SkRect.h"
12 #include "modules/svg/include/SkSVGAttributeParser.h"
13 #include "modules/svg/include/SkSVGRect.h"
14 #include "modules/svg/include/SkSVGRenderContext.h"
15 #include "modules/svg/src/SkSVGRectPriv.h"
16
17 #include <algorithm>
18 #include <tuple>
19
20 class SkPaint;
21
22 std::tuple<float, float> ResolveOptionalRadii(const std::optional<SkSVGLength>& opt_rx,
23 const std::optional<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.has_value()
45 ? lctx.resolve(*opt_rx, SkSVGLengthContext::LengthType::kHorizontal)
46 : 0;
47 const float ry = opt_ry.has_value()
48 ? lctx.resolve(*opt_ry, SkSVGLengthContext::LengthType::kVertical)
49 : 0;
50
51 return std::make_tuple(opt_rx.has_value() ? rx : ry,
52 opt_ry.has_value() ? ry : rx);
53 }
54
55 SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {}
56
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
67 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
85 void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
86 const SkPaint& paint, SkPathFillType) const {
87 canvas->drawRRect(this->resolve(lctx), paint);
88 }
89
90 SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const {
91 return this->mapToParent(SkPath::RRect(this->resolve(ctx.lengthContext())));
92 }
93
94 SkRect SkSVGRect::onTransformableObjectBoundingBox(const SkSVGRenderContext& ctx) const {
95 return ctx.lengthContext().resolveRect(fX, fY, fWidth, fHeight);
96 }