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 #include "config.h"
     27 #include "IntRect.h"
     28 
     29 #include "FloatRect.h"
     30 #include <algorithm>
     31 
     32 using std::max;
     33 using std::min;
     34 
     35 namespace WebCore {
     36 
     37 IntRect::IntRect(const FloatRect& r)
     38     : m_location(IntPoint(static_cast<int>(r.x()), static_cast<int>(r.y())))
     39     , m_size(IntSize(static_cast<int>(r.width()), static_cast<int>(r.height())))
     40 {
     41 }
     42 
     43 bool IntRect::intersects(const IntRect& other) const
     44 {
     45     // Checking emptiness handles negative widths as well as zero.
     46     return !isEmpty() && !other.isEmpty()
     47         && x() < other.maxX() && other.x() < maxX()
     48         && y() < other.maxY() && other.y() < maxY();
     49 }
     50 
     51 bool IntRect::contains(const IntRect& other) const
     52 {
     53     return x() <= other.x() && maxX() >= other.maxX()
     54         && y() <= other.y() && maxY() >= other.maxY();
     55 }
     56 
     57 void IntRect::intersect(const IntRect& other)
     58 {
     59     int l = max(x(), other.x());
     60     int t = max(y(), other.y());
     61     int r = min(maxX(), other.maxX());
     62     int b = min(maxY(), other.maxY());
     63 
     64     // Return a clean empty rectangle for non-intersecting cases.
     65     if (l >= r || t >= b) {
     66         l = 0;
     67         t = 0;
     68         r = 0;
     69         b = 0;
     70     }
     71 
     72     m_location.setX(l);
     73     m_location.setY(t);
     74     m_size.setWidth(r - l);
     75     m_size.setHeight(b - t);
     76 }
     77 
     78 void IntRect::unite(const IntRect& other)
     79 {
     80     // Handle empty special cases first.
     81     if (other.isEmpty())
     82         return;
     83     if (isEmpty()) {
     84         *this = other;
     85         return;
     86     }
     87 
     88     int l = min(x(), other.x());
     89     int t = min(y(), other.y());
     90     int r = max(maxX(), other.maxX());
     91     int b = max(maxY(), other.maxY());
     92 
     93     m_location.setX(l);
     94     m_location.setY(t);
     95     m_size.setWidth(r - l);
     96     m_size.setHeight(b - t);
     97 }
     98 
     99 void IntRect::uniteIfNonZero(const IntRect& other)
    100 {
    101     // Handle empty special cases first.
    102     if (!other.width() && !other.height())
    103         return;
    104     if (!width() && !height()) {
    105         *this = other;
    106         return;
    107     }
    108 
    109     int left = min(x(), other.x());
    110     int top = min(y(), other.y());
    111     int right = max(maxX(), other.maxX());
    112     int bottom = max(maxY(), other.maxY());
    113 
    114     m_location.setX(left);
    115     m_location.setY(top);
    116     m_size.setWidth(right - left);
    117     m_size.setHeight(bottom - top);
    118 }
    119 
    120 void IntRect::scale(float s)
    121 {
    122     m_location.setX((int)(x() * s));
    123     m_location.setY((int)(y() * s));
    124     m_size.setWidth((int)(width() * s));
    125     m_size.setHeight((int)(height() * s));
    126 }
    127 
    128 IntRect unionRect(const Vector<IntRect>& rects)
    129 {
    130     IntRect result;
    131 
    132     size_t count = rects.size();
    133     for (size_t i = 0; i < count; ++i)
    134         result.unite(rects[i]);
    135 
    136     return result;
    137 }
    138 
    139 } // namespace WebCore
    140