2 * Copyright (C) 2006, 2007 Eric Seidel <eric@webkit.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "PathTraversalState.h"
23 #include <wtf/MathExtras.h>
24 #include <wtf/Vector.h>
28 static const float kPathSegmentLengthTolerance = 0.00001f;
30 static inline FloatPoint midPoint(const FloatPoint& first, const FloatPoint& second)
32 return FloatPoint((first.x() + second.x()) / 2.0f, (first.y() + second.y()) / 2.0f);
35 static inline float distanceLine(const FloatPoint& start, const FloatPoint& end)
37 return sqrtf((end.x() - start.x()) * (end.x() - start.x()) + (end.y() - start.y()) * (end.y() - start.y()));
40 struct QuadraticBezier {
42 QuadraticBezier(const FloatPoint& s, const FloatPoint& c, const FloatPoint& e)
49 float approximateDistance() const
51 return distanceLine(start, control) + distanceLine(control, end);
54 void split(QuadraticBezier& left, QuadraticBezier& right) const
56 left.control = midPoint(start, control);
57 right.control = midPoint(control, end);
59 FloatPoint leftControlToRightControl = midPoint(left.control, right.control);
60 left.end = leftControlToRightControl;
61 right.start = leftControlToRightControl;
74 CubicBezier(const FloatPoint& s, const FloatPoint& c1, const FloatPoint& c2, const FloatPoint& e)
82 float approximateDistance() const
84 return distanceLine(start, control1) + distanceLine(control1, control2) + distanceLine(control2, end);
87 void split(CubicBezier& left, CubicBezier& right) const
89 FloatPoint startToControl1 = midPoint(control1, control2);
92 left.control1 = midPoint(start, control1);
93 left.control2 = midPoint(left.control1, startToControl1);
95 right.control2 = midPoint(control2, end);
96 right.control1 = midPoint(right.control2, startToControl1);
99 FloatPoint leftControl2ToRightControl1 = midPoint(left.control2, right.control1);
100 left.end = leftControl2ToRightControl1;
101 right.start = leftControl2ToRightControl1;
110 // FIXME: This function is possibly very slow due to the ifs required for proper path measuring
111 // A simple speed-up would be to use an additional boolean template parameter to control whether
112 // to use the "fast" version of this function with no PathTraversalState updating, vs. the slow
113 // version which does update the PathTraversalState. We'll have to shark it to see if that's necessary.
114 // Another check which is possible up-front (to send us down the fast path) would be to check if
115 // approximateDistance() + current total distance > desired distance
116 template<class CurveType>
117 static float curveLength(PathTraversalState& traversalState, CurveType curve)
119 static const unsigned curveStackDepthLimit = 20;
121 Vector<CurveType> curveStack;
122 curveStack.append(curve);
124 float totalLength = 0;
126 float length = curve.approximateDistance();
127 if ((length - distanceLine(curve.start, curve.end)) > kPathSegmentLengthTolerance && curveStack.size() <= curveStackDepthLimit) {
129 CurveType rightCurve;
130 curve.split(leftCurve, rightCurve);
132 curveStack.append(rightCurve);
134 totalLength += length;
135 if (traversalState.m_action == PathTraversalState::TraversalPointAtLength
136 || traversalState.m_action == PathTraversalState::TraversalNormalAngleAtLength) {
137 traversalState.m_previous = curve.start;
138 traversalState.m_current = curve.end;
139 if (traversalState.m_totalLength + totalLength > traversalState.m_desiredLength)
142 curve = curveStack.last();
143 curveStack.removeLast();
145 } while (!curveStack.isEmpty());
150 PathTraversalState::PathTraversalState(PathTraversalAction action)
160 float PathTraversalState::closeSubpath()
162 float distance = distanceLine(m_current, m_start);
163 m_current = m_control1 = m_control2 = m_start;
167 float PathTraversalState::moveTo(const FloatPoint& point)
169 m_current = m_start = m_control1 = m_control2 = point;
173 float PathTraversalState::lineTo(const FloatPoint& point)
175 float distance = distanceLine(m_current, point);
176 m_current = m_control1 = m_control2 = point;
180 float PathTraversalState::quadraticBezierTo(const FloatPoint& newControl, const FloatPoint& newEnd)
182 float distance = curveLength<QuadraticBezier>(*this, QuadraticBezier(m_current, newControl, newEnd));
184 m_control1 = newControl;
187 if (m_action != TraversalPointAtLength && m_action != TraversalNormalAngleAtLength)
193 float PathTraversalState::cubicBezierTo(const FloatPoint& newControl1, const FloatPoint& newControl2, const FloatPoint& newEnd)
195 float distance = curveLength<CubicBezier>(*this, CubicBezier(m_current, newControl1, newControl2, newEnd));
198 m_control2 = newControl2;
200 if (m_action != TraversalPointAtLength && m_action != TraversalNormalAngleAtLength)
206 void PathTraversalState::processSegment()
208 if (m_action == TraversalSegmentAtLength && m_totalLength >= m_desiredLength)
211 if ((m_action == TraversalPointAtLength || m_action == TraversalNormalAngleAtLength) && m_totalLength >= m_desiredLength) {
212 FloatSize change = m_current - m_previous;
213 float slope = atan2f(change.height(), change.width());
214 if (m_action == TraversalPointAtLength) {
215 float offset = m_desiredLength - m_totalLength;
216 m_current.move(offset * cosf(slope), offset * sinf(slope));
218 m_normalAngle = rad2deg(slope);
221 m_previous = m_current;