1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2005 Nokia.  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 FloatSize_h
28 #define FloatSize_h
29 
30 #include <wtf/Platform.h>
31 
32 #if PLATFORM(CG)
33 typedef struct CGSize CGSize;
34 #endif
35 
36 #if PLATFORM(MAC)
37 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
38 typedef struct CGSize NSSize;
39 #else
40 typedef struct _NSSize NSSize;
41 #endif
42 #endif
43 
44 namespace WebCore
45 {
46 
47 class IntSize;
48 
49 class FloatSize
50 {
51 public:
FloatSize()52     FloatSize() : m_width(0), m_height(0) { }
FloatSize(float width,float height)53     FloatSize(float width, float height) : m_width(width), m_height(height) { }
54     FloatSize(const IntSize &);
55 
56     static FloatSize narrowPrecision(double width, double height);
57 
width()58     float width() const
59     {
60         return m_width;
61     }
height()62     float height() const
63     {
64         return m_height;
65     }
66 
setWidth(float width)67     void setWidth(float width)
68     {
69         m_width = width;
70     }
setHeight(float height)71     void setHeight(float height)
72     {
73         m_height = height;
74     }
75 
isEmpty()76     bool isEmpty() const
77     {
78         return m_width <= 0 || m_height <= 0;
79     }
80 
expandedTo(const FloatSize & other)81     FloatSize expandedTo(const FloatSize &other) const
82     {
83         return FloatSize(m_width > other.m_width ? m_width : other.m_width,
84                          m_height > other.m_height ? m_height : other.m_height);
85     }
86 
87 #if PLATFORM(CG)
88     explicit FloatSize(const CGSize &); // don't do this implicitly since it's lossy
89     operator CGSize() const;
90 #endif
91 
92 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
93     explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy
94     operator NSSize() const;
95 #endif
96 
97 private:
98     float m_width, m_height;
99 };
100 
101 inline FloatSize &operator+=(FloatSize &a, const FloatSize &b)
102 {
103     a.setWidth(a.width() + b.width());
104     a.setHeight(a.height() + b.height());
105     return a;
106 }
107 
108 inline FloatSize &operator-=(FloatSize &a, const FloatSize &b)
109 {
110     a.setWidth(a.width() - b.width());
111     a.setHeight(a.height() - b.height());
112     return a;
113 }
114 
115 inline FloatSize operator+(const FloatSize &a, const FloatSize &b)
116 {
117     return FloatSize(a.width() + b.width(), a.height() + b.height());
118 }
119 
120 inline FloatSize operator-(const FloatSize &a, const FloatSize &b)
121 {
122     return FloatSize(a.width() - b.width(), a.height() - b.height());
123 }
124 
125 inline FloatSize operator-(const FloatSize &size)
126 {
127     return FloatSize(-size.width(), -size.height());
128 }
129 
130 inline bool operator==(const FloatSize &a, const FloatSize &b)
131 {
132     return a.width() == b.width() && a.height() == b.height();
133 }
134 
135 inline bool operator!=(const FloatSize &a, const FloatSize &b)
136 {
137     return a.width() != b.width() || a.height() != b.height();
138 }
139 
140 } // namespace WebCore
141 
142 #endif // FloatSize_h
143