• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2006, 2007 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 COMPUTER, 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 COMPUTER, 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 #ifndef FloatPoint_h
28 #define FloatPoint_h
29 
30 #include "FloatSize.h"
31 #include "IntPoint.h"
32 #include <wtf/MathExtras.h>
33 #include <wtf/Platform.h>
34 
35 #if PLATFORM(CG)
36 typedef struct CGPoint CGPoint;
37 #endif
38 
39 #if PLATFORM(MAC)
40 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
41 typedef struct CGPoint NSPoint;
42 #else
43 typedef struct _NSPoint NSPoint;
44 #endif
45 #endif
46 
47 #if PLATFORM(QT)
48 #include "qglobal.h"
49 QT_BEGIN_NAMESPACE
50 class QPointF;
51 QT_END_NAMESPACE
52 #endif
53 
54 #if PLATFORM(SKIA)
55 struct SkPoint;
56 #endif
57 
58 namespace WebCore {
59 
60 class TransformationMatrix;
61 class IntPoint;
62 
63 class FloatPoint {
64 public:
FloatPoint()65     FloatPoint() : m_x(0), m_y(0) { }
FloatPoint(float x,float y)66     FloatPoint(float x, float y) : m_x(x), m_y(y) { }
67     FloatPoint(const IntPoint&);
68 
69     static FloatPoint narrowPrecision(double x, double y);
70 
x()71     float x() const { return m_x; }
y()72     float y() const { return m_y; }
73 
setX(float x)74     void setX(float x) { m_x = x; }
setY(float y)75     void setY(float y) { m_y = y; }
move(float dx,float dy)76     void move(float dx, float dy) { m_x += dx; m_y += dy; }
77 
78 #if PLATFORM(CG)
79     FloatPoint(const CGPoint&);
80     operator CGPoint() const;
81 #endif
82 
83 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
84     FloatPoint(const NSPoint&);
85     operator NSPoint() const;
86 #endif
87 
88 #if PLATFORM(QT)
89     FloatPoint(const QPointF&);
90     operator QPointF() const;
91 #endif
92 
93 #if (PLATFORM(SKIA) || PLATFORM(SGL))
94     operator SkPoint() const;
95     FloatPoint(const SkPoint&);
96 #endif
97 
98     FloatPoint matrixTransform(const TransformationMatrix&) const;
99 
100 private:
101     float m_x, m_y;
102 };
103 
104 
105 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
106 {
107     a.move(b.width(), b.height());
108     return a;
109 }
110 
111 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
112 {
113     a.move(-b.width(), -b.height());
114     return a;
115 }
116 
117 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
118 {
119     return FloatPoint(a.x() + b.width(), a.y() + b.height());
120 }
121 
122 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
123 {
124     return FloatSize(a.x() - b.x(), a.y() - b.y());
125 }
126 
127 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
128 {
129     return FloatPoint(a.x() - b.width(), a.y() - b.height());
130 }
131 
132 inline bool operator==(const FloatPoint& a, const FloatPoint& b)
133 {
134     return a.x() == b.x() && a.y() == b.y();
135 }
136 
137 inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
138 {
139     return a.x() != b.x() || a.y() != b.y();
140 }
141 
roundedIntPoint(const FloatPoint & p)142 inline IntPoint roundedIntPoint(const FloatPoint& p)
143 {
144     return IntPoint(static_cast<int>(roundf(p.x())), static_cast<int>(roundf(p.y())));
145 }
146 
147 }
148 
149 #endif
150