Coverage Report

Created: 2024-05-20 07:14

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