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#pragma once
32
33#include "FloatPoint.h"
34#include "LayoutSize.h"
35
36namespace WebCore {
37
38class LayoutPoint {
39public:
40 LayoutPoint() : m_x(0), m_y(0) { }
41 template<typename T, typename U> LayoutPoint(T x, U y) : m_x(x), m_y(y) { }
42 LayoutPoint(const IntPoint& point) : m_x(point.x()), m_y(point.y()) { }
43 explicit LayoutPoint(const FloatPoint& size) : m_x(size.x()), m_y(size.y()) { }
44 explicit LayoutPoint(const LayoutSize& size) : m_x(size.width()), m_y(size.height()) { }
45
46 static LayoutPoint zero() { return LayoutPoint(); }
47
48 LayoutUnit x() const { return m_x; }
49 LayoutUnit y() const { return m_y; }
50
51 template<typename T> void setX(T x) { m_x = x; }
52 template<typename T> void setY(T y) { m_y = y; }
53
54 void move(const LayoutSize& s) { move(s.width(), s.height()); }
55 void moveBy(const LayoutPoint& offset) { move(offset.x(), offset.y()); }
56 template<typename T, typename U> void move(T dx, U dy) { m_x += dx; m_y += dy; }
57
58 void scale(float s)
59 {
60 m_x *= s;
61 m_y *= s;
62 }
63
64 void scale(float sx, float sy)
65 {
66 m_x *= sx;
67 m_y *= sy;
68 }
69
70 LayoutPoint scaled(float s) const
71 {
72 return { m_x * s, m_y * s };
73 }
74
75 LayoutPoint scaled(float sx, float sy) const
76 {
77 return { m_x * sx, m_y * sy };
78 }
79
80 LayoutPoint constrainedBetween(const LayoutPoint& min, const LayoutPoint& max) const;
81
82 LayoutPoint expandedTo(const LayoutPoint& other) const
83 {
84 return { std::max(m_x, other.m_x), std::max(m_y, other.m_y) };
85 }
86
87 LayoutPoint shrunkTo(const LayoutPoint& other) const
88 {
89 return { std::min(m_x, other.m_x), std::min(m_y, other.m_y) };
90 }
91
92 void clampNegativeToZero()
93 {
94 *this = expandedTo(zero());
95 }
96
97 LayoutPoint transposedPoint() const
98 {
99 return { m_y, m_x };
100 }
101
102 LayoutPoint fraction() const
103 {
104 return { m_x.fraction(), m_y.fraction() };
105 }
106
107 operator FloatPoint() const { return { m_x, m_y }; }
108
109private:
110 LayoutUnit m_x, m_y;
111};
112
113inline LayoutPoint& operator+=(LayoutPoint& a, const LayoutSize& b)
114{
115 a.move(b.width(), b.height());
116 return a;
117}
118
119inline LayoutPoint& operator-=(LayoutPoint& a, const LayoutSize& b)
120{
121 a.move(-b.width(), -b.height());
122 return a;
123}
124
125inline LayoutPoint operator+(const LayoutPoint& a, const LayoutSize& b)
126{
127 return LayoutPoint(a.x() + b.width(), a.y() + b.height());
128}
129
130inline LayoutPoint operator+(const LayoutPoint& a, const LayoutPoint& b)
131{
132 return LayoutPoint(a.x() + b.x(), a.y() + b.y());
133}
134
135inline LayoutSize operator-(const LayoutPoint& a, const LayoutPoint& b)
136{
137 return LayoutSize(a.x() - b.x(), a.y() - b.y());
138}
139
140inline LayoutPoint operator-(const LayoutPoint& a, const LayoutSize& b)
141{
142 return LayoutPoint(a.x() - b.width(), a.y() - b.height());
143}
144
145inline LayoutPoint operator-(const LayoutPoint& point)
146{
147 return LayoutPoint(-point.x(), -point.y());
148}
149
150inline bool operator==(const LayoutPoint& a, const LayoutPoint& b)
151{
152 return a.x() == b.x() && a.y() == b.y();
153}
154
155inline bool operator!=(const LayoutPoint& a, const LayoutPoint& b)
156{
157 return a.x() != b.x() || a.y() != b.y();
158}
159
160inline LayoutPoint toLayoutPoint(const LayoutSize& size)
161{
162 return LayoutPoint(size.width(), size.height());
163}
164
165inline LayoutSize toLayoutSize(const LayoutPoint& point)
166{
167 return LayoutSize(point.x(), point.y());
168}
169
170inline IntPoint flooredIntPoint(const LayoutPoint& point)
171{
172 return IntPoint(point.x().floor(), point.y().floor());
173}
174
175inline IntPoint roundedIntPoint(const LayoutPoint& point)
176{
177 return IntPoint(point.x().round(), point.y().round());
178}
179
180inline IntPoint roundedIntPoint(const LayoutSize& size)
181{
182 return roundedIntPoint(toLayoutPoint(size));
183}
184
185inline IntPoint ceiledIntPoint(const LayoutPoint& point)
186{
187 return IntPoint(point.x().ceil(), point.y().ceil());
188}
189
190inline LayoutPoint flooredLayoutPoint(const FloatPoint& p)
191{
192 return LayoutPoint(LayoutUnit::fromFloatFloor(p.x()), LayoutUnit::fromFloatFloor(p.y()));
193}
194
195inline LayoutPoint ceiledLayoutPoint(const FloatPoint& p)
196{
197 return LayoutPoint(LayoutUnit::fromFloatCeil(p.x()), LayoutUnit::fromFloatCeil(p.y()));
198}
199
200inline IntSize snappedIntSize(const LayoutSize& size, const LayoutPoint& location)
201{
202 auto snap = [] (LayoutUnit a, LayoutUnit b) {
203 LayoutUnit fraction = b.fraction();
204 return roundToInt(fraction + a) - roundToInt(fraction);
205 };
206 return IntSize(snap(size.width(), location.x()), snap(size.height(), location.y()));
207}
208
209inline FloatPoint roundPointToDevicePixels(const LayoutPoint& point, float pixelSnappingFactor, bool directionalRoundingToRight = true, bool directionalRoundingToBottom = true)
210{
211 return FloatPoint(roundToDevicePixel(point.x(), pixelSnappingFactor, !directionalRoundingToRight), roundToDevicePixel(point.y(), pixelSnappingFactor, !directionalRoundingToBottom));
212}
213
214inline FloatPoint floorPointToDevicePixels(const LayoutPoint& point, float pixelSnappingFactor)
215{
216 return FloatPoint(floorToDevicePixel(point.x(), pixelSnappingFactor), floorToDevicePixel(point.y(), pixelSnappingFactor));
217}
218
219inline FloatPoint ceilPointToDevicePixels(const LayoutPoint& point, float pixelSnappingFactor)
220{
221 return FloatPoint(ceilToDevicePixel(point.x(), pixelSnappingFactor), ceilToDevicePixel(point.y(), pixelSnappingFactor));
222}
223
224inline FloatSize snapSizeToDevicePixel(const LayoutSize& size, const LayoutPoint& location, float pixelSnappingFactor)
225{
226 auto snap = [&] (LayoutUnit a, LayoutUnit b) {
227 LayoutUnit fraction = b.fraction();
228 return roundToDevicePixel(fraction + a, pixelSnappingFactor) - roundToDevicePixel(fraction, pixelSnappingFactor);
229 };
230 return FloatSize(snap(size.width(), location.x()), snap(size.height(), location.y()));
231}
232
233WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const LayoutPoint&);
234
235} // namespace WebCore
236
237