Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2003, 2006, 2009 Apple 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 INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef IntRect_h
     27 #define IntRect_h
     28 
     29 #include "core/platform/graphics/IntPoint.h"
     30 #include "wtf/FastAllocBase.h"
     31 #include "wtf/Vector.h"
     32 
     33 #if OS(DARWIN)
     34 typedef struct CGRect CGRect;
     35 
     36 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
     37 typedef struct CGRect NSRect;
     38 #else
     39 typedef struct _NSRect NSRect;
     40 #endif
     41 #endif
     42 
     43 struct SkRect;
     44 struct SkIRect;
     45 
     46 namespace WebCore {
     47 
     48 class FloatRect;
     49 class LayoutRect;
     50 
     51 class IntRect {
     52     WTF_MAKE_FAST_ALLOCATED;
     53 public:
     54     IntRect() { }
     55     IntRect(const IntPoint& location, const IntSize& size)
     56         : m_location(location), m_size(size) { }
     57     IntRect(int x, int y, int width, int height)
     58         : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
     59 
     60     explicit IntRect(const FloatRect&); // don't do this implicitly since it's lossy
     61     explicit IntRect(const LayoutRect&); // don't do this implicitly since it's lossy
     62 
     63     IntPoint location() const { return m_location; }
     64     IntSize size() const { return m_size; }
     65 
     66     void setLocation(const IntPoint& location) { m_location = location; }
     67     void setSize(const IntSize& size) { m_size = size; }
     68 
     69     int x() const { return m_location.x(); }
     70     int y() const { return m_location.y(); }
     71     int maxX() const { return x() + width(); }
     72     int maxY() const { return y() + height(); }
     73     int width() const { return m_size.width(); }
     74     int height() const { return m_size.height(); }
     75 
     76     void setX(int x) { m_location.setX(x); }
     77     void setY(int y) { m_location.setY(y); }
     78     void setWidth(int width) { m_size.setWidth(width); }
     79     void setHeight(int height) { m_size.setHeight(height); }
     80 
     81     bool isEmpty() const { return m_size.isEmpty(); }
     82 
     83     // NOTE: The result is rounded to integer values, and thus may be not the exact
     84     // center point.
     85     IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
     86 
     87     void move(const IntSize& size) { m_location += size; }
     88     void moveBy(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
     89     void move(int dx, int dy) { m_location.move(dx, dy); }
     90 
     91     void expand(const IntSize& size) { m_size += size; }
     92     void expand(int dw, int dh) { m_size.expand(dw, dh); }
     93     void contract(const IntSize& size) { m_size -= size; }
     94     void contract(int dw, int dh) { m_size.expand(-dw, -dh); }
     95 
     96     void shiftXEdgeTo(int edge)
     97     {
     98         int delta = edge - x();
     99         setX(edge);
    100         setWidth(std::max(0, width() - delta));
    101     }
    102     void shiftMaxXEdgeTo(int edge)
    103     {
    104         int delta = edge - maxX();
    105         setWidth(std::max(0, width() + delta));
    106     }
    107     void shiftYEdgeTo(int edge)
    108     {
    109         int delta = edge - y();
    110         setY(edge);
    111         setHeight(std::max(0, height() - delta));
    112     }
    113     void shiftMaxYEdgeTo(int edge)
    114     {
    115         int delta = edge - maxY();
    116         setHeight(std::max(0, height() + delta));
    117     }
    118 
    119     IntPoint minXMinYCorner() const { return m_location; } // typically topLeft
    120     IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
    121     IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
    122     IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
    123 
    124     bool intersects(const IntRect&) const;
    125     bool contains(const IntRect&) const;
    126 
    127     // This checks to see if the rect contains x,y in the traditional sense.
    128     // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
    129     bool contains(int px, int py) const
    130         { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
    131     bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
    132 
    133     void intersect(const IntRect&);
    134     void unite(const IntRect&);
    135     void uniteIfNonZero(const IntRect&);
    136 
    137     void inflateX(int dx)
    138     {
    139         m_location.setX(m_location.x() - dx);
    140         m_size.setWidth(m_size.width() + dx + dx);
    141     }
    142     void inflateY(int dy)
    143     {
    144         m_location.setY(m_location.y() - dy);
    145         m_size.setHeight(m_size.height() + dy + dy);
    146     }
    147     void inflate(int d) { inflateX(d); inflateY(d); }
    148     void scale(float s);
    149 
    150     IntSize differenceToPoint(const IntPoint&) const;
    151     int distanceSquaredToPoint(const IntPoint& p) const { return differenceToPoint(p).diagonalLengthSquared(); }
    152 
    153     IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
    154 
    155 #if OS(DARWIN)
    156     operator CGRect() const;
    157 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
    158     operator NSRect() const;
    159 #endif
    160 #endif
    161 
    162     operator SkRect() const;
    163     operator SkIRect() const;
    164 
    165 private:
    166     IntPoint m_location;
    167     IntSize m_size;
    168 };
    169 
    170 inline IntRect intersection(const IntRect& a, const IntRect& b)
    171 {
    172     IntRect c = a;
    173     c.intersect(b);
    174     return c;
    175 }
    176 
    177 inline IntRect unionRect(const IntRect& a, const IntRect& b)
    178 {
    179     IntRect c = a;
    180     c.unite(b);
    181     return c;
    182 }
    183 
    184 IntRect unionRect(const Vector<IntRect>&);
    185 
    186 inline bool operator==(const IntRect& a, const IntRect& b)
    187 {
    188     return a.location() == b.location() && a.size() == b.size();
    189 }
    190 
    191 inline bool operator!=(const IntRect& a, const IntRect& b)
    192 {
    193     return a.location() != b.location() || a.size() != b.size();
    194 }
    195 
    196 #if OS(DARWIN)
    197 IntRect enclosingIntRect(const CGRect&);
    198 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
    199 IntRect enclosingIntRect(const NSRect&);
    200 #endif
    201 #endif
    202 
    203 } // namespace WebCore
    204 
    205 #endif // IntRect_h
    206