2011-03-08 Andreas Kling <kling@webkit.org>
[WebKit-https.git] / Source / WebCore / platform / graphics / Path.cpp
1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  *                     2006 Rob Buis <buis@kde.org>
4  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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 COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26  */
27
28
29 #include "config.h"
30 #include "Path.h"
31
32 #include "FloatPoint.h"
33 #include "FloatRect.h"
34 #include "PathTraversalState.h"
35 #include <math.h>
36 #include <wtf/MathExtras.h>
37
38 // Approximation of control point positions on a bezier to simulate a quarter of a circle.
39 static const float gCircleControlPoint = 0.448f;
40
41 namespace WebCore {
42
43 #if !PLATFORM(OPENVG) && !PLATFORM(QT)
44 static void pathLengthApplierFunction(void* info, const PathElement* element)
45 {
46     PathTraversalState& traversalState = *static_cast<PathTraversalState*>(info);
47     if (traversalState.m_success)
48         return;
49     traversalState.m_previous = traversalState.m_current;
50     FloatPoint* points = element->points;
51     float segmentLength = 0;
52     switch (element->type) {
53         case PathElementMoveToPoint:
54             segmentLength = traversalState.moveTo(points[0]);
55             break;
56         case PathElementAddLineToPoint:
57             segmentLength = traversalState.lineTo(points[0]);
58             break;
59         case PathElementAddQuadCurveToPoint:
60             segmentLength = traversalState.quadraticBezierTo(points[0], points[1]);
61             break;
62         case PathElementAddCurveToPoint:
63             segmentLength = traversalState.cubicBezierTo(points[0], points[1], points[2]);
64             break;
65         case PathElementCloseSubpath:
66             segmentLength = traversalState.closeSubpath();
67             break;
68     }
69     traversalState.m_totalLength += segmentLength; 
70     if ((traversalState.m_action == PathTraversalState::TraversalPointAtLength || 
71          traversalState.m_action == PathTraversalState::TraversalNormalAngleAtLength) &&
72         (traversalState.m_totalLength >= traversalState.m_desiredLength)) {
73         FloatSize change = traversalState.m_current - traversalState.m_previous;
74         float slope = atan2f(change.height(), change.width());
75
76         if (traversalState.m_action == PathTraversalState::TraversalPointAtLength) {
77             float offset = traversalState.m_desiredLength - traversalState.m_totalLength;
78             traversalState.m_current.move(offset * cosf(slope), offset * sinf(slope));
79         } else
80             traversalState.m_normalAngle = rad2deg(slope);
81
82         traversalState.m_success = true;
83     }
84 }
85
86 float Path::length() const
87 {
88     PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
89     apply(&traversalState, pathLengthApplierFunction);
90     return traversalState.m_totalLength;
91 }
92
93 FloatPoint Path::pointAtLength(float length, bool& ok) const
94 {
95     PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
96     traversalState.m_desiredLength = length;
97     apply(&traversalState, pathLengthApplierFunction);
98     ok = traversalState.m_success;
99     return traversalState.m_current;
100 }
101
102 float Path::normalAngleAtLength(float length, bool& ok) const
103 {
104     PathTraversalState traversalState(PathTraversalState::TraversalNormalAngleAtLength);
105     traversalState.m_desiredLength = length;
106     apply(&traversalState, pathLengthApplierFunction);
107     ok = traversalState.m_success;
108     return traversalState.m_normalAngle;
109 }
110 #endif
111
112 void Path::addRoundedRect(const FloatRect& rect, const FloatSize& roundingRadii)
113 {
114     if (rect.isEmpty())
115         return;
116
117     FloatSize radius(roundingRadii);
118     FloatSize halfSize(rect.width() / 2, rect.height() / 2);
119
120     // If rx is greater than half of the width of the rectangle
121     // then set rx to half of the width (required in SVG spec)
122     if (radius.width() > halfSize.width())
123         radius.setWidth(halfSize.width());
124
125     // If ry is greater than half of the height of the rectangle
126     // then set ry to half of the height (required in SVG spec)
127     if (radius.height() > halfSize.height())
128         radius.setHeight(halfSize.height());
129
130     moveTo(FloatPoint(rect.x() + radius.width(), rect.y()));
131
132     if (radius.width() < halfSize.width())
133         addLineTo(FloatPoint(rect.x() + rect.width() - roundingRadii.width(), rect.y()));
134
135     addBezierCurveTo(FloatPoint(rect.x() + rect.width() - radius.width() * gCircleControlPoint, rect.y()), FloatPoint(rect.x() + rect.width(), rect.y() + radius.height() * gCircleControlPoint), FloatPoint(rect.x() + rect.width(), rect.y() + radius.height()));
136
137     if (radius.height() < halfSize.height())
138         addLineTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - radius.height()));
139
140     addBezierCurveTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - radius.height() * gCircleControlPoint), FloatPoint(rect.x() + rect.width() - radius.width() * gCircleControlPoint, rect.y() + rect.height()), FloatPoint(rect.x() + rect.width() - radius.width(), rect.y() + rect.height()));
141
142     if (radius.width() < halfSize.width())
143         addLineTo(FloatPoint(rect.x() + radius.width(), rect.y() + rect.height()));
144
145     addBezierCurveTo(FloatPoint(rect.x() + radius.width() * gCircleControlPoint, rect.y() + rect.height()), FloatPoint(rect.x(), rect.y() + rect.height() - radius.height() * gCircleControlPoint), FloatPoint(rect.x(), rect.y() + rect.height() - radius.height()));
146
147     if (radius.height() < halfSize.height())
148         addLineTo(FloatPoint(rect.x(), rect.y() + radius.height()));
149
150     addBezierCurveTo(FloatPoint(rect.x(), rect.y() + radius.height() * gCircleControlPoint), FloatPoint(rect.x() + radius.width() * gCircleControlPoint, rect.y()), FloatPoint(rect.x() + radius.width(), rect.y()));
151
152     closeSubpath();
153 }
154
155 void Path::addRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
156 {
157     if (rect.isEmpty())
158         return;
159
160     if (rect.width() < topLeftRadius.width() + topRightRadius.width()
161             || rect.width() < bottomLeftRadius.width() + bottomRightRadius.width()
162             || rect.height() < topLeftRadius.height() + bottomLeftRadius.height()
163             || rect.height() < topRightRadius.height() + bottomRightRadius.height()) {
164         // If all the radii cannot be accommodated, return a rect.
165         addRect(rect);
166         return;
167     }
168
169     moveTo(FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
170
171     addLineTo(FloatPoint(rect.x() + rect.width() - topRightRadius.width(), rect.y()));
172     addBezierCurveTo(FloatPoint(rect.x() + rect.width() - topRightRadius.width() * gCircleControlPoint, rect.y()),
173                      FloatPoint(rect.x() + rect.width(), rect.y() + topRightRadius.height() * gCircleControlPoint),
174                      FloatPoint(rect.x() + rect.width(), rect.y() + topRightRadius.height()));
175     addLineTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - bottomRightRadius.height()));
176     addBezierCurveTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - bottomRightRadius.height() * gCircleControlPoint),
177                      FloatPoint(rect.x() + rect.width() - bottomRightRadius.width() * gCircleControlPoint, rect.y() + rect.height()),
178                      FloatPoint(rect.x() + rect.width() - bottomRightRadius.width(), rect.y() + rect.height()));
179     addLineTo(FloatPoint(rect.x() + bottomLeftRadius.width(), rect.y() + rect.height()));
180     addBezierCurveTo(FloatPoint(rect.x() + bottomLeftRadius.width() * gCircleControlPoint, rect.y() + rect.height()),
181                      FloatPoint(rect.x(), rect.y() + rect.height() - bottomLeftRadius.height() * gCircleControlPoint),
182                      FloatPoint(rect.x(), rect.y() + rect.height() - bottomLeftRadius.height()));
183     addLineTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height()));
184     addBezierCurveTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height() * gCircleControlPoint),
185                      FloatPoint(rect.x() + topLeftRadius.width() * gCircleControlPoint, rect.y()),
186                      FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
187
188     closeSubpath();
189 }
190
191 }