• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, 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 COMPUTER, 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 COMPUTER, 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 <wtf/Platform.h>
30 
31 #if PLATFORM(CG)
32 typedef struct CGSize CGSize;
33 #endif
34 
35 #if PLATFORM(MAC)
36 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
37 typedef struct CGSize NSSize;
38 #else
39 typedef struct _NSSize NSSize;
40 #endif
41 #endif
42 
43 #if PLATFORM(WIN)
44 typedef struct tagSIZE SIZE;
45 #elif PLATFORM(QT)
46 #include <qglobal.h>
47 QT_BEGIN_NAMESPACE
48 class QSize;
49 QT_END_NAMESPACE
50 #endif
51 
52 namespace WebCore {
53 
54 class IntSize {
55 public:
IntSize()56     IntSize() : m_width(0), m_height(0) { }
IntSize(int width,int height)57     IntSize(int width, int height) : m_width(width), m_height(height) { }
58 
width()59     int width() const { return m_width; }
height()60     int height() const { return m_height; }
61 
setWidth(int width)62     void setWidth(int width) { m_width = width; }
setHeight(int height)63     void setHeight(int height) { m_height = height; }
64 
isEmpty()65     bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
66 
expand(int width,int height)67     void expand(int width, int height)
68     {
69         m_width += width;
70         m_height += height;
71     }
72 
scale(float scale)73     void scale(float scale)
74     {
75         m_width = static_cast<int>(static_cast<float>(m_width) * scale);
76         m_height = static_cast<int>(static_cast<float>(m_height) * scale);
77     }
78 
expandedTo(const IntSize & other)79     IntSize expandedTo(const IntSize& other) const
80     {
81         return IntSize(m_width > other.m_width ? m_width : other.m_width,
82             m_height > other.m_height ? m_height : other.m_height);
83     }
84 
shrunkTo(const IntSize & other)85     IntSize shrunkTo(const IntSize& other) const
86     {
87         return IntSize(m_width < other.m_width ? m_width : other.m_width,
88             m_height < other.m_height ? m_height : other.m_height);
89     }
90 
clampNegativeToZero()91     void clampNegativeToZero()
92     {
93         *this = expandedTo(IntSize());
94     }
95 
96 #if PLATFORM(CG)
97     explicit IntSize(const CGSize&); // don't do this implicitly since it's lossy
98     operator CGSize() const;
99 #endif
100 
101 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
102     explicit IntSize(const NSSize &); // don't do this implicitly since it's lossy
103     operator NSSize() const;
104 #endif
105 
106 #if PLATFORM(WIN)
107     IntSize(const SIZE&);
108     operator SIZE() const;
109 #endif
110 
111 #if PLATFORM(QT)
112     IntSize(const QSize&);
113     operator QSize() const;
114 #endif
115 
116 
117 private:
118     int m_width, m_height;
119 };
120 
121 inline IntSize& operator+=(IntSize& a, const IntSize& b)
122 {
123     a.setWidth(a.width() + b.width());
124     a.setHeight(a.height() + b.height());
125     return a;
126 }
127 
128 inline IntSize& operator-=(IntSize& a, const IntSize& b)
129 {
130     a.setWidth(a.width() - b.width());
131     a.setHeight(a.height() - b.height());
132     return a;
133 }
134 
135 inline IntSize operator+(const IntSize& a, const IntSize& b)
136 {
137     return IntSize(a.width() + b.width(), a.height() + b.height());
138 }
139 
140 inline IntSize operator-(const IntSize& a, const IntSize& b)
141 {
142     return IntSize(a.width() - b.width(), a.height() - b.height());
143 }
144 
145 inline IntSize operator-(const IntSize& size)
146 {
147     return IntSize(-size.width(), -size.height());
148 }
149 
150 inline bool operator==(const IntSize& a, const IntSize& b)
151 {
152     return a.width() == b.width() && a.height() == b.height();
153 }
154 
155 inline bool operator!=(const IntSize& a, const IntSize& b)
156 {
157     return a.width() != b.width() || a.height() != b.height();
158 }
159 
160 } // namespace WebCore
161 
162 #endif // IntSize_h
163