2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2005 Nokia. All rights reserved.
4 * 2008 Eric Seidel <eric@webkit.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "platform/geometry/IntPoint.h"
32 #include "wtf/MathExtras.h"
35 typedef struct CGSize CGSize;
38 #import <Foundation/Foundation.h>
47 class PLATFORM_EXPORT FloatSize {
49 FloatSize() : m_width(0), m_height(0) { }
50 FloatSize(float width, float height) : m_width(width), m_height(height) { }
51 FloatSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
52 FloatSize(const LayoutSize&);
54 static FloatSize narrowPrecision(double width, double height);
56 float width() const { return m_width; }
57 float height() const { return m_height; }
59 void setWidth(float width) { m_width = width; }
60 void setHeight(float height) { m_height = height; }
62 bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
64 bool isExpressibleAsIntSize() const;
66 float aspectRatio() const { return m_width / m_height; }
68 void expand(float width, float height)
74 void scale(float s) { scale(s, s); }
76 void scale(float scaleX, float scaleY)
82 FloatSize expandedTo(const FloatSize& other) const
84 return FloatSize(m_width > other.m_width ? m_width : other.m_width,
85 m_height > other.m_height ? m_height : other.m_height);
88 FloatSize shrunkTo(const FloatSize& other) const
90 return FloatSize(m_width < other.m_width ? m_width : other.m_width,
91 m_height < other.m_height ? m_height : other.m_height);
94 float diagonalLength() const;
95 float diagonalLengthSquared() const
97 return m_width * m_width + m_height * m_height;
100 FloatSize transposedSize() const
102 return FloatSize(m_height, m_width);
105 FloatSize scaledBy(float scale) const
107 return scaledBy(scale, scale);
110 FloatSize scaledBy(float scaleX, float scaleY) const
112 return FloatSize(m_width * scaleX, m_height * scaleY);
116 explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy
117 operator CGSize() const;
118 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
119 explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy
120 operator NSSize() const;
125 float m_width, m_height;
128 inline FloatSize& operator+=(FloatSize& a, const FloatSize& b)
130 a.setWidth(a.width() + b.width());
131 a.setHeight(a.height() + b.height());
135 inline FloatSize& operator-=(FloatSize& a, const FloatSize& b)
137 a.setWidth(a.width() - b.width());
138 a.setHeight(a.height() - b.height());
142 inline FloatSize operator+(const FloatSize& a, const FloatSize& b)
144 return FloatSize(a.width() + b.width(), a.height() + b.height());
147 inline FloatSize operator-(const FloatSize& a, const FloatSize& b)
149 return FloatSize(a.width() - b.width(), a.height() - b.height());
152 inline FloatSize operator-(const FloatSize& size)
154 return FloatSize(-size.width(), -size.height());
157 inline FloatSize operator*(const FloatSize& a, const float b)
159 return FloatSize(a.width() * b, a.height() * b);
162 inline FloatSize operator*(const float a, const FloatSize& b)
164 return FloatSize(a * b.width(), a * b.height());
167 inline bool operator==(const FloatSize& a, const FloatSize& b)
169 return a.width() == b.width() && a.height() == b.height();
172 inline bool operator!=(const FloatSize& a, const FloatSize& b)
174 return a.width() != b.width() || a.height() != b.height();
177 inline IntSize roundedIntSize(const FloatSize& p)
179 return IntSize(clampTo<int>(roundf(p.width())), clampTo<int>(roundf(p.height())));
182 inline IntSize flooredIntSize(const FloatSize& p)
184 return IntSize(clampTo<int>(floorf(p.width())), clampTo<int>(floorf(p.height())));
187 inline IntSize expandedIntSize(const FloatSize& p)
189 return IntSize(clampTo<int>(ceilf(p.width())), clampTo<int>(ceilf(p.height())));
192 inline IntPoint flooredIntPoint(const FloatSize& p)
194 return IntPoint(clampTo<int>(floorf(p.width())), clampTo<int>(floorf(p.height())));
199 #endif // FloatSize_h