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 (at) 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 "platform/geometry/IntPoint.h" 32 #include "wtf/MathExtras.h" 33 34 35 #if OS(MACOSX) 36 typedef struct CGSize CGSize; 37 38 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 39 typedef struct CGSize NSSize; 40 #else 41 typedef struct _NSSize NSSize; 42 #endif 43 #endif 44 45 namespace WebCore { 46 47 class IntSize; 48 class LayoutSize; 49 50 class PLATFORM_EXPORT FloatSize { 51 public: 52 FloatSize() : m_width(0), m_height(0) { } 53 FloatSize(float width, float height) : m_width(width), m_height(height) { } 54 FloatSize(const IntSize&); 55 FloatSize(const LayoutSize&); 56 57 static FloatSize narrowPrecision(double width, double height); 58 59 float width() const { return m_width; } 60 float height() const { return m_height; } 61 62 void setWidth(float width) { m_width = width; } 63 void setHeight(float height) { m_height = height; } 64 65 bool isEmpty() const { return m_width <= 0 || m_height <= 0; } 66 bool isZero() const; 67 bool isExpressibleAsIntSize() const; 68 69 float aspectRatio() const { return m_width / m_height; } 70 71 void expand(float width, float height) 72 { 73 m_width += width; 74 m_height += height; 75 } 76 77 void scale(float s) { scale(s, s); } 78 79 void scale(float scaleX, float scaleY) 80 { 81 m_width *= scaleX; 82 m_height *= scaleY; 83 } 84 85 FloatSize expandedTo(const FloatSize& other) const 86 { 87 return FloatSize(m_width > other.m_width ? m_width : other.m_width, 88 m_height > other.m_height ? m_height : other.m_height); 89 } 90 91 FloatSize shrunkTo(const FloatSize& other) const 92 { 93 return FloatSize(m_width < other.m_width ? m_width : other.m_width, 94 m_height < other.m_height ? m_height : other.m_height); 95 } 96 97 float diagonalLength() const; 98 float diagonalLengthSquared() const 99 { 100 return m_width * m_width + m_height * m_height; 101 } 102 103 FloatSize transposedSize() const 104 { 105 return FloatSize(m_height, m_width); 106 } 107 108 #if OS(MACOSX) 109 explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy 110 operator CGSize() const; 111 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 112 explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy 113 operator NSSize() const; 114 #endif 115 #endif 116 117 private: 118 float m_width, m_height; 119 }; 120 121 inline FloatSize& operator+=(FloatSize& a, const FloatSize& b) 122 { 123 a.setWidth(a.width() + b.width()); 124 a.setHeight(a.height() + b.height()); 125 return a; 126 } 127 128 inline FloatSize& operator-=(FloatSize& a, const FloatSize& b) 129 { 130 a.setWidth(a.width() - b.width()); 131 a.setHeight(a.height() - b.height()); 132 return a; 133 } 134 135 inline FloatSize operator+(const FloatSize& a, const FloatSize& b) 136 { 137 return FloatSize(a.width() + b.width(), a.height() + b.height()); 138 } 139 140 inline FloatSize operator-(const FloatSize& a, const FloatSize& b) 141 { 142 return FloatSize(a.width() - b.width(), a.height() - b.height()); 143 } 144 145 inline FloatSize operator-(const FloatSize& size) 146 { 147 return FloatSize(-size.width(), -size.height()); 148 } 149 150 inline FloatSize operator*(const FloatSize& a, const float b) 151 { 152 return FloatSize(a.width() * b, a.height() * b); 153 } 154 155 inline FloatSize operator*(const float a, const FloatSize& b) 156 { 157 return FloatSize(a * b.width(), a * b.height()); 158 } 159 160 inline bool operator==(const FloatSize& a, const FloatSize& b) 161 { 162 return a.width() == b.width() && a.height() == b.height(); 163 } 164 165 inline bool operator!=(const FloatSize& a, const FloatSize& b) 166 { 167 return a.width() != b.width() || a.height() != b.height(); 168 } 169 170 inline IntSize roundedIntSize(const FloatSize& p) 171 { 172 return IntSize(clampToInteger(roundf(p.width())), clampToInteger(roundf(p.height()))); 173 } 174 175 inline IntSize flooredIntSize(const FloatSize& p) 176 { 177 return IntSize(clampToInteger(floorf(p.width())), clampToInteger(floorf(p.height()))); 178 } 179 180 inline IntSize expandedIntSize(const FloatSize& p) 181 { 182 return IntSize(clampToInteger(ceilf(p.width())), clampToInteger(ceilf(p.height()))); 183 } 184 185 inline IntPoint flooredIntPoint(const FloatSize& p) 186 { 187 return IntPoint(clampToInteger(floorf(p.width())), clampToInteger(floorf(p.height()))); 188 } 189 190 } // namespace WebCore 191 192 #endif // FloatSize_h 193