Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-path-stroke.c
1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
3  *
4  * Copyright © 2002 University of Southern California
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  * The Original Code is the cairo graphics library.
30  *
31  * The Initial Developer of the Original Code is University of Southern
32  * California.
33  *
34  * Contributor(s):
35  *      Carl D. Worth <cworth@cworth.org>
36  *      Chris Wilson <chris@chris-wilson.co.uk>
37  */
38
39 #define _BSD_SOURCE /* for hypot() */
40 #include "cairoint.h"
41
42 #include "cairo-box-inline.h"
43 #include "cairo-boxes-private.h"
44 #include "cairo-error-private.h"
45 #include "cairo-path-fixed-private.h"
46 #include "cairo-slope-private.h"
47 #include "cairo-stroke-dash-private.h"
48 #include "cairo-traps-private.h"
49
50 typedef struct cairo_stroker {
51     cairo_stroke_style_t style;
52
53     const cairo_matrix_t *ctm;
54     const cairo_matrix_t *ctm_inverse;
55     double half_line_width;
56     double tolerance;
57     double ctm_determinant;
58     cairo_bool_t ctm_det_positive;
59
60     void *closure;
61     cairo_status_t (*add_external_edge) (void *closure,
62                                          const cairo_point_t *p1,
63                                          const cairo_point_t *p2);
64     cairo_status_t (*add_triangle) (void *closure,
65                                     const cairo_point_t triangle[3]);
66     cairo_status_t (*add_triangle_fan) (void *closure,
67                                         const cairo_point_t *midpt,
68                                         const cairo_point_t *points,
69                                         int npoints);
70     cairo_status_t (*add_convex_quad) (void *closure,
71                                        const cairo_point_t quad[4]);
72
73     cairo_pen_t   pen;
74
75     cairo_point_t current_point;
76     cairo_point_t first_point;
77
78     cairo_bool_t has_initial_sub_path;
79
80     cairo_bool_t has_current_face;
81     cairo_stroke_face_t current_face;
82
83     cairo_bool_t has_first_face;
84     cairo_stroke_face_t first_face;
85
86     cairo_stroker_dash_t dash;
87
88     cairo_bool_t has_bounds;
89     cairo_box_t bounds;
90 } cairo_stroker_t;
91
92 static cairo_bool_t
93 _cairo_stroke_segment_intersect (cairo_point_t *p1, cairo_point_t *p2,
94                  cairo_point_t *p3, cairo_point_t *p4,
95                                  cairo_point_t *p)
96 {
97     double x1, y1, x2, y2, x3, y3, x4, y4;
98     double pre, post;
99     double x, y, d;
100
101     x1 = _cairo_fixed_to_double (p1->x);
102     y1 = _cairo_fixed_to_double (p1->y);
103     x2 = _cairo_fixed_to_double (p2->x);
104     y2 = _cairo_fixed_to_double (p2->y);
105     x3 = _cairo_fixed_to_double (p3->x);
106     y3 = _cairo_fixed_to_double (p3->y);
107     x4 = _cairo_fixed_to_double (p4->x);
108     y4 = _cairo_fixed_to_double (p4->y);
109
110     d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
111     if (d == 0)
112         return FALSE;
113
114     pre = x1 * y2 - y1 * x2;
115     post = x3 * y4 - y3 * x4;
116     x = (pre * (x3 - x4) - (x1 - x2) * post) / d;
117     y = (pre * (y3 - y4) - (y1 - y2) * post) / d;
118
119     /* check if x, y are within both segments */
120     if (x < MIN (x1, x2) || x > MAX (x1, x2) ||
121         x < MIN (x3, x4) || x > MAX (x3, x4))
122     return FALSE;
123     if (y < MIN (y1, y2) || y > MAX (y1, y2) ||
124         y < MIN (y1, y2) || y > MAX (y3, y4))
125     return FALSE;
126
127     p->x = _cairo_fixed_from_double (x);
128     p->y = _cairo_fixed_from_double (y);
129     return TRUE;
130 }
131
132 static void
133 _cairo_stroker_limit (cairo_stroker_t *stroker,
134                       const cairo_path_fixed_t *path,
135                       const cairo_box_t *boxes,
136                       int num_boxes)
137 {
138     double dx, dy;
139     cairo_fixed_t fdx, fdy;
140
141     stroker->has_bounds = TRUE;
142     _cairo_boxes_get_extents (boxes, num_boxes, &stroker->bounds);
143
144     /* Extend the bounds in each direction to account for the maximum area
145      * we might generate trapezoids, to capture line segments that are outside
146      * of the bounds but which might generate rendering that's within bounds.
147      */
148
149     _cairo_stroke_style_max_distance_from_path (&stroker->style, path,
150                                                 stroker->ctm, &dx, &dy);
151
152     fdx = _cairo_fixed_from_double (dx);
153     fdy = _cairo_fixed_from_double (dy);
154
155     stroker->bounds.p1.x -= fdx;
156     stroker->bounds.p2.x += fdx;
157
158     stroker->bounds.p1.y -= fdy;
159     stroker->bounds.p2.y += fdy;
160 }
161
162 static cairo_status_t
163 _cairo_stroker_init (cairo_stroker_t            *stroker,
164                      const cairo_path_fixed_t   *path,
165                      const cairo_stroke_style_t *stroke_style,
166                      const cairo_matrix_t       *ctm,
167                      const cairo_matrix_t       *ctm_inverse,
168                      double                      tolerance,
169                      const cairo_box_t          *limits,
170                      int                         num_limits)
171 {
172     cairo_status_t status;
173
174     stroker->style = *stroke_style;
175     stroker->ctm = ctm;
176     stroker->ctm_inverse = ctm_inverse;
177     stroker->tolerance = tolerance;
178     stroker->half_line_width = stroke_style->line_width / 2.0;
179
180     stroker->ctm_determinant = _cairo_matrix_compute_determinant (stroker->ctm);
181     stroker->ctm_det_positive = stroker->ctm_determinant >= 0.0;
182
183     status = _cairo_pen_init (&stroker->pen,
184                               stroker->half_line_width, tolerance, ctm);
185     if (unlikely (status))
186         return status;
187
188     stroker->has_current_face = FALSE;
189     stroker->has_first_face = FALSE;
190     stroker->has_initial_sub_path = FALSE;
191
192     _cairo_stroker_dash_init (&stroker->dash, stroke_style);
193
194     stroker->add_external_edge = NULL;
195
196     stroker->has_bounds = FALSE;
197     if (num_limits)
198         _cairo_stroker_limit (stroker, path, limits, num_limits);
199
200     return CAIRO_STATUS_SUCCESS;
201 }
202
203 static void
204 _cairo_stroker_fini (cairo_stroker_t *stroker)
205 {
206     _cairo_pen_fini (&stroker->pen);
207 }
208
209 static void
210 _translate_point (cairo_point_t *point, const cairo_point_t *offset)
211 {
212     point->x += offset->x;
213     point->y += offset->y;
214 }
215
216 static int
217 _cairo_stroker_join_is_clockwise (const cairo_stroke_face_t *in,
218                                   const cairo_stroke_face_t *out)
219 {
220     cairo_slope_t in_slope, out_slope;
221
222     _cairo_slope_init (&in_slope, &in->point, &in->cw);
223     _cairo_slope_init (&out_slope, &out->point, &out->cw);
224
225     return _cairo_slope_compare (&in_slope, &out_slope) < 0;
226 }
227
228 /**
229  * _cairo_slope_compare_sgn:
230  *
231  * Return -1, 0 or 1 depending on the relative slopes of
232  * two lines.
233  **/
234 static int
235 _cairo_slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
236 {
237     double  c = (dx1 * dy2 - dx2 * dy1);
238
239     if (c > 0) return 1;
240     if (c < 0) return -1;
241     return 0;
242 }
243
244 static inline int
245 _range_step (int i, int step, int max)
246 {
247     i += step;
248     if (i < 0)
249         i = max - 1;
250     if (i >= max)
251         i = 0;
252     return i;
253 }
254
255 /*
256  * Construct a fan around the midpoint using the vertices from pen between
257  * inpt and outpt.
258  */
259 static cairo_status_t
260 _tessellate_fan (cairo_stroker_t *stroker,
261                  const cairo_slope_t *in_vector,
262                  const cairo_slope_t *out_vector,
263                  const cairo_point_t *midpt,
264                  const cairo_point_t *inpt,
265                  const cairo_point_t *outpt,
266                  cairo_bool_t clockwise)
267 {
268     cairo_point_t stack_points[64], *points = stack_points;
269     cairo_pen_t *pen = &stroker->pen;
270     int start, stop, num_points = 0;
271     cairo_status_t status;
272
273     if (stroker->has_bounds &&
274         ! _cairo_box_contains_point (&stroker->bounds, midpt))
275         goto BEVEL;
276
277     assert (stroker->pen.num_vertices);
278
279     if (clockwise) {
280         _cairo_pen_find_active_ccw_vertices (pen,
281                                              in_vector, out_vector,
282                                              &start, &stop);
283         if (stroker->add_external_edge) {
284             cairo_point_t last;
285             last = *inpt;
286             while (start != stop) {
287                 cairo_point_t p = *midpt;
288                 _translate_point (&p, &pen->vertices[start].point);
289
290                 status = stroker->add_external_edge (stroker->closure,
291                                                      &last, &p);
292                 if (unlikely (status))
293                     return status;
294                 last = p;
295
296                 if (start-- == 0)
297                     start += pen->num_vertices;
298             }
299             status = stroker->add_external_edge (stroker->closure,
300                                                  &last, outpt);
301         } else {
302             if (start == stop)
303                 goto BEVEL;
304
305             num_points = stop - start;
306             if (num_points < 0)
307                 num_points += pen->num_vertices;
308             num_points += 2;
309             if (num_points > ARRAY_LENGTH(stack_points)) {
310                 points = _cairo_malloc_ab (num_points, sizeof (cairo_point_t));
311                 if (unlikely (points == NULL))
312                     return _cairo_error (CAIRO_STATUS_NO_MEMORY);
313             }
314
315             points[0] = *inpt;
316             num_points = 1;
317             while (start != stop) {
318                 points[num_points] = *midpt;
319                 _translate_point (&points[num_points], &pen->vertices[start].point);
320                 num_points++;
321
322                 if (start-- == 0)
323                     start += pen->num_vertices;
324             }
325             points[num_points++] = *outpt;
326         }
327     } else {
328         _cairo_pen_find_active_cw_vertices (pen,
329                                             in_vector, out_vector,
330                                             &start, &stop);
331         if (stroker->add_external_edge) {
332             cairo_point_t last;
333             last = *inpt;
334             while (start != stop) {
335                 cairo_point_t p = *midpt;
336                 _translate_point (&p, &pen->vertices[start].point);
337
338                 status = stroker->add_external_edge (stroker->closure,
339                                                      &p, &last);
340                 if (unlikely (status))
341                     return status;
342                 last = p;
343
344                 if (++start == pen->num_vertices)
345                     start = 0;
346             }
347             status = stroker->add_external_edge (stroker->closure,
348                                                  outpt, &last);
349         } else {
350             if (start == stop)
351                 goto BEVEL;
352
353             num_points = stop - start;
354             if (num_points < 0)
355                 num_points += pen->num_vertices;
356             num_points += 2;
357             if (num_points > ARRAY_LENGTH(stack_points)) {
358                 points = _cairo_malloc_ab (num_points, sizeof (cairo_point_t));
359                 if (unlikely (points == NULL))
360                     return _cairo_error (CAIRO_STATUS_NO_MEMORY);
361             }
362
363             points[0] = *inpt;
364             num_points = 1;
365             while (start != stop) {
366                 points[num_points] = *midpt;
367                 _translate_point (&points[num_points], &pen->vertices[start].point);
368                 num_points++;
369
370                 if (++start == pen->num_vertices)
371                     start = 0;
372             }
373             points[num_points++] = *outpt;
374         }
375     }
376
377     if (num_points) {
378         status = stroker->add_triangle_fan (stroker->closure,
379                                             midpt, points, num_points);
380     }
381
382     if (points != stack_points)
383         free (points);
384
385     return status;
386
387 BEVEL:
388     /* Ensure a leak free connection... */
389     if (stroker->add_external_edge != NULL) {
390         if (clockwise)
391             return stroker->add_external_edge (stroker->closure, inpt, outpt);
392         else
393             return stroker->add_external_edge (stroker->closure, outpt, inpt);
394     } else {
395         stack_points[0] = *midpt;
396         stack_points[1] = *inpt;
397         stack_points[2] = *outpt;
398         return stroker->add_triangle (stroker->closure, stack_points);
399     }
400 }
401
402 static cairo_status_t
403 _cairo_stroker_join (cairo_stroker_t *stroker,
404                      const cairo_stroke_face_t *in,
405                      const cairo_stroke_face_t *out)
406 {
407     int  clockwise = _cairo_stroker_join_is_clockwise (out, in);
408     const cairo_point_t *inpt, *outpt;
409     cairo_point_t points[4];
410     cairo_status_t status;
411
412     if (in->cw.x  == out->cw.x  && in->cw.y  == out->cw.y &&
413         in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
414     {
415         return CAIRO_STATUS_SUCCESS;
416     }
417
418     if (clockwise) {
419         if (stroker->add_external_edge != NULL) {
420             status = stroker->add_external_edge (stroker->closure,
421                                                  &out->cw, &in->point);
422             if (unlikely (status))
423                 return status;
424
425             status = stroker->add_external_edge (stroker->closure,
426                                                  &in->point, &in->cw);
427             if (unlikely (status))
428                 return status;
429         }
430
431         inpt = &in->ccw;
432         outpt = &out->ccw;
433     } else {
434         if (stroker->add_external_edge != NULL) {
435             status = stroker->add_external_edge (stroker->closure,
436                                                  &in->ccw, &in->point);
437             if (unlikely (status))
438                 return status;
439
440             status = stroker->add_external_edge (stroker->closure,
441                                                  &in->point, &out->ccw);
442             if (unlikely (status))
443                 return status;
444         }
445
446         inpt = &in->cw;
447         outpt = &out->cw;
448     }
449
450     switch (stroker->style.line_join) {
451     case CAIRO_LINE_JOIN_ROUND:
452         /* construct a fan around the common midpoint */
453         return _tessellate_fan (stroker,
454                                 &in->dev_vector,
455                                 &out->dev_vector,
456                                 &in->point, inpt, outpt,
457                                 clockwise);
458
459     case CAIRO_LINE_JOIN_MITER:
460     default: {
461         /* dot product of incoming slope vector with outgoing slope vector */
462         double  in_dot_out = -in->usr_vector.x * out->usr_vector.x +
463                              -in->usr_vector.y * out->usr_vector.y;
464         double  ml = stroker->style.miter_limit;
465
466         /* Check the miter limit -- lines meeting at an acute angle
467          * can generate long miters, the limit converts them to bevel
468          *
469          * Consider the miter join formed when two line segments
470          * meet at an angle psi:
471          *
472          *         /.\
473          *        /. .\
474          *       /./ \.\
475          *      /./psi\.\
476          *
477          * We can zoom in on the right half of that to see:
478          *
479          *          |\
480          *          | \ psi/2
481          *          |  \
482          *          |   \
483          *          |    \
484          *          |     \
485          *        miter    \
486          *       length     \
487          *          |        \
488          *          |        .\
489          *          |    .     \
490          *          |.   line   \
491          *           \    width  \
492          *            \           \
493          *
494          *
495          * The right triangle in that figure, (the line-width side is
496          * shown faintly with three '.' characters), gives us the
497          * following expression relating miter length, angle and line
498          * width:
499          *
500          *      1 /sin (psi/2) = miter_length / line_width
501          *
502          * The right-hand side of this relationship is the same ratio
503          * in which the miter limit (ml) is expressed. We want to know
504          * when the miter length is within the miter limit. That is
505          * when the following condition holds:
506          *
507          *      1/sin(psi/2) <= ml
508          *      1 <= ml sin(psi/2)
509          *      1 <= ml² sin²(psi/2)
510          *      2 <= ml² 2 sin²(psi/2)
511          *                              2·sin²(psi/2) = 1-cos(psi)
512          *      2 <= ml² (1-cos(psi))
513          *
514          *                              in · out = |in| |out| cos (psi)
515          *
516          * in and out are both unit vectors, so:
517          *
518          *                              in · out = cos (psi)
519          *
520          *      2 <= ml² (1 - in · out)
521          *
522          */
523         if (2 <= ml * ml * (1 - in_dot_out)) {
524             double              x1, y1, x2, y2;
525             double              mx, my;
526             double              dx1, dx2, dy1, dy2;
527             double              ix, iy;
528             double              fdx1, fdy1, fdx2, fdy2;
529             double              mdx, mdy;
530
531             /*
532              * we've got the points already transformed to device
533              * space, but need to do some computation with them and
534              * also need to transform the slope from user space to
535              * device space
536              */
537             /* outer point of incoming line face */
538             x1 = _cairo_fixed_to_double (inpt->x);
539             y1 = _cairo_fixed_to_double (inpt->y);
540             dx1 = in->usr_vector.x;
541             dy1 = in->usr_vector.y;
542             cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
543
544             /* outer point of outgoing line face */
545             x2 = _cairo_fixed_to_double (outpt->x);
546             y2 = _cairo_fixed_to_double (outpt->y);
547             dx2 = out->usr_vector.x;
548             dy2 = out->usr_vector.y;
549             cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
550
551             /*
552              * Compute the location of the outer corner of the miter.
553              * That's pretty easy -- just the intersection of the two
554              * outer edges.  We've got slopes and points on each
555              * of those edges.  Compute my directly, then compute
556              * mx by using the edge with the larger dy; that avoids
557              * dividing by values close to zero.
558              */
559             my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
560                   (dx1 * dy2 - dx2 * dy1));
561             if (fabs (dy1) >= fabs (dy2))
562                 mx = (my - y1) * dx1 / dy1 + x1;
563             else
564                 mx = (my - y2) * dx2 / dy2 + x2;
565
566             /*
567              * When the two outer edges are nearly parallel, slight
568              * perturbations in the position of the outer points of the lines
569              * caused by representing them in fixed point form can cause the
570              * intersection point of the miter to move a large amount. If
571              * that moves the miter intersection from between the two faces,
572              * then draw a bevel instead.
573              */
574
575             ix = _cairo_fixed_to_double (in->point.x);
576             iy = _cairo_fixed_to_double (in->point.y);
577
578             /* slope of one face */
579             fdx1 = x1 - ix; fdy1 = y1 - iy;
580
581             /* slope of the other face */
582             fdx2 = x2 - ix; fdy2 = y2 - iy;
583
584             /* slope from the intersection to the miter point */
585             mdx = mx - ix; mdy = my - iy;
586
587             /*
588              * Make sure the miter point line lies between the two
589              * faces by comparing the slopes
590              */
591             if (_cairo_slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
592                 _cairo_slope_compare_sgn (fdx2, fdy2, mdx, mdy))
593             {
594                 if (stroker->add_external_edge != NULL) {
595                     points[0].x = _cairo_fixed_from_double (mx);
596                     points[0].y = _cairo_fixed_from_double (my);
597
598                     if (clockwise) {
599                         status = stroker->add_external_edge (stroker->closure,
600                                                              inpt, &points[0]);
601                         if (unlikely (status))
602                             return status;
603
604                         status = stroker->add_external_edge (stroker->closure,
605                                                              &points[0], outpt);
606                         if (unlikely (status))
607                             return status;
608                     } else {
609                         status = stroker->add_external_edge (stroker->closure,
610                                                              outpt, &points[0]);
611                         if (unlikely (status))
612                             return status;
613
614                         status = stroker->add_external_edge (stroker->closure,
615                                                              &points[0], inpt);
616                         if (unlikely (status))
617                             return status;
618                     }
619
620                     return CAIRO_STATUS_SUCCESS;
621                 } else {
622                     points[0] = in->point;
623                     points[1] = *inpt;
624                     points[2].x = _cairo_fixed_from_double (mx);
625                     points[2].y = _cairo_fixed_from_double (my);
626                     points[3] = *outpt;
627
628                     return stroker->add_convex_quad (stroker->closure, points);
629                 }
630             }
631         }
632     }
633
634     /* fall through ... */
635
636     case CAIRO_LINE_JOIN_BEVEL:
637         if (stroker->add_external_edge != NULL) {
638             if (clockwise) {
639                 return stroker->add_external_edge (stroker->closure,
640                                                    inpt, outpt);
641             } else {
642                 return stroker->add_external_edge (stroker->closure,
643                                                    outpt, inpt);
644             }
645         } else {
646             points[0] = in->point;
647             points[1] = *inpt;
648             points[2] = *outpt;
649
650             return stroker->add_triangle (stroker->closure, points);
651         }
652     }
653 }
654
655 static cairo_status_t
656 _cairo_stroker_add_cap (cairo_stroker_t *stroker,
657                         const cairo_stroke_face_t *f)
658 {
659     switch (stroker->style.line_cap) {
660     case CAIRO_LINE_CAP_ROUND: {
661         cairo_slope_t slope;
662
663         slope.dx = -f->dev_vector.dx;
664         slope.dy = -f->dev_vector.dy;
665
666         return _tessellate_fan (stroker,
667                                 &f->dev_vector,
668                                 &slope,
669                                 &f->point, &f->cw, &f->ccw,
670                                 FALSE);
671
672     }
673
674     case CAIRO_LINE_CAP_SQUARE: {
675         double dx, dy;
676         cairo_slope_t   fvector;
677         cairo_point_t   quad[4];
678
679         dx = f->usr_vector.x;
680         dy = f->usr_vector.y;
681         dx *= stroker->half_line_width;
682         dy *= stroker->half_line_width;
683         cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
684         fvector.dx = _cairo_fixed_from_double (dx);
685         fvector.dy = _cairo_fixed_from_double (dy);
686
687         quad[0] = f->ccw;
688         quad[1].x = f->ccw.x + fvector.dx;
689         quad[1].y = f->ccw.y + fvector.dy;
690         quad[2].x = f->cw.x + fvector.dx;
691         quad[2].y = f->cw.y + fvector.dy;
692         quad[3] = f->cw;
693
694         if (stroker->add_external_edge != NULL) {
695             cairo_status_t status;
696
697             status = stroker->add_external_edge (stroker->closure,
698                                                  &quad[0], &quad[1]);
699             if (unlikely (status))
700                 return status;
701
702             status = stroker->add_external_edge (stroker->closure,
703                                                  &quad[1], &quad[2]);
704             if (unlikely (status))
705                 return status;
706
707             status = stroker->add_external_edge (stroker->closure,
708                                                  &quad[2], &quad[3]);
709             if (unlikely (status))
710                 return status;
711
712             return CAIRO_STATUS_SUCCESS;
713         } else {
714             return stroker->add_convex_quad (stroker->closure, quad);
715         }
716     }
717
718     case CAIRO_LINE_CAP_BUTT:
719     default:
720         if (stroker->add_external_edge != NULL) {
721             return stroker->add_external_edge (stroker->closure,
722                                                &f->ccw, &f->cw);
723         } else {
724             return CAIRO_STATUS_SUCCESS;
725         }
726     }
727 }
728
729 static cairo_status_t
730 _cairo_stroker_add_leading_cap (cairo_stroker_t     *stroker,
731                                 const cairo_stroke_face_t *face)
732 {
733     cairo_stroke_face_t reversed;
734     cairo_point_t t;
735
736     reversed = *face;
737
738     /* The initial cap needs an outward facing vector. Reverse everything */
739     reversed.usr_vector.x = -reversed.usr_vector.x;
740     reversed.usr_vector.y = -reversed.usr_vector.y;
741     reversed.dev_vector.dx = -reversed.dev_vector.dx;
742     reversed.dev_vector.dy = -reversed.dev_vector.dy;
743     t = reversed.cw;
744     reversed.cw = reversed.ccw;
745     reversed.ccw = t;
746
747     return _cairo_stroker_add_cap (stroker, &reversed);
748 }
749
750 static cairo_status_t
751 _cairo_stroker_add_trailing_cap (cairo_stroker_t     *stroker,
752                                  const cairo_stroke_face_t *face)
753 {
754     return _cairo_stroker_add_cap (stroker, face);
755 }
756
757 static inline cairo_bool_t
758 _compute_normalized_device_slope (double *dx, double *dy,
759                                   const cairo_matrix_t *ctm_inverse,
760                                   double *mag_out)
761 {
762     double dx0 = *dx, dy0 = *dy;
763     double mag;
764
765     cairo_matrix_transform_distance (ctm_inverse, &dx0, &dy0);
766
767     if (dx0 == 0.0 && dy0 == 0.0) {
768         if (mag_out)
769             *mag_out = 0.0;
770         return FALSE;
771     }
772
773     if (dx0 == 0.0) {
774         *dx = 0.0;
775         if (dy0 > 0.0) {
776             mag = dy0;
777             *dy = 1.0;
778         } else {
779             mag = -dy0;
780             *dy = -1.0;
781         }
782     } else if (dy0 == 0.0) {
783         *dy = 0.0;
784         if (dx0 > 0.0) {
785             mag = dx0;
786             *dx = 1.0;
787         } else {
788             mag = -dx0;
789             *dx = -1.0;
790         }
791     } else {
792         mag = hypot (dx0, dy0);
793         *dx = dx0 / mag;
794         *dy = dy0 / mag;
795     }
796
797     if (mag_out)
798         *mag_out = mag;
799
800     return TRUE;
801 }
802
803 static void
804 _compute_face (const cairo_point_t *point,
805                const cairo_slope_t *dev_slope,
806                double slope_dx,
807                double slope_dy,
808                cairo_stroker_t *stroker,
809                cairo_stroke_face_t *face)
810 {
811     double face_dx, face_dy;
812     cairo_point_t offset_ccw, offset_cw;
813
814     /*
815      * rotate to get a line_width/2 vector along the face, note that
816      * the vector must be rotated the right direction in device space,
817      * but by 90° in user space. So, the rotation depends on
818      * whether the ctm reflects or not, and that can be determined
819      * by looking at the determinant of the matrix.
820      */
821     if (stroker->ctm_det_positive)
822     {
823         face_dx = - slope_dy * stroker->half_line_width;
824         face_dy = slope_dx * stroker->half_line_width;
825     }
826     else
827     {
828         face_dx = slope_dy * stroker->half_line_width;
829         face_dy = - slope_dx * stroker->half_line_width;
830     }
831
832     /* back to device space */
833     cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
834
835     offset_ccw.x = _cairo_fixed_from_double (face_dx);
836     offset_ccw.y = _cairo_fixed_from_double (face_dy);
837     offset_cw.x = -offset_ccw.x;
838     offset_cw.y = -offset_ccw.y;
839
840     face->ccw = *point;
841     _translate_point (&face->ccw, &offset_ccw);
842
843     face->point = *point;
844
845     face->cw = *point;
846     _translate_point (&face->cw, &offset_cw);
847
848     face->usr_vector.x = slope_dx;
849     face->usr_vector.y = slope_dy;
850
851     face->dev_vector = *dev_slope;
852 }
853
854 static cairo_status_t
855 _cairo_stroker_add_caps (cairo_stroker_t *stroker)
856 {
857     cairo_status_t status;
858
859     /* check for a degenerative sub_path */
860     if (stroker->has_initial_sub_path
861         && ! stroker->has_first_face
862         && ! stroker->has_current_face
863         && stroker->style.line_cap == CAIRO_LINE_CAP_ROUND)
864     {
865         /* pick an arbitrary slope to use */
866         double dx = 1.0, dy = 0.0;
867         cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
868         cairo_stroke_face_t face;
869
870         _compute_normalized_device_slope (&dx, &dy,
871                                           stroker->ctm_inverse, NULL);
872
873         /* arbitrarily choose first_point
874          * first_point and current_point should be the same */
875         _compute_face (&stroker->first_point, &slope, dx, dy, stroker, &face);
876
877         status = _cairo_stroker_add_leading_cap (stroker, &face);
878         if (unlikely (status))
879             return status;
880
881         status = _cairo_stroker_add_trailing_cap (stroker, &face);
882         if (unlikely (status))
883             return status;
884     }
885
886     if (stroker->has_first_face) {
887         status = _cairo_stroker_add_leading_cap (stroker,
888                                                  &stroker->first_face);
889         if (unlikely (status))
890             return status;
891     }
892
893     if (stroker->has_current_face) {
894         status = _cairo_stroker_add_trailing_cap (stroker,
895                                                   &stroker->current_face);
896         if (unlikely (status))
897             return status;
898     }
899
900     return CAIRO_STATUS_SUCCESS;
901 }
902
903 static cairo_status_t
904 _cairo_stroker_add_sub_edge (cairo_stroker_t *stroker,
905                              const cairo_point_t *p1,
906                              const cairo_point_t *p2,
907                              cairo_slope_t *dev_slope,
908                              double slope_dx, double slope_dy,
909                              cairo_stroke_face_t *start,
910                              cairo_stroke_face_t *end)
911 {
912     _compute_face (p1, dev_slope, slope_dx, slope_dy, stroker, start);
913     *end = *start;
914
915     if (p1->x == p2->x && p1->y == p2->y)
916         return CAIRO_STATUS_SUCCESS;
917
918     end->point = *p2;
919     end->ccw.x += p2->x - p1->x;
920     end->ccw.y += p2->y - p1->y;
921     end->cw.x += p2->x - p1->x;
922     end->cw.y += p2->y - p1->y;
923
924     if (stroker->add_external_edge != NULL) {
925         cairo_status_t status;
926
927         status = stroker->add_external_edge (stroker->closure,
928                                              &end->cw, &start->cw);
929         if (unlikely (status))
930             return status;
931
932         status = stroker->add_external_edge (stroker->closure,
933                                              &start->ccw, &end->ccw);
934         if (unlikely (status))
935             return status;
936
937         return CAIRO_STATUS_SUCCESS;
938     } else {
939         cairo_point_t quad[4];
940
941         quad[0] = start->cw;
942         quad[1] = end->cw;
943         quad[2] = end->ccw;
944         quad[3] = start->ccw;
945
946         return stroker->add_convex_quad (stroker->closure, quad);
947     }
948 }
949
950 static cairo_status_t
951 _cairo_stroker_move_to (void *closure,
952                         const cairo_point_t *point)
953 {
954     cairo_stroker_t *stroker = closure;
955     cairo_status_t status;
956
957     /* reset the dash pattern for new sub paths */
958     _cairo_stroker_dash_start (&stroker->dash);
959
960     /* Cap the start and end of the previous sub path as needed */
961     status = _cairo_stroker_add_caps (stroker);
962     if (unlikely (status))
963         return status;
964
965     stroker->first_point = *point;
966     stroker->current_point = *point;
967
968     stroker->has_first_face = FALSE;
969     stroker->has_current_face = FALSE;
970     stroker->has_initial_sub_path = FALSE;
971
972     return CAIRO_STATUS_SUCCESS;
973 }
974
975 static cairo_status_t
976 _cairo_stroker_line_to (void *closure,
977                         const cairo_point_t *point)
978 {
979     cairo_stroker_t *stroker = closure;
980     cairo_stroke_face_t start, end;
981     cairo_point_t *p1 = &stroker->current_point;
982     cairo_slope_t dev_slope;
983     double slope_dx, slope_dy;
984     cairo_status_t status;
985
986     stroker->has_initial_sub_path = TRUE;
987
988     if (p1->x == point->x && p1->y == point->y)
989         return CAIRO_STATUS_SUCCESS;
990
991     _cairo_slope_init (&dev_slope, p1, point);
992     slope_dx = _cairo_fixed_to_double (point->x - p1->x);
993     slope_dy = _cairo_fixed_to_double (point->y - p1->y);
994     _compute_normalized_device_slope (&slope_dx, &slope_dy,
995                                       stroker->ctm_inverse, NULL);
996
997     status = _cairo_stroker_add_sub_edge (stroker,
998                                           p1, point,
999                                           &dev_slope,
1000                                           slope_dx, slope_dy,
1001                                           &start, &end);
1002     if (unlikely (status))
1003         return status;
1004
1005     if (stroker->has_current_face) {
1006         /* Join with final face from previous segment */
1007         status = _cairo_stroker_join (stroker,
1008                                       &stroker->current_face,
1009                                       &start);
1010         if (unlikely (status))
1011             return status;
1012     } else if (! stroker->has_first_face) {
1013         /* Save sub path's first face in case needed for closing join */
1014         stroker->first_face = start;
1015         stroker->has_first_face = TRUE;
1016     }
1017     stroker->current_face = end;
1018     stroker->has_current_face = TRUE;
1019
1020     stroker->current_point = *point;
1021
1022     return CAIRO_STATUS_SUCCESS;
1023 }
1024
1025 static cairo_status_t
1026 _cairo_stroker_spline_to (void *closure,
1027                           const cairo_point_t *point,
1028                           const cairo_slope_t *tangent)
1029 {
1030     cairo_stroker_t *stroker = closure;
1031     cairo_stroke_face_t new_face;
1032     double slope_dx, slope_dy;
1033     cairo_point_t points[3];
1034     cairo_point_t intersect_point;
1035
1036     stroker->has_initial_sub_path = TRUE;
1037
1038     if (stroker->current_point.x == point->x &&
1039         stroker->current_point.y == point->y)
1040         return CAIRO_STATUS_SUCCESS;
1041
1042     slope_dx = _cairo_fixed_to_double (tangent->dx);
1043     slope_dy = _cairo_fixed_to_double (tangent->dy);
1044
1045     if (! _compute_normalized_device_slope (&slope_dx, &slope_dy,
1046                                       stroker->ctm_inverse, NULL))
1047         return CAIRO_STATUS_SUCCESS;
1048
1049     _compute_face (point, tangent,
1050                    slope_dx, slope_dy,
1051                    stroker, &new_face);
1052
1053     assert(stroker->has_current_face);
1054
1055     if (_cairo_stroke_segment_intersect (&stroker->current_face.cw,
1056                      &stroker->current_face.ccw,
1057                                          &new_face.cw,
1058                                          &new_face.ccw,
1059                                          &intersect_point)) {
1060     points[0] = stroker->current_face.ccw;
1061     points[1] = new_face.ccw;
1062     points[2] = intersect_point;
1063     stroker->add_triangle (stroker->closure, points);
1064
1065     points[0] = stroker->current_face.cw;
1066     points[1] = new_face.cw;
1067     stroker->add_triangle (stroker->closure, points);
1068     }
1069     else {
1070     points[0] = stroker->current_face.ccw;
1071     points[1] = stroker->current_face.cw;
1072     points[2] = new_face.cw;
1073     stroker->add_triangle (stroker->closure, points);
1074
1075     points[0] = stroker->current_face.ccw;
1076     points[1] = new_face.cw;
1077     points[2] = new_face.ccw;
1078     stroker->add_triangle (stroker->closure, points);
1079     }
1080
1081     stroker->current_face = new_face;
1082     stroker->has_current_face = TRUE;
1083     stroker->current_point = *point;
1084
1085     return CAIRO_STATUS_SUCCESS;
1086 }
1087
1088 /*
1089  * Dashed lines.  Cap each dash end, join around turns when on
1090  */
1091 static cairo_status_t
1092 _cairo_stroker_line_to_dashed (void *closure,
1093                                const cairo_point_t *p2)
1094 {
1095     cairo_stroker_t *stroker = closure;
1096     double mag, remain, step_length = 0;
1097     double slope_dx, slope_dy;
1098     double dx2, dy2;
1099     cairo_stroke_face_t sub_start, sub_end;
1100     cairo_point_t *p1 = &stroker->current_point;
1101     cairo_slope_t dev_slope;
1102     cairo_line_t segment;
1103     cairo_bool_t fully_in_bounds;
1104     cairo_status_t status;
1105
1106     stroker->has_initial_sub_path = stroker->dash.dash_starts_on;
1107
1108     if (p1->x == p2->x && p1->y == p2->y)
1109         return CAIRO_STATUS_SUCCESS;
1110
1111     fully_in_bounds = TRUE;
1112     if (stroker->has_bounds &&
1113         (! _cairo_box_contains_point (&stroker->bounds, p1) ||
1114          ! _cairo_box_contains_point (&stroker->bounds, p2)))
1115     {
1116         fully_in_bounds = FALSE;
1117     }
1118
1119     _cairo_slope_init (&dev_slope, p1, p2);
1120
1121     slope_dx = _cairo_fixed_to_double (p2->x - p1->x);
1122     slope_dy = _cairo_fixed_to_double (p2->y - p1->y);
1123
1124     if (! _compute_normalized_device_slope (&slope_dx, &slope_dy,
1125                                             stroker->ctm_inverse, &mag))
1126     {
1127         return CAIRO_STATUS_SUCCESS;
1128     }
1129
1130     remain = mag;
1131     segment.p1 = *p1;
1132     while (remain) {
1133         step_length = MIN (stroker->dash.dash_remain, remain);
1134         remain -= step_length;
1135         dx2 = slope_dx * (mag - remain);
1136         dy2 = slope_dy * (mag - remain);
1137         cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
1138         segment.p2.x = _cairo_fixed_from_double (dx2) + p1->x;
1139         segment.p2.y = _cairo_fixed_from_double (dy2) + p1->y;
1140
1141         if (stroker->dash.dash_on &&
1142             (fully_in_bounds ||
1143              (! stroker->has_first_face && stroker->dash.dash_starts_on) ||
1144              _cairo_box_intersects_line_segment (&stroker->bounds, &segment)))
1145         {
1146             status = _cairo_stroker_add_sub_edge (stroker,
1147                                                   &segment.p1, &segment.p2,
1148                                                   &dev_slope,
1149                                                   slope_dx, slope_dy,
1150                                                   &sub_start, &sub_end);
1151             if (unlikely (status))
1152                 return status;
1153
1154             if (stroker->has_current_face)
1155             {
1156                 /* Join with final face from previous segment */
1157                 status = _cairo_stroker_join (stroker,
1158                                               &stroker->current_face,
1159                                               &sub_start);
1160                 if (unlikely (status))
1161                     return status;
1162
1163                 stroker->has_current_face = FALSE;
1164             }
1165             else if (! stroker->has_first_face &&
1166                        stroker->dash.dash_starts_on)
1167             {
1168                 /* Save sub path's first face in case needed for closing join */
1169                 stroker->first_face = sub_start;
1170                 stroker->has_first_face = TRUE;
1171             }
1172             else
1173             {
1174                 /* Cap dash start if not connecting to a previous segment */
1175                 status = _cairo_stroker_add_leading_cap (stroker, &sub_start);
1176                 if (unlikely (status))
1177                     return status;
1178             }
1179
1180             if (remain) {
1181                 /* Cap dash end if not at end of segment */
1182                 status = _cairo_stroker_add_trailing_cap (stroker, &sub_end);
1183                 if (unlikely (status))
1184                     return status;
1185             } else {
1186                 stroker->current_face = sub_end;
1187                 stroker->has_current_face = TRUE;
1188             }
1189         } else {
1190             if (stroker->has_current_face) {
1191                 /* Cap final face from previous segment */
1192                 status = _cairo_stroker_add_trailing_cap (stroker,
1193                                                           &stroker->current_face);
1194                 if (unlikely (status))
1195                     return status;
1196
1197                 stroker->has_current_face = FALSE;
1198             }
1199         }
1200
1201         _cairo_stroker_dash_step (&stroker->dash, step_length);
1202         segment.p1 = segment.p2;
1203     }
1204
1205     if (stroker->dash.dash_on && ! stroker->has_current_face) {
1206         /* This segment ends on a transition to dash_on, compute a new face
1207          * and add cap for the beginning of the next dash_on step.
1208          *
1209          * Note: this will create a degenerate cap if this is not the last line
1210          * in the path. Whether this behaviour is desirable or not is debatable.
1211          * On one side these degenerate caps can not be reproduced with regular
1212          * path stroking.
1213          * On the other hand, Acroread 7 also produces the degenerate caps.
1214          */
1215         _compute_face (p2, &dev_slope,
1216                        slope_dx, slope_dy,
1217                        stroker,
1218                        &stroker->current_face);
1219
1220         status = _cairo_stroker_add_leading_cap (stroker,
1221                                                  &stroker->current_face);
1222         if (unlikely (status))
1223             return status;
1224
1225         stroker->has_current_face = TRUE;
1226     }
1227
1228     stroker->current_point = *p2;
1229
1230     return CAIRO_STATUS_SUCCESS;
1231 }
1232 static cairo_status_t
1233 _cairo_stroker_curve_to (void *closure,
1234                          const cairo_point_t *b,
1235                          const cairo_point_t *c,
1236                          const cairo_point_t *d)
1237 {
1238     cairo_stroker_t *stroker = closure;
1239     cairo_spline_t spline;
1240     cairo_line_join_t line_join_save;
1241     cairo_stroke_face_t face;
1242     double slope_dx, slope_dy;
1243     cairo_spline_add_point_func_t line_to;
1244     cairo_spline_add_point_func_t spline_to;
1245     cairo_status_t status = CAIRO_STATUS_SUCCESS;
1246
1247     line_to = stroker->dash.dashed ?
1248         (cairo_spline_add_point_func_t) _cairo_stroker_line_to_dashed :
1249         (cairo_spline_add_point_func_t) _cairo_stroker_line_to;
1250
1251     /* spline_to is only capable of rendering non-degenerate splines. */
1252     spline_to = stroker->dash.dashed ?
1253         (cairo_spline_add_point_func_t) _cairo_stroker_line_to_dashed :
1254         (cairo_spline_add_point_func_t) _cairo_stroker_spline_to;
1255
1256     if (! _cairo_spline_init (&spline,
1257                               spline_to,
1258                               stroker,
1259                               &stroker->current_point, b, c, d))
1260     {
1261         cairo_slope_t fallback_slope;
1262         _cairo_slope_init (&fallback_slope, &stroker->current_point, d);
1263         return line_to (closure, d, &fallback_slope);
1264     }
1265
1266     /* If the line width is so small that the pen is reduced to a
1267        single point, then we have nothing to do. */
1268     if (stroker->pen.num_vertices <= 1)
1269         return CAIRO_STATUS_SUCCESS;
1270
1271     /* Compute the initial face */
1272     if (! stroker->dash.dashed || stroker->dash.dash_on) {
1273         slope_dx = _cairo_fixed_to_double (spline.initial_slope.dx);
1274         slope_dy = _cairo_fixed_to_double (spline.initial_slope.dy);
1275         if (_compute_normalized_device_slope (&slope_dx, &slope_dy,
1276                                               stroker->ctm_inverse, NULL))
1277         {
1278             _compute_face (&stroker->current_point,
1279                            &spline.initial_slope,
1280                            slope_dx, slope_dy,
1281                            stroker, &face);
1282         }
1283         if (stroker->has_current_face) {
1284             status = _cairo_stroker_join (stroker,
1285                                           &stroker->current_face, &face);
1286             if (unlikely (status))
1287                 return status;
1288         } else if (! stroker->has_first_face) {
1289             stroker->first_face = face;
1290             stroker->has_first_face = TRUE;
1291         }
1292
1293         stroker->current_face = face;
1294         stroker->has_current_face = TRUE;
1295     }
1296
1297     /* Temporarily modify the stroker to use round joins to guarantee
1298      * smooth stroked curves. */
1299     line_join_save = stroker->style.line_join;
1300     stroker->style.line_join = CAIRO_LINE_JOIN_ROUND;
1301
1302     status = _cairo_spline_decompose (&spline, stroker->tolerance);
1303     if (unlikely (status))
1304         return status;
1305
1306     /* And join the final face */
1307     if (! stroker->dash.dashed || stroker->dash.dash_on) {
1308         slope_dx = _cairo_fixed_to_double (spline.final_slope.dx);
1309         slope_dy = _cairo_fixed_to_double (spline.final_slope.dy);
1310         if (_compute_normalized_device_slope (&slope_dx, &slope_dy,
1311                                               stroker->ctm_inverse, NULL))
1312         {
1313             _compute_face (&stroker->current_point,
1314                            &spline.final_slope,
1315                            slope_dx, slope_dy,
1316                            stroker, &face);
1317         }
1318
1319         status = _cairo_stroker_join (stroker, &stroker->current_face, &face);
1320         if (unlikely (status))
1321             return status;
1322
1323         stroker->current_face = face;
1324     }
1325
1326     stroker->style.line_join = line_join_save;
1327
1328     return CAIRO_STATUS_SUCCESS;
1329 }
1330
1331 static cairo_status_t
1332 _cairo_stroker_close_path (void *closure)
1333 {
1334     cairo_stroker_t *stroker = closure;
1335     cairo_status_t status;
1336
1337     if (stroker->dash.dashed)
1338         status = _cairo_stroker_line_to_dashed (stroker, &stroker->first_point);
1339     else
1340         status = _cairo_stroker_line_to (stroker, &stroker->first_point);
1341     if (unlikely (status))
1342         return status;
1343
1344     if (stroker->has_first_face && stroker->has_current_face) {
1345         /* Join first and final faces of sub path */
1346         status = _cairo_stroker_join (stroker,
1347                                       &stroker->current_face,
1348                                       &stroker->first_face);
1349         if (unlikely (status))
1350             return status;
1351     } else {
1352         /* Cap the start and end of the sub path as needed */
1353         status = _cairo_stroker_add_caps (stroker);
1354         if (unlikely (status))
1355             return status;
1356     }
1357
1358     stroker->has_initial_sub_path = FALSE;
1359     stroker->has_first_face = FALSE;
1360     stroker->has_current_face = FALSE;
1361
1362     return CAIRO_STATUS_SUCCESS;
1363 }
1364
1365 cairo_status_t
1366 _cairo_path_fixed_stroke_to_shaper (cairo_path_fixed_t  *path,
1367                                     const cairo_stroke_style_t  *stroke_style,
1368                                     const cairo_matrix_t        *ctm,
1369                                     const cairo_matrix_t        *ctm_inverse,
1370                                     double               tolerance,
1371                                     cairo_status_t (*add_triangle) (void *closure,
1372                                                                     const cairo_point_t triangle[3]),
1373                                     cairo_status_t (*add_triangle_fan) (void *closure,
1374                                                                         const cairo_point_t *midpt,
1375                                                                         const cairo_point_t *points,
1376                                                                         int npoints),
1377                                     cairo_status_t (*add_convex_quad) (void *closure,
1378                                                                        const cairo_point_t quad[4]),
1379                                     void *closure)
1380 {
1381     cairo_stroker_t stroker;
1382     cairo_status_t status;
1383
1384     status = _cairo_stroker_init (&stroker, path, stroke_style,
1385                                   ctm, ctm_inverse, tolerance,
1386                                   NULL, 0);
1387     if (unlikely (status))
1388         return status;
1389
1390     stroker.add_triangle = add_triangle;
1391     stroker.add_triangle_fan = add_triangle_fan;
1392     stroker.add_convex_quad = add_convex_quad;
1393     stroker.closure = closure;
1394
1395     status = _cairo_path_fixed_interpret (path,
1396                                           _cairo_stroker_move_to,
1397                                           stroker.dash.dashed ?
1398                                           _cairo_stroker_line_to_dashed :
1399                                           _cairo_stroker_line_to,
1400                                           _cairo_stroker_curve_to,
1401                                           _cairo_stroker_close_path,
1402                                           &stroker);
1403
1404     if (unlikely (status))
1405         goto BAIL;
1406
1407     /* Cap the start and end of the final sub path as needed */
1408     status = _cairo_stroker_add_caps (&stroker);
1409
1410 BAIL:
1411     _cairo_stroker_fini (&stroker);
1412
1413     return status;
1414 }
1415
1416 cairo_status_t
1417 _cairo_path_fixed_stroke_dashed_to_polygon (const cairo_path_fixed_t    *path,
1418                                             const cairo_stroke_style_t  *stroke_style,
1419                                             const cairo_matrix_t        *ctm,
1420                                             const cairo_matrix_t        *ctm_inverse,
1421                                             double               tolerance,
1422                                             cairo_polygon_t *polygon)
1423 {
1424     cairo_stroker_t stroker;
1425     cairo_status_t status;
1426
1427     status = _cairo_stroker_init (&stroker, path, stroke_style,
1428                                   ctm, ctm_inverse, tolerance,
1429                                   polygon->limits, polygon->num_limits);
1430     if (unlikely (status))
1431         return status;
1432
1433     stroker.add_external_edge = _cairo_polygon_add_external_edge,
1434     stroker.closure = polygon;
1435
1436     status = _cairo_path_fixed_interpret (path,
1437                                           _cairo_stroker_move_to,
1438                                           stroker.dash.dashed ?
1439                                           _cairo_stroker_line_to_dashed :
1440                                           _cairo_stroker_line_to,
1441                                           _cairo_stroker_curve_to,
1442                                           _cairo_stroker_close_path,
1443                                           &stroker);
1444
1445     if (unlikely (status))
1446         goto BAIL;
1447
1448     /* Cap the start and end of the final sub path as needed */
1449     status = _cairo_stroker_add_caps (&stroker);
1450
1451 BAIL:
1452     _cairo_stroker_fini (&stroker);
1453
1454     return status;
1455 }
1456
1457 cairo_int_status_t
1458 _cairo_path_fixed_stroke_to_traps (const cairo_path_fixed_t     *path,
1459                                    const cairo_stroke_style_t   *stroke_style,
1460                                    const cairo_matrix_t *ctm,
1461                                    const cairo_matrix_t *ctm_inverse,
1462                                    double                tolerance,
1463                                    cairo_traps_t        *traps)
1464 {
1465     cairo_int_status_t status;
1466     cairo_polygon_t polygon;
1467
1468     _cairo_polygon_init (&polygon, traps->limits, traps->num_limits);
1469     status = _cairo_path_fixed_stroke_to_polygon (path,
1470                                                   stroke_style,
1471                                                   ctm,
1472                                                   ctm_inverse,
1473                                                   tolerance,
1474                                                   &polygon);
1475     if (unlikely (status))
1476         goto BAIL;
1477
1478     status = _cairo_polygon_status (&polygon);
1479     if (unlikely (status))
1480         goto BAIL;
1481
1482     status = _cairo_bentley_ottmann_tessellate_polygon (traps, &polygon,
1483                                                         CAIRO_FILL_RULE_WINDING);
1484
1485 BAIL:
1486     _cairo_polygon_fini (&polygon);
1487
1488     return status;
1489 }