getBBox() on a SVGPathElement with curves incorrectly includes control points
[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 namespace WebCore {
39
40 #if !PLATFORM(OPENVG) && !PLATFORM(QT)
41 static void pathLengthApplierFunction(void* info, const PathElement* element)
42 {
43     PathTraversalState& traversalState = *static_cast<PathTraversalState*>(info);
44     if (traversalState.m_success)
45         return;
46     FloatPoint* points = element->points;
47     float segmentLength = 0;
48     switch (element->type) {
49         case PathElementMoveToPoint:
50             segmentLength = traversalState.moveTo(points[0]);
51             break;
52         case PathElementAddLineToPoint:
53             segmentLength = traversalState.lineTo(points[0]);
54             break;
55         case PathElementAddQuadCurveToPoint:
56             segmentLength = traversalState.quadraticBezierTo(points[0], points[1]);
57             break;
58         case PathElementAddCurveToPoint:
59             segmentLength = traversalState.cubicBezierTo(points[0], points[1], points[2]);
60             break;
61         case PathElementCloseSubpath:
62             segmentLength = traversalState.closeSubpath();
63             break;
64     }
65     traversalState.m_totalLength += segmentLength; 
66     traversalState.processSegment();
67 }
68
69 float Path::length() const
70 {
71     PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
72     apply(&traversalState, pathLengthApplierFunction);
73     return traversalState.m_totalLength;
74 }
75
76 FloatPoint Path::pointAtLength(float length, bool& ok) const
77 {
78     PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
79     traversalState.m_desiredLength = length;
80     apply(&traversalState, pathLengthApplierFunction);
81     ok = traversalState.m_success;
82     return traversalState.m_current;
83 }
84
85 float Path::normalAngleAtLength(float length, bool& ok) const
86 {
87     PathTraversalState traversalState(PathTraversalState::TraversalNormalAngleAtLength);
88     traversalState.m_desiredLength = length ? length : std::numeric_limits<float>::epsilon();
89     apply(&traversalState, pathLengthApplierFunction);
90     ok = traversalState.m_success;
91     return traversalState.m_normalAngle;
92 }
93 #endif
94
95 void Path::addRoundedRect(const RoundedRect& r)
96 {
97     addRoundedRect(r.rect(), r.radii().topLeft(), r.radii().topRight(), r.radii().bottomLeft(), r.radii().bottomRight());
98 }
99
100 void Path::addRoundedRect(const FloatRect& rect, const FloatSize& roundingRadii)
101 {
102     if (rect.isEmpty())
103         return;
104
105     FloatSize radius(roundingRadii);
106     FloatSize halfSize(rect.width() / 2, rect.height() / 2);
107
108     // If rx is greater than half of the width of the rectangle
109     // then set rx to half of the width (required in SVG spec)
110     if (radius.width() > halfSize.width())
111         radius.setWidth(halfSize.width());
112
113     // If ry is greater than half of the height of the rectangle
114     // then set ry to half of the height (required in SVG spec)
115     if (radius.height() > halfSize.height())
116         radius.setHeight(halfSize.height());
117
118     addBeziersForRoundedRect(rect, radius, radius, radius, radius);
119 }
120
121 void Path::addRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
122 {
123     if (rect.isEmpty())
124         return;
125
126     if (rect.width() < topLeftRadius.width() + topRightRadius.width()
127             || rect.width() < bottomLeftRadius.width() + bottomRightRadius.width()
128             || rect.height() < topLeftRadius.height() + bottomLeftRadius.height()
129             || rect.height() < topRightRadius.height() + bottomRightRadius.height()) {
130         // If all the radii cannot be accommodated, return a rect.
131         addRect(rect);
132         return;
133     }
134
135     addBeziersForRoundedRect(rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
136 }
137
138 // Approximation of control point positions on a bezier to simulate a quarter of a circle.
139 // This is 1-kappa, where kappa = 4 * (sqrt(2) - 1) / 3
140 static const float gCircleControlPoint = 0.447715f;
141
142 void Path::addBeziersForRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
143 {
144     moveTo(FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
145
146     addLineTo(FloatPoint(rect.maxX() - topRightRadius.width(), rect.y()));
147     addBezierCurveTo(FloatPoint(rect.maxX() - topRightRadius.width() * gCircleControlPoint, rect.y()),
148                      FloatPoint(rect.maxX(), rect.y() + topRightRadius.height() * gCircleControlPoint),
149                      FloatPoint(rect.maxX(), rect.y() + topRightRadius.height()));
150     addLineTo(FloatPoint(rect.maxX(), rect.maxY() - bottomRightRadius.height()));
151     addBezierCurveTo(FloatPoint(rect.maxX(), rect.maxY() - bottomRightRadius.height() * gCircleControlPoint),
152                      FloatPoint(rect.maxX() - bottomRightRadius.width() * gCircleControlPoint, rect.maxY()),
153                      FloatPoint(rect.maxX() - bottomRightRadius.width(), rect.maxY()));
154     addLineTo(FloatPoint(rect.x() + bottomLeftRadius.width(), rect.maxY()));
155     addBezierCurveTo(FloatPoint(rect.x() + bottomLeftRadius.width() * gCircleControlPoint, rect.maxY()),
156                      FloatPoint(rect.x(), rect.maxY() - bottomLeftRadius.height() * gCircleControlPoint),
157                      FloatPoint(rect.x(), rect.maxY() - bottomLeftRadius.height()));
158     addLineTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height()));
159     addBezierCurveTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height() * gCircleControlPoint),
160                      FloatPoint(rect.x() + topLeftRadius.width() * gCircleControlPoint, rect.y()),
161                      FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
162
163     closeSubpath();
164 }
165
166 #if !USE(CG)
167 FloatRect Path::fastBoundingRect() const
168 {
169     return boundingRect();
170 }
171 #endif
172
173 }