1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 #pragma once
9
10 #include <tuple>
11
12 #include <folly/Hash.h>
13 #include <ABI49_0_0React/renderer/graphics/ABI49_0_0Float.h>
14 #include <ABI49_0_0React/renderer/graphics/ABI49_0_0Rect.h>
15
16 namespace ABI49_0_0facebook {
17 namespace ABI49_0_0React {
18
19 /*
20 * Generic data structure describes some values associated with *edges*
21 * of a rectangle.
22 */
23 template <typename T>
24 struct RectangleEdges {
25 T left{};
26 T top{};
27 T right{};
28 T bottom{};
29
30 bool operator==(RectangleEdges<T> const &rhs) const noexcept {
31 return std::tie(this->left, this->top, this->right, this->bottom) ==
32 std::tie(rhs.left, rhs.top, rhs.right, rhs.bottom);
33 }
34
35 bool operator!=(RectangleEdges<T> const &rhs) const noexcept {
36 return !(*this == rhs);
37 }
38
isUniformRectangleEdges39 bool isUniform() const noexcept {
40 return left == top && left == right && left == bottom;
41 }
42
43 static RectangleEdges<T> const ZERO;
44 };
45
46 template <typename T>
47 RectangleEdges<T> const RectangleEdges<T>::ZERO = {};
48
49 template <typename T>
50 RectangleEdges<T> operator+(
51 RectangleEdges<T> const &lhs,
52 RectangleEdges<T> const &rhs) noexcept {
53 return RectangleEdges<T>{
54 lhs.left + rhs.left,
55 lhs.top + rhs.top,
56 lhs.right + rhs.right,
57 lhs.bottom + rhs.bottom};
58 }
59
60 template <typename T>
61 RectangleEdges<T> operator-(
62 RectangleEdges<T> const &lhs,
63 RectangleEdges<T> const &rhs) noexcept {
64 return RectangleEdges<T>{
65 lhs.left - rhs.left,
66 lhs.top - rhs.top,
67 lhs.right - rhs.right,
68 lhs.bottom - rhs.bottom};
69 }
70
71 /*
72 * EdgeInsets
73 */
74 using EdgeInsets = RectangleEdges<Float>;
75
76 /*
77 * Adjusts a rectangle by the given edge insets.
78 */
insetBy(Rect const & rect,EdgeInsets const & insets)79 inline Rect insetBy(Rect const &rect, EdgeInsets const &insets) noexcept {
80 return Rect{
81 {rect.origin.x + insets.left, rect.origin.y + insets.top},
82 {rect.size.width - insets.left - insets.right,
83 rect.size.height - insets.top - insets.bottom}};
84 }
85
86 /*
87 * Adjusts a rectangle by the given edge outsets.
88 */
outsetBy(Rect const & rect,EdgeInsets const & outsets)89 inline Rect outsetBy(Rect const &rect, EdgeInsets const &outsets) noexcept {
90 return Rect{
91 {rect.origin.x - outsets.left, rect.origin.y - outsets.top},
92 {rect.size.width + outsets.left + outsets.right,
93 rect.size.height + outsets.top + outsets.bottom}};
94 }
95
96 } // namespace ABI49_0_0React
97 } // namespace ABI49_0_0facebook
98
99 namespace std {
100
101 template <typename T>
102 struct hash<ABI49_0_0facebook::ABI49_0_0React::RectangleEdges<T>> {
103 size_t operator()(
104 ABI49_0_0facebook::ABI49_0_0React::RectangleEdges<T> const &edges) const noexcept {
105 return folly::hash::hash_combine(
106 0, edges.left, edges.right, edges.top, edges.bottom);
107 }
108 };
109
110 } // namespace std
111