1/* 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> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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. 26 */ 27 28#ifndef FloatSize_h 29#define FloatSize_h 30 31#include "IntPoint.h" 32#include <wtf/MathExtras.h> 33 34#if PLATFORM(QT) 35QT_BEGIN_NAMESPACE 36class QSizeF; 37QT_END_NAMESPACE 38#endif 39 40#if PLATFORM(BLACKBERRY) 41namespace BlackBerry { 42namespace Platform { 43class FloatSize; 44} 45} 46#endif 47 48#if USE(CG) 49typedef struct CGSize CGSize; 50#endif 51 52#if PLATFORM(MAC) 53#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 54typedef struct CGSize NSSize; 55#else 56typedef struct _NSSize NSSize; 57#endif 58#endif 59 60namespace WebCore { 61 62class IntSize; 63class LayoutSize; 64 65class FloatSize { 66public: 67 FloatSize() : m_width(0), m_height(0) { } 68 FloatSize(float width, float height) : m_width(width), m_height(height) { } 69 FloatSize(const IntSize&); 70 FloatSize(const LayoutSize&); 71 72 static FloatSize narrowPrecision(double width, double height); 73 74 float width() const { return m_width; } 75 float height() const { return m_height; } 76 77 void setWidth(float width) { m_width = width; } 78 void setHeight(float height) { m_height = height; } 79 80 bool isEmpty() const { return m_width <= 0 || m_height <= 0; } 81 bool isZero() const; 82 bool isExpressibleAsIntSize() const; 83 84 float aspectRatio() const { return m_width / m_height; } 85 86 void expand(float width, float height) 87 { 88 m_width += width; 89 m_height += height; 90 } 91 92 void scale(float s) { scale(s, s); } 93 94 void scale(float scaleX, float scaleY) 95 { 96 m_width *= scaleX; 97 m_height *= scaleY; 98 } 99 100 FloatSize expandedTo(const FloatSize& other) const 101 { 102 return FloatSize(m_width > other.m_width ? m_width : other.m_width, 103 m_height > other.m_height ? m_height : other.m_height); 104 } 105 106 FloatSize shrunkTo(const FloatSize& other) const 107 { 108 return FloatSize(m_width < other.m_width ? m_width : other.m_width, 109 m_height < other.m_height ? m_height : other.m_height); 110 } 111 112 float diagonalLength() const; 113 float diagonalLengthSquared() const 114 { 115 return m_width * m_width + m_height * m_height; 116 } 117 118 FloatSize transposedSize() const 119 { 120 return FloatSize(m_height, m_width); 121 } 122 123#if PLATFORM(QT) 124 explicit FloatSize(const QSizeF&); 125 operator QSizeF() const; 126#endif 127 128#if PLATFORM(BLACKBERRY) 129 FloatSize(const BlackBerry::Platform::FloatSize&); 130 operator BlackBerry::Platform::FloatSize() const; 131#endif 132 133#if USE(CG) 134 explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy 135 operator CGSize() const; 136#endif 137 138#if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) 139 explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy 140 operator NSSize() const; 141#endif 142 143private: 144 float m_width, m_height; 145}; 146 147inline FloatSize& operator+=(FloatSize& a, const FloatSize& b) 148{ 149 a.setWidth(a.width() + b.width()); 150 a.setHeight(a.height() + b.height()); 151 return a; 152} 153 154inline FloatSize& operator-=(FloatSize& a, const FloatSize& b) 155{ 156 a.setWidth(a.width() - b.width()); 157 a.setHeight(a.height() - b.height()); 158 return a; 159} 160 161inline FloatSize operator+(const FloatSize& a, const FloatSize& b) 162{ 163 return FloatSize(a.width() + b.width(), a.height() + b.height()); 164} 165 166inline FloatSize operator-(const FloatSize& a, const FloatSize& b) 167{ 168 return FloatSize(a.width() - b.width(), a.height() - b.height()); 169} 170 171inline FloatSize operator-(const FloatSize& size) 172{ 173 return FloatSize(-size.width(), -size.height()); 174} 175 176inline FloatSize operator*(const FloatSize& a, const float b) 177{ 178 return FloatSize(a.width() * b, a.height() * b); 179} 180 181inline FloatSize operator*(const float a, const FloatSize& b) 182{ 183 return FloatSize(a * b.width(), a * b.height()); 184} 185 186inline bool operator==(const FloatSize& a, const FloatSize& b) 187{ 188 return a.width() == b.width() && a.height() == b.height(); 189} 190 191inline bool operator!=(const FloatSize& a, const FloatSize& b) 192{ 193 return a.width() != b.width() || a.height() != b.height(); 194} 195 196inline IntSize roundedIntSize(const FloatSize& p) 197{ 198 return IntSize(clampToInteger(roundf(p.width())), clampToInteger(roundf(p.height()))); 199} 200 201inline IntSize flooredIntSize(const FloatSize& p) 202{ 203 return IntSize(clampToInteger(floorf(p.width())), clampToInteger(floorf(p.height()))); 204} 205 206inline IntSize expandedIntSize(const FloatSize& p) 207{ 208 return IntSize(clampToInteger(ceilf(p.width())), clampToInteger(ceilf(p.height()))); 209} 210 211inline IntPoint flooredIntPoint(const FloatSize& p) 212{ 213 return IntPoint(clampToInteger(floorf(p.width())), clampToInteger(floorf(p.height()))); 214} 215 216} // namespace WebCore 217 218#endif // FloatSize_h 219