initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / 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 "IntPoint.h"
32 #include <wtf/MathExtras.h>
33
34 #if USE(CG) || (PLATFORM(WX) && OS(DARWIN)) || USE(SKIA_ON_MAC_CHROMIUM)
35 typedef struct CGSize CGSize;
36 #endif
37
38 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN)) || (PLATFORM(QT) && USE(QTKIT))
39 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
40 typedef struct CGSize NSSize;
41 #else
42 typedef struct _NSSize NSSize;
43 #endif
44 #endif
45
46 namespace WebCore {
47
48 class IntSize;
49
50 class FloatSize {
51 public:
52     FloatSize() : m_width(0), m_height(0) { }
53     FloatSize(float width, float height) : m_width(width), m_height(height) { }
54     FloatSize(const IntSize&);
55
56     static FloatSize narrowPrecision(double width, double height);
57
58     float width() const { return m_width; }
59     float height() const { return m_height; }
60
61     void setWidth(float width) { m_width = width; }
62     void setHeight(float height) { m_height = height; }
63
64     bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
65     bool isZero() const;
66     bool isExpressibleAsIntSize() const;
67
68     float aspectRatio() const { return m_width / m_height; }
69
70     void expand(float width, float height)
71     {
72         m_width += width;
73         m_height += height;
74     }
75
76     void scale(float s) { scale(s, s); }
77
78     void scale(float scaleX, float scaleY)
79     {
80         m_width *= scaleX;
81         m_height *= scaleY;
82     }
83
84     FloatSize expandedTo(const FloatSize& other) const
85     {
86         return FloatSize(m_width > other.m_width ? m_width : other.m_width,
87             m_height > other.m_height ? m_height : other.m_height);
88     }
89
90     FloatSize shrunkTo(const FloatSize& other) const
91     {
92        return FloatSize(m_width < other.m_width ? m_width : other.m_width,
93            m_height < other.m_height ? m_height : other.m_height);
94     }
95
96     float diagonalLength() const;
97     float diagonalLengthSquared() const
98     {
99         return m_width * m_width + m_height * m_height;
100     }
101
102     FloatSize transposedSize() const
103     {
104         return FloatSize(m_height, m_width);
105     }
106
107 #if USE(CG) || (PLATFORM(WX) && OS(DARWIN)) || USE(SKIA_ON_MAC_CHROMIUM)
108     explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy
109     operator CGSize() const;
110 #endif
111
112 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
113         || (PLATFORM(CHROMIUM) && OS(DARWIN)) || (PLATFORM(QT) && USE(QTKIT))
114     explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy
115     operator NSSize() const;
116 #endif
117
118 private:
119     float m_width, m_height;
120 };
121
122 inline FloatSize& operator+=(FloatSize& a, const FloatSize& b)
123 {
124     a.setWidth(a.width() + b.width());
125     a.setHeight(a.height() + b.height());
126     return a;
127 }
128
129 inline FloatSize& operator-=(FloatSize& a, const FloatSize& b)
130 {
131     a.setWidth(a.width() - b.width());
132     a.setHeight(a.height() - b.height());
133     return a;
134 }
135
136 inline FloatSize operator+(const FloatSize& a, const FloatSize& b)
137 {
138     return FloatSize(a.width() + b.width(), a.height() + b.height());
139 }
140
141 inline FloatSize operator-(const FloatSize& a, const FloatSize& b)
142 {
143     return FloatSize(a.width() - b.width(), a.height() - b.height());
144 }
145
146 inline FloatSize operator-(const FloatSize& size)
147 {
148     return FloatSize(-size.width(), -size.height());
149 }
150
151 inline bool operator==(const FloatSize& a, const FloatSize& b)
152 {
153     return a.width() == b.width() && a.height() == b.height();
154 }
155
156 inline bool operator!=(const FloatSize& a, const FloatSize& b)
157 {
158     return a.width() != b.width() || a.height() != b.height();
159 }
160
161 inline IntSize roundedIntSize(const FloatSize& p)
162 {
163     return IntSize(static_cast<int>(roundf(p.width())), static_cast<int>(roundf(p.height())));
164 }
165
166 inline IntSize expandedIntSize(const FloatSize& p)
167 {
168     return IntSize(clampToInteger(ceilf(p.width())), clampToInteger(ceilf(p.height())));
169 }
170
171 inline IntPoint flooredIntPoint(const FloatSize& p)
172 {
173     return IntPoint(static_cast<int>(p.width()), static_cast<int>(p.height()));
174 }
175
176 } // namespace WebCore
177
178 #endif // FloatSize_h