1 /*
2 * Copyright (c) 2012, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LAYOUT_POINT_H_
32 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LAYOUT_POINT_H_
33
34 #include <iosfwd>
35 #include "third_party/blink/renderer/platform/geometry/double_point.h"
36 #include "third_party/blink/renderer/platform/geometry/float_point.h"
37 #include "third_party/blink/renderer/platform/geometry/layout_size.h"
38 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
39 #include "third_party/blink/renderer/platform/wtf/forward.h"
40
41 namespace blink {
42
43 class PLATFORM_EXPORT LayoutPoint {
44 DISALLOW_NEW();
45
46 public:
47 constexpr LayoutPoint() = default;
LayoutPoint(LayoutUnit x,LayoutUnit y)48 constexpr LayoutPoint(LayoutUnit x, LayoutUnit y) : x_(x), y_(y) {}
LayoutPoint(int x,int y)49 constexpr LayoutPoint(int x, int y) : x_(LayoutUnit(x)), y_(LayoutUnit(y)) {}
LayoutPoint(const IntPoint & point)50 constexpr LayoutPoint(const IntPoint& point) : x_(point.X()), y_(point.Y()) {}
LayoutPoint(const FloatPoint & point)51 constexpr explicit LayoutPoint(const FloatPoint& point)
52 : x_(point.X()), y_(point.Y()) {}
LayoutPoint(const DoublePoint & point)53 constexpr explicit LayoutPoint(const DoublePoint& point)
54 : x_(point.X()), y_(point.Y()) {}
LayoutPoint(const LayoutSize & size)55 constexpr explicit LayoutPoint(const LayoutSize& size)
56 : x_(size.Width()), y_(size.Height()) {}
57
FloatPoint()58 constexpr explicit operator FloatPoint() const {
59 return FloatPoint(x_.ToFloat(), y_.ToFloat());
60 }
DoublePoint()61 constexpr explicit operator DoublePoint() const {
62 return DoublePoint(x_.ToDouble(), y_.ToDouble());
63 }
64
Zero()65 static constexpr LayoutPoint Zero() { return LayoutPoint(); }
66
X()67 constexpr LayoutUnit X() const { return x_; }
Y()68 constexpr LayoutUnit Y() const { return y_; }
69
SetX(LayoutUnit x)70 void SetX(LayoutUnit x) { x_ = x; }
SetY(LayoutUnit y)71 void SetY(LayoutUnit y) { y_ = y; }
72
Move(const LayoutSize & s)73 void Move(const LayoutSize& s) { Move(s.Width(), s.Height()); }
Move(const IntSize & s)74 void Move(const IntSize& s) { Move(s.Width(), s.Height()); }
MoveBy(const LayoutPoint & offset)75 void MoveBy(const LayoutPoint& offset) { Move(offset.X(), offset.Y()); }
Move(int dx,int dy)76 void Move(int dx, int dy) { Move(LayoutUnit(dx), LayoutUnit(dy)); }
Move(LayoutUnit dx,LayoutUnit dy)77 void Move(LayoutUnit dx, LayoutUnit dy) {
78 x_ += dx;
79 y_ += dy;
80 }
Scale(float sx,float sy)81 void Scale(float sx, float sy) {
82 x_ *= sx;
83 y_ *= sy;
84 }
85
86 LayoutPoint ExpandedTo(const LayoutPoint&) const;
87 LayoutPoint ShrunkTo(const LayoutPoint&) const;
88
ClampNegativeToZero()89 void ClampNegativeToZero() { *this = ExpandedTo(Zero()); }
90
TransposedPoint()91 LayoutPoint TransposedPoint() const { return LayoutPoint(y_, x_); }
92
93 String ToString() const;
94
95 private:
96 LayoutUnit x_, y_;
97 };
98
99 ALWAYS_INLINE LayoutPoint& operator+=(LayoutPoint& a, const LayoutSize& b) {
100 a.Move(b.Width(), b.Height());
101 return a;
102 }
103
104 ALWAYS_INLINE LayoutPoint& operator+=(LayoutPoint& a, const LayoutPoint& b) {
105 a.Move(b.X(), b.Y());
106 return a;
107 }
108
109 inline LayoutPoint& operator+=(LayoutPoint& a, const IntSize& b) {
110 a.Move(b.Width(), b.Height());
111 return a;
112 }
113
114 ALWAYS_INLINE LayoutPoint& operator-=(LayoutPoint& a, const LayoutPoint& b) {
115 a.Move(-b.X(), -b.Y());
116 return a;
117 }
118
119 ALWAYS_INLINE LayoutPoint& operator-=(LayoutPoint& a, const LayoutSize& b) {
120 a.Move(-b.Width(), -b.Height());
121 return a;
122 }
123
124 inline LayoutPoint& operator-=(LayoutPoint& a, const IntSize& b) {
125 a.Move(-b.Width(), -b.Height());
126 return a;
127 }
128
129 inline LayoutPoint operator+(const LayoutPoint& a, const LayoutSize& b) {
130 return LayoutPoint(a.X() + b.Width(), a.Y() + b.Height());
131 }
132
133 ALWAYS_INLINE LayoutPoint operator+(const LayoutPoint& a,
134 const LayoutPoint& b) {
135 return LayoutPoint(a.X() + b.X(), a.Y() + b.Y());
136 }
137
138 ALWAYS_INLINE LayoutSize operator-(const LayoutPoint& a, const LayoutPoint& b) {
139 return LayoutSize(a.X() - b.X(), a.Y() - b.Y());
140 }
141
142 ALWAYS_INLINE LayoutSize operator-(const LayoutPoint& a, const IntPoint& b) {
143 return LayoutSize(a.X() - b.X(), a.Y() - b.Y());
144 }
145
146 inline LayoutPoint operator-(const LayoutPoint& a, const LayoutSize& b) {
147 return LayoutPoint(a.X() - b.Width(), a.Y() - b.Height());
148 }
149
150 inline LayoutPoint operator-(const LayoutPoint& a, const IntSize& b) {
151 return LayoutPoint(a.X() - b.Width(), a.Y() - b.Height());
152 }
153
154 inline LayoutPoint operator-(const LayoutPoint& point) {
155 return LayoutPoint(-point.X(), -point.Y());
156 }
157
158 constexpr ALWAYS_INLINE bool operator==(const LayoutPoint& a,
159 const LayoutPoint& b) {
160 return a.X() == b.X() && a.Y() == b.Y();
161 }
162
163 constexpr bool operator!=(const LayoutPoint& a, const LayoutPoint& b) {
164 return !(a == b);
165 }
166
ToPoint(const LayoutSize & size)167 constexpr LayoutPoint ToPoint(const LayoutSize& size) {
168 return LayoutPoint(size.Width(), size.Height());
169 }
170
ToLayoutPoint(const LayoutSize & p)171 constexpr LayoutPoint ToLayoutPoint(const LayoutSize& p) {
172 return LayoutPoint(p.Width(), p.Height());
173 }
174
ToSize(const LayoutPoint & a)175 constexpr LayoutSize ToSize(const LayoutPoint& a) {
176 return LayoutSize(a.X(), a.Y());
177 }
178
FlooredIntPoint(const LayoutPoint & point)179 inline IntPoint FlooredIntPoint(const LayoutPoint& point) {
180 return IntPoint(point.X().Floor(), point.Y().Floor());
181 }
182
RoundedIntPoint(const LayoutPoint & point)183 inline IntPoint RoundedIntPoint(const LayoutPoint& point) {
184 return IntPoint(point.X().Round(), point.Y().Round());
185 }
186
RoundedIntPoint(const LayoutSize & size)187 inline IntPoint RoundedIntPoint(const LayoutSize& size) {
188 return IntPoint(size.Width().Round(), size.Height().Round());
189 }
190
CeiledIntPoint(const LayoutPoint & point)191 inline IntPoint CeiledIntPoint(const LayoutPoint& point) {
192 return IntPoint(point.X().Ceil(), point.Y().Ceil());
193 }
194
FlooredLayoutPoint(const FloatPoint & p)195 inline LayoutPoint FlooredLayoutPoint(const FloatPoint& p) {
196 return LayoutPoint(LayoutUnit::FromFloatFloor(p.X()),
197 LayoutUnit::FromFloatFloor(p.Y()));
198 }
199
CeiledLayoutPoint(const FloatPoint & p)200 inline LayoutPoint CeiledLayoutPoint(const FloatPoint& p) {
201 return LayoutPoint(LayoutUnit::FromFloatCeil(p.X()),
202 LayoutUnit::FromFloatCeil(p.Y()));
203 }
204
PixelSnappedIntSize(const LayoutSize & s,const LayoutPoint & p)205 inline IntSize PixelSnappedIntSize(const LayoutSize& s, const LayoutPoint& p) {
206 return IntSize(SnapSizeToPixel(s.Width(), p.X()),
207 SnapSizeToPixel(s.Height(), p.Y()));
208 }
209
RoundedIntSize(const LayoutPoint & p)210 inline IntSize RoundedIntSize(const LayoutPoint& p) {
211 return IntSize(p.X().Round(), p.Y().Round());
212 }
213
ToLayoutSize(const LayoutPoint & p)214 inline LayoutSize ToLayoutSize(const LayoutPoint& p) {
215 return LayoutSize(p.X(), p.Y());
216 }
217
FlooredLayoutPoint(const FloatSize & s)218 inline LayoutPoint FlooredLayoutPoint(const FloatSize& s) {
219 return FlooredLayoutPoint(FloatPoint(s));
220 }
221
222 PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const LayoutPoint&);
223 PLATFORM_EXPORT WTF::TextStream& operator<<(WTF::TextStream&,
224 const LayoutPoint&);
225
226 } // namespace blink
227
228 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LAYOUT_POINT_H_
229