2011-05-20 Dirk Schulze <krit@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     FloatPoint* points = element->points;
50     float segmentLength = 0;
51     switch (element->type) {
52         case PathElementMoveToPoint:
53             segmentLength = traversalState.moveTo(points[0]);
54             break;
55         case PathElementAddLineToPoint:
56             segmentLength = traversalState.lineTo(points[0]);
57             break;
58         case PathElementAddQuadCurveToPoint:
59             segmentLength = traversalState.quadraticBezierTo(points[0], points[1]);
60             break;
61         case PathElementAddCurveToPoint:
62             segmentLength = traversalState.cubicBezierTo(points[0], points[1], points[2]);
63             break;
64         case PathElementCloseSubpath:
65             segmentLength = traversalState.closeSubpath();
66             break;
67     }
68     traversalState.m_totalLength += segmentLength; 
69     traversalState.processSegment();
70 }
71
72 float Path::length() const
73 {
74     PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
75     apply(&traversalState, pathLengthApplierFunction);
76     return traversalState.m_totalLength;
77 }
78
79 FloatPoint Path::pointAtLength(float length, bool& ok) const
80 {
81     PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
82     traversalState.m_desiredLength = length;
83     apply(&traversalState, pathLengthApplierFunction);
84     ok = traversalState.m_success;
85     return traversalState.m_current;
86 }
87
88 float Path::normalAngleAtLength(float length, bool& ok) const
89 {
90     PathTraversalState traversalState(PathTraversalState::TraversalNormalAngleAtLength);
91     traversalState.m_desiredLength = length ? length : std::numeric_limits<float>::epsilon();
92     apply(&traversalState, pathLengthApplierFunction);
93     ok = traversalState.m_success;
94     return traversalState.m_normalAngle;
95 }
96 #endif
97
98 void Path::addRoundedRect(const FloatRect& rect, const FloatSize& roundingRadii)
99 {
100     if (rect.isEmpty())
101         return;
102
103     FloatSize radius(roundingRadii);
104     FloatSize halfSize(rect.width() / 2, rect.height() / 2);
105
106     // If rx is greater than half of the width of the rectangle
107     // then set rx to half of the width (required in SVG spec)
108     if (radius.width() > halfSize.width())
109         radius.setWidth(halfSize.width());
110
111     // If ry is greater than half of the height of the rectangle
112     // then set ry to half of the height (required in SVG spec)
113     if (radius.height() > halfSize.height())
114         radius.setHeight(halfSize.height());
115
116     moveTo(FloatPoint(rect.x() + radius.width(), rect.y()));
117
118     if (radius.width() < halfSize.width())
119         addLineTo(FloatPoint(rect.x() + rect.width() - roundingRadii.width(), rect.y()));
120
121     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()));
122
123     if (radius.height() < halfSize.height())
124         addLineTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - radius.height()));
125
126     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()));
127
128     if (radius.width() < halfSize.width())
129         addLineTo(FloatPoint(rect.x() + radius.width(), rect.y() + rect.height()));
130
131     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()));
132
133     if (radius.height() < halfSize.height())
134         addLineTo(FloatPoint(rect.x(), rect.y() + radius.height()));
135
136     addBezierCurveTo(FloatPoint(rect.x(), rect.y() + radius.height() * gCircleControlPoint), FloatPoint(rect.x() + radius.width() * gCircleControlPoint, rect.y()), FloatPoint(rect.x() + radius.width(), rect.y()));
137
138     closeSubpath();
139 }
140
141 void Path::addRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
142 {
143     if (rect.isEmpty())
144         return;
145
146     if (rect.width() < topLeftRadius.width() + topRightRadius.width()
147             || rect.width() < bottomLeftRadius.width() + bottomRightRadius.width()
148             || rect.height() < topLeftRadius.height() + bottomLeftRadius.height()
149             || rect.height() < topRightRadius.height() + bottomRightRadius.height()) {
150         // If all the radii cannot be accommodated, return a rect.
151         addRect(rect);
152         return;
153     }
154
155     moveTo(FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
156
157     addLineTo(FloatPoint(rect.x() + rect.width() - topRightRadius.width(), rect.y()));
158     addBezierCurveTo(FloatPoint(rect.x() + rect.width() - topRightRadius.width() * gCircleControlPoint, rect.y()),
159                      FloatPoint(rect.x() + rect.width(), rect.y() + topRightRadius.height() * gCircleControlPoint),
160                      FloatPoint(rect.x() + rect.width(), rect.y() + topRightRadius.height()));
161     addLineTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - bottomRightRadius.height()));
162     addBezierCurveTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - bottomRightRadius.height() * gCircleControlPoint),
163                      FloatPoint(rect.x() + rect.width() - bottomRightRadius.width() * gCircleControlPoint, rect.y() + rect.height()),
164                      FloatPoint(rect.x() + rect.width() - bottomRightRadius.width(), rect.y() + rect.height()));
165     addLineTo(FloatPoint(rect.x() + bottomLeftRadius.width(), rect.y() + rect.height()));
166     addBezierCurveTo(FloatPoint(rect.x() + bottomLeftRadius.width() * gCircleControlPoint, rect.y() + rect.height()),
167                      FloatPoint(rect.x(), rect.y() + rect.height() - bottomLeftRadius.height() * gCircleControlPoint),
168                      FloatPoint(rect.x(), rect.y() + rect.height() - bottomLeftRadius.height()));
169     addLineTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height()));
170     addBezierCurveTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height() * gCircleControlPoint),
171                      FloatPoint(rect.x() + topLeftRadius.width() * gCircleControlPoint, rect.y()),
172                      FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
173
174     closeSubpath();
175 }
176
177 void Path::addRoundedRect(const RoundedIntRect& r)
178 {
179     addRoundedRect(r.rect(), r.radii().topLeft(), r.radii().topRight(), r.radii().bottomLeft(), r.radii().bottomRight());
180 }
181
182 }