1 /* 2 Copyright (C) 2007 Krzysztof Kowalczyk <kkowalczyk (at) gmail.com> 3 Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <wildfox (at) kde.org> 4 2004, 2005, 2006 Rob Buis <buis (at) kde.org> 5 2005, 2007 Apple Inc. All Rights reserved. 6 2007 Alp Toker <alp (at) atoker.com> 7 2008 Dirk Schulze <krit (at) webkit.org> 8 2011 Igalia S.L. 9 10 This library is free software; you can redistribute it and/or 11 modify it under the terms of the GNU Library General Public 12 License as published by the Free Software Foundation; either 13 version 2 of the License, or (at your option) any later version. 14 15 This library is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 Library General Public License for more details. 19 20 You should have received a copy of the GNU Library General Public License 21 aint with this library; see the file COPYING.LIB. If not, write to 22 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 23 Boston, MA 02110-1301, USA. 24 */ 25 26 #include "config.h" 27 #include "Path.h" 28 29 #include "AffineTransform.h" 30 #include "FloatRect.h" 31 #include "GraphicsContext.h" 32 #include "OwnPtrCairo.h" 33 #include "PlatformPathCairo.h" 34 #include "PlatformString.h" 35 #include "StrokeStyleApplier.h" 36 #include <cairo.h> 37 #include <math.h> 38 #include <wtf/MathExtras.h> 39 40 namespace WebCore { 41 42 Path::Path() 43 : m_path(new CairoPath()) 44 { 45 } 46 47 Path::~Path() 48 { 49 delete m_path; 50 } 51 52 Path::Path(const Path& other) 53 : m_path(new CairoPath()) 54 { 55 cairo_t* cr = platformPath()->context(); 56 OwnPtr<cairo_path_t> p(cairo_copy_path(other.platformPath()->context())); 57 cairo_append_path(cr, p.get()); 58 } 59 60 Path& Path::operator=(const Path& other) 61 { 62 if (&other == this) 63 return *this; 64 65 clear(); 66 cairo_t* cr = platformPath()->context(); 67 OwnPtr<cairo_path_t> p(cairo_copy_path(other.platformPath()->context())); 68 cairo_append_path(cr, p.get()); 69 return *this; 70 } 71 72 void Path::clear() 73 { 74 cairo_t* cr = platformPath()->context(); 75 cairo_new_path(cr); 76 } 77 78 bool Path::isEmpty() const 79 { 80 return !cairo_has_current_point(platformPath()->context()); 81 } 82 83 bool Path::hasCurrentPoint() const 84 { 85 return !isEmpty(); 86 } 87 88 FloatPoint Path::currentPoint() const 89 { 90 // FIXME: Is this the correct way? 91 double x; 92 double y; 93 cairo_get_current_point(platformPath()->context(), &x, &y); 94 return FloatPoint(x, y); 95 } 96 97 void Path::translate(const FloatSize& p) 98 { 99 cairo_t* cr = platformPath()->context(); 100 cairo_translate(cr, -p.width(), -p.height()); 101 } 102 103 void Path::moveTo(const FloatPoint& p) 104 { 105 cairo_t* cr = platformPath()->context(); 106 cairo_move_to(cr, p.x(), p.y()); 107 } 108 109 void Path::addLineTo(const FloatPoint& p) 110 { 111 cairo_t* cr = platformPath()->context(); 112 cairo_line_to(cr, p.x(), p.y()); 113 } 114 115 void Path::addRect(const FloatRect& rect) 116 { 117 cairo_t* cr = platformPath()->context(); 118 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); 119 } 120 121 /* 122 * inspired by libsvg-cairo 123 */ 124 void Path::addQuadCurveTo(const FloatPoint& controlPoint, const FloatPoint& point) 125 { 126 cairo_t* cr = platformPath()->context(); 127 double x, y; 128 double x1 = controlPoint.x(); 129 double y1 = controlPoint.y(); 130 double x2 = point.x(); 131 double y2 = point.y(); 132 cairo_get_current_point(cr, &x, &y); 133 cairo_curve_to(cr, 134 x + 2.0 / 3.0 * (x1 - x), y + 2.0 / 3.0 * (y1 - y), 135 x2 + 2.0 / 3.0 * (x1 - x2), y2 + 2.0 / 3.0 * (y1 - y2), 136 x2, y2); 137 } 138 139 void Path::addBezierCurveTo(const FloatPoint& controlPoint1, const FloatPoint& controlPoint2, const FloatPoint& controlPoint3) 140 { 141 cairo_t* cr = platformPath()->context(); 142 cairo_curve_to(cr, controlPoint1.x(), controlPoint1.y(), 143 controlPoint2.x(), controlPoint2.y(), 144 controlPoint3.x(), controlPoint3.y()); 145 } 146 147 void Path::addArc(const FloatPoint& p, float r, float startAngle, float endAngle, bool anticlockwise) 148 { 149 // http://bugs.webkit.org/show_bug.cgi?id=16449 150 // cairo_arc() functions hang or crash when passed inf as radius or start/end angle 151 if (!isfinite(r) || !isfinite(startAngle) || !isfinite(endAngle)) 152 return; 153 154 cairo_t* cr = platformPath()->context(); 155 float sweep = endAngle - startAngle; 156 const float twoPI = 2 * piFloat; 157 if ((sweep <= -twoPI || sweep >= twoPI) 158 && ((anticlockwise && (endAngle < startAngle)) || (!anticlockwise && (startAngle < endAngle)))) { 159 if (anticlockwise) 160 cairo_arc_negative(cr, p.x(), p.y(), r, startAngle, startAngle - twoPI); 161 else 162 cairo_arc(cr, p.x(), p.y(), r, startAngle, startAngle + twoPI); 163 cairo_new_sub_path(cr); 164 cairo_arc(cr, p.x(), p.y(), r, endAngle, endAngle); 165 } else { 166 if (anticlockwise) 167 cairo_arc_negative(cr, p.x(), p.y(), r, startAngle, endAngle); 168 else 169 cairo_arc(cr, p.x(), p.y(), r, startAngle, endAngle); 170 } 171 } 172 173 static inline float areaOfTriangleFormedByPoints(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3) 174 { 175 return p1.x() * (p2.y() - p3.y()) + p2.x() * (p3.y() - p1.y()) + p3.x() * (p1.y() - p2.y()); 176 } 177 178 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius) 179 { 180 if (isEmpty()) 181 return; 182 183 cairo_t* cr = platformPath()->context(); 184 185 double x0, y0; 186 cairo_get_current_point(cr, &x0, &y0); 187 FloatPoint p0(x0, y0); 188 189 // Draw only a straight line to p1 if any of the points are equal or the radius is zero 190 // or the points are collinear (triangle that the points form has area of zero value). 191 if ((p1.x() == p0.x() && p1.y() == p0.y()) || (p1.x() == p2.x() && p1.y() == p2.y()) || !radius 192 || !areaOfTriangleFormedByPoints(p0, p1, p2)) { 193 cairo_line_to(cr, p1.x(), p1.y()); 194 return; 195 } 196 197 FloatPoint p1p0((p0.x() - p1.x()),(p0.y() - p1.y())); 198 FloatPoint p1p2((p2.x() - p1.x()),(p2.y() - p1.y())); 199 float p1p0_length = sqrtf(p1p0.x() * p1p0.x() + p1p0.y() * p1p0.y()); 200 float p1p2_length = sqrtf(p1p2.x() * p1p2.x() + p1p2.y() * p1p2.y()); 201 202 double cos_phi = (p1p0.x() * p1p2.x() + p1p0.y() * p1p2.y()) / (p1p0_length * p1p2_length); 203 // all points on a line logic 204 if (cos_phi == -1) { 205 cairo_line_to(cr, p1.x(), p1.y()); 206 return; 207 } 208 if (cos_phi == 1) { 209 // add infinite far away point 210 unsigned int max_length = 65535; 211 double factor_max = max_length / p1p0_length; 212 FloatPoint ep((p0.x() + factor_max * p1p0.x()), (p0.y() + factor_max * p1p0.y())); 213 cairo_line_to(cr, ep.x(), ep.y()); 214 return; 215 } 216 217 float tangent = radius / tan(acos(cos_phi) / 2); 218 float factor_p1p0 = tangent / p1p0_length; 219 FloatPoint t_p1p0((p1.x() + factor_p1p0 * p1p0.x()), (p1.y() + factor_p1p0 * p1p0.y())); 220 221 FloatPoint orth_p1p0(p1p0.y(), -p1p0.x()); 222 float orth_p1p0_length = sqrt(orth_p1p0.x() * orth_p1p0.x() + orth_p1p0.y() * orth_p1p0.y()); 223 float factor_ra = radius / orth_p1p0_length; 224 225 // angle between orth_p1p0 and p1p2 to get the right vector orthographic to p1p0 226 double cos_alpha = (orth_p1p0.x() * p1p2.x() + orth_p1p0.y() * p1p2.y()) / (orth_p1p0_length * p1p2_length); 227 if (cos_alpha < 0.f) 228 orth_p1p0 = FloatPoint(-orth_p1p0.x(), -orth_p1p0.y()); 229 230 FloatPoint p((t_p1p0.x() + factor_ra * orth_p1p0.x()), (t_p1p0.y() + factor_ra * orth_p1p0.y())); 231 232 // calculate angles for addArc 233 orth_p1p0 = FloatPoint(-orth_p1p0.x(), -orth_p1p0.y()); 234 float sa = acos(orth_p1p0.x() / orth_p1p0_length); 235 if (orth_p1p0.y() < 0.f) 236 sa = 2 * piDouble - sa; 237 238 // anticlockwise logic 239 bool anticlockwise = false; 240 241 float factor_p1p2 = tangent / p1p2_length; 242 FloatPoint t_p1p2((p1.x() + factor_p1p2 * p1p2.x()), (p1.y() + factor_p1p2 * p1p2.y())); 243 FloatPoint orth_p1p2((t_p1p2.x() - p.x()),(t_p1p2.y() - p.y())); 244 float orth_p1p2_length = sqrtf(orth_p1p2.x() * orth_p1p2.x() + orth_p1p2.y() * orth_p1p2.y()); 245 float ea = acos(orth_p1p2.x() / orth_p1p2_length); 246 if (orth_p1p2.y() < 0) 247 ea = 2 * piDouble - ea; 248 if ((sa > ea) && ((sa - ea) < piDouble)) 249 anticlockwise = true; 250 if ((sa < ea) && ((ea - sa) > piDouble)) 251 anticlockwise = true; 252 253 cairo_line_to(cr, t_p1p0.x(), t_p1p0.y()); 254 255 addArc(p, radius, sa, ea, anticlockwise); 256 } 257 258 void Path::addEllipse(const FloatRect& rect) 259 { 260 cairo_t* cr = platformPath()->context(); 261 cairo_save(cr); 262 float yRadius = .5 * rect.height(); 263 float xRadius = .5 * rect.width(); 264 cairo_translate(cr, rect.x() + xRadius, rect.y() + yRadius); 265 cairo_scale(cr, xRadius, yRadius); 266 cairo_arc(cr, 0., 0., 1., 0., 2 * piDouble); 267 cairo_restore(cr); 268 } 269 270 void Path::closeSubpath() 271 { 272 cairo_t* cr = platformPath()->context(); 273 cairo_close_path(cr); 274 } 275 276 FloatRect Path::boundingRect() const 277 { 278 cairo_t* cr = platformPath()->context(); 279 double x0, x1, y0, y1; 280 cairo_path_extents(cr, &x0, &y0, &x1, &y1); 281 return FloatRect(x0, y0, x1 - x0, y1 - y0); 282 } 283 284 FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier) const 285 { 286 cairo_t* cr = platformPath()->context(); 287 if (applier) { 288 GraphicsContext gc(cr); 289 applier->strokeStyle(&gc); 290 } 291 292 double x0, x1, y0, y1; 293 cairo_stroke_extents(cr, &x0, &y0, &x1, &y1); 294 return FloatRect(x0, y0, x1 - x0, y1 - y0); 295 } 296 297 bool Path::contains(const FloatPoint& point, WindRule rule) const 298 { 299 if (!isfinite(point.x()) || !isfinite(point.y())) 300 return false; 301 cairo_t* cr = platformPath()->context(); 302 cairo_fill_rule_t cur = cairo_get_fill_rule(cr); 303 cairo_set_fill_rule(cr, rule == RULE_EVENODD ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING); 304 bool contains = cairo_in_fill(cr, point.x(), point.y()); 305 cairo_set_fill_rule(cr, cur); 306 return contains; 307 } 308 309 bool Path::strokeContains(StrokeStyleApplier* applier, const FloatPoint& point) const 310 { 311 ASSERT(applier); 312 cairo_t* cr = platformPath()->context(); 313 GraphicsContext gc(cr); 314 applier->strokeStyle(&gc); 315 316 return cairo_in_stroke(cr, point.x(), point.y()); 317 } 318 319 void Path::apply(void* info, PathApplierFunction function) const 320 { 321 cairo_t* cr = platformPath()->context(); 322 OwnPtr<cairo_path_t> path(cairo_copy_path(cr)); 323 cairo_path_data_t* data; 324 PathElement pelement; 325 FloatPoint points[3]; 326 pelement.points = points; 327 328 for (int i = 0; i < path->num_data; i += path->data[i].header.length) { 329 data = &path->data[i]; 330 switch (data->header.type) { 331 case CAIRO_PATH_MOVE_TO: 332 pelement.type = PathElementMoveToPoint; 333 pelement.points[0] = FloatPoint(data[1].point.x,data[1].point.y); 334 function(info, &pelement); 335 break; 336 case CAIRO_PATH_LINE_TO: 337 pelement.type = PathElementAddLineToPoint; 338 pelement.points[0] = FloatPoint(data[1].point.x,data[1].point.y); 339 function(info, &pelement); 340 break; 341 case CAIRO_PATH_CURVE_TO: 342 pelement.type = PathElementAddCurveToPoint; 343 pelement.points[0] = FloatPoint(data[1].point.x,data[1].point.y); 344 pelement.points[1] = FloatPoint(data[2].point.x,data[2].point.y); 345 pelement.points[2] = FloatPoint(data[3].point.x,data[3].point.y); 346 function(info, &pelement); 347 break; 348 case CAIRO_PATH_CLOSE_PATH: 349 pelement.type = PathElementCloseSubpath; 350 function(info, &pelement); 351 break; 352 } 353 } 354 } 355 356 void Path::transform(const AffineTransform& trans) 357 { 358 cairo_t* cr = platformPath()->context(); 359 cairo_matrix_t c_matrix = cairo_matrix_t(trans); 360 cairo_matrix_invert(&c_matrix); 361 cairo_transform(cr, &c_matrix); 362 } 363 364 } // namespace WebCore 365