Home | History | Annotate | Download | only in geometry
      1 /*
      2  * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2010 Google Inc. 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  *
      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 AND ITS CONTRIBUTORS "AS IS" AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #ifndef RoundedRect_h
     28 #define RoundedRect_h
     29 
     30 #include "platform/geometry/FloatQuad.h"
     31 #include "platform/geometry/IntRect.h"
     32 
     33 namespace WebCore {
     34 
     35 // This class is used to represent rectangles with rounded corners. It is only
     36 // used for painting. It uses integer units because using layout units leads to
     37 // blurry rounded corners.
     38 class PLATFORM_EXPORT RoundedRect {
     39 public:
     40     class PLATFORM_EXPORT Radii {
     41     public:
     42         Radii() { }
     43         Radii(const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight)
     44             : m_topLeft(topLeft)
     45             , m_topRight(topRight)
     46             , m_bottomLeft(bottomLeft)
     47             , m_bottomRight(bottomRight)
     48         {
     49         }
     50 
     51         void setTopLeft(const IntSize& size) { m_topLeft = size; }
     52         void setTopRight(const IntSize& size) { m_topRight = size; }
     53         void setBottomLeft(const IntSize& size) { m_bottomLeft = size; }
     54         void setBottomRight(const IntSize& size) { m_bottomRight = size; }
     55         const IntSize& topLeft() const { return m_topLeft; }
     56         const IntSize& topRight() const { return m_topRight; }
     57         const IntSize& bottomLeft() const { return m_bottomLeft; }
     58         const IntSize& bottomRight() const { return m_bottomRight; }
     59 
     60         bool isZero() const;
     61 
     62         void includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
     63         void excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge);
     64 
     65         void scale(float factor);
     66         void expand(int topWidth, int bottomWidth, int leftWidth, int rightWidth);
     67         void expand(int size) { expand(size, size, size, size); }
     68         void shrink(int topWidth, int bottomWidth, int leftWidth, int rightWidth) { expand(-topWidth, -bottomWidth, -leftWidth, -rightWidth); }
     69         void shrink(int size) { shrink(size, size, size, size); }
     70 
     71     private:
     72         IntSize m_topLeft;
     73         IntSize m_topRight;
     74         IntSize m_bottomLeft;
     75         IntSize m_bottomRight;
     76     };
     77 
     78     explicit RoundedRect(const IntRect&, const Radii& = Radii());
     79     RoundedRect(int x, int y, int width, int height);
     80     RoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
     81 
     82     const IntRect& rect() const { return m_rect; }
     83     const Radii& radii() const { return m_radii; }
     84     bool isRounded() const { return !m_radii.isZero(); }
     85     bool isEmpty() const { return m_rect.isEmpty(); }
     86 
     87     // Returns a quickly computed rect enclosed by the rounded rect.
     88     IntRect radiusCenterRect() const;
     89 
     90     void setRect(const IntRect& rect) { m_rect = rect; }
     91     void setRadii(const Radii& radii) { m_radii = radii; }
     92 
     93     void move(const IntSize& size) { m_rect.move(size); }
     94     void inflate(int size) { m_rect.inflate(size);  }
     95     void inflateWithRadii(int size);
     96     void expandRadii(int size) { m_radii.expand(size); }
     97     void shrinkRadii(int size) { m_radii.shrink(size); }
     98 
     99     void includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
    100     void excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge);
    101 
    102     bool isRenderable() const;
    103     void adjustRadii();
    104 
    105     // Tests whether the quad intersects any part of this rounded rectangle.
    106     // This only works for convex quads.
    107     bool intersectsQuad(const FloatQuad&) const;
    108 
    109 private:
    110     IntRect m_rect;
    111     Radii m_radii;
    112 };
    113 
    114 inline bool operator==(const RoundedRect::Radii& a, const RoundedRect::Radii& b)
    115 {
    116     return a.topLeft() == b.topLeft() && a.topRight() == b.topRight() && a.bottomLeft() == b.bottomLeft() && a.bottomRight() == b.bottomRight();
    117 }
    118 
    119 inline bool operator!=(const RoundedRect::Radii& a, const RoundedRect::Radii& b)
    120 {
    121     return !(a == b);
    122 }
    123 
    124 inline bool operator==(const RoundedRect& a, const RoundedRect& b)
    125 {
    126     return a.rect() == b.rect() && a.radii() == b.radii();
    127 }
    128 
    129 
    130 } // namespace WebCore
    131 
    132 #endif // RoundedRect_h
    133