2 Copyright (C) 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
3 Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <wildfox@kde.org>
4 2004, 2005, 2006 Rob Buis <buis@kde.org>
5 2005, 2007 Apple Inc. All Rights reserved.
6 2007 Alp Toker <alp@atoker.com>
7 2008 Dirk Schulze <krit@webkit.org>
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 aint with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
28 #include "AffineTransform.h"
29 #include "CairoPath.h"
30 #include "FloatRect.h"
31 #include "GraphicsContext.h"
32 #include "NotImplemented.h"
33 #include "PlatformString.h"
34 #include "StrokeStyleApplier.h"
38 #include <wtf/MathExtras.h>
43 : m_path(new CairoPath())
52 Path::Path(const Path& other)
53 : m_path(new CairoPath())
55 cairo_t* cr = platformPath()->m_cr;
56 cairo_path_t* p = cairo_copy_path(other.platformPath()->m_cr);
57 cairo_append_path(cr, p);
58 cairo_path_destroy(p);
61 Path& Path::operator=(const Path& other)
67 cairo_t* cr = platformPath()->m_cr;
68 cairo_path_t* p = cairo_copy_path(other.platformPath()->m_cr);
69 cairo_append_path(cr, p);
70 cairo_path_destroy(p);
76 cairo_t* cr = platformPath()->m_cr;
80 bool Path::isEmpty() const
82 cairo_t* cr = platformPath()->m_cr;
83 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,5,10)
84 return !cairo_has_current_point(cr);
86 cairo_path_t* p = cairo_copy_path(cr);
87 bool hasData = p->num_data;
88 cairo_path_destroy(p);
93 void Path::translate(const FloatSize& p)
95 cairo_t* cr = platformPath()->m_cr;
96 cairo_translate(cr, p.width(), p.height());
99 void Path::moveTo(const FloatPoint& p)
101 cairo_t* cr = platformPath()->m_cr;
102 cairo_move_to(cr, p.x(), p.y());
105 void Path::addLineTo(const FloatPoint& p)
107 cairo_t* cr = platformPath()->m_cr;
108 cairo_line_to(cr, p.x(), p.y());
111 void Path::addRect(const FloatRect& rect)
113 cairo_t* cr = platformPath()->m_cr;
114 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
118 * inspired by libsvg-cairo
120 void Path::addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& point)
122 cairo_t* cr = platformPath()->m_cr;
124 double x1 = controlPoint.x();
125 double y1 = controlPoint.y();
126 double x2 = point.x();
127 double y2 = point.y();
128 cairo_get_current_point(cr, &x, &y);
130 x + 2.0 / 3.0 * (x1 - x), y + 2.0 / 3.0 * (y1 - y),
131 x2 + 2.0 / 3.0 * (x1 - x2), y2 + 2.0 / 3.0 * (y1 - y2),
135 void Path::addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& controlPoint3)
137 cairo_t* cr = platformPath()->m_cr;
138 cairo_curve_to(cr, controlPoint1.x(), controlPoint1.y(),
139 controlPoint2.x(), controlPoint2.y(),
140 controlPoint3.x(), controlPoint3.y());
143 void Path::addArc(const FloatPoint& p, float r, float sa, float ea, bool anticlockwise)
145 // http://bugs.webkit.org/show_bug.cgi?id=16449
146 // cairo_arc() functions hang or crash when passed inf as radius or start/end angle
147 if (!isfinite(r) || !isfinite(sa) || !isfinite(ea))
150 cairo_t* cr = platformPath()->m_cr;
152 cairo_arc_negative(cr, p.x(), p.y(), r, sa, ea);
154 cairo_arc(cr, p.x(), p.y(), r, sa, ea);
157 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
162 cairo_t* cr = platformPath()->m_cr;
165 cairo_get_current_point(cr, &x0, &y0);
166 FloatPoint p0(x0, y0);
167 if ((p1.x() == p0.x() && p1.y() == p0.y()) || (p1.x() == p2.x() && p1.y() == p2.y()) || radius == 0.f) {
168 cairo_line_to(cr, p1.x(), p1.y());
172 FloatPoint p1p0((p0.x() - p1.x()),(p0.y() - p1.y()));
173 FloatPoint p1p2((p2.x() - p1.x()),(p2.y() - p1.y()));
174 float p1p0_length = sqrtf(p1p0.x() * p1p0.x() + p1p0.y() * p1p0.y());
175 float p1p2_length = sqrtf(p1p2.x() * p1p2.x() + p1p2.y() * p1p2.y());
177 double cos_phi = (p1p0.x() * p1p2.x() + p1p0.y() * p1p2.y()) / (p1p0_length * p1p2_length);
178 // all points on a line logic
180 cairo_line_to(cr, p1.x(), p1.y());
184 // add infinite far away point
185 unsigned int max_length = 65535;
186 double factor_max = max_length / p1p0_length;
187 FloatPoint ep((p0.x() + factor_max * p1p0.x()), (p0.y() + factor_max * p1p0.y()));
188 cairo_line_to(cr, ep.x(), ep.y());
192 float tangent = radius / tan(acos(cos_phi) / 2);
193 float factor_p1p0 = tangent / p1p0_length;
194 FloatPoint t_p1p0((p1.x() + factor_p1p0 * p1p0.x()), (p1.y() + factor_p1p0 * p1p0.y()));
196 FloatPoint orth_p1p0(p1p0.y(), -p1p0.x());
197 float orth_p1p0_length = sqrt(orth_p1p0.x() * orth_p1p0.x() + orth_p1p0.y() * orth_p1p0.y());
198 float factor_ra = radius / orth_p1p0_length;
200 // angle between orth_p1p0 and p1p2 to get the right vector orthographic to p1p0
201 double cos_alpha = (orth_p1p0.x() * p1p2.x() + orth_p1p0.y() * p1p2.y()) / (orth_p1p0_length * p1p2_length);
203 orth_p1p0 = FloatPoint(-orth_p1p0.x(), -orth_p1p0.y());
205 FloatPoint p((t_p1p0.x() + factor_ra * orth_p1p0.x()), (t_p1p0.y() + factor_ra * orth_p1p0.y()));
207 // calculate angles for addArc
208 orth_p1p0 = FloatPoint(-orth_p1p0.x(), -orth_p1p0.y());
209 float sa = acos(orth_p1p0.x() / orth_p1p0_length);
210 if (orth_p1p0.y() < 0.f)
211 sa = 2 * piDouble - sa;
213 // anticlockwise logic
214 bool anticlockwise = false;
216 float factor_p1p2 = tangent / p1p2_length;
217 FloatPoint t_p1p2((p1.x() + factor_p1p2 * p1p2.x()), (p1.y() + factor_p1p2 * p1p2.y()));
218 FloatPoint orth_p1p2((t_p1p2.x() - p.x()),(t_p1p2.y() - p.y()));
219 float orth_p1p2_length = sqrtf(orth_p1p2.x() * orth_p1p2.x() + orth_p1p2.y() * orth_p1p2.y());
220 float ea = acos(orth_p1p2.x() / orth_p1p2_length);
221 if (orth_p1p2.y() < 0)
222 ea = 2 * piDouble - ea;
223 if ((sa > ea) && ((sa - ea) < piDouble))
224 anticlockwise = true;
225 if ((sa < ea) && ((ea - sa) > piDouble))
226 anticlockwise = true;
228 cairo_line_to(cr, t_p1p0.x(), t_p1p0.y());
230 addArc(p, radius, sa, ea, anticlockwise);
233 void Path::addEllipse(const FloatRect& rect)
235 cairo_t* cr = platformPath()->m_cr;
237 float yRadius = .5 * rect.height();
238 float xRadius = .5 * rect.width();
239 cairo_translate(cr, rect.x() + xRadius, rect.y() + yRadius);
240 cairo_scale(cr, xRadius, yRadius);
241 cairo_arc(cr, 0., 0., 1., 0., 2 * piDouble);
245 void Path::closeSubpath()
247 cairo_t* cr = platformPath()->m_cr;
248 cairo_close_path(cr);
251 FloatRect Path::boundingRect() const
253 cairo_t* cr = platformPath()->m_cr;
254 double x0, x1, y0, y1;
255 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
256 cairo_path_extents(cr, &x0, &y0, &x1, &y1);
258 cairo_stroke_extents(cr, &x0, &y0, &x1, &y1);
260 return FloatRect(x0, y0, x1 - x0, y1 - y0);
263 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier)
265 cairo_t* cr = platformPath()->m_cr;
267 GraphicsContext gc(cr);
268 applier->strokeStyle(&gc);
271 double x0, x1, y0, y1;
272 cairo_stroke_extents(cr, &x0, &y0, &x1, &y1);
273 return FloatRect(x0, y0, x1 - x0, y1 - y0);
276 bool Path::contains(const FloatPoint& point, WindRule rule) const
278 if (!boundingRect().contains(point))
281 cairo_t* cr = platformPath()->m_cr;
282 cairo_fill_rule_t cur = cairo_get_fill_rule(cr);
283 cairo_set_fill_rule(cr, rule == RULE_EVENODD ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
284 bool contains = cairo_in_fill(cr, point.x(), point.y());
285 cairo_set_fill_rule(cr, cur);
289 void Path::apply(void* info, PathApplierFunction function) const
291 cairo_t* cr = platformPath()->m_cr;
292 cairo_path_t* path = cairo_copy_path(cr);
293 cairo_path_data_t* data;
294 PathElement pelement;
295 FloatPoint points[3];
296 pelement.points = points;
298 for (int i = 0; i < path->num_data; i += path->data[i].header.length) {
299 data = &path->data[i];
300 switch (data->header.type) {
301 case CAIRO_PATH_MOVE_TO:
302 pelement.type = PathElementMoveToPoint;
303 pelement.points[0] = FloatPoint(data[1].point.x,data[1].point.y);
304 function(info, &pelement);
306 case CAIRO_PATH_LINE_TO:
307 pelement.type = PathElementAddLineToPoint;
308 pelement.points[0] = FloatPoint(data[1].point.x,data[1].point.y);
309 function(info, &pelement);
311 case CAIRO_PATH_CURVE_TO:
312 pelement.type = PathElementAddCurveToPoint;
313 pelement.points[0] = FloatPoint(data[1].point.x,data[1].point.y);
314 pelement.points[1] = FloatPoint(data[2].point.x,data[2].point.y);
315 pelement.points[2] = FloatPoint(data[3].point.x,data[3].point.y);
316 function(info, &pelement);
318 case CAIRO_PATH_CLOSE_PATH:
319 pelement.type = PathElementCloseSubpath;
320 function(info, &pelement);
324 cairo_path_destroy(path);
327 void Path::transform(const AffineTransform& trans)
329 cairo_t* m_cr = platformPath()->m_cr;
330 cairo_matrix_t c_matrix = cairo_matrix_t(trans);
331 cairo_matrix_invert(&c_matrix);
332 cairo_transform(m_cr, &c_matrix);
335 String Path::debugString() const
341 cairo_path_t* path = cairo_copy_path(platformPath()->m_cr);
342 cairo_path_data_t* data;
344 for (int i = 0; i < path->num_data; i += path->data[i].header.length) {
345 data = &path->data[i];
346 switch (data->header.type) {
347 case CAIRO_PATH_MOVE_TO:
348 if (i < (path->num_data - path->data[i].header.length))
349 pathString += String::format("M%.2f,%.2f ",
350 data[1].point.x, data[1].point.y);
352 case CAIRO_PATH_LINE_TO:
353 pathString += String::format("L%.2f,%.2f ",
354 data[1].point.x, data[1].point.y);
356 case CAIRO_PATH_CURVE_TO:
357 pathString += String::format("C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f ",
358 data[1].point.x, data[1].point.y,
359 data[2].point.x, data[2].point.y,
360 data[3].point.x, data[3].point.y);
362 case CAIRO_PATH_CLOSE_PATH:
368 cairo_path_destroy(path);
369 return pathString.simplifyWhiteSpace();
372 } // namespace WebCore