| 1 | /* |
| 2 | * Copyright (C) 2004-2016 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2005 Nokia. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include "FloatSize.h" |
| 30 | #include "IntPoint.h" |
| 31 | #include <wtf/MathExtras.h> |
| 32 | |
| 33 | #if PLATFORM(MAC) && defined __OBJC__ |
| 34 | #import <Foundation/NSGeometry.h> |
| 35 | #endif |
| 36 | |
| 37 | #if USE(CG) |
| 38 | typedef struct CGPoint CGPoint; |
| 39 | #endif |
| 40 | |
| 41 | #if PLATFORM(MAC) |
| 42 | #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES |
| 43 | typedef struct CGPoint NSPoint; |
| 44 | #else |
| 45 | typedef struct _NSPoint NSPoint; |
| 46 | #endif |
| 47 | #endif // PLATFORM(MAC) |
| 48 | |
| 49 | #if PLATFORM(WIN) |
| 50 | struct D2D_POINT_2F; |
| 51 | typedef D2D_POINT_2F D2D1_POINT_2F; |
| 52 | #endif |
| 53 | |
| 54 | namespace WTF { |
| 55 | class TextStream; |
| 56 | } |
| 57 | |
| 58 | namespace WebCore { |
| 59 | |
| 60 | class AffineTransform; |
| 61 | class TransformationMatrix; |
| 62 | class IntPoint; |
| 63 | class IntSize; |
| 64 | |
| 65 | class FloatPoint { |
| 66 | public: |
| 67 | FloatPoint() { } |
| 68 | FloatPoint(float x, float y) : m_x(x), m_y(y) { } |
| 69 | WEBCORE_EXPORT FloatPoint(const IntPoint&); |
| 70 | explicit FloatPoint(const FloatSize& size) : m_x(size.width()), m_y(size.height()) { } |
| 71 | |
| 72 | static FloatPoint zero() { return FloatPoint(); } |
| 73 | bool isZero() const { return !m_x && !m_y; } |
| 74 | |
| 75 | WEBCORE_EXPORT static FloatPoint narrowPrecision(double x, double y); |
| 76 | |
| 77 | float x() const { return m_x; } |
| 78 | float y() const { return m_y; } |
| 79 | |
| 80 | void setX(float x) { m_x = x; } |
| 81 | void setY(float y) { m_y = y; } |
| 82 | |
| 83 | void set(float x, float y) |
| 84 | { |
| 85 | m_x = x; |
| 86 | m_y = y; |
| 87 | } |
| 88 | |
| 89 | void move(float dx, float dy) |
| 90 | { |
| 91 | m_x += dx; |
| 92 | m_y += dy; |
| 93 | } |
| 94 | |
| 95 | void move(const IntSize& a) |
| 96 | { |
| 97 | m_x += a.width(); |
| 98 | m_y += a.height(); |
| 99 | } |
| 100 | |
| 101 | void move(const FloatSize& a) |
| 102 | { |
| 103 | m_x += a.width(); |
| 104 | m_y += a.height(); |
| 105 | } |
| 106 | |
| 107 | void moveBy(const IntPoint& a) |
| 108 | { |
| 109 | m_x += a.x(); |
| 110 | m_y += a.y(); |
| 111 | } |
| 112 | |
| 113 | void moveBy(const FloatPoint& a) |
| 114 | { |
| 115 | m_x += a.x(); |
| 116 | m_y += a.y(); |
| 117 | } |
| 118 | |
| 119 | void scale(float scale) |
| 120 | { |
| 121 | m_x *= scale; |
| 122 | m_y *= scale; |
| 123 | } |
| 124 | |
| 125 | void scale(float scaleX, float scaleY) |
| 126 | { |
| 127 | m_x *= scaleX; |
| 128 | m_y *= scaleY; |
| 129 | } |
| 130 | |
| 131 | FloatPoint scaled(float scale) const |
| 132 | { |
| 133 | return { m_x * scale, m_y * scale }; |
| 134 | } |
| 135 | |
| 136 | FloatPoint scaled(float scaleX, float scaleY) const |
| 137 | { |
| 138 | return { m_x * scaleX, m_y * scaleY }; |
| 139 | } |
| 140 | |
| 141 | WEBCORE_EXPORT void normalize(); |
| 142 | |
| 143 | float dot(const FloatPoint& a) const |
| 144 | { |
| 145 | return m_x * a.x() + m_y * a.y(); |
| 146 | } |
| 147 | |
| 148 | float slopeAngleRadians() const; |
| 149 | float length() const; |
| 150 | |
| 151 | float lengthSquared() const |
| 152 | { |
| 153 | return m_x * m_x + m_y * m_y; |
| 154 | } |
| 155 | |
| 156 | WEBCORE_EXPORT FloatPoint constrainedBetween(const FloatPoint& min, const FloatPoint& max) const; |
| 157 | |
| 158 | FloatPoint shrunkTo(const FloatPoint& other) const |
| 159 | { |
| 160 | return { std::min(m_x, other.m_x), std::min(m_y, other.m_y) }; |
| 161 | } |
| 162 | |
| 163 | FloatPoint expandedTo(const FloatPoint& other) const |
| 164 | { |
| 165 | return { std::max(m_x, other.m_x), std::max(m_y, other.m_y) }; |
| 166 | } |
| 167 | |
| 168 | FloatPoint transposedPoint() const |
| 169 | { |
| 170 | return { m_y, m_x }; |
| 171 | } |
| 172 | |
| 173 | #if USE(CG) |
| 174 | WEBCORE_EXPORT FloatPoint(const CGPoint&); |
| 175 | WEBCORE_EXPORT operator CGPoint() const; |
| 176 | #endif |
| 177 | |
| 178 | #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) |
| 179 | WEBCORE_EXPORT FloatPoint(const NSPoint&); |
| 180 | WEBCORE_EXPORT operator NSPoint() const; |
| 181 | #endif |
| 182 | |
| 183 | #if PLATFORM(WIN) |
| 184 | WEBCORE_EXPORT FloatPoint(const D2D1_POINT_2F&); |
| 185 | WEBCORE_EXPORT operator D2D1_POINT_2F() const; |
| 186 | #endif |
| 187 | |
| 188 | WEBCORE_EXPORT FloatPoint matrixTransform(const TransformationMatrix&) const; |
| 189 | WEBCORE_EXPORT FloatPoint matrixTransform(const AffineTransform&) const; |
| 190 | |
| 191 | private: |
| 192 | float m_x { 0 }; |
| 193 | float m_y { 0 }; |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b) |
| 198 | { |
| 199 | a.move(b.width(), b.height()); |
| 200 | return a; |
| 201 | } |
| 202 | |
| 203 | inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b) |
| 204 | { |
| 205 | a.move(b.x(), b.y()); |
| 206 | return a; |
| 207 | } |
| 208 | |
| 209 | inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b) |
| 210 | { |
| 211 | a.move(-b.width(), -b.height()); |
| 212 | return a; |
| 213 | } |
| 214 | |
| 215 | inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b) |
| 216 | { |
| 217 | return FloatPoint(a.x() + b.width(), a.y() + b.height()); |
| 218 | } |
| 219 | |
| 220 | inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b) |
| 221 | { |
| 222 | return FloatPoint(a.x() + b.x(), a.y() + b.y()); |
| 223 | } |
| 224 | |
| 225 | inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b) |
| 226 | { |
| 227 | return FloatSize(a.x() - b.x(), a.y() - b.y()); |
| 228 | } |
| 229 | |
| 230 | inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b) |
| 231 | { |
| 232 | return FloatPoint(a.x() - b.width(), a.y() - b.height()); |
| 233 | } |
| 234 | |
| 235 | inline FloatPoint operator-(const FloatPoint& a) |
| 236 | { |
| 237 | return FloatPoint(-a.x(), -a.y()); |
| 238 | } |
| 239 | |
| 240 | inline bool operator==(const FloatPoint& a, const FloatPoint& b) |
| 241 | { |
| 242 | return a.x() == b.x() && a.y() == b.y(); |
| 243 | } |
| 244 | |
| 245 | inline bool operator!=(const FloatPoint& a, const FloatPoint& b) |
| 246 | { |
| 247 | return a.x() != b.x() || a.y() != b.y(); |
| 248 | } |
| 249 | |
| 250 | inline float operator*(const FloatPoint& a, const FloatPoint& b) |
| 251 | { |
| 252 | // dot product |
| 253 | return a.dot(b); |
| 254 | } |
| 255 | |
| 256 | inline IntSize flooredIntSize(const FloatPoint& p) |
| 257 | { |
| 258 | return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))); |
| 259 | } |
| 260 | |
| 261 | inline IntPoint roundedIntPoint(const FloatPoint& p) |
| 262 | { |
| 263 | return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y()))); |
| 264 | } |
| 265 | |
| 266 | inline IntPoint flooredIntPoint(const FloatPoint& p) |
| 267 | { |
| 268 | return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))); |
| 269 | } |
| 270 | |
| 271 | inline IntPoint ceiledIntPoint(const FloatPoint& p) |
| 272 | { |
| 273 | return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y()))); |
| 274 | } |
| 275 | |
| 276 | inline FloatPoint floorPointToDevicePixels(const FloatPoint& p, float deviceScaleFactor) |
| 277 | { |
| 278 | return FloatPoint(floorf(p.x() * deviceScaleFactor) / deviceScaleFactor, floorf(p.y() * deviceScaleFactor) / deviceScaleFactor); |
| 279 | } |
| 280 | |
| 281 | inline FloatPoint ceilPointToDevicePixels(const FloatPoint& p, float deviceScaleFactor) |
| 282 | { |
| 283 | return FloatPoint(ceilf(p.x() * deviceScaleFactor) / deviceScaleFactor, ceilf(p.y() * deviceScaleFactor) / deviceScaleFactor); |
| 284 | } |
| 285 | |
| 286 | inline FloatSize toFloatSize(const FloatPoint& a) |
| 287 | { |
| 288 | return FloatSize(a.x(), a.y()); |
| 289 | } |
| 290 | |
| 291 | inline FloatPoint toFloatPoint(const FloatSize& a) |
| 292 | { |
| 293 | return FloatPoint(a.width(), a.height()); |
| 294 | } |
| 295 | |
| 296 | inline bool areEssentiallyEqual(const FloatPoint& a, const FloatPoint& b) |
| 297 | { |
| 298 | return WTF::areEssentiallyEqual(a.x(), b.x()) && WTF::areEssentiallyEqual(a.y(), b.y()); |
| 299 | } |
| 300 | |
| 301 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const FloatPoint&); |
| 302 | |
| 303 | } |
| 304 | |
| 305 | |