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