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#if USE(CG) 30typedef struct CGSize CGSize; 31#endif 32 33#if PLATFORM(MAC) 34#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 35typedef struct CGSize NSSize; 36#else 37typedef struct _NSSize NSSize; 38#endif 39#endif 40 41#if PLATFORM(WIN) 42typedef struct tagSIZE SIZE; 43#elif PLATFORM(QT) 44#include <qglobal.h> 45QT_BEGIN_NAMESPACE 46class QSize; 47QT_END_NAMESPACE 48#elif PLATFORM(BLACKBERRY) 49namespace BlackBerry { 50namespace Platform { 51class IntSize; 52} 53} 54#endif 55 56namespace WebCore { 57 58class IntSize { 59public: 60 IntSize() : m_width(0), m_height(0) { } 61 IntSize(int width, int height) : m_width(width), m_height(height) { } 62 63 int width() const { return m_width; } 64 int height() const { return m_height; } 65 66 void setWidth(int width) { m_width = width; } 67 void setHeight(int height) { m_height = height; } 68 69 bool isEmpty() const { return m_width <= 0 || m_height <= 0; } 70 bool isZero() const { return !m_width && !m_height; } 71 72 float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); } 73 74 void expand(int width, int height) 75 { 76 m_width += width; 77 m_height += height; 78 } 79 80 void scale(float widthScale, float heightScale) 81 { 82 m_width = static_cast<int>(static_cast<float>(m_width) * widthScale); 83 m_height = static_cast<int>(static_cast<float>(m_height) * heightScale); 84 } 85 86 void scale(float scale) 87 { 88 this->scale(scale, scale); 89 } 90 91 IntSize expandedTo(const IntSize& other) const 92 { 93 return IntSize(m_width > other.m_width ? m_width : other.m_width, 94 m_height > other.m_height ? m_height : other.m_height); 95 } 96 97 IntSize shrunkTo(const IntSize& other) const 98 { 99 return IntSize(m_width < other.m_width ? m_width : other.m_width, 100 m_height < other.m_height ? m_height : other.m_height); 101 } 102 103 void clampNegativeToZero() 104 { 105 *this = expandedTo(IntSize()); 106 } 107 108 void clampToMinimumSize(const IntSize& minimumSize) 109 { 110 if (m_width < minimumSize.width()) 111 m_width = minimumSize.width(); 112 if (m_height < minimumSize.height()) 113 m_height = minimumSize.height(); 114 } 115 116 int area() const 117 { 118 return m_width * m_height; 119 } 120 121 int diagonalLengthSquared() const 122 { 123 return m_width * m_width + m_height * m_height; 124 } 125 126 IntSize transposedSize() const 127 { 128 return IntSize(m_height, m_width); 129 } 130 131#if USE(CG) 132 explicit IntSize(const CGSize&); // don't do this implicitly since it's lossy 133 operator CGSize() const; 134#endif 135 136#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 137 explicit IntSize(const NSSize &); // don't do this implicitly since it's lossy 138 operator NSSize() const; 139#endif 140 141#if PLATFORM(WIN) 142 IntSize(const SIZE&); 143 operator SIZE() const; 144#endif 145 146#if PLATFORM(QT) 147 IntSize(const QSize&); 148 operator QSize() const; 149#endif 150 151#if PLATFORM(BLACKBERRY) 152 IntSize(const BlackBerry::Platform::IntSize&); 153 operator BlackBerry::Platform::IntSize() const; 154#endif 155 156private: 157 int m_width, m_height; 158}; 159 160inline IntSize& operator+=(IntSize& a, const IntSize& b) 161{ 162 a.setWidth(a.width() + b.width()); 163 a.setHeight(a.height() + b.height()); 164 return a; 165} 166 167inline IntSize& operator-=(IntSize& a, const IntSize& b) 168{ 169 a.setWidth(a.width() - b.width()); 170 a.setHeight(a.height() - b.height()); 171 return a; 172} 173 174inline IntSize operator+(const IntSize& a, const IntSize& b) 175{ 176 return IntSize(a.width() + b.width(), a.height() + b.height()); 177} 178 179inline IntSize operator-(const IntSize& a, const IntSize& b) 180{ 181 return IntSize(a.width() - b.width(), a.height() - b.height()); 182} 183 184inline IntSize operator-(const IntSize& size) 185{ 186 return IntSize(-size.width(), -size.height()); 187} 188 189inline bool operator==(const IntSize& a, const IntSize& b) 190{ 191 return a.width() == b.width() && a.height() == b.height(); 192} 193 194inline bool operator!=(const IntSize& a, const IntSize& b) 195{ 196 return a.width() != b.width() || a.height() != b.height(); 197} 198 199} // namespace WebCore 200 201#endif // IntSize_h 202