Flutter Engine
 
Loading...
Searching...
No Matches
rounding_radii.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7namespace impeller {
8
9static inline void NormalizeEmptyToZero(Size& radii) {
10 if (radii.IsEmpty()) {
11 radii = Size();
12 }
13}
14
15static inline void AdjustScale(Scalar& radius1,
16 Scalar& radius2,
17 Scalar dimension,
18 Scalar& scale) {
19 FML_DCHECK(radius1 >= 0.0f && radius2 >= 0.0f);
20 FML_DCHECK(dimension > 0.0f);
21 if (radius1 + radius2 > dimension) {
22 scale = std::min(scale, dimension / (radius1 + radius2));
23 }
24}
25
26RoundingRadii RoundingRadii::Scaled(const Rect& in_bounds) const {
27 Rect bounds = in_bounds.GetPositive();
28 if (bounds.IsEmpty() || //
30 // Normalize empty radii.
31 return RoundingRadii();
32 }
33
34 // Copy the incoming radii so that we can work on normalizing them to the
35 // particular rectangle they are paired with without disturbing the caller.
36 RoundingRadii radii = *this;
37
38 // If any corner is flat or has a negative value, normalize it to zeros
39 // We do this first so that the unnecessary non-flat part of that radius
40 // does not contribute to the global scaling below.
45
46 // Now determine a global scale to apply to all of the radii to ensure
47 // that none of the adjacent pairs of radius values sum to larger than
48 // the corresponding dimension of the rectangle.
49 Size size = bounds.GetSize();
50 Scalar scale = 1.0f;
51 // clang-format off
52 AdjustScale(radii.top_left.width, radii.top_right.width, size.width,
53 scale);
54 AdjustScale(radii.bottom_left.width, radii.bottom_right.width, size.width,
55 scale);
56 AdjustScale(radii.top_left.height, radii.bottom_left.height, size.height,
57 scale);
58 AdjustScale(radii.top_right.height, radii.bottom_right.height, size.height,
59 scale);
60 // clang-format on
61 if (scale < 1.0f) {
62 radii = radii * scale;
63 }
64
65 return radii;
66}
67
68} // namespace impeller
#define FML_DCHECK(condition)
Definition logging.h:122
float Scalar
Definition scalar.h:19
static void AdjustScale(Scalar &radius1, Scalar &radius2, Scalar dimension, Scalar &scale)
TSize< Scalar > Size
Definition size.h:159
static void NormalizeEmptyToZero(Size &radii)
constexpr bool AreAllCornersEmpty() const
constexpr bool IsFinite() const
RoundingRadii Scaled(const Rect &bounds) const
Returns a scaled copy of this object, ensuring that the sum of the corner radii on each side does not...
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
Definition rect.h:327
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition rect.h:297
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
Definition rect.h:398
Type height
Definition size.h:29
Type width
Definition size.h:28
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition size.h:123