Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / geometry / IntSize.h
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 #include "public/platform/WebCommon.h"
32
33 #if OS(MACOSX)
34 typedef struct CGSize CGSize;
35
36 #ifdef __OBJC__
37 #import <Foundation/Foundation.h>
38 #endif
39 #endif
40
41 namespace blink {
42
43 class PLATFORM_EXPORT IntSize {
44 public:
45     IntSize() : m_width(0), m_height(0) { }
46     IntSize(int width, int height) : m_width(width), m_height(height) { }
47
48     int width() const { return m_width; }
49     int height() const { return m_height; }
50
51     void setWidth(int width) { m_width = width; }
52     void setHeight(int height) { m_height = height; }
53
54     bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
55     bool isZero() const { return !m_width && !m_height; }
56
57     float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
58
59     void expand(int width, int height)
60     {
61         m_width += width;
62         m_height += height;
63     }
64
65     void scale(float widthScale, float heightScale)
66     {
67         m_width = static_cast<int>(static_cast<float>(m_width) * widthScale);
68         m_height = static_cast<int>(static_cast<float>(m_height) * heightScale);
69     }
70
71     void scale(float scale)
72     {
73         this->scale(scale, scale);
74     }
75
76     IntSize expandedTo(const IntSize& other) const
77     {
78         return IntSize(m_width > other.m_width ? m_width : other.m_width,
79             m_height > other.m_height ? m_height : other.m_height);
80     }
81
82     IntSize shrunkTo(const IntSize& other) const
83     {
84         return IntSize(m_width < other.m_width ? m_width : other.m_width,
85             m_height < other.m_height ? m_height : other.m_height);
86     }
87
88     void clampNegativeToZero()
89     {
90         *this = expandedTo(IntSize());
91     }
92
93     void clampToMinimumSize(const IntSize& minimumSize)
94     {
95         if (m_width < minimumSize.width())
96             m_width = minimumSize.width();
97         if (m_height < minimumSize.height())
98             m_height = minimumSize.height();
99     }
100
101     // Return area in a uint64_t to avoid overflow.
102     uint64_t area() const
103     {
104         return static_cast<uint64_t>(width()) * 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(__OBJC__) && !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
127 private:
128     int m_width, m_height;
129 };
130
131 inline 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
138 inline 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
145 inline IntSize operator+(const IntSize& a, const IntSize& b)
146 {
147     return IntSize(a.width() + b.width(), a.height() + b.height());
148 }
149
150 inline IntSize operator-(const IntSize& a, const IntSize& b)
151 {
152     return IntSize(a.width() - b.width(), a.height() - b.height());
153 }
154
155 inline IntSize operator-(const IntSize& size)
156 {
157     return IntSize(-size.width(), -size.height());
158 }
159
160 inline bool operator==(const IntSize& a, const IntSize& b)
161 {
162     return a.width() == b.width() && a.height() == b.height();
163 }
164
165 inline bool operator!=(const IntSize& a, const IntSize& b)
166 {
167     return a.width() != b.width() || a.height() != b.height();
168 }
169
170 } // namespace blink
171
172 #endif // IntSize_h