2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "platform/geometry/FloatPoint.h"
35 #include "platform/geometry/LayoutSize.h"
36 #include "wtf/MathExtras.h"
43 LayoutPoint(LayoutUnit x, LayoutUnit y) : m_x(x), m_y(y) { }
44 LayoutPoint(const IntPoint& point) : m_x(point.x()), m_y(point.y()) { }
45 explicit LayoutPoint(const FloatPoint& size) : m_x(size.x()), m_y(size.y()) { }
46 explicit LayoutPoint(const LayoutSize& size) : m_x(size.width()), m_y(size.height()) { }
48 static LayoutPoint zero() { return LayoutPoint(); }
50 LayoutUnit x() const { return m_x; }
51 LayoutUnit y() const { return m_y; }
53 void setX(LayoutUnit x) { m_x = x; }
54 void setY(LayoutUnit y) { m_y = y; }
56 void move(const LayoutSize& s) { move(s.width(), s.height()); }
57 void moveBy(const LayoutPoint& offset) { move(offset.x(), offset.y()); }
58 void move(LayoutUnit dx, LayoutUnit dy) { m_x += dx; m_y += dy; }
59 void scale(float sx, float sy)
65 LayoutPoint expandedTo(const LayoutPoint& other) const
67 return LayoutPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
70 LayoutPoint shrunkTo(const LayoutPoint& other) const
72 return LayoutPoint(std::min(m_x, other.m_x), std::min(m_y, other.m_y));
75 void clampNegativeToZero()
77 *this = expandedTo(zero());
80 LayoutPoint transposedPoint() const
82 return LayoutPoint(m_y, m_x);
89 ALWAYS_INLINE LayoutPoint& operator+=(LayoutPoint& a, const LayoutSize& b)
91 a.move(b.width(), b.height());
95 ALWAYS_INLINE LayoutPoint& operator-=(LayoutPoint& a, const LayoutSize& b)
97 a.move(-b.width(), -b.height());
101 inline LayoutPoint operator+(const LayoutPoint& a, const LayoutSize& b)
103 return LayoutPoint(a.x() + b.width(), a.y() + b.height());
106 ALWAYS_INLINE LayoutPoint operator+(const LayoutPoint& a, const LayoutPoint& b)
108 return LayoutPoint(a.x() + b.x(), a.y() + b.y());
111 ALWAYS_INLINE LayoutSize operator-(const LayoutPoint& a, const LayoutPoint& b)
113 return LayoutSize(a.x() - b.x(), a.y() - b.y());
116 inline LayoutPoint operator-(const LayoutPoint& a, const LayoutSize& b)
118 return LayoutPoint(a.x() - b.width(), a.y() - b.height());
121 inline LayoutPoint operator-(const LayoutPoint& point)
123 return LayoutPoint(-point.x(), -point.y());
126 ALWAYS_INLINE bool operator==(const LayoutPoint& a, const LayoutPoint& b)
128 return a.x() == b.x() && a.y() == b.y();
131 inline bool operator!=(const LayoutPoint& a, const LayoutPoint& b)
133 return a.x() != b.x() || a.y() != b.y();
136 inline LayoutPoint toPoint(const LayoutSize& size)
138 return LayoutPoint(size.width(), size.height());
141 inline LayoutPoint toLayoutPoint(const LayoutSize& p)
143 return LayoutPoint(p.width(), p.height());
146 inline LayoutSize toSize(const LayoutPoint& a)
148 return LayoutSize(a.x(), a.y());
151 inline IntPoint flooredIntPoint(const LayoutPoint& point)
153 return IntPoint(point.x().floor(), point.y().floor());
156 inline IntPoint roundedIntPoint(const LayoutPoint& point)
158 return IntPoint(point.x().round(), point.y().round());
161 inline IntPoint roundedIntPoint(const LayoutSize& size)
163 return IntPoint(size.width().round(), size.height().round());
166 inline IntPoint ceiledIntPoint(const LayoutPoint& point)
168 return IntPoint(point.x().ceil(), point.y().ceil());
171 inline LayoutPoint flooredLayoutPoint(const FloatPoint& p)
173 return LayoutPoint(LayoutUnit::fromFloatFloor(p.x()), LayoutUnit::fromFloatFloor(p.y()));
176 inline LayoutPoint ceiledLayoutPoint(const FloatPoint& p)
178 return LayoutPoint(LayoutUnit::fromFloatCeil(p.x()), LayoutUnit::fromFloatCeil(p.y()));
181 inline IntSize pixelSnappedIntSize(const LayoutSize& s, const LayoutPoint& p)
183 return IntSize(snapSizeToPixel(s.width(), p.x()), snapSizeToPixel(s.height(), p.y()));
186 inline LayoutPoint roundedLayoutPoint(const FloatPoint& p)
188 return LayoutPoint(p);
191 inline LayoutSize toLayoutSize(const LayoutPoint& p)
193 return LayoutSize(p.x(), p.y());
196 inline LayoutPoint flooredLayoutPoint(const FloatSize& s)
198 return flooredLayoutPoint(FloatPoint(s));
202 } // namespace WebCore
204 #endif // LayoutPoint_h