1/*
2 * Copyright (C) 2004-2016 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "IntSize.h"
29#include <cmath>
30
31#if PLATFORM(MAC) && defined __OBJC__
32#import <Foundation/NSGeometry.h>
33#endif
34
35#if USE(CG)
36typedef struct CGPoint CGPoint;
37#endif
38
39
40#if !PLATFORM(IOS_FAMILY)
41#if OS(DARWIN)
42#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
43typedef struct CGPoint NSPoint;
44#else
45typedef struct _NSPoint NSPoint;
46#endif
47#endif
48#endif // !PLATFORM(IOS_FAMILY)
49
50#if PLATFORM(WIN)
51typedef struct tagPOINT POINT;
52typedef struct tagPOINTS POINTS;
53
54struct D2D_POINT_2U;
55typedef D2D_POINT_2U D2D1_POINT_2U;
56
57struct D2D_POINT_2F;
58typedef D2D_POINT_2F D2D1_POINT_2F;
59#endif
60
61namespace WTF {
62class TextStream;
63}
64
65namespace WebCore {
66
67class FloatPoint;
68
69class IntPoint {
70public:
71 IntPoint() : m_x(0), m_y(0) { }
72 IntPoint(int x, int y) : m_x(x), m_y(y) { }
73 explicit IntPoint(const IntSize& size) : m_x(size.width()), m_y(size.height()) { }
74 WEBCORE_EXPORT explicit IntPoint(const FloatPoint&); // don't do this implicitly since it's lossy
75
76 static IntPoint zero() { return IntPoint(); }
77 bool isZero() const { return !m_x && !m_y; }
78
79 int x() const { return m_x; }
80 int y() const { return m_y; }
81
82 void setX(int x) { m_x = x; }
83 void setY(int y) { m_y = y; }
84
85 void move(const IntSize& s) { move(s.width(), s.height()); }
86 void moveBy(const IntPoint& offset) { move(offset.x(), offset.y()); }
87 void move(int dx, int dy) { m_x += dx; m_y += dy; }
88 void scale(float sx, float sy)
89 {
90 m_x = lroundf(static_cast<float>(m_x * sx));
91 m_y = lroundf(static_cast<float>(m_y * sy));
92 }
93
94 void scale(float scale)
95 {
96 this->scale(scale, scale);
97 }
98
99 IntPoint expandedTo(const IntPoint& other) const
100 {
101 return {
102 m_x > other.m_x ? m_x : other.m_x,
103 m_y > other.m_y ? m_y : other.m_y
104 };
105 }
106
107 IntPoint shrunkTo(const IntPoint& other) const
108 {
109 return {
110 m_x < other.m_x ? m_x : other.m_x,
111 m_y < other.m_y ? m_y : other.m_y
112 };
113 }
114
115 WEBCORE_EXPORT IntPoint constrainedBetween(const IntPoint& min, const IntPoint& max) const;
116
117 int distanceSquaredToPoint(const IntPoint&) const;
118
119 void clampNegativeToZero()
120 {
121 *this = expandedTo(zero());
122 }
123
124 IntPoint transposedPoint() const
125 {
126 return IntPoint(m_y, m_x);
127 }
128
129#if USE(CG)
130 WEBCORE_EXPORT explicit IntPoint(const CGPoint&); // don't do this implicitly since it's lossy
131 WEBCORE_EXPORT operator CGPoint() const;
132#endif
133
134#if !PLATFORM(IOS_FAMILY)
135#if OS(DARWIN) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
136 WEBCORE_EXPORT explicit IntPoint(const NSPoint&); // don't do this implicitly since it's lossy
137 WEBCORE_EXPORT operator NSPoint() const;
138#endif
139#endif // !PLATFORM(IOS_FAMILY)
140
141#if PLATFORM(WIN)
142 IntPoint(const POINT&);
143 operator POINT() const;
144 IntPoint(const POINTS&);
145 operator POINTS() const;
146
147 IntPoint(const D2D1_POINT_2U&);
148 explicit IntPoint(const D2D1_POINT_2F&); // Don't do this implicitly, since it's lossy.
149 operator D2D1_POINT_2F() const;
150 operator D2D1_POINT_2U() const;
151#endif
152
153private:
154 int m_x, m_y;
155};
156
157inline IntPoint& operator+=(IntPoint& a, const IntSize& b)
158{
159 a.move(b.width(), b.height());
160 return a;
161}
162
163inline IntPoint& operator-=(IntPoint& a, const IntSize& b)
164{
165 a.move(-b.width(), -b.height());
166 return a;
167}
168
169inline IntPoint operator+(const IntPoint& a, const IntSize& b)
170{
171 return IntPoint(a.x() + b.width(), a.y() + b.height());
172}
173
174inline IntPoint operator+(const IntPoint& a, const IntPoint& b)
175{
176 return IntPoint(a.x() + b.x(), a.y() + b.y());
177}
178
179inline IntSize operator-(const IntPoint& a, const IntPoint& b)
180{
181 return IntSize(a.x() - b.x(), a.y() - b.y());
182}
183
184inline IntPoint operator-(const IntPoint& a, const IntSize& b)
185{
186 return IntPoint(a.x() - b.width(), a.y() - b.height());
187}
188
189inline IntPoint operator-(const IntPoint& point)
190{
191 return IntPoint(-point.x(), -point.y());
192}
193
194inline bool operator==(const IntPoint& a, const IntPoint& b)
195{
196 return a.x() == b.x() && a.y() == b.y();
197}
198
199inline bool operator!=(const IntPoint& a, const IntPoint& b)
200{
201 return a.x() != b.x() || a.y() != b.y();
202}
203
204inline IntSize toIntSize(const IntPoint& a)
205{
206 return IntSize(a.x(), a.y());
207}
208
209inline int IntPoint::distanceSquaredToPoint(const IntPoint& point) const
210{
211 return ((*this) - point).diagonalLengthSquared();
212}
213
214WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const IntPoint&);
215
216} // namespace WebCore
217
218namespace WTF {
219template<> struct DefaultHash<WebCore::IntPoint>;
220template<> struct HashTraits<WebCore::IntPoint>;
221}
222