1/*
2 * Copyright (C) 2003, 2006 Apple 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 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 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 PLATFORM(IOS)
35#include <CoreGraphics/CoreGraphics.h>
36#endif
37
38#if USE(CG)
39typedef struct CGSize CGSize;
40#endif
41
42#if PLATFORM(MAC)
43#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
44typedef struct CGSize NSSize;
45#else
46typedef struct _NSSize NSSize;
47#endif
48#endif // PLATFORM(MAC)
49
50namespace WebCore {
51
52class IntSize;
53
54class FloatSize {
55public:
56    FloatSize() : m_width(0), m_height(0) { }
57    FloatSize(float width, float height) : m_width(width), m_height(height) { }
58    FloatSize(const IntSize&);
59
60    static FloatSize narrowPrecision(double width, double height);
61
62    float width() const { return m_width; }
63    float height() const { return m_height; }
64
65    void setWidth(float width) { m_width = width; }
66    void setHeight(float height) { m_height = height; }
67
68    bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
69    bool isZero() const;
70    bool isExpressibleAsIntSize() const;
71
72    float aspectRatio() const { return m_width / m_height; }
73
74    void expand(float width, float height)
75    {
76        m_width += width;
77        m_height += height;
78    }
79
80    void scale(float s) { scale(s, s); }
81
82    void scale(float scaleX, float scaleY)
83    {
84        m_width *= scaleX;
85        m_height *= scaleY;
86    }
87
88    FloatSize expandedTo(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    FloatSize shrunkTo(const FloatSize& other) const
95    {
96       return FloatSize(m_width < other.m_width ? m_width : other.m_width,
97           m_height < other.m_height ? m_height : other.m_height);
98    }
99
100    float diagonalLength() const;
101    float diagonalLengthSquared() const
102    {
103        return m_width * m_width + m_height * m_height;
104    }
105
106    FloatSize transposedSize() const
107    {
108        return FloatSize(m_height, m_width);
109    }
110
111#if USE(CG)
112    explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy
113    operator CGSize() const;
114#endif
115
116#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
117    explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy
118    operator NSSize() const;
119#endif
120
121    void dump(WTF::PrintStream& out) const;
122
123private:
124    float m_width, m_height;
125};
126
127inline FloatSize& operator+=(FloatSize& a, const FloatSize& b)
128{
129    a.setWidth(a.width() + b.width());
130    a.setHeight(a.height() + b.height());
131    return a;
132}
133
134inline FloatSize& operator-=(FloatSize& a, const FloatSize& b)
135{
136    a.setWidth(a.width() - b.width());
137    a.setHeight(a.height() - b.height());
138    return a;
139}
140
141inline FloatSize operator+(const FloatSize& a, const FloatSize& b)
142{
143    return FloatSize(a.width() + b.width(), a.height() + b.height());
144}
145
146inline FloatSize operator-(const FloatSize& a, const FloatSize& b)
147{
148    return FloatSize(a.width() - b.width(), a.height() - b.height());
149}
150
151inline FloatSize operator-(const FloatSize& size)
152{
153    return FloatSize(-size.width(), -size.height());
154}
155
156inline FloatSize operator*(const FloatSize& a, const float b)
157{
158    return FloatSize(a.width() * b, a.height() * b);
159}
160
161inline FloatSize operator*(const float a, const FloatSize& b)
162{
163    return FloatSize(a * b.width(), a * b.height());
164}
165
166inline bool operator==(const FloatSize& a, const FloatSize& b)
167{
168    return a.width() == b.width() && a.height() == b.height();
169}
170
171inline bool operator!=(const FloatSize& a, const FloatSize& b)
172{
173    return a.width() != b.width() || a.height() != b.height();
174}
175
176inline IntSize roundedIntSize(const FloatSize& p)
177{
178    return IntSize(clampToInteger(roundf(p.width())), clampToInteger(roundf(p.height())));
179}
180
181inline IntSize flooredIntSize(const FloatSize& p)
182{
183    return IntSize(clampToInteger(floorf(p.width())), clampToInteger(floorf(p.height())));
184}
185
186inline IntSize expandedIntSize(const FloatSize& p)
187{
188    return IntSize(clampToInteger(ceilf(p.width())), clampToInteger(ceilf(p.height())));
189}
190
191inline IntPoint flooredIntPoint(const FloatSize& p)
192{
193    return IntPoint(clampToInteger(floorf(p.width())), clampToInteger(floorf(p.height())));
194}
195
196} // namespace WebCore
197
198#endif // FloatSize_h
199