Home | History | Annotate | Download | only in graphics
      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 "core/platform/graphics/FloatQuad.h"
     31 #include "core/platform/graphics/IntRect.h"
     32 
     33 namespace WebCore {
     34 
     35 
     36 class RoundedRect {
     37 public:
     38     class Radii {
     39     public:
     40         Radii() {}
     41         Radii(const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight)
     42             : m_topLeft(topLeft)
     43             , m_topRight(topRight)
     44             , m_bottomLeft(bottomLeft)
     45             , m_bottomRight(bottomRight)
     46         {
     47         }
     48 
     49         void setTopLeft(const IntSize& size) { m_topLeft = size; }
     50         void setTopRight(const IntSize& size) { m_topRight = size; }
     51         void setBottomLeft(const IntSize& size) { m_bottomLeft = size; }
     52         void setBottomRight(const IntSize& size) { m_bottomRight = size; }
     53         const IntSize& topLeft() const { return m_topLeft; }
     54         const IntSize& topRight() const { return m_topRight; }
     55         const IntSize& bottomLeft() const { return m_bottomLeft; }
     56         const IntSize& bottomRight() const { return m_bottomRight; }
     57 
     58         bool isZero() const;
     59 
     60         void includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
     61         void excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge);
     62 
     63         void scale(float factor);
     64         void expand(int topWidth, int bottomWidth, int leftWidth, int rightWidth);
     65         void expand(int size) { expand(size, size, size, size); }
     66         void shrink(int topWidth, int bottomWidth, int leftWidth, int rightWidth) { expand(-topWidth, -bottomWidth, -leftWidth, -rightWidth); }
     67         void shrink(int size) { shrink(size, size, size, size); }
     68 
     69     private:
     70         IntSize m_topLeft;
     71         IntSize m_topRight;
     72         IntSize m_bottomLeft;
     73         IntSize m_bottomRight;
     74     };
     75 
     76     explicit RoundedRect(const IntRect&, const Radii& = Radii());
     77     RoundedRect(int x, int y, int width, int height);
     78     RoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
     79 
     80     const IntRect& rect() const { return m_rect; }
     81     const Radii& radii() const { return m_radii; }
     82     bool isRounded() const { return !m_radii.isZero(); }
     83     bool isEmpty() const { return m_rect.isEmpty(); }
     84 
     85     // Returns a quickly computed rect enclosed by the rounded rect.
     86     IntRect radiusCenterRect() const;
     87 
     88     void setRect(const IntRect& rect) { m_rect = rect; }
     89     void setRadii(const Radii& radii) { m_radii = radii; }
     90 
     91     void move(const IntSize& size) { m_rect.move(size); }
     92     void inflate(int size) { m_rect.inflate(size);  }
     93     void inflateWithRadii(int size);
     94     void expandRadii(int size) { m_radii.expand(size); }
     95     void shrinkRadii(int size) { m_radii.shrink(size); }
     96 
     97     void includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
     98     void excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge);
     99 
    100     bool isRenderable() const;
    101     void adjustRadii();
    102 
    103     // Tests whether the quad intersects any part of this rounded rectangle.
    104     // This only works for convex quads.
    105     bool intersectsQuad(const FloatQuad&) const;
    106 
    107 private:
    108     IntRect m_rect;
    109     Radii m_radii;
    110 };
    111 
    112 inline bool operator==(const RoundedRect::Radii& a, const RoundedRect::Radii& b)
    113 {
    114     return a.topLeft() == b.topLeft() && a.topRight() == b.topRight() && a.bottomLeft() == b.bottomLeft() && a.bottomRight() == b.bottomRight();
    115 }
    116 
    117 inline bool operator!=(const RoundedRect::Radii& a, const RoundedRect::Radii& b)
    118 {
    119     return !(a == b);
    120 }
    121 
    122 inline bool operator==(const RoundedRect& a, const RoundedRect& b)
    123 {
    124     return a.rect() == b.rect() && a.radii() == b.radii();
    125 }
    126 
    127 
    128 } // namespace WebCore
    129 
    130 #endif // RoundedRect_h
    131