2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "RoundedRect.h"
36 bool RoundedRect::Radii::isZero() const
38 return m_topLeft.isZero() && m_topRight.isZero() && m_bottomLeft.isZero() && m_bottomRight.isZero();
41 void RoundedRect::Radii::scale(float factor)
46 // If either radius on a corner becomes zero, reset both radii on that corner.
47 m_topLeft.scale(factor);
48 if (!m_topLeft.width() || !m_topLeft.height())
49 m_topLeft = IntSize();
50 m_topRight.scale(factor);
51 if (!m_topRight.width() || !m_topRight.height())
52 m_topRight = IntSize();
53 m_bottomLeft.scale(factor);
54 if (!m_bottomLeft.width() || !m_bottomLeft.height())
55 m_bottomLeft = IntSize();
56 m_bottomRight.scale(factor);
57 if (!m_bottomRight.width() || !m_bottomRight.height())
58 m_bottomRight = IntSize();
62 void RoundedRect::Radii::expand(int topWidth, int bottomWidth, int leftWidth, int rightWidth)
64 m_topLeft.setWidth(max<int>(0, m_topLeft.width() + leftWidth));
65 m_topLeft.setHeight(max<int>(0, m_topLeft.height() + topWidth));
67 m_topRight.setWidth(max<int>(0, m_topRight.width() + rightWidth));
68 m_topRight.setHeight(max<int>(0, m_topRight.height() + topWidth));
70 m_bottomLeft.setWidth(max<int>(0, m_bottomLeft.width() + leftWidth));
71 m_bottomLeft.setHeight(max<int>(0, m_bottomLeft.height() + bottomWidth));
73 m_bottomRight.setWidth(max<int>(0, m_bottomRight.width() + rightWidth));
74 m_bottomRight.setHeight(max<int>(0, m_bottomRight.height() + bottomWidth));
77 void RoundedRect::inflateWithRadii(int size)
82 // Considering the inflation factor of shorter size to scale the radii seems appropriate here
84 if (m_rect.width() < m_rect.height())
85 factor = old.width() ? (float)m_rect.width() / old.width() : int(0);
87 factor = old.height() ? (float)m_rect.height() / old.height() : int(0);
89 m_radii.scale(factor);
92 void RoundedRect::Radii::includeLogicalEdges(const RoundedRect::Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
94 if (includeLogicalLeftEdge) {
96 m_bottomLeft = edges.bottomLeft();
98 m_topRight = edges.topRight();
99 m_topLeft = edges.topLeft();
102 if (includeLogicalRightEdge) {
104 m_topRight = edges.topRight();
106 m_bottomLeft = edges.bottomLeft();
107 m_bottomRight = edges.bottomRight();
111 void RoundedRect::Radii::excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge)
113 if (excludeLogicalLeftEdge) {
115 m_bottomLeft = IntSize();
117 m_topRight = IntSize();
118 m_topLeft = IntSize();
121 if (excludeLogicalRightEdge) {
123 m_topRight = IntSize();
125 m_bottomLeft = IntSize();
126 m_bottomRight = IntSize();
130 RoundedRect::RoundedRect(int x, int y, int width, int height)
131 : m_rect(x, y, width, height)
135 RoundedRect::RoundedRect(const IntRect& rect, const Radii& radii)
141 RoundedRect::RoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight)
143 , m_radii(topLeft, topRight, bottomLeft, bottomRight)
147 void RoundedRect::includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
149 m_radii.includeLogicalEdges(edges, isHorizontal, includeLogicalLeftEdge, includeLogicalRightEdge);
152 void RoundedRect::excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge)
154 m_radii.excludeLogicalEdges(isHorizontal, excludeLogicalLeftEdge, excludeLogicalRightEdge);
157 bool RoundedRect::isRenderable() const
159 return m_radii.topLeft().width() + m_radii.topRight().width() <= m_rect.width()
160 && m_radii.bottomLeft().width() + m_radii.bottomRight().width() <= m_rect.width()
161 && m_radii.topLeft().height() + m_radii.topRight().height() <= m_rect.height()
162 && m_radii.bottomLeft().height() + m_radii.bottomRight().height() <= m_rect.height();
165 } // namespace WebCore