1/*
2 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
3 * Copyright (C) 2013 Google Inc. 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 IntSize_h
28#define IntSize_h
29
30#include "platform/PlatformExport.h"
31
32#if OS(MACOSX)
33typedef struct CGSize CGSize;
34
35#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
36typedef struct CGSize NSSize;
37#else
38typedef struct _NSSize NSSize;
39#endif
40#endif
41
42namespace WebCore {
43
44class PLATFORM_EXPORT IntSize {
45public:
46    IntSize() : m_width(0), m_height(0) { }
47    IntSize(int width, int height) : m_width(width), m_height(height) { }
48
49    int width() const { return m_width; }
50    int height() const { return m_height; }
51
52    void setWidth(int width) { m_width = width; }
53    void setHeight(int height) { m_height = height; }
54
55    bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
56    bool isZero() const { return !m_width && !m_height; }
57
58    float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
59
60    void expand(int width, int height)
61    {
62        m_width += width;
63        m_height += height;
64    }
65
66    void scale(float widthScale, float heightScale)
67    {
68        m_width = static_cast<int>(static_cast<float>(m_width) * widthScale);
69        m_height = static_cast<int>(static_cast<float>(m_height) * heightScale);
70    }
71
72    void scale(float scale)
73    {
74        this->scale(scale, scale);
75    }
76
77    IntSize expandedTo(const IntSize& other) const
78    {
79        return IntSize(m_width > other.m_width ? m_width : other.m_width,
80            m_height > other.m_height ? m_height : other.m_height);
81    }
82
83    IntSize shrunkTo(const IntSize& other) const
84    {
85        return IntSize(m_width < other.m_width ? m_width : other.m_width,
86            m_height < other.m_height ? m_height : other.m_height);
87    }
88
89    void clampNegativeToZero()
90    {
91        *this = expandedTo(IntSize());
92    }
93
94    void clampToMinimumSize(const IntSize& minimumSize)
95    {
96        if (m_width < minimumSize.width())
97            m_width = minimumSize.width();
98        if (m_height < minimumSize.height())
99            m_height = minimumSize.height();
100    }
101
102    int area() const
103    {
104        return m_width * m_height;
105    }
106
107    int diagonalLengthSquared() const
108    {
109        return m_width * m_width + m_height * m_height;
110    }
111
112    IntSize transposedSize() const
113    {
114        return IntSize(m_height, m_width);
115    }
116
117#if OS(MACOSX)
118    explicit IntSize(const CGSize&); // don't do this implicitly since it's lossy
119    operator CGSize() const;
120
121#if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
122    explicit IntSize(const NSSize &); // don't do this implicitly since it's lossy
123    operator NSSize() const;
124#endif
125#endif
126
127private:
128    int m_width, m_height;
129};
130
131inline IntSize& operator+=(IntSize& a, const IntSize& b)
132{
133    a.setWidth(a.width() + b.width());
134    a.setHeight(a.height() + b.height());
135    return a;
136}
137
138inline IntSize& operator-=(IntSize& a, const IntSize& b)
139{
140    a.setWidth(a.width() - b.width());
141    a.setHeight(a.height() - b.height());
142    return a;
143}
144
145inline IntSize operator+(const IntSize& a, const IntSize& b)
146{
147    return IntSize(a.width() + b.width(), a.height() + b.height());
148}
149
150inline IntSize operator-(const IntSize& a, const IntSize& b)
151{
152    return IntSize(a.width() - b.width(), a.height() - b.height());
153}
154
155inline IntSize operator-(const IntSize& size)
156{
157    return IntSize(-size.width(), -size.height());
158}
159
160inline bool operator==(const IntSize& a, const IntSize& b)
161{
162    return a.width() == b.width() && a.height() == b.height();
163}
164
165inline bool operator!=(const IntSize& a, const IntSize& b)
166{
167    return a.width() != b.width() || a.height() != b.height();
168}
169
170} // namespace WebCore
171
172#endif // IntSize_h
173