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