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 LayoutPoint_h
32 #define LayoutPoint_h
33
34 #include "platform/geometry/FloatPoint.h"
35 #include "platform/geometry/LayoutSize.h"
36 #include "wtf/MathExtras.h"
37
38 namespace WebCore {
39
40 class LayoutPoint {
41 public:
LayoutPoint()42 LayoutPoint() { }
LayoutPoint(LayoutUnit x,LayoutUnit y)43 LayoutPoint(LayoutUnit x, LayoutUnit y) : m_x(x), m_y(y) { }
LayoutPoint(const IntPoint & point)44 LayoutPoint(const IntPoint& point) : m_x(point.x()), m_y(point.y()) { }
LayoutPoint(const FloatPoint & size)45 explicit LayoutPoint(const FloatPoint& size) : m_x(size.x()), m_y(size.y()) { }
LayoutPoint(const LayoutSize & size)46 explicit LayoutPoint(const LayoutSize& size) : m_x(size.width()), m_y(size.height()) { }
47
zero()48 static LayoutPoint zero() { return LayoutPoint(); }
49
x()50 LayoutUnit x() const { return m_x; }
y()51 LayoutUnit y() const { return m_y; }
52
setX(LayoutUnit x)53 void setX(LayoutUnit x) { m_x = x; }
setY(LayoutUnit y)54 void setY(LayoutUnit y) { m_y = y; }
55
move(const LayoutSize & s)56 void move(const LayoutSize& s) { move(s.width(), s.height()); }
moveBy(const LayoutPoint & offset)57 void moveBy(const LayoutPoint& offset) { move(offset.x(), offset.y()); }
move(LayoutUnit dx,LayoutUnit dy)58 void move(LayoutUnit dx, LayoutUnit dy) { m_x += dx; m_y += dy; }
scale(float sx,float sy)59 void scale(float sx, float sy)
60 {
61 m_x *= sx;
62 m_y *= sy;
63 }
64
expandedTo(const LayoutPoint & other)65 LayoutPoint expandedTo(const LayoutPoint& other) const
66 {
67 return LayoutPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
68 }
69
shrunkTo(const LayoutPoint & other)70 LayoutPoint shrunkTo(const LayoutPoint& other) const
71 {
72 return LayoutPoint(std::min(m_x, other.m_x), std::min(m_y, other.m_y));
73 }
74
clampNegativeToZero()75 void clampNegativeToZero()
76 {
77 *this = expandedTo(zero());
78 }
79
transposedPoint()80 LayoutPoint transposedPoint() const
81 {
82 return LayoutPoint(m_y, m_x);
83 }
84
85 private:
86 LayoutUnit m_x, m_y;
87 };
88
89 ALWAYS_INLINE LayoutPoint& operator+=(LayoutPoint& a, const LayoutSize& b)
90 {
91 a.move(b.width(), b.height());
92 return a;
93 }
94
95 ALWAYS_INLINE LayoutPoint& operator-=(LayoutPoint& a, const LayoutSize& b)
96 {
97 a.move(-b.width(), -b.height());
98 return a;
99 }
100
101 inline LayoutPoint operator+(const LayoutPoint& a, const LayoutSize& b)
102 {
103 return LayoutPoint(a.x() + b.width(), a.y() + b.height());
104 }
105
106 ALWAYS_INLINE LayoutPoint operator+(const LayoutPoint& a, const LayoutPoint& b)
107 {
108 return LayoutPoint(a.x() + b.x(), a.y() + b.y());
109 }
110
111 ALWAYS_INLINE LayoutSize operator-(const LayoutPoint& a, const LayoutPoint& b)
112 {
113 return LayoutSize(a.x() - b.x(), a.y() - b.y());
114 }
115
116 inline LayoutPoint operator-(const LayoutPoint& a, const LayoutSize& b)
117 {
118 return LayoutPoint(a.x() - b.width(), a.y() - b.height());
119 }
120
121 inline LayoutPoint operator-(const LayoutPoint& point)
122 {
123 return LayoutPoint(-point.x(), -point.y());
124 }
125
126 ALWAYS_INLINE bool operator==(const LayoutPoint& a, const LayoutPoint& b)
127 {
128 return a.x() == b.x() && a.y() == b.y();
129 }
130
131 inline bool operator!=(const LayoutPoint& a, const LayoutPoint& b)
132 {
133 return a.x() != b.x() || a.y() != b.y();
134 }
135
toPoint(const LayoutSize & size)136 inline LayoutPoint toPoint(const LayoutSize& size)
137 {
138 return LayoutPoint(size.width(), size.height());
139 }
140
toLayoutPoint(const LayoutSize & p)141 inline LayoutPoint toLayoutPoint(const LayoutSize& p)
142 {
143 return LayoutPoint(p.width(), p.height());
144 }
145
toSize(const LayoutPoint & a)146 inline LayoutSize toSize(const LayoutPoint& a)
147 {
148 return LayoutSize(a.x(), a.y());
149 }
150
flooredIntPoint(const LayoutPoint & point)151 inline IntPoint flooredIntPoint(const LayoutPoint& point)
152 {
153 return IntPoint(point.x().floor(), point.y().floor());
154 }
155
roundedIntPoint(const LayoutPoint & point)156 inline IntPoint roundedIntPoint(const LayoutPoint& point)
157 {
158 return IntPoint(point.x().round(), point.y().round());
159 }
160
roundedIntPoint(const LayoutSize & size)161 inline IntPoint roundedIntPoint(const LayoutSize& size)
162 {
163 return IntPoint(size.width().round(), size.height().round());
164 }
165
ceiledIntPoint(const LayoutPoint & point)166 inline IntPoint ceiledIntPoint(const LayoutPoint& point)
167 {
168 return IntPoint(point.x().ceil(), point.y().ceil());
169 }
170
flooredLayoutPoint(const FloatPoint & p)171 inline LayoutPoint flooredLayoutPoint(const FloatPoint& p)
172 {
173 return LayoutPoint(LayoutUnit::fromFloatFloor(p.x()), LayoutUnit::fromFloatFloor(p.y()));
174 }
175
ceiledLayoutPoint(const FloatPoint & p)176 inline LayoutPoint ceiledLayoutPoint(const FloatPoint& p)
177 {
178 return LayoutPoint(LayoutUnit::fromFloatCeil(p.x()), LayoutUnit::fromFloatCeil(p.y()));
179 }
180
pixelSnappedIntSize(const LayoutSize & s,const LayoutPoint & p)181 inline IntSize pixelSnappedIntSize(const LayoutSize& s, const LayoutPoint& p)
182 {
183 return IntSize(snapSizeToPixel(s.width(), p.x()), snapSizeToPixel(s.height(), p.y()));
184 }
185
roundedLayoutPoint(const FloatPoint & p)186 inline LayoutPoint roundedLayoutPoint(const FloatPoint& p)
187 {
188 return LayoutPoint(p);
189 }
190
toLayoutSize(const LayoutPoint & p)191 inline LayoutSize toLayoutSize(const LayoutPoint& p)
192 {
193 return LayoutSize(p.x(), p.y());
194 }
195
flooredLayoutPoint(const FloatSize & s)196 inline LayoutPoint flooredLayoutPoint(const FloatSize& s)
197 {
198 return flooredLayoutPoint(FloatPoint(s));
199 }
200
201
202 } // namespace WebCore
203
204 #endif // LayoutPoint_h
205