File: float_rect.h

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (273 lines) | stat: -rw-r--r-- 9,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
 * Copyright (C) 2005 Nokia.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_RECT_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_RECT_H_

#include <iosfwd>

#include "build/build_config.h"
#include "third_party/blink/renderer/platform/geometry/float_point.h"
#include "third_party/blink/renderer/platform/geometry/float_rect_outsets.h"
#include "third_party/blink/renderer/platform/geometry/int_rect.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/saturated_arithmetic.h"

#if defined(OS_MACOSX)
typedef struct CGRect CGRect;

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
#endif

struct SkRect;

namespace gfx {
class RectF;
}

namespace blink {

class LayoutRect;
class LayoutSize;

class PLATFORM_EXPORT FloatRect {
  DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();

 public:
  enum ContainsMode { kInsideOrOnStroke, kInsideButNotOnStroke };

  constexpr FloatRect() = default;
  constexpr FloatRect(const FloatPoint& location, const FloatSize& size)
      : location_(location), size_(size) {}
  constexpr FloatRect(float x, float y, float width, float height)
      : location_(FloatPoint(x, y)), size_(FloatSize(width, height)) {}
  explicit FloatRect(const IntRect&);
  explicit FloatRect(const LayoutRect&);
  FloatRect(const SkRect&);

  static FloatRect NarrowPrecision(double x,
                                   double y,
                                   double width,
                                   double height);

  constexpr FloatPoint Location() const { return location_; }
  constexpr FloatSize Size() const { return size_; }

  void SetLocation(const FloatPoint& location) { location_ = location; }
  void SetSize(const FloatSize& size) { size_ = size; }

  constexpr float X() const { return location_.X(); }
  constexpr float Y() const { return location_.Y(); }
  constexpr float MaxX() const { return X() + Width(); }
  constexpr float MaxY() const { return Y() + Height(); }
  constexpr float Width() const { return size_.Width(); }
  constexpr float Height() const { return size_.Height(); }

  void SetX(float x) { location_.SetX(x); }
  void SetY(float y) { location_.SetY(y); }
  void SetWidth(float width) { size_.SetWidth(width); }
  void SetHeight(float height) { size_.SetHeight(height); }

  constexpr bool IsEmpty() const { return size_.IsEmpty(); }
  constexpr bool IsZero() const { return size_.IsZero(); }
  bool IsExpressibleAsIntRect() const;

  FloatPoint Center() const {
    return FloatPoint(X() + Width() / 2, Y() + Height() / 2);
  }

  void Move(const FloatSize& delta) { location_ += delta; }
  void Move(const LayoutSize&);
  void Move(const IntSize&);
  void MoveBy(const FloatPoint& delta) { location_.Move(delta.X(), delta.Y()); }
  void Move(float dx, float dy) { location_.Move(dx, dy); }

  void Expand(const FloatSize& size) { size_ += size; }
  void Expand(float dw, float dh) { size_.Expand(dw, dh); }
  void Expand(const FloatRectOutsets& outsets) {
    location_.Move(-outsets.Left(), -outsets.Top());
    size_.Expand(outsets.Left() + outsets.Right(),
                 outsets.Top() + outsets.Bottom());
  }

  void Contract(const FloatSize& size) { size_ -= size; }
  void Contract(float dw, float dh) { size_.Expand(-dw, -dh); }

  void ShiftXEdgeTo(float);
  void ShiftMaxXEdgeTo(float);
  void ShiftYEdgeTo(float);
  void ShiftMaxYEdgeTo(float);

  FloatPoint MinXMinYCorner() const { return location_; }  // typically topLeft
  FloatPoint MaxXMinYCorner() const {
    return FloatPoint(location_.X() + size_.Width(), location_.Y());
  }  // typically topRight
  FloatPoint MinXMaxYCorner() const {
    return FloatPoint(location_.X(), location_.Y() + size_.Height());
  }  // typically bottomLeft
  FloatPoint MaxXMaxYCorner() const {
    return FloatPoint(location_.X() + size_.Width(),
                      location_.Y() + size_.Height());
  }  // typically bottomRight

  bool Intersects(const IntRect&) const;
  bool Intersects(const FloatRect&) const;
  bool Contains(const IntRect&) const;
  bool Contains(const FloatRect&) const;
  bool Contains(const FloatPoint&, ContainsMode = kInsideOrOnStroke) const;

