initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / RoundedRect.cpp
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 #include "config.h"
28 #include "RoundedRect.h"
29
30 #include <algorithm>
31
32 using namespace std;
33
34 namespace WebCore {
35
36 bool RoundedRect::Radii::isZero() const
37 {
38     return m_topLeft.isZero() && m_topRight.isZero() && m_bottomLeft.isZero() && m_bottomRight.isZero();
39 }
40
41 void RoundedRect::Radii::scale(float factor)
42 {
43     if (factor == 1)
44         return;
45
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 = LayoutSize();
50     m_topRight.scale(factor);
51     if (!m_topRight.width() || !m_topRight.height())
52         m_topRight = LayoutSize();
53     m_bottomLeft.scale(factor);
54     if (!m_bottomLeft.width() || !m_bottomLeft.height())
55         m_bottomLeft = LayoutSize();
56     m_bottomRight.scale(factor);
57     if (!m_bottomRight.width() || !m_bottomRight.height())
58         m_bottomRight = LayoutSize();
59
60 }
61
62 void RoundedRect::Radii::expand(LayoutUnit topWidth, LayoutUnit bottomWidth, LayoutUnit leftWidth, LayoutUnit rightWidth)
63 {
64     m_topLeft.setWidth(max<LayoutUnit>(0, m_topLeft.width() + leftWidth));
65     m_topLeft.setHeight(max<LayoutUnit>(0, m_topLeft.height() + topWidth));
66
67     m_topRight.setWidth(max<LayoutUnit>(0, m_topRight.width() + rightWidth));
68     m_topRight.setHeight(max<LayoutUnit>(0, m_topRight.height() + topWidth));
69
70     m_bottomLeft.setWidth(max<LayoutUnit>(0, m_bottomLeft.width() + leftWidth));
71     m_bottomLeft.setHeight(max<LayoutUnit>(0, m_bottomLeft.height() + bottomWidth));
72
73     m_bottomRight.setWidth(max<LayoutUnit>(0, m_bottomRight.width() + rightWidth));
74     m_bottomRight.setHeight(max<LayoutUnit>(0, m_bottomRight.height() + bottomWidth));
75 }
76
77 void RoundedRect::inflateWithRadii(LayoutUnit size)
78 {
79     LayoutRect old = m_rect;
80
81     m_rect.inflate(size);
82     // Considering the inflation factor of shorter size to scale the radii seems appropriate here
83     float factor;
84     if (m_rect.width() < m_rect.height())
85         factor = old.width() ? (float)m_rect.width() / old.width() : 0;
86     else
87         factor = old.height() ? (float)m_rect.height() / old.height() : 0;
88
89     m_radii.scale(factor);
90 }
91
92 void RoundedRect::Radii::includeLogicalEdges(const RoundedRect::Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
93 {
94     if (includeLogicalLeftEdge) {
95         if (isHorizontal)
96             m_bottomLeft = edges.bottomLeft();
97         else
98             m_topRight = edges.topRight();
99         m_topLeft = edges.topLeft();
100     }
101
102     if (includeLogicalRightEdge) {
103         if (isHorizontal)
104             m_topRight = edges.topRight();
105         else
106             m_bottomLeft = edges.bottomLeft();
107         m_bottomRight = edges.bottomRight();
108     }
109 }
110
111 void RoundedRect::Radii::excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge)
112 {
113     if (excludeLogicalLeftEdge) {
114         if (isHorizontal)
115             m_bottomLeft = LayoutSize();
116         else
117             m_topRight = LayoutSize();
118         m_topLeft = LayoutSize();
119     }
120         
121     if (excludeLogicalRightEdge) {
122         if (isHorizontal)
123             m_topRight = LayoutSize();
124         else
125             m_bottomLeft = LayoutSize();
126         m_bottomRight = LayoutSize();
127     }
128 }
129
130 RoundedRect::RoundedRect(LayoutUnit x, LayoutUnit y, LayoutUnit width, LayoutUnit height)
131     : m_rect(x, y, width, height)
132 {
133 }
134
135 RoundedRect::RoundedRect(const LayoutRect& rect, const Radii& radii)
136     : m_rect(rect)
137     , m_radii(radii)
138 {
139 }
140
141 RoundedRect::RoundedRect(const LayoutRect& rect, const LayoutSize& topLeft, const LayoutSize& topRight, const LayoutSize& bottomLeft, const LayoutSize& bottomRight)
142     : m_rect(rect)
143     , m_radii(topLeft, topRight, bottomLeft, bottomRight)
144 {
145 }
146
147 void RoundedRect::includeLogicalEdges(const Radii& edges, bool isHorizontal, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
148 {
149     m_radii.includeLogicalEdges(edges, isHorizontal, includeLogicalLeftEdge, includeLogicalRightEdge);
150 }
151
152 void RoundedRect::excludeLogicalEdges(bool isHorizontal, bool excludeLogicalLeftEdge, bool excludeLogicalRightEdge)
153 {
154     m_radii.excludeLogicalEdges(isHorizontal, excludeLogicalLeftEdge, excludeLogicalRightEdge);
155 }
156
157 bool RoundedRect::isRenderable() const
158 {
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();
163 }
164
165 } // namespace WebCore