| Flutter Impeller |

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
5 #include "flutter/impeller/geometry/rounding_radii.h"
6
7 namespace impeller {
8
9 static inline void NormalizeEmptyToZero(Size& radii) {
10 if (radii.IsEmpty()) {
11 radii = Size();
12 }
13 }
14
15 static 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
26 RoundingRadii RoundingRadii::Scaled(const Rect& in_bounds) const {
27 Rect bounds = in_bounds.GetPositive();
28 if (bounds.IsEmpty() || //
29 AreAllCornersEmpty() || !IsFinite()) {
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.
41 NormalizeEmptyToZero(radii.top_left);
42 NormalizeEmptyToZero(radii.top_right);
43 NormalizeEmptyToZero(radii.bottom_left);
44 NormalizeEmptyToZero(radii.bottom_right);
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
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::AdjustScale
static void AdjustScale(Scalar &radius1, Scalar &radius2, Scalar dimension, Scalar &scale)
Definition: rounding_radii.cc:15
impeller::Size
TSize< Scalar > Size
Definition: size.h:171
impeller::NormalizeEmptyToZero
static void NormalizeEmptyToZero(Size &radii)
Definition: rounding_radii.cc:9
rounding_radii.h
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller::RoundingRadii
Definition: rounding_radii.h:14
impeller::RoundingRadii::top_right
Size top_right
Definition: rounding_radii.h:16
impeller::RoundingRadii::bottom_left
Size bottom_left
Definition: rounding_radii.h:17
impeller::RoundingRadii::bottom_right
Size bottom_right
Definition: rounding_radii.h:18
impeller::RoundingRadii::top_left
Size top_left
Definition: rounding_radii.h:15
impeller::RoundingRadii::AreAllCornersEmpty
constexpr bool AreAllCornersEmpty() const
Definition: rounding_radii.h:35
impeller::RoundingRadii::IsFinite
constexpr bool IsFinite() const
Definition: rounding_radii.h:28
impeller::RoundingRadii::Scaled
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...
Definition: rounding_radii.cc:26
impeller::TRect< Scalar >
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:301
impeller::TRect::GetSize
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:331
impeller::TRect::GetPositive
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
Definition: rect.h:402
impeller::TSize< Scalar >
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:123