  void Intersect(const IntRect&);
  void Intersect(const FloatRect&);
  // Set this rect to be the intersection of itself and the argument rect
  // using edge-inclusive geometry. If the two rectangles overlap but the
  // overlap region is zero-area (either because one of the two rectangles
  // is zero-area, or because the rectangles overlap at an edge or a corner),
  // the result is the zero-area intersection. The return value indicates
  // whether the two rectangle actually have an intersection, since checking
  // the result for isEmpty() is not conclusive.
  bool InclusiveIntersect(const FloatRect&);
  void Unite(const FloatRect&);
  void UniteEvenIfEmpty(const FloatRect&);
  void UniteIfNonZero(const FloatRect&);
  void Extend(const FloatPoint&);

  // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int
  // version is really checking for containment of 1x1 rect, but that doesn't
  // make sense with floats.
  bool Contains(float px, float py) const {
    return px >= X() && px <= MaxX() && py >= Y() && py <= MaxY();
  }

  void InflateX(float dx) {
    location_.SetX(location_.X() - dx);
    size_.SetWidth(size_.Width() + dx + dx);
  }
  void InflateY(float dy) {
    location_.SetY(location_.Y() - dy);
    size_.SetHeight(size_.Height() + dy + dy);
  }
  void Inflate(float d) {
    InflateX(d);
    InflateY(d);
  }
  void Scale(float s) { Scale(s, s); }
  void Scale(float sx, float sy);

  FloatRect TransposedRect() const {
    return FloatRect(location_.TransposedPoint(), size_.TransposedSize());
  }

  float SquaredDistanceTo(const FloatPoint&) const;

#if defined(OS_MACOSX)
  FloatRect(const CGRect&);
  operator CGRect() const;
#endif

  operator SkRect() const;
  operator gfx::RectF() const;

#if DCHECK_IS_ON()
  bool MayNotHaveExactIntRectRepresentation() const;
  bool EqualWithinEpsilon(const FloatRect& other, float epsilon) const;
#endif

  String ToString() const;

 private:
  FloatPoint location_;
  FloatSize size_;

  void SetLocationAndSizeFromEdges(float left,
                                   float top,
                                   float right,
                                   float bottom) {
    location_.Set(left, top);
    size_.SetWidth(right - left);
    size_.SetHeight(bottom - top);
  }
};

inline FloatRect Intersection(const FloatRect& a, const FloatRect& b) {
  FloatRect c = a;
  c.Intersect(b);
  return c;
}

inline FloatRect UnionRect(const FloatRect& a, const FloatRect& b) {
  FloatRect c = a;
  c.Unite(b);
  return c;
}

PLATFORM_EXPORT FloatRect UnionRect(const Vector<FloatRect>&);

inline FloatRect& operator+=(FloatRect& a, const FloatRect& b) {
  a.Move(b.X(), b.Y());
  a.SetWidth(a.Width() + b.Width());
  a.SetHeight(a.Height() + b.Height());
  return a;
}

constexpr FloatRect operator+(const FloatRect& a, const FloatRect& b) {
  return FloatRect(a.Location() + b.Location(), a.Size() + b.Size());
}

constexpr bool operator==(const FloatRect& a, const FloatRect& b) {
  return a.Location() == b.Location() && a.Size() == b.Size();
}

constexpr bool operator!=(const FloatRect& a, const FloatRect& b) {
  return !(a == b);
}

// Returns a IntRect containing the given FloatRect.
inline IntRect EnclosingIntRect(const FloatRect& rect) {
  IntPoint location = FlooredIntPoint(rect.Location());
  IntPoint max_point = CeiledIntPoint(rect.MaxXMaxYCorner());
  return IntRect(location, IntSize(ClampSub(max_point.X(), location.X()),
                                   ClampSub(max_point.Y(), location.Y())));
}

// Returns a valid IntRect contained within the given FloatRect.
PLATFORM_EXPORT IntRect EnclosedIntRect(const FloatRect&);

PLATFORM_EXPORT IntRect RoundedIntRect(const FloatRect&);

// Map supplied rect from srcRect to an equivalent rect in destRect.
PLATFORM_EXPORT FloatRect MapRect(const FloatRect&,
                                  const FloatRect& src_rect,
                                  const FloatRect& dest_rect);

PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const FloatRect&);
PLATFORM_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const FloatRect&);

}  // namespace blink

#endif