| 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 "FloatQuad.h" |
| 31 | #include "LayoutRect.h" |
| 32 | #include "LayoutSize.h" |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class FloatRoundedRect; |
| 37 | class LayoutUnit; |
| 38 | class Region; |
| 39 | |
| 40 | class RoundedRect { |
| 41 | public: |
| 42 | class Radii { |
| 43 | public: |
| 44 | Radii() {} |
| 45 | Radii(const LayoutSize& topLeft, const LayoutSize& topRight, const LayoutSize& bottomLeft, const LayoutSize& bottomRight) |
| 46 | : m_topLeft(topLeft) |
| 47 | , m_topRight(topRight) |
| 48 | , m_bottomLeft(bottomLeft) |
| 49 | , m_bottomRight(bottomRight) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void setTopLeft(const LayoutSize& size) { m_topLeft = size; } |
| 54 | void setTopRight(const LayoutSize& size) { m_topRight = size; } |
| 55 | void setBottomLeft(const LayoutSize& size) { m_bottomLeft = size; } |
| 56 | void setBottomRight(const LayoutSize& size) { m_bottomRight = size; } |
| 57 | const LayoutSize& topLeft() const { return m_topLeft; } |
| 58 | const LayoutSize& topRight() const { return m_topRight; } |
| 59 | const LayoutSize& bottomLeft() const { return m_bottomLeft; } |
| 60 | const LayoutSize& bottomRight() const { return m_bottomRight; } |
| 61 | |
| 62 | bool isZero() const; |
| 63 | |
| 64 | void includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge); |
| 65 | void excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge); |
| 66 | |
| 67 | void scale(float factor); |
| 68 | void expand(const LayoutUnit& topWidth, const LayoutUnit& bottomWidth, const LayoutUnit& leftWidth, const LayoutUnit& rightWidth); |
| 69 | void expand(const LayoutUnit& size) { expand(size, size, size, size); } |
| 70 | void shrink(const LayoutUnit& topWidth, const LayoutUnit& bottomWidth, const LayoutUnit& leftWidth, const LayoutUnit& rightWidth) { expand(-topWidth, -bottomWidth, -leftWidth, -rightWidth); } |
| 71 | void shrink(const LayoutUnit& size) { shrink(size, size, size, size); } |
| 72 | |
| 73 | Radii transposedRadii() const { return Radii(m_topLeft.transposedSize(), m_topRight.transposedSize(), m_bottomLeft.transposedSize(), m_bottomRight.transposedSize()); } |
| 74 | |
| 75 | private: |
| 76 | LayoutSize m_topLeft; |
| 77 | LayoutSize m_topRight; |
| 78 | LayoutSize m_bottomLeft; |
| 79 | LayoutSize m_bottomRight; |
| 80 | }; |
| 81 | |
| 82 | WEBCORE_EXPORT explicit RoundedRect(const LayoutRect&, const Radii& = Radii()); |
| 83 | RoundedRect(const LayoutUnit&, const LayoutUnit&, const LayoutUnit& width, const LayoutUnit& height); |
| 84 | WEBCORE_EXPORT RoundedRect(const LayoutRect&, const LayoutSize& topLeft, const LayoutSize& topRight, const LayoutSize& bottomLeft, const LayoutSize& bottomRight); |
| 85 | |
| 86 | const LayoutRect& rect() const { return m_rect; } |
| 87 | const Radii& radii() const { return m_radii; } |
| 88 | bool isRounded() const { return !m_radii.isZero(); } |
| 89 | bool isEmpty() const { return m_rect.isEmpty(); } |
| 90 | |
| 91 | void setRect(const LayoutRect& rect) { m_rect = rect; } |
| 92 | void setRadii(const Radii& radii) { m_radii = radii; } |
| 93 | |
| 94 | void move(const LayoutSize& size) { m_rect.move(size); } |
| 95 | void moveBy(const LayoutPoint& offset) { m_rect.moveBy(offset); } |
| 96 | void inflate(const LayoutUnit& size) { m_rect.inflate(size); } |
| 97 | void inflateWithRadii(const LayoutUnit& size); |
| 98 | void expandRadii(const LayoutUnit& size) { m_radii.expand(size); } |
| 99 | void shrinkRadii(const LayoutUnit& size) { m_radii.shrink(size); } |
| 100 | |
| 101 | void includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge); |
| 102 | void excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge); |
| 103 | |
| 104 | bool isRenderable() const; |
| 105 | void adjustRadii(); |
| 106 | |
| 107 | // Tests whether the quad intersects any part of this rounded rectangle. |
| 108 | // This only works for convex quads. |
| 109 | bool intersectsQuad(const FloatQuad&) const; |
| 110 | WEBCORE_EXPORT bool contains(const LayoutRect&) const; |
| 111 | |
| 112 | FloatRoundedRect pixelSnappedRoundedRectForPainting(float deviceScaleFactor) const; |
| 113 | |
| 114 | RoundedRect transposedRect() const { return RoundedRect(m_rect.transposedRect(), m_radii.transposedRadii()); } |
| 115 | |
| 116 | private: |
| 117 | LayoutRect m_rect; |
| 118 | Radii m_radii; |
| 119 | }; |
| 120 | |
| 121 | inline bool operator==(const RoundedRect::Radii& a, const RoundedRect::Radii& b) |
| 122 | { |
| 123 | return a.topLeft() == b.topLeft() && a.topRight() == b.topRight() && a.bottomLeft() == b.bottomLeft() && a.bottomRight() == b.bottomRight(); |
| 124 | } |
| 125 | |
| 126 | inline bool operator==(const RoundedRect& a, const RoundedRect& b) |
| 127 | { |
| 128 | return a.rect() == b.rect() && a.radii() == b.radii(); |
| 129 | } |
| 130 | |
| 131 | // Snip away rectangles from corners, roughly one per step length of arc. |
| 132 | WEBCORE_EXPORT Region approximateAsRegion(const RoundedRect&, unsigned stepLength = 20); |
| 133 | |
| 134 | } // namespace WebCore |
| 135 | |
| 136 | #endif // RoundedRect_h |
| 137 | |