Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / geometry / FloatSize.h
1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2005 Nokia.  All rights reserved.
4  *               2008 Eric Seidel <eric@webkit.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef FloatSize_h
29 #define FloatSize_h
30
31 #include "platform/geometry/IntPoint.h"
32 #include "wtf/MathExtras.h"
33
34 #if OS(MACOSX)
35 typedef struct CGSize CGSize;
36
37 #ifdef __OBJC__
38 #import <Foundation/Foundation.h>
39 #endif
40 #endif
41
42 namespace blink {
43
44 class IntSize;
45 class LayoutSize;
46
47 class PLATFORM_EXPORT FloatSize {
48 public:
49     FloatSize() : m_width(0), m_height(0) { }
50     FloatSize(float width, float height) : m_width(width), m_height(height) { }
51     FloatSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
52     FloatSize(const LayoutSize&);
53
54     static FloatSize narrowPrecision(double width, double height);
55
56     float width() const { return m_width; }
57     float height() const { return m_height; }
58
59     void setWidth(float width) { m_width = width; }
60     void setHeight(float height) { m_height = height; }
61
62     bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
63     bool isZero() const;
64     bool isExpressibleAsIntSize() const;
65
66     float aspectRatio() const { return m_width / m_height; }
67
68     void expand(float width, float height)
69     {
70         m_width += width;
71         m_height += height;
72     }
73
74     void scale(float s) { scale(s, s); }
75
76     void scale(float scaleX, float scaleY)
77     {
78         m_width *= scaleX;
79         m_height *= scaleY;
80     }
81
82     FloatSize expandedTo(const FloatSize& other) const
83     {
84         return FloatSize(m_width > other.m_width ? m_width : other.m_width,
85             m_height > other.m_height ? m_height : other.m_height);
86     }
87
88     FloatSize shrunkTo(const FloatSize& other) const
89     {
90         return FloatSize(m_width < other.m_width ? m_width : other.m_width,
91             m_height < other.m_height ? m_height : other.m_height);
92     }
93
94     float diagonalLength() const;
95     float diagonalLengthSquared() const
96     {
97         return m_width * m_width + m_height * m_height;
98     }
99
100     FloatSize transposedSize() const
101     {
102         return FloatSize(m_height, m_width);
103     }
104
105     FloatSize scaledBy(float scale) const
106     {
107         return scaledBy(scale, scale);
108     }
109
110     FloatSize scaledBy(float scaleX, float scaleY) const
111     {
112         return FloatSize(m_width * scaleX, m_height * scaleY);
113     }
114
115 #if OS(MACOSX)
116     explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy
117     operator CGSize() const;
118 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
119     explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy
120     operator NSSize() const;
121 #endif
122 #endif
123
124 private:
125     float m_width, m_height;
126 };
127
128 inline FloatSize& operator+=(FloatSize& a, const FloatSize& b)
129 {
130     a.setWidth(a.width() + b.width());
131     a.setHeight(a.height() + b.height());
132     return a;
133 }
134
135 inline FloatSize& operator-=(FloatSize& a, const FloatSize& b)
136 {
137     a.setWidth(a.width() - b.width());
138     a.setHeight(a.height() - b.height());
139     return a;
140 }
141
142 inline FloatSize operator+(const FloatSize& a, const FloatSize& b)
143 {
144     return FloatSize(a.width() + b.width(), a.height() + b.height());
145 }
146
147 inline FloatSize operator-(const FloatSize& a, const FloatSize& b)
148 {
149     return FloatSize(a.width() - b.width(), a.height() - b.height());
150 }
151
152 inline FloatSize operator-(const FloatSize& size)
153 {
154     return FloatSize(-size.width(), -size.height());
155 }
156
157 inline FloatSize operator*(const FloatSize& a, const float b)
158 {
159     return FloatSize(a.width() * b, a.height() * b);
160 }
161
162 inline FloatSize operator*(const float a, const FloatSize& b)
163 {
164     return FloatSize(a * b.width(), a * b.height());
165 }
166
167 inline bool operator==(const FloatSize& a, const FloatSize& b)
168 {
169     return a.width() == b.width() && a.height() == b.height();
170 }
171
172 inline bool operator!=(const FloatSize& a, const FloatSize& b)
173 {
174     return a.width() != b.width() || a.height() != b.height();
175 }
176
177 inline IntSize roundedIntSize(const FloatSize& p)
178 {
179     return IntSize(clampTo<int>(roundf(p.width())), clampTo<int>(roundf(p.height())));
180 }
181
182 inline IntSize flooredIntSize(const FloatSize& p)
183 {
184     return IntSize(clampTo<int>(floorf(p.width())), clampTo<int>(floorf(p.height())));
185 }
186
187 inline IntSize expandedIntSize(const FloatSize& p)
188 {
189     return IntSize(clampTo<int>(ceilf(p.width())), clampTo<int>(ceilf(p.height())));
190 }
191
192 inline IntPoint flooredIntPoint(const FloatSize& p)
193 {
194     return IntPoint(clampTo<int>(floorf(p.width())), clampTo<int>(floorf(p.height())));
195 }
196
197 } // namespace blink
198
199 #endif // FloatSize_h