Flutter Engine
The Flutter Engine
rect_f.cc
Go to the documentation of this file.
1// Copyright (c) 2012 The Chromium 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 "rect_f.h"
6
7#include <algorithm>
8#include <limits>
9
11#include "base/logging.h"
13#include "base/string_utils.h"
14#include "insets_f.h"
15
16#if defined(OS_IOS)
17#include <CoreGraphics/CoreGraphics.h>
18#elif defined(OS_APPLE)
19#include <ApplicationServices/ApplicationServices.h>
20#endif
21
22namespace gfx {
23
24static void AdjustAlongAxis(float dst_origin,
25 float dst_size,
26 float* origin,
27 float* size) {
28 *size = std::min(dst_size, *size);
29 if (*origin < dst_origin)
30 *origin = dst_origin;
31 else
32 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
33}
34
35#if defined(OS_APPLE)
36RectF::RectF(const CGRect& r)
37 : origin_(r.origin.x, r.origin.y), size_(r.size.width, r.size.height) {}
38
39CGRect RectF::ToCGRect() const {
40 return CGRectMake(x(), y(), width(), height());
41}
42#endif
43
44void RectF::Inset(const InsetsF& insets) {
45 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
46}
47
48void RectF::Inset(float left, float top, float right, float bottom) {
49 origin_ += Vector2dF(left, top);
50 set_width(std::max(width() - left - right, 0.0f));
51 set_height(std::max(height() - top - bottom, 0.0f));
52}
53
54void RectF::Offset(float horizontal, float vertical) {
55 origin_ += Vector2dF(horizontal, vertical);
56}
57
59 origin_ += offset;
60}
61
63 origin_ -= offset;
64}
65
66InsetsF RectF::InsetsFrom(const RectF& inner) const {
67 return InsetsF(inner.y() - y(), inner.x() - x(), bottom() - inner.bottom(),
68 right() - inner.right());
69}
70
71bool RectF::operator<(const RectF& other) const {
72 if (origin_ != other.origin_)
73 return origin_ < other.origin_;
74
75 if (width() == other.width())
76 return height() < other.height();
77 return width() < other.width();
78}
79
80bool RectF::Contains(float point_x, float point_y) const {
81 return point_x >= x() && point_x < right() && point_y >= y() &&
82 point_y < bottom();
83}
84
85bool RectF::Contains(const RectF& rect) const {
86 return rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
87 rect.bottom() <= bottom();
88}
89
90bool RectF::Intersects(const RectF& rect) const {
91 return !IsEmpty() && !rect.IsEmpty() && rect.x() < right() &&
92 rect.right() > x() && rect.y() < bottom() && rect.bottom() > y();
93}
94
95void RectF::Intersect(const RectF& rect) {
96 if (IsEmpty() || rect.IsEmpty()) {
97 SetRect(0, 0, 0, 0);
98 return;
99 }
100
101 float rx = std::max(x(), rect.x());
102 float ry = std::max(y(), rect.y());
103 float rr = std::min(right(), rect.right());
104 float rb = std::min(bottom(), rect.bottom());
105
106 if (rx >= rr || ry >= rb) {
107 SetRect(0, 0, 0, 0);
108 return;
109 }
110
111 SetRect(rx, ry, rr - rx, rb - ry);
112}
113
114void RectF::Union(const RectF& rect) {
115 if (IsEmpty()) {
116 *this = rect;
117 return;
118 }
119 if (rect.IsEmpty())
120 return;
121
122 float rx = std::min(x(), rect.x());
123 float ry = std::min(y(), rect.y());
124 float rr = std::max(right(), rect.right());
125 float rb = std::max(bottom(), rect.bottom());
126
127 SetRect(rx, ry, rr - rx, rb - ry);
128}
129
130void RectF::Subtract(const RectF& rect) {
131 if (!Intersects(rect))
132 return;
133 if (rect.Contains(*this)) {
134 SetRect(0, 0, 0, 0);
135 return;
136 }
137
138 float rx = x();
139 float ry = y();
140 float rr = right();
141 float rb = bottom();
142
143 if (rect.y() <= y() && rect.bottom() >= bottom()) {
144 // complete intersection in the y-direction
145 if (rect.x() <= x()) {
146 rx = rect.right();
147 } else if (rect.right() >= right()) {
148 rr = rect.x();
149 }
150 } else if (rect.x() <= x() && rect.right() >= right()) {
151 // complete intersection in the x-direction
152 if (rect.y() <= y()) {
153 ry = rect.bottom();
154 } else if (rect.bottom() >= bottom()) {
155 rb = rect.y();
156 }
157 }
158 SetRect(rx, ry, rr - rx, rb - ry);
159}
160
161void RectF::AdjustToFit(const RectF& rect) {
162 float new_x = x();
163 float new_y = y();
164 float new_width = width();
165 float new_height = height();
166 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
167 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
168 SetRect(new_x, new_y, new_width, new_height);
169}
170
171PointF RectF::CenterPoint() const {
172 return PointF(x() + width() / 2, y() + height() / 2);
173}
174
175void RectF::ClampToCenteredSize(const SizeF& size) {
176 float new_width = std::min(width(), size.width());
177 float new_height = std::min(height(), size.height());
178 float new_x = x() + (width() - new_width) / 2;
179 float new_y = y() + (height() - new_height) / 2;
180 SetRect(new_x, new_y, new_width, new_height);
181}
182
183void RectF::Transpose() {
184 SetRect(y(), x(), height(), width());
185}
186
187void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
188 BASE_DCHECK(left_half);
189 BASE_DCHECK(right_half);
190
191 left_half->SetRect(x(), y(), width() / 2, height());
192 right_half->SetRect(left_half->right(), y(), width() - left_half->width(),
193 height());
194}
195
196bool RectF::SharesEdgeWith(const RectF& rect) const {
197 return (y() == rect.y() && height() == rect.height() &&
198 (x() == rect.right() || right() == rect.x())) ||
199 (x() == rect.x() && width() == rect.width() &&
200 (y() == rect.bottom() || bottom() == rect.y()));
201}
202
203float RectF::ManhattanDistanceToPoint(const PointF& point) const {
204 float x_distance =
205 std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
206 float y_distance =
207 std::max<float>(0, std::max(y() - point.y(), point.y() - bottom()));
208
209 return x_distance + y_distance;
210}
211
212float RectF::ManhattanInternalDistance(const RectF& rect) const {
213 RectF c(*this);
214 c.Union(rect);
215
216 static constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
217 float x = std::max(0.f, c.width() - width() - rect.width() + kEpsilon);
218 float y = std::max(0.f, c.height() - height() - rect.height() + kEpsilon);
219 return x + y;
220}
221
222bool RectF::IsExpressibleAsRect() const {
223 return base::IsValueInRangeForNumericType<int>(x()) &&
224 base::IsValueInRangeForNumericType<int>(y()) &&
225 base::IsValueInRangeForNumericType<int>(width()) &&
226 base::IsValueInRangeForNumericType<int>(height()) &&
227 base::IsValueInRangeForNumericType<int>(right()) &&
228 base::IsValueInRangeForNumericType<int>(bottom());
229}
230
231std::string RectF::ToString() const {
232 return base::StringPrintf("%s %s", origin().ToString().c_str(),
233 size().ToString().c_str());
234}
235
237 RectF result = a;
238 result.Intersect(b);
239 return result;
240}
241
242RectF UnionRects(const RectF& a, const RectF& b) {
243 RectF result = a;
244 result.Union(b);
245 return result;
246}
247
248RectF SubtractRects(const RectF& a, const RectF& b) {
249 RectF result = a;
250 result.Subtract(b);
251 return result;
252}
253
254RectF BoundingRect(const PointF& p1, const PointF& p2) {
255 float rx = std::min(p1.x(), p2.x());
256 float ry = std::min(p1.y(), p2.y());
257 float rr = std::max(p1.x(), p2.x());
258 float rb = std::max(p1.y(), p2.y());
259 return RectF(rx, ry, rr - rx, rb - ry);
260}
261
262} // namespace gfx
static void Union(SkRegion *rgn, const SkIRect &rect)
Definition: RegionTest.cpp:27
static constexpr double kEpsilon
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
constexpr float left() const
Definition: insets_f.h:29
constexpr float bottom() const
Definition: insets_f.h:30
constexpr float top() const
Definition: insets_f.h:28
constexpr float right() const
Definition: insets_f.h:31
constexpr float x() const
Definition: point_f.h:27
constexpr float y() const
Definition: point_f.h:28
constexpr RectF()=default
constexpr float y() const
Definition: rect_f.h:50
constexpr float width() const
Definition: rect_f.h:53
constexpr float height() const
Definition: rect_f.h:56
void Union(const RectF &rect)
Definition: rect_f.cc:114
constexpr float right() const
Definition: rect_f.h:65
constexpr float bottom() const
Definition: rect_f.h:66
void SetRect(float x, float y, float width, float height)
Definition: rect_f.h:79
constexpr float x() const
Definition: rect_f.h:47
static bool b
struct MyStruct a[10]
GAsyncResult * result
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
std::string StringPrintf(const std::string &format, Args... args)
Definition: string_utils.h:18
bool Contains(const Container &container, const Value &value)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Definition: insets.cc:10
RectF BoundingRect(const PointF &p1, const PointF &p2)
Definition: rect_f.cc:254
static void AdjustAlongAxis(float dst_origin, float dst_size, float *origin, float *size)
Definition: rect_f.cc:24
void AdjustAlongAxis(int dst_origin, int dst_size, int *origin, int *size)
Definition: rect.cc:49
RectF SubtractRects(const RectF &a, const RectF &b)
Definition: rect_f.cc:248
RectF IntersectRects(const RectF &a, const RectF &b)
Definition: rect_f.cc:236
RectF UnionRects(const RectF &a, const RectF &b)
Definition: rect_f.cc:242
constexpr size_t size(const T(&array)[N]) noexcept
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
constexpr bool operator<(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:188
SINT Vec< N, T > & operator-=(Vec< N, T > &x, const Vec< N, T > &y)
Definition: SkVx.h:451
SINT Vec< N, T > & operator+=(Vec< N, T > &x, const Vec< N, T > &y)
Definition: SkVx.h:450
bool EMSCRIPTEN_KEEPALIVE IsEmpty(const SkPath &path)
int32_t height
int32_t width
SeparatedVector2 offset
#define BASE_DCHECK(condition)
Definition: logging.h:63