1 /* 2 * Copyright (C) 2003, 2006, 2007 Apple 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 FloatRect_h 28 #define FloatRect_h 29 30 #include "FloatPoint.h" 31 32 #if PLATFORM(CG) 33 typedef struct CGRect CGRect; 34 #endif 35 36 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN)) 37 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 38 typedef struct CGRect NSRect; 39 #else 40 typedef struct _NSRect NSRect; 41 #endif 42 #endif 43 44 #if PLATFORM(QT) 45 QT_BEGIN_NAMESPACE 46 class QRectF; 47 QT_END_NAMESPACE 48 #endif 49 50 #if PLATFORM(WX) && USE(WXGC) 51 class wxRect2DDouble; 52 #endif 53 54 #if PLATFORM(HAIKU) 55 class BRect; 56 #endif 57 58 #if PLATFORM(SKIA) 59 struct SkRect; 60 #endif 61 62 namespace WebCore { 63 64 #if PLATFORM(OPENVG) 65 class VGRect; 66 #endif 67 68 class IntRect; 69 70 class FloatRect { 71 public: 72 FloatRect() { } 73 FloatRect(const FloatPoint& location, const FloatSize& size) 74 : m_location(location), m_size(size) { } 75 FloatRect(float x, float y, float width, float height) 76 : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { } 77 FloatRect(const IntRect&); 78 79 static FloatRect narrowPrecision(double x, double y, double width, double height); 80 81 FloatPoint location() const { return m_location; } 82 FloatSize size() const { return m_size; } 83 84 void setLocation(const FloatPoint& location) { m_location = location; } 85 void setSize(const FloatSize& size) { m_size = size; } 86 87 float x() const { return m_location.x(); } 88 float y() const { return m_location.y(); } 89 float width() const { return m_size.width(); } 90 float height() const { return m_size.height(); } 91 92 void setX(float x) { m_location.setX(x); } 93 void setY(float y) { m_location.setY(y); } 94 void setWidth(float width) { m_size.setWidth(width); } 95 void setHeight(float height) { m_size.setHeight(height); } 96 97 bool isEmpty() const { return m_size.isEmpty(); } 98 99 float right() const { return x() + width(); } 100 float bottom() const { return y() + height(); } 101 102 void move(const FloatSize& delta) { m_location += delta; } 103 void move(float dx, float dy) { m_location.move(dx, dy); } 104 105 bool intersects(const FloatRect&) const; 106 bool contains(const FloatRect&) const; 107 108 void intersect(const FloatRect&); 109 void unite(const FloatRect&); 110 111 // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version 112 // is really checking for containment of 1x1 rect, but that doesn't make sense with floats. 113 bool contains(float px, float py) const 114 { return px >= x() && px <= right() && py >= y() && py <= bottom(); } 115 bool contains(const FloatPoint& point) const { return contains(point.x(), point.y()); } 116 117 118 void inflateX(float dx) { 119 m_location.setX(m_location.x() - dx); 120 m_size.setWidth(m_size.width() + dx + dx); 121 } 122 void inflateY(float dy) { 123 m_location.setY(m_location.y() - dy); 124 m_size.setHeight(m_size.height() + dy + dy); 125 } 126 void inflate(float d) { inflateX(d); inflateY(d); } 127 void scale(float s) { scale(s, s); } 128 void scale(float sx, float sy); 129 130 #if PLATFORM(CG) 131 FloatRect(const CGRect&); 132 operator CGRect() const; 133 #endif 134 135 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \ 136 || (PLATFORM(CHROMIUM) && OS(DARWIN)) 137 FloatRect(const NSRect&); 138 operator NSRect() const; 139 #endif 140 141 #if PLATFORM(QT) 142 FloatRect(const QRectF&); 143 operator QRectF() const; 144 #endif 145 146 #if PLATFORM(WX) && USE(WXGC) 147 FloatRect(const wxRect2DDouble&); 148 operator wxRect2DDouble() const; 149 #endif 150 151 #if PLATFORM(HAIKU) 152 FloatRect(const BRect&); 153 operator BRect() const; 154 #endif 155 156 #if PLATFORM(SKIA) 157 FloatRect(const SkRect&); 158 operator SkRect() const; 159 #endif 160 161 #if PLATFORM(OPENVG) 162 operator VGRect() const; 163 #endif 164 165 private: 166 FloatPoint m_location; 167 FloatSize m_size; 168 }; 169 170 inline FloatRect intersection(const FloatRect& a, const FloatRect& b) 171 { 172 FloatRect c = a; 173 c.intersect(b); 174 return c; 175 } 176 177 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b) 178 { 179 FloatRect c = a; 180 c.unite(b); 181 return c; 182 } 183 184 185 inline bool operator==(const FloatRect& a, const FloatRect& b) 186 { 187 return a.location() == b.location() && a.size() == b.size(); 188 } 189 190 inline bool operator!=(const FloatRect& a, const FloatRect& b) 191 { 192 return a.location() != b.location() || a.size() != b.size(); 193 } 194 195 IntRect enclosingIntRect(const FloatRect&); 196 197 // Map rect r from srcRect to an equivalent rect in destRect. 198 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect); 199 200 } 201 202 #endif 203