| Flutter Impeller |

rounding_radii.h
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 #ifndef FLUTTER_IMPELLER_GEOMETRY_ROUNDING_RADII_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_ROUNDING_RADII_H_
7
8 #include "flutter/impeller/geometry/point.h"
9 #include "flutter/impeller/geometry/rect.h"
10 #include "flutter/impeller/geometry/size.h"
11
12 namespace impeller {
13
14 struct RoundingRadii {
15 Size top_left;
16 Size top_right;
17 Size bottom_left;
18 Size bottom_right;
19
20 constexpr static RoundingRadii MakeRadius(Scalar radius) {
21 return {Size(radius), Size(radius), Size(radius), Size(radius)};
22 }
23
24 constexpr static RoundingRadii MakeRadii(Size radii) {
25 return {radii, radii, radii, radii};
26 }
27
28 constexpr bool IsFinite() const {
29 return top_left.IsFinite() && //
30 top_right.IsFinite() && //
31 bottom_left.IsFinite() && //
32 bottom_right.IsFinite();
33 }
34
35 constexpr bool AreAllCornersEmpty() const {
36 return top_left.IsEmpty() && //
37 top_right.IsEmpty() && //
38 bottom_left.IsEmpty() && //
39 bottom_right.IsEmpty();
40 }
41
42 constexpr bool AreAllCornersSame(Scalar tolerance = kEhCloseEnough) const {
43 return ScalarNearlyEqual(top_left.width, top_right.width, tolerance) &&
44 ScalarNearlyEqual(top_left.width, bottom_right.width, tolerance) &&
45 ScalarNearlyEqual(top_left.width, bottom_left.width, tolerance) &&
46 ScalarNearlyEqual(top_left.height, top_right.height, tolerance) &&
47 ScalarNearlyEqual(top_left.height, bottom_right.height, tolerance) &&
48 ScalarNearlyEqual(top_left.height, bottom_left.height, tolerance);
49 }
50
51 /// @brief Returns a scaled copy of this object, ensuring that the sum of the
52 /// corner radii on each side does not exceed the width or height of
53 /// the given bounds.
54 ///
55 /// See the [Skia scaling
56 /// implementation](https://github.com/google/skia/blob/main/src/core/SkRRect.cpp)
57 /// for more details.
58 RoundingRadii Scaled(const Rect& bounds) const;
59
60 constexpr inline RoundingRadii operator*(Scalar scale) {
61 return {
62 .top_left = top_left * scale,
63 .top_right = top_right * scale,
64 .bottom_left = bottom_left * scale,
65 .bottom_right = bottom_right * scale,
66 };
67 }
68
69 [[nodiscard]] constexpr bool operator==(const RoundingRadii& rr) const {
70 return top_left == rr.top_left && //
71 top_right == rr.top_right && //
72 bottom_left == rr.bottom_left && //
73 bottom_right == rr.bottom_right;
74 }
75
76 [[nodiscard]] constexpr bool operator!=(const RoundingRadii& rr) const {
77 return !(*this == rr);
78 }
79 };
80
81 } // namespace impeller
82
83 namespace std {
84
85 inline std::ostream& operator<<(std::ostream& out,
86 const impeller::RoundingRadii& rr) {
87 out << "(" //
88 << "ul: " << rr.top_left << ", " //
89 << "ur: " << rr.top_right << ", " //
90 << "ll: " << rr.bottom_left << ", " //
91 << "lr: " << rr.bottom_right //
92 << ")";
93 return out;
94 }
95
96 } // namespace std
97
98 #endif // FLUTTER_IMPELLER_GEOMETRY_ROUNDING_RADII_H_
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::Size
TSize< Scalar > Size
Definition: size.h:171
impeller::ScalarNearlyEqual
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:35
std
Definition: comparable.h:95
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:926
point.h
rect.h
size.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::operator==
constexpr bool operator==(const RoundingRadii &rr) const
Definition: rounding_radii.h:69
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::MakeRadii
constexpr static RoundingRadii MakeRadii(Size radii)
Definition: rounding_radii.h:24
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::RoundingRadii::operator!=
constexpr bool operator!=(const RoundingRadii &rr) const
Definition: rounding_radii.h:76
impeller::RoundingRadii::operator*
constexpr RoundingRadii operator*(Scalar scale)
Definition: rounding_radii.h:60
impeller::RoundingRadii::AreAllCornersSame
constexpr bool AreAllCornersSame(Scalar tolerance=kEhCloseEnough) const
Definition: rounding_radii.h:42
impeller::RoundingRadii::MakeRadius
constexpr static RoundingRadii MakeRadius(Scalar radius)
Definition: rounding_radii.h:20
impeller::TRect< Scalar >
impeller::TSize< Scalar >
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TSize::IsFinite
IsFinite() const
Definition: size.h:126
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