Home | History | Annotate | Download | only in graphics
      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 "core/platform/graphics/FloatPoint.h"
     31 #include "wtf/Vector.h"
     32 
     33 #if OS(DARWIN)
     34 typedef struct CGRect CGRect;
     35 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
     36 typedef struct CGRect NSRect;
     37 #else
     38 typedef struct _NSRect NSRect;
     39 #endif
     40 #endif
     41 
     42 struct SkRect;
     43 
     44 namespace WebCore {
     45 
     46 class LayoutRect;
     47 class IntRect;
     48 class IntPoint;
     49 
     50 class FloatRect {
     51 public:
     52     enum ContainsMode {
     53         InsideOrOnStroke,
     54         InsideButNotOnStroke
     55     };
     56 
     57     FloatRect() { }
     58     FloatRect(const FloatPoint& location, const FloatSize& size)
     59         : m_location(location), m_size(size) { }
     60     FloatRect(float x, float y, float width, float height)
     61         : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { }
     62     FloatRect(const IntRect&);
     63     FloatRect(const LayoutRect&);
     64     FloatRect(const SkRect&);
     65 
     66     static FloatRect narrowPrecision(double x, double y, double width, double height);
     67 
     68     FloatPoint location() const { return m_location; }
     69     FloatSize size() const { return m_size; }
     70 
     71     void setLocation(const FloatPoint& location) { m_location = location; }
     72     void setSize(const FloatSize& size) { m_size = size; }
     73 
     74     float x() const { return m_location.x(); }
     75     float y() const { return m_location.y(); }
     76     float maxX() const { return x() + width(); }
     77     float maxY() const { return y() + height(); }
     78     float width() const { return m_size.width(); }
     79     float height() const { return m_size.height(); }
     80 
     81     void setX(float x) { m_location.setX(x); }
     82     void setY(float y) { m_location.setY(y); }
     83     void setWidth(float width) { m_size.setWidth(width); }
     84     void setHeight(float height) { m_size.setHeight(height); }
     85 
     86     bool isEmpty() const { return m_size.isEmpty(); }
     87     bool isZero() const { return m_size.isZero(); }
     88     bool isExpressibleAsIntRect() const;
     89 
     90     FloatPoint center() const { return FloatPoint(x() + width() / 2, y() + height() / 2); }
     91 
     92     void move(const FloatSize& delta) { m_location += delta; }
     93     void moveBy(const FloatPoint& delta) { m_location.move(delta.x(), delta.y()); }
     94     void move(float dx, float dy) { m_location.move(dx, dy); }
     95 
     96     void expand(const FloatSize& size) { m_size += size; }
     97     void expand(float dw, float dh) { m_size.expand(dw, dh); }
     98     void contract(const FloatSize& size) { m_size -= size; }
     99     void contract(float dw, float dh) { m_size.expand(-dw, -dh); }
    100 
    101     void shiftXEdgeTo(float edge)
    102     {
    103         float delta = edge - x();
    104         setX(edge);
    105         setWidth(std::max(0.0f, width() - delta));
    106     }
    107     void shiftMaxXEdgeTo(float edge)
    108     {
    109         float delta = edge - maxX();
    110         setWidth(std::max(0.0f, width() + delta));
    111     }
    112     void shiftYEdgeTo(float edge)
    113     {
    114         float delta = edge - y();
    115         setY(edge);
    116         setHeight(std::max(0.0f, height() - delta));
    117     }
    118     void shiftMaxYEdgeTo(float edge)
    119     {
    120         float delta = edge - maxY();
    121         setHeight(std::max(0.0f, height() + delta));
    122     }
    123 
    124     FloatPoint minXMinYCorner() const { return m_location; } // typically topLeft
    125     FloatPoint maxXMinYCorner() const { return FloatPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
    126     FloatPoint minXMaxYCorner() const { return FloatPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
    127     FloatPoint maxXMaxYCorner() const { return FloatPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
    128 
    129     bool intersects(const FloatRect&) const;
    130     bool contains(const FloatRect&) const;
    131     bool contains(const FloatPoint&, ContainsMode = InsideOrOnStroke) const;
    132 
    133     void intersect(const FloatRect&);
    134     void unite(const FloatRect&);
    135     void uniteEvenIfEmpty(const FloatRect&);
    136     void uniteIfNonZero(const FloatRect&);
    137     void extend(const FloatPoint&);
    138 
    139     // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version
    140     // is really checking for containment of 1x1 rect, but that doesn't make sense with floats.
    141     bool contains(float px, float py) const
    142         { return px >= x() && px <= maxX() && py >= y() && py <= maxY(); }
    143 
    144     void inflateX(float dx) {
    145         m_location.setX(m_location.x() - dx);
    146         m_size.setWidth(m_size.width() + dx + dx);
    147     }
    148     void inflateY(float dy) {
    149         m_location.setY(m_location.y() - dy);
    150         m_size.setHeight(m_size.height() + dy + dy);
    151     }
    152     void inflate(float d) { inflateX(d); inflateY(d); }
    153     void scale(float s) { scale(s, s); }
    154     void scale(float sx, float sy);
    155 
    156     FloatRect transposedRect() const { return FloatRect(m_location.transposedPoint(), m_size.transposedSize()); }
    157 
    158     // Re-initializes this rectangle to fit the sets of passed points.
    159     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1);
    160     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2);
    161     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3);
    162 
    163 #if OS(DARWIN)
    164     FloatRect(const CGRect&);
    165     operator CGRect() const;
    166 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
    167     FloatRect(const NSRect&);
    168     operator NSRect() const;
    169 #endif
    170 #endif
    171 
    172     operator SkRect() const;
    173 
    174 private:
    175     FloatPoint m_location;
    176     FloatSize m_size;
    177 
    178     void setLocationAndSizeFromEdges(float left, float top, float right, float bottom)
    179     {
    180         m_location.set(left, top);
    181         m_size.setWidth(right - left);
    182         m_size.setHeight(bottom - top);
    183     }
    184 };
    185 
    186 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
    187 {
    188     FloatRect c = a;
    189     c.intersect(b);
    190     return c;
    191 }
    192 
    193 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
    194 {
    195     FloatRect c = a;
    196     c.unite(b);
    197     return c;
    198 }
    199 
    200 FloatRect unionRect(const Vector<FloatRect>&);
    201 
    202 inline FloatRect& operator+=(FloatRect& a, const FloatRect& b)
    203 {
    204     a.move(b.x(), b.y());
    205     a.setWidth(a.width() + b.width());
    206     a.setHeight(a.height() + b.height());
    207     return a;
    208 }
    209 
    210 inline FloatRect operator+(const FloatRect& a, const FloatRect& b)
    211 {
    212     FloatRect c = a;
    213     c += b;
    214     return c;
    215 }
    216 
    217 inline bool operator==(const FloatRect& a, const FloatRect& b)
    218 {
    219     return a.location() == b.location() && a.size() == b.size();
    220 }
    221 
    222 inline bool operator!=(const FloatRect& a, const FloatRect& b)
    223 {
    224     return a.location() != b.location() || a.size() != b.size();
    225 }
    226 
    227 IntRect enclosingIntRect(const FloatRect&);
    228 
    229 // Returns a valid IntRect contained within the given FloatRect.
    230 IntRect enclosedIntRect(const FloatRect&);
    231 
    232 IntRect roundedIntRect(const FloatRect&);
    233 
    234 // Map rect r from srcRect to an equivalent rect in destRect.
    235 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect);
    236 
    237 }
    238 
    239 #endif
    240