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 _DEFAULT_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
    double spline_cusp_tolerance;
71
    double half_line_width;
72
    cairo_bool_t ctm_det_positive;
73

            
74
    cairo_pen_t pen;
75

            
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_bool_t has_bounds;
87
    cairo_box_t bounds;
88
};
89

            
90
static inline double
91
normalize_slope (double *dx, double *dy);
92

            
93
static void
94
compute_face (const cairo_point_t *point,
95
	      const cairo_slope_t *dev_slope,
96
	      struct stroker *stroker,
97
	      cairo_stroke_face_t *face);
98

            
99
static cairo_uint64_t
100
point_distance_sq (const cairo_point_t *p1,
101
			const cairo_point_t *p2)
102
{
103
    int32_t dx = p1->x - p2->x;
104
    int32_t dy = p1->y - p2->y;
105
    return _cairo_int32x32_64_mul (dx, dx) + _cairo_int32x32_64_mul (dy, dy);
106
}
107

            
108
static cairo_bool_t
109
2921664
within_tolerance (const cairo_point_t *p1,
110
	      const cairo_point_t *p2,
111
	      cairo_uint64_t tolerance)
112
{
113
2921664
    return FALSE;
114
    return _cairo_int64_lt (point_distance_sq (p1, p2), tolerance);
115
}
116

            
117
static void
118
2574939
contour_add_point (struct stroker *stroker,
119
		   struct stroke_contour *c,
120
		   const cairo_point_t *point)
121
{
122
2574939
    if (! within_tolerance (point, _cairo_contour_last_point (&c->contour),
123
			stroker->contour_tolerance))
124
2574939
	_cairo_contour_add_point (&c->contour, point);
125
    //*_cairo_contour_last_point (&c->contour) = *point;
126
2574939
}
127

            
128
static void
129
1410873
translate_point (cairo_point_t *point, const cairo_point_t *offset)
130
{
131
1410873
    point->x += offset->x;
132
1410873
    point->y += offset->y;
133
1410873
}
134

            
135
static int
136
673128
slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
137
{
138
673128
    double  c = (dx1 * dy2 - dx2 * dy1);
139

            
140
673128
    if (c > 0) return 1;
141
338445
    if (c < 0) return -1;
142
4662
    return 0;
143
}
144

            
145
/*
146
 * Construct a fan around the midpoint using the vertices from pen between
147
 * inpt and outpt.
148
 */
149
static void
150
6888
add_fan (struct stroker *stroker,
151
	 const cairo_slope_t *in_vector,
152
	 const cairo_slope_t *out_vector,
153
	 const cairo_point_t *midpt,
154
	 cairo_bool_t clockwise,
155
	 struct stroke_contour *c)
156
{
157
6888
    cairo_pen_t *pen = &stroker->pen;
158
    int start, stop;
159

            
160
8274
    if (stroker->has_bounds &&
161
1386
	! _cairo_box_contains_point (&stroker->bounds, midpt))
162
12
	return;
163

            
164
6876
    assert (stroker->pen.num_vertices);
165

            
166
6876
    if (clockwise) {
167
1266
	_cairo_pen_find_active_cw_vertices (pen,
168
					    in_vector, out_vector,
169
					    &start, &stop);
170
11820
	while (start != stop) {
171
10554
	    cairo_point_t p = *midpt;
172
10554
	    translate_point (&p, &pen->vertices[start].point);
173
10554
	    contour_add_point (stroker, c, &p);
174

            
175
10554
	    if (++start == pen->num_vertices)
176
342
		start = 0;
177
	}
178
    } else {
179
5610
	_cairo_pen_find_active_ccw_vertices (pen,
180
					     in_vector, out_vector,
181
					     &start, &stop);
182
98955
	while (start != stop) {
183
93345
	    cairo_point_t p = *midpt;
184
93345
	    translate_point (&p, &pen->vertices[start].point);
185
93345
	    contour_add_point (stroker, c, &p);
186

            
187
93345
	    if (start-- == 0)
188
3495
		start += pen->num_vertices;
189
	}
190
    }
191
}
192

            
193
static int
194
45903
join_is_clockwise (const cairo_stroke_face_t *in,
195
		   const cairo_stroke_face_t *out)
196
{
197
45903
    return _cairo_slope_compare (&in->dev_vector, &out->dev_vector) < 0;
198
}
199

            
200
static void
201
335895
inner_join (struct stroker *stroker,
202
	    const cairo_stroke_face_t *in,
203
	    const cairo_stroke_face_t *out,
204
	    int clockwise)
205
{
206
#if 0
207
    cairo_point_t last;
208
    const cairo_point_t *p, *outpt;
209
    struct stroke_contour *inner;
210
    cairo_int64_t d_p, d_last;
211
    cairo_int64_t half_line_width;
212
    cairo_bool_t negate;
213

            
214
    /* XXX line segments shorter than line-width */
215

            
216
    if (clockwise) {
217
	inner = &stroker->ccw;
218
	outpt = &out->ccw;
219
	negate = 1;
220
    } else {
221
	inner = &stroker->cw;
222
	outpt = &out->cw;
223
	negate = 0;
224
    }
225

            
226
    half_line_width = CAIRO_FIXED_ONE*CAIRO_FIXED_ONE/2 * stroker->style.line_width * out->length + .5;
227

            
228
    /* On the inside, the previous end-point is always
229
     * closer to the new face by definition.
230
     */
231
    last = *_cairo_contour_last_point (&inner->contour);
232
    d_last = distance_from_face (out, &last, negate);
233
    _cairo_contour_remove_last_point (&inner->contour);
234

            
235
prev:
236
    if (inner->contour.chain.num_points == 0) {
237
	contour_add_point (stroker, inner, outpt);
238
	return;
239
    }
240
    p = _cairo_contour_last_point (&inner->contour);
241
    d_p = distance_from_face (out, p, negate);
242
    if (_cairo_int64_lt (d_p, half_line_width) &&
243
	!_cairo_int64_negative (distance_along_face (out, p)))
244
    {
245
	last = *p;
246
	d_last = d_p;
247
	_cairo_contour_remove_last_point (&inner->contour);
248
	goto prev;
249
    }
250

            
251
    compute_inner_joint (&last, d_last, p, d_p, half_line_width);
252
    contour_add_point (stroker, inner, &last);
253
#else
254
    const cairo_point_t *outpt;
255
    struct stroke_contour *inner;
256

            
257
335895
    if (clockwise) {
258
196470
	inner = &stroker->ccw;
259
196470
	outpt = &out->ccw;
260
    } else {
261
139425
	inner = &stroker->cw;
262
139425
	outpt = &out->cw;
263
    }
264
335895
    contour_add_point (stroker, inner, &in->point);
265
335895
    contour_add_point (stroker, inner, outpt);
266
#endif
267
335895
}
268

            
269
static void
270
19032
inner_close (struct stroker *stroker,
271
	     const cairo_stroke_face_t *in,
272
	     cairo_stroke_face_t *out)
273
{
274
#if 0
275
    cairo_point_t last;
276
    const cairo_point_t *p, *outpt, *inpt;
277
    struct stroke_contour *inner;
278
    struct _cairo_contour_chain *chain;
279

            
280
    /* XXX line segments shorter than line-width */
281

            
282
    if (join_is_clockwise (in, out)) {
283
	inner = &stroker->ccw;
284
	outpt = &in->ccw;
285
	inpt = &out->ccw;
286
    } else {
287
	inner = &stroker->cw;
288
	outpt = &in->cw;
289
	inpt = &out->cw;
290
    }
291

            
292
    if (inner->contour.chain.num_points == 0) {
293
	contour_add_point (stroker, inner, &in->point);
294
	contour_add_point (stroker, inner, inpt);
295
	*_cairo_contour_first_point (&inner->contour) =
296
	    *_cairo_contour_last_point (&inner->contour);
297
	return;
298
    }
299

            
300
    line_width = stroker->style.line_width/2;
301
    line_width *= CAIRO_FIXED_ONE;
302

            
303
    d_last = sign * distance_from_face (out, outpt);
304
    last = *outpt;
305

            
306
    for (chain = &inner->contour.chain; chain; chain = chain->next) {
307
	for (i = 0; i < chain->num_points; i++) {
308
	    p = &chain->points[i];
309
	    if ((d_p = sign * distance_from_face (in, p)) >= line_width &&
310
		distance_from_edge (stroker, inpt, &last, p) < line_width)
311
	    {
312
		goto out;
313
	    }
314

            
315
	    if (p->x != last.x || p->y != last.y) {
316
		last = *p;
317
		d_last = d_p;
318
	    }
319
	}
320
    }
321
out:
322

            
323
    if (d_p != d_last) {
324
	double dot = (line_width - d_last) / (d_p - d_last);
325
	last.x += dot * (p->x - last.x);
326
	last.y += dot * (p->y - last.y);
327
    }
328
    *_cairo_contour_last_point (&inner->contour) = last;
329

            
330
    for (chain = &inner->contour.chain; chain; chain = chain->next) {
331
	for (i = 0; i < chain->num_points; i++) {
332
	    cairo_point_t *pp = &chain->points[i];
333
	    if (pp == p)
334
		return;
335
	    *pp = last;
336
	}
337
    }
338
#else
339
    const cairo_point_t *inpt;
340
    struct stroke_contour *inner;
341

            
342
19032
    if (join_is_clockwise (in, out)) {
343
18180
	inner = &stroker->ccw;
344
18180
	inpt = &out->ccw;
345
    } else {
346
852
	inner = &stroker->cw;
347
852
	inpt = &out->cw;
348
    }
349

            
350
19032
    contour_add_point (stroker, inner, &in->point);
351
19032
    contour_add_point (stroker, inner, inpt);
352
19032
    *_cairo_contour_first_point (&inner->contour) =
353
19032
	*_cairo_contour_last_point (&inner->contour);
354
#endif
355
19032
}
356

            
357
static void
358
19032
outer_close (struct stroker *stroker,
359
	     const cairo_stroke_face_t *in,
360
	     const cairo_stroke_face_t *out)
361
{
362
    const cairo_point_t	*inpt, *outpt;
363
    struct stroke_contour *outer;
364
    int	clockwise;
365

            
366
19032
    if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
367
333
	in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
368
    {
369
333
	return;
370
    }
371

            
372
18699
    clockwise = join_is_clockwise (in, out);
373
18699
    if (clockwise) {
374
18132
	inpt = &in->cw;
375
18132
	outpt = &out->cw;
376
18132
	outer = &stroker->cw;
377
    } else {
378
567
	inpt = &in->ccw;
379
567
	outpt = &out->ccw;
380
567
	outer = &stroker->ccw;
381
    }
382

            
383
18699
    if (within_tolerance (inpt, outpt, stroker->contour_tolerance)) {
384
	*_cairo_contour_first_point (&outer->contour) =
385
	    *_cairo_contour_last_point (&outer->contour);
386
	return;
387
    }
388

            
389
18699
    switch (stroker->style.line_join) {
390
93
    case CAIRO_LINE_JOIN_ROUND:
391
93
	if ((in->dev_slope.x * out->dev_slope.x +
392
93
	     in->dev_slope.y * out->dev_slope.y) < stroker->spline_cusp_tolerance)
393
	{
394
	    /* construct a fan around the common midpoint */
395
84
	    add_fan (stroker,
396
		     &in->dev_vector, &out->dev_vector, &in->point,
397
		     clockwise, outer);
398
	} /* else: bevel join */
399
93
	break;
400

            
401
18543
    case CAIRO_LINE_JOIN_MITER:
402
    default: {
403
	/* dot product of incoming slope vector with outgoing slope vector */
404
18543
	double	in_dot_out = in->dev_slope.x * out->dev_slope.x +
405
18543
			     in->dev_slope.y * out->dev_slope.y;
406
18543
	double	ml = stroker->style.miter_limit;
407

            
408
	/* Check the miter limit -- lines meeting at an acute angle
409
	 * can generate long miters, the limit converts them to bevel
410
	 *
411
	 * Consider the miter join formed when two line segments
412
	 * meet at an angle psi:
413
	 *
414
	 *	   /.\
415
	 *	  /. .\
416
	 *	 /./ \.\
417
	 *	/./psi\.\
418
	 *
419
	 * We can zoom in on the right half of that to see:
420
	 *
421
	 *	    |\
422
	 *	    | \ psi/2
423
	 *	    |  \
424
	 *	    |   \
425
	 *	    |    \
426
	 *	    |     \
427
	 *	  miter    \
428
	 *	 length     \
429
	 *	    |        \
430
	 *	    |        .\
431
	 *	    |    .     \
432
	 *	    |.   line   \
433
	 *	     \    width  \
434
	 *	      \           \
435
	 *
436
	 *
437
	 * The right triangle in that figure, (the line-width side is
438
	 * shown faintly with three '.' characters), gives us the
439
	 * following expression relating miter length, angle and line
440
	 * width:
441
	 *
442
	 *	1 /sin (psi/2) = miter_length / line_width
443
	 *
444
	 * The right-hand side of this relationship is the same ratio
445
	 * in which the miter limit (ml) is expressed. We want to know
446
	 * when the miter length is within the miter limit. That is
447
	 * when the following condition holds:
448
	 *
449
	 *	1/sin(psi/2) <= ml
450
	 *	1 <= ml sin(psi/2)
451
	 *	1 <= ml² sin²(psi/2)
452
	 *	2 <= ml² 2 sin²(psi/2)
453
	 *				2·sin²(psi/2) = 1-cos(psi)
454
	 *	2 <= ml² (1-cos(psi))
455
	 *
456
	 *				in · out = |in| |out| cos (psi)
457
	 *
458
	 * in and out are both unit vectors, so:
459
	 *
460
	 *				in · out = cos (psi)
461
	 *
462
	 *	2 <= ml² (1 - in · out)
463
	 *
464
	 */
465
18543
	if (2 <= ml * ml * (1 + in_dot_out)) {
466
	    double		x1, y1, x2, y2;
467
	    double		mx, my;
468
	    double		dx1, dx2, dy1, dy2;
469
	    double		ix, iy;
470
	    double		fdx1, fdy1, fdx2, fdy2;
471
	    double		mdx, mdy;
472

            
473
	    /*
474
	     * we've got the points already transformed to device
475
	     * space, but need to do some computation with them and
476
	     * also need to transform the slope from user space to
477
	     * device space
478
	     */
479
	    /* outer point of incoming line face */
480
17463
	    x1 = _cairo_fixed_to_double (inpt->x);
481
17463
	    y1 = _cairo_fixed_to_double (inpt->y);
482
17463
	    dx1 = in->dev_slope.x;
483
17463
	    dy1 = in->dev_slope.y;
484

            
485
	    /* outer point of outgoing line face */
486
17463
	    x2 = _cairo_fixed_to_double (outpt->x);
487
17463
	    y2 = _cairo_fixed_to_double (outpt->y);
488
17463
	    dx2 = out->dev_slope.x;
489
17463
	    dy2 = out->dev_slope.y;
490

            
491
	    /*
492
	     * Compute the location of the outer corner of the miter.
493
	     * That's pretty easy -- just the intersection of the two
494
	     * outer edges.  We've got slopes and points on each
495
	     * of those edges.  Compute my directly, then compute
496
	     * mx by using the edge with the larger dy; that avoids
497
	     * dividing by values close to zero.
498
	     */
499
17463
	    my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
500
17463
		  (dx1 * dy2 - dx2 * dy1));
501
17463
	    if (fabs (dy1) >= fabs (dy2))
502
16983
		mx = (my - y1) * dx1 / dy1 + x1;
503
	    else
504
480
		mx = (my - y2) * dx2 / dy2 + x2;
505

            
506
	    /*
507
	     * When the two outer edges are nearly parallel, slight
508
	     * perturbations in the position of the outer points of the lines
509
	     * caused by representing them in fixed point form can cause the
510
	     * intersection point of the miter to move a large amount. If
511
	     * that moves the miter intersection from between the two faces,
512
	     * then draw a bevel instead.
513
	     */
514

            
515
17463
	    ix = _cairo_fixed_to_double (in->point.x);
516
17463
	    iy = _cairo_fixed_to_double (in->point.y);
517

            
518
	    /* slope of one face */
519
17463
	    fdx1 = x1 - ix; fdy1 = y1 - iy;
520

            
521
	    /* slope of the other face */
522
17463
	    fdx2 = x2 - ix; fdy2 = y2 - iy;
523

            
524
	    /* slope from the intersection to the miter point */
525
17463
	    mdx = mx - ix; mdy = my - iy;
526

            
527
	    /*
528
	     * Make sure the miter point line lies between the two
529
	     * faces by comparing the slopes
530
	     */
531
34926
	    if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
532
17463
		slope_compare_sgn (fdx2, fdy2, mdx, mdy))
533
	    {
534
		cairo_point_t p;
535

            
536
17358
		p.x = _cairo_fixed_from_double (mx);
537
17358
		p.y = _cairo_fixed_from_double (my);
538

            
539
17358
		*_cairo_contour_last_point (&outer->contour) = p;
540
17358
		*_cairo_contour_first_point (&outer->contour) = p;
541
17358
		return;
542
	    }
543
	}
544
1185
	break;
545
    }
546

            
547
63
    case CAIRO_LINE_JOIN_BEVEL:
548
63
	break;
549
    }
550
1341
    contour_add_point (stroker, outer, outpt);
551
}
552

            
553
static void
554
335895
outer_join (struct stroker *stroker,
555
	    const cairo_stroke_face_t *in,
556
	    const cairo_stroke_face_t *out,
557
	    int clockwise)
558
{
559
    const cairo_point_t	*inpt, *outpt;
560
    struct stroke_contour *outer;
561

            
562
335895
    if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
563
9603
	in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
564
    {
565
9603
	return;
566
    }
567
326292
    if (clockwise) {
568
194001
	inpt = &in->cw;
569
194001
	outpt = &out->cw;
570
194001
	outer = &stroker->cw;
571
    } else {
572
132291
	inpt = &in->ccw;
573
132291
	outpt = &out->ccw;
574
132291
	outer = &stroker->ccw;
575
    }
576

            
577
326292
    switch (stroker->style.line_join) {
578
2511
    case CAIRO_LINE_JOIN_ROUND:
579
2511
	if ((in->dev_slope.x * out->dev_slope.x +
580
2511
	     in->dev_slope.y * out->dev_slope.y) < stroker->spline_cusp_tolerance)
581
	{
582
	    /* construct a fan around the common midpoint */
583
2223
	    add_fan (stroker,
584
		     &in->dev_vector, &out->dev_vector, &in->point,
585
		     clockwise, outer);
586
	} /* else: bevel join */
587
2511
	break;
588

            
589
322188
    case CAIRO_LINE_JOIN_MITER:
590
    default: {
591
	/* dot product of incoming slope vector with outgoing slope vector */
592
322188
	double	in_dot_out = in->dev_slope.x * out->dev_slope.x +
593
322188
			     in->dev_slope.y * out->dev_slope.y;
594
322188
	double	ml = stroker->style.miter_limit;
595

            
596
	/* Check the miter limit -- lines meeting at an acute angle
597
	 * can generate long miters, the limit converts them to bevel
598
	 *
599
	 * Consider the miter join formed when two line segments
600
	 * meet at an angle psi:
601
	 *
602
	 *	   /.\
603
	 *	  /. .\
604
	 *	 /./ \.\
605
	 *	/./psi\.\
606
	 *
607
	 * We can zoom in on the right half of that to see:
608
	 *
609
	 *	    |\
610
	 *	    | \ psi/2
611
	 *	    |  \
612
	 *	    |   \
613
	 *	    |    \
614
	 *	    |     \
615
	 *	  miter    \
616
	 *	 length     \
617
	 *	    |        \
618
	 *	    |        .\
619
	 *	    |    .     \
620
	 *	    |.   line   \
621
	 *	     \    width  \
622
	 *	      \           \
623
	 *
624
	 *
625
	 * The right triangle in that figure, (the line-width side is
626
	 * shown faintly with three '.' characters), gives us the
627
	 * following expression relating miter length, angle and line
628
	 * width:
629
	 *
630
	 *	1 /sin (psi/2) = miter_length / line_width
631
	 *
632
	 * The right-hand side of this relationship is the same ratio
633
	 * in which the miter limit (ml) is expressed. We want to know
634
	 * when the miter length is within the miter limit. That is
635
	 * when the following condition holds:
636
	 *
637
	 *	1/sin(psi/2) <= ml
638
	 *	1 <= ml sin(psi/2)
639
	 *	1 <= ml² sin²(psi/2)
640
	 *	2 <= ml² 2 sin²(psi/2)
641
	 *				2·sin²(psi/2) = 1-cos(psi)
642
	 *	2 <= ml² (1-cos(psi))
643
	 *
644
	 *				in · out = |in| |out| cos (psi)
645
	 *
646
	 * in and out are both unit vectors, so:
647
	 *
648
	 *				in · out = cos (psi)
649
	 *
650
	 *	2 <= ml² (1 - in · out)
651
	 *
652
	 */
653
322188
	if (2 <= ml * ml * (1 + in_dot_out)) {
654
	    double		x1, y1, x2, y2;
655
	    double		mx, my;
656
	    double		dx1, dx2, dy1, dy2;
657
	    double		ix, iy;
658
	    double		fdx1, fdy1, fdx2, fdy2;
659
	    double		mdx, mdy;
660

            
661
	    /*
662
	     * we've got the points already transformed to device
663
	     * space, but need to do some computation with them and
664
	     * also need to transform the slope from user space to
665
	     * device space
666
	     */
667
	    /* outer point of incoming line face */
668
319101
	    x1 = _cairo_fixed_to_double (inpt->x);
669
319101
	    y1 = _cairo_fixed_to_double (inpt->y);
670
319101
	    dx1 = in->dev_slope.x;
671
319101
	    dy1 = in->dev_slope.y;
672

            
673
	    /* outer point of outgoing line face */
674
319101
	    x2 = _cairo_fixed_to_double (outpt->x);
675
319101
	    y2 = _cairo_fixed_to_double (outpt->y);
676
319101
	    dx2 = out->dev_slope.x;
677
319101
	    dy2 = out->dev_slope.y;
678

            
679
	    /*
680
	     * Compute the location of the outer corner of the miter.
681
	     * That's pretty easy -- just the intersection of the two
682
	     * outer edges.  We've got slopes and points on each
683
	     * of those edges.  Compute my directly, then compute
684
	     * mx by using the edge with the larger dy; that avoids
685
	     * dividing by values close to zero.
686
	     */
687
319101
	    my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
688
319101
		  (dx1 * dy2 - dx2 * dy1));
689
319101
	    if (fabs (dy1) >= fabs (dy2))
690
122046
		mx = (my - y1) * dx1 / dy1 + x1;
691
	    else
692
197055
		mx = (my - y2) * dx2 / dy2 + x2;
693

            
694
	    /*
695
	     * When the two outer edges are nearly parallel, slight
696
	     * perturbations in the position of the outer points of the lines
697
	     * caused by representing them in fixed point form can cause the
698
	     * intersection point of the miter to move a large amount. If
699
	     * that moves the miter intersection from between the two faces,
700
	     * then draw a bevel instead.
701
	     */
702

            
703
319101
	    ix = _cairo_fixed_to_double (in->point.x);
704
319101
	    iy = _cairo_fixed_to_double (in->point.y);
705

            
706
	    /* slope of one face */
707
319101
	    fdx1 = x1 - ix; fdy1 = y1 - iy;
708

            
709
	    /* slope of the other face */
710
319101
	    fdx2 = x2 - ix; fdy2 = y2 - iy;
711

            
712
	    /* slope from the intersection to the miter point */
713
319101
	    mdx = mx - ix; mdy = my - iy;
714

            
715
	    /*
716
	     * Make sure the miter point line lies between the two
717
	     * faces by comparing the slopes
718
	     */
719
638202
	    if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
720
319101
		slope_compare_sgn (fdx2, fdy2, mdx, mdy))
721
	    {
722
		cairo_point_t p;
723

            
724
283347
		p.x = _cairo_fixed_from_double (mx);
725
283347
		p.y = _cairo_fixed_from_double (my);
726

            
727
283347
		*_cairo_contour_last_point (&outer->contour) = p;
728
283347
		return;
729
	    }
730
	}
731
38841
	break;
732
    }
733

            
734
1593
    case CAIRO_LINE_JOIN_BEVEL:
735
1593
	break;
736
    }
737
42945
    contour_add_point (stroker,outer, outpt);
738
}
739

            
740
static void
741
196008
add_cap (struct stroker *stroker,
742
	 const cairo_stroke_face_t *f,
743
	 struct stroke_contour *c)
744
{
745
196008
    switch (stroker->style.line_cap) {
746
4278
    case CAIRO_LINE_CAP_ROUND: {
747
	cairo_slope_t slope;
748

            
749
4278
	slope.dx = -f->dev_vector.dx;
750
4278
	slope.dy = -f->dev_vector.dy;
751

            
752
4278
	add_fan (stroker, &f->dev_vector, &slope, &f->point, FALSE, c);
753
4278
	break;
754
    }
755

            
756
630
    case CAIRO_LINE_CAP_SQUARE: {
757
	cairo_slope_t fvector;
758
	cairo_point_t p;
759
	double dx, dy;
760

            
761
630
	dx = f->usr_vector.x;
762
630
	dy = f->usr_vector.y;
763
630
	dx *= stroker->half_line_width;
764
630
	dy *= stroker->half_line_width;
765
630
	cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
766
630
	fvector.dx = _cairo_fixed_from_double (dx);
767
630
	fvector.dy = _cairo_fixed_from_double (dy);
768

            
769
630
	p.x = f->ccw.x + fvector.dx;
770
630
	p.y = f->ccw.y + fvector.dy;
771
630
	contour_add_point (stroker, c, &p);
772

            
773
630
	p.x = f->cw.x + fvector.dx;
774
630
	p.y = f->cw.y + fvector.dy;
775
630
	contour_add_point (stroker, c, &p);
776
    }
777

            
778
191730
    case CAIRO_LINE_CAP_BUTT:
779
    default:
780
191730
	break;
781
    }
782
196008
    contour_add_point (stroker, c, &f->cw);
783
196008
}
784

            
785
static void
786
98004
add_leading_cap (struct stroker *stroker,
787
		 const cairo_stroke_face_t *face,
788
		 struct stroke_contour *c)
789
{
790
    cairo_stroke_face_t reversed;
791
    cairo_point_t t;
792

            
793
98004
    reversed = *face;
794

            
795
    /* The initial cap needs an outward facing vector. Reverse everything */
796
98004
    reversed.usr_vector.x = -reversed.usr_vector.x;
797
98004
    reversed.usr_vector.y = -reversed.usr_vector.y;
798
98004
    reversed.dev_vector.dx = -reversed.dev_vector.dx;
799
98004
    reversed.dev_vector.dy = -reversed.dev_vector.dy;
800

            
801
98004
    t = reversed.cw;
802
98004
    reversed.cw = reversed.ccw;
803
98004
    reversed.ccw = t;
804

            
805
98004
    add_cap (stroker, &reversed, c);
806
98004
}
807

            
808
static void
809
98004
add_trailing_cap (struct stroker *stroker,
810
		  const cairo_stroke_face_t *face,
811
		  struct stroke_contour *c)
812
{
813
98004
    add_cap (stroker, face, c);
814
98004
}
815

            
816
static inline double
817
932322
normalize_slope (double *dx, double *dy)
818
{
819
932322
    double dx0 = *dx, dy0 = *dy;
820
    double mag;
821

            
822
932322
    assert (dx0 != 0.0 || dy0 != 0.0);
823

            
824
932322
    if (dx0 == 0.0) {
825
17841
	*dx = 0.0;
826
17841
	if (dy0 > 0.0) {
827
10785
	    mag = dy0;
828
10785
	    *dy = 1.0;
829
	} else {
830
7056
	    mag = -dy0;
831
7056
	    *dy = -1.0;
832
	}
833
914481
    } else if (dy0 == 0.0) {
834
172236
	*dy = 0.0;
835
172236
	if (dx0 > 0.0) {
836
146205
	    mag = dx0;
837
146205
	    *dx = 1.0;
838
	} else {
839
26031
	    mag = -dx0;
840
26031
	    *dx = -1.0;
841
	}
842
    } else {
843
742245
	mag = hypot (dx0, dy0);
844
742245
	*dx = dx0 / mag;
845
742245
	*dy = dy0 / mag;
846
    }
847

            
848
932322
    return mag;
849
}
850

            
851
static void
852
653487
compute_face (const cairo_point_t *point,
853
	      const cairo_slope_t *dev_slope,
854
	      struct stroker *stroker,
855
	      cairo_stroke_face_t *face)
856
{
857
    double face_dx, face_dy;
858
    cairo_point_t offset_ccw, offset_cw;
859
    double slope_dx, slope_dy;
860

            
861
653487
    slope_dx = _cairo_fixed_to_double (dev_slope->dx);
862
653487
    slope_dy = _cairo_fixed_to_double (dev_slope->dy);
863
653487
    face->length = normalize_slope (&slope_dx, &slope_dy);
864
653487
    face->dev_slope.x = slope_dx;
865
653487
    face->dev_slope.y = slope_dy;
866

            
867
    /*
868
     * rotate to get a line_width/2 vector along the face, note that
869
     * the vector must be rotated the right direction in device space,
870
     * but by 90° in user space. So, the rotation depends on
871
     * whether the ctm reflects or not, and that can be determined
872
     * by looking at the determinant of the matrix.
873
     */
874
653487
    if (! _cairo_matrix_is_identity (stroker->ctm_inverse)) {
875
	/* Normalize the matrix! */
876
278835
	cairo_matrix_transform_distance (stroker->ctm_inverse,
877
					 &slope_dx, &slope_dy);
878
278835
	normalize_slope (&slope_dx, &slope_dy);
879

            
880
278835
	if (stroker->ctm_det_positive) {
881
153408
	    face_dx = - slope_dy * stroker->half_line_width;
882
153408
	    face_dy = slope_dx * stroker->half_line_width;
883
	} else {
884
125427
	    face_dx = slope_dy * stroker->half_line_width;
885
125427
	    face_dy = - slope_dx * stroker->half_line_width;
886
	}
887

            
888
	/* back to device space */
889
278835
	cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
890
    } else {
891
374652
	face_dx = - slope_dy * stroker->half_line_width;
892
374652
	face_dy = slope_dx * stroker->half_line_width;
893
    }
894

            
895
653487
    offset_ccw.x = _cairo_fixed_from_double (face_dx);
896
653487
    offset_ccw.y = _cairo_fixed_from_double (face_dy);
897
653487
    offset_cw.x = -offset_ccw.x;
898
653487
    offset_cw.y = -offset_ccw.y;
899

            
900
653487
    face->ccw = *point;
901
653487
    translate_point (&face->ccw, &offset_ccw);
902

            
903
653487
    face->point = *point;
904

            
905
653487
    face->cw = *point;
906
653487
    translate_point (&face->cw, &offset_cw);
907

            
908
653487
    face->usr_vector.x = slope_dx;
909
653487
    face->usr_vector.y = slope_dy;
910

            
911
653487
    face->dev_vector = *dev_slope;
912
653487
}
913

            
914
static void
915
155313
add_caps (struct stroker *stroker)
916
{
917
    /* check for a degenerative sub_path */
918
155313
    if (stroker->has_initial_sub_path &&
919
96360
	! stroker->has_first_face &&
920
234
	! stroker->has_current_face &&
921
234
	stroker->style.line_cap == CAIRO_LINE_CAP_ROUND)
922
222
    {
923
	/* pick an arbitrary slope to use */
924
222
	cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
925
	cairo_stroke_face_t face;
926

            
927
	/* arbitrarily choose first_point */
928
222
	compute_face (&stroker->first_point, &slope, stroker, &face);
929

            
930
222
	add_leading_cap (stroker, &face, &stroker->ccw);
931
222
	add_trailing_cap (stroker, &face, &stroker->ccw);
932

            
933
	/* ensure the circle is complete */
934
222
	_cairo_contour_add_point (&stroker->ccw.contour,
935
222
				  _cairo_contour_first_point (&stroker->ccw.contour));
936

            
937
222
	_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
938
222
	_cairo_contour_reset (&stroker->ccw.contour);
939
    } else {
940
155091
	if (stroker->has_current_face)
941
97782
	    add_trailing_cap (stroker, &stroker->current_face, &stroker->ccw);
942

            
943
#if DEBUG
944
	{
945
	    FILE *file = fopen ("contours.txt", "a");
946
	    _cairo_debug_print_contour (file, &stroker->path);
947
	    _cairo_debug_print_contour (file, &stroker->cw.contour);
948
	    _cairo_debug_print_contour (file, &stroker->ccw.contour);
949
	    fclose (file);
950
	    _cairo_contour_reset (&stroker->path);
951
	}
952
#endif
953

            
954
155091
	_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
955
155091
	_cairo_contour_reset (&stroker->ccw.contour);
956

            
957
155091
	if (stroker->has_first_face) {
958
97782
	    _cairo_contour_add_point (&stroker->ccw.contour,
959
97782
				      &stroker->first_face.cw);
960
97782
	    add_leading_cap (stroker, &stroker->first_face, &stroker->ccw);
961
#if DEBUG
962
	    {
963
		FILE *file = fopen ("contours.txt", "a");
964
		_cairo_debug_print_contour (file, &stroker->ccw.contour);
965
		fclose (file);
966
	    }
967
#endif
968

            
969
97782
	    _cairo_polygon_add_contour (stroker->polygon,
970
97782
					&stroker->ccw.contour);
971
97782
	    _cairo_contour_reset (&stroker->ccw.contour);
972
	}
973

            
974
155091
	_cairo_polygon_add_contour (stroker->polygon, &stroker->cw.contour);
975
155091
	_cairo_contour_reset (&stroker->cw.contour);
976
    }
977
155313
}
978

            
979
static cairo_status_t
980
close_path (void *closure);
981

            
982
static cairo_status_t
983
117522
move_to (void *closure,
984
	 const cairo_point_t *point)
985
{
986
117522
    struct stroker *stroker = closure;
987

            
988
    /* Cap the start and end of the previous sub path as needed */
989
117522
    add_caps (stroker);
990

            
991
117522
    stroker->has_first_face = FALSE;
992
117522
    stroker->has_current_face = FALSE;
993
117522
    stroker->has_initial_sub_path = FALSE;
994

            
995
117522
    stroker->first_point = *point;
996

            
997
#if DEBUG
998
    _cairo_contour_add_point (&stroker->path, point);
999
#endif
117522
    stroker->current_face.point = *point;
117522
    return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
442911
line_to (void *closure,
	 const cairo_point_t *point)
{
442911
    struct stroker *stroker = closure;
    cairo_stroke_face_t start;
442911
    cairo_point_t *p1 = &stroker->current_face.point;
    cairo_slope_t dev_slope;
442911
    stroker->has_initial_sub_path = TRUE;
442911
    if (p1->x == point->x && p1->y == point->y)
609
	return CAIRO_STATUS_SUCCESS;
#if DEBUG
    _cairo_contour_add_point (&stroker->path, point);
#endif
442302
    _cairo_slope_init (&dev_slope, p1, point);
442302
    compute_face (p1, &dev_slope, stroker, &start);
442302
    if (stroker->has_current_face) {
328173
	int clockwise = _cairo_slope_compare (&stroker->current_face.dev_vector,
					      &start.dev_vector);
328173
	if (clockwise) {
328026
	    clockwise = clockwise < 0;
	    /* Join with final face from previous segment */
328026
	    if (! within_tolerance (&stroker->current_face.ccw, &start.ccw,
				    stroker->contour_tolerance) ||
		! within_tolerance (&stroker->current_face.cw, &start.cw,
				    stroker->contour_tolerance))
	    {
328026
		outer_join (stroker, &stroker->current_face, &start, clockwise);
328026
		inner_join (stroker, &stroker->current_face, &start, clockwise);
	    }
	}
    } else {
114129
	if (! stroker->has_first_face) {
	    /* Save sub path's first face in case needed for closing join */
114129
	    stroker->first_face = start;
114129
	    stroker->has_first_face = TRUE;
	}
114129
	stroker->has_current_face = TRUE;
114129
	contour_add_point (stroker, &stroker->cw, &start.cw);
114129
	contour_add_point (stroker, &stroker->ccw, &start.ccw);
    }
442302
    stroker->current_face = start;
442302
    stroker->current_face.point = *point;
442302
    stroker->current_face.ccw.x += dev_slope.dx;
442302
    stroker->current_face.ccw.y += dev_slope.dy;
442302
    stroker->current_face.cw.x += dev_slope.dx;
442302
    stroker->current_face.cw.y += dev_slope.dy;
442302
    contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
442302
    contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
442302
    return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
200421
spline_to (void *closure,
	   const cairo_point_t *point,
	   const cairo_slope_t *tangent)
{
200421
    struct stroker *stroker = closure;
    cairo_stroke_face_t face;
#if DEBUG
    _cairo_contour_add_point (&stroker->path, point);
#endif
200421
    if ((tangent->dx | tangent->dy) == 0) {
	struct stroke_contour *outer;
	cairo_point_t t;
	int clockwise;
12
	face = stroker->current_face;
12
	face.usr_vector.x = -face.usr_vector.x;
12
	face.usr_vector.y = -face.usr_vector.y;
12
	face.dev_vector.dx = -face.dev_vector.dx;
12
	face.dev_vector.dy = -face.dev_vector.dy;
12
	t = face.cw;
12
	face.cw = face.ccw;
12
	face.ccw = t;
12
	clockwise = join_is_clockwise (&stroker->current_face, &face);
12
	if (clockwise) {
12
	    outer = &stroker->cw;
	} else {
	    outer = &stroker->ccw;
	}
12
	add_fan (stroker,
12
		 &stroker->current_face.dev_vector,
		 &face.dev_vector,
12
		 &stroker->current_face.point,
		 clockwise, outer);
    } else {
200409
	compute_face (point, tangent, stroker, &face);
200409
	if ((face.dev_slope.x * stroker->current_face.dev_slope.x +
200409
	     face.dev_slope.y * stroker->current_face.dev_slope.y) < stroker->spline_cusp_tolerance)
	{
	    struct stroke_contour *outer;
291
	    int clockwise = join_is_clockwise (&stroker->current_face, &face);
291
	    stroker->current_face.cw.x += face.point.x - stroker->current_face.point.x;
291
	    stroker->current_face.cw.y += face.point.y - stroker->current_face.point.y;
291
	    contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
291
	    stroker->current_face.ccw.x += face.point.x - stroker->current_face.point.x;
291
	    stroker->current_face.ccw.y += face.point.y - stroker->current_face.point.y;
291
	    contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
291
	    if (clockwise) {
132
		outer = &stroker->cw;
	    } else {
159
		outer = &stroker->ccw;
	    }
291
	    add_fan (stroker,
291
		     &stroker->current_face.dev_vector,
		     &face.dev_vector,
291
		     &stroker->current_face.point,
		     clockwise, outer);
	}
200409
	contour_add_point (stroker, &stroker->cw, &face.cw);
200409
	contour_add_point (stroker, &stroker->ccw, &face.ccw);
    }
200421
    stroker->current_face = face;
200421
    return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
11454
curve_to (void *closure,
	  const cairo_point_t *b,
	  const cairo_point_t *c,
	  const cairo_point_t *d)
{
11454
    struct stroker *stroker = closure;
    cairo_spline_t spline;
    cairo_stroke_face_t face;
11454
    if (stroker->has_bounds &&
6600
	! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
6600
				    &stroker->bounds))
852
	return line_to (closure, d);
10602
    if (! _cairo_spline_init (&spline, spline_to, stroker,
10602
			      &stroker->current_face.point, b, c, d))
48
	return line_to (closure, d);
10554
    compute_face (&stroker->current_face.point, &spline.initial_slope,
		  stroker, &face);
10554
    if (stroker->has_current_face) {
7869
	int clockwise = join_is_clockwise (&stroker->current_face, &face);
	/* Join with final face from previous segment */
7869
	outer_join (stroker, &stroker->current_face, &face, clockwise);
7869
	inner_join (stroker, &stroker->current_face, &face, clockwise);
    } else {
2685
	if (! stroker->has_first_face) {
	    /* Save sub path's first face in case needed for closing join */
2685
	    stroker->first_face = face;
2685
	    stroker->has_first_face = TRUE;
	}
2685
	stroker->has_current_face = TRUE;
2685
	contour_add_point (stroker, &stroker->cw, &face.cw);
2685
	contour_add_point (stroker, &stroker->ccw, &face.ccw);
    }
10554
    stroker->current_face = face;
10554
    return _cairo_spline_decompose (&spline, stroker->tolerance);
}
static cairo_status_t
19065
close_path (void *closure)
{
19065
    struct stroker *stroker = closure;
    cairo_status_t status;
19065
    status = line_to (stroker, &stroker->first_point);
19065
    if (unlikely (status))
	return status;
19065
    if (stroker->has_first_face && stroker->has_current_face) {
	/* Join first and final faces of sub path */
19032
	outer_close (stroker, &stroker->current_face, &stroker->first_face);
19032
	inner_close (stroker, &stroker->current_face, &stroker->first_face);
#if 0
	*_cairo_contour_first_point (&stroker->ccw.contour) =
	    *_cairo_contour_last_point (&stroker->ccw.contour);
	*_cairo_contour_first_point (&stroker->cw.contour) =
	    *_cairo_contour_last_point (&stroker->cw.contour);
#endif
19032
	_cairo_polygon_add_contour (stroker->polygon, &stroker->cw.contour);
19032
	_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
#if DEBUG
	{
	    FILE *file = fopen ("contours.txt", "a");
	    _cairo_debug_print_contour (file, &stroker->path);
	    _cairo_debug_print_contour (file, &stroker->cw.contour);
	    _cairo_debug_print_contour (file, &stroker->ccw.contour);
	    fclose (file);
	    _cairo_contour_reset (&stroker->path);
	}
#endif
19032
	_cairo_contour_reset (&stroker->cw.contour);
19032
	_cairo_contour_reset (&stroker->ccw.contour);
    } else {
	/* Cap the start and end of the sub path as needed */
33
	add_caps (stroker);
    }
19065
    stroker->has_initial_sub_path = FALSE;
19065
    stroker->has_first_face = FALSE;
19065
    stroker->has_current_face = FALSE;
19065
    return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
38529
_cairo_path_fixed_stroke_to_polygon (const cairo_path_fixed_t	*path,
				     const cairo_stroke_style_t	*style,
				     const cairo_matrix_t	*ctm,
				     const cairo_matrix_t	*ctm_inverse,
				     double		 tolerance,
				     cairo_polygon_t *polygon)
{
    struct stroker stroker;
    cairo_status_t status;
38529
    if (style->num_dashes) {
771
	return _cairo_path_fixed_stroke_dashed_to_polygon (path,
							   style,
							   ctm,
							   ctm_inverse,
							   tolerance,
							   polygon);
    }
37758
    stroker.has_bounds = polygon->num_limits;
37758
    if (stroker.has_bounds) {
	/* Extend the bounds in each direction to account for the maximum area
	 * we might generate trapezoids, to capture line segments that are
	 * outside of the bounds but which might generate rendering that's
	 * within bounds.
	 */
	double dx, dy;
	cairo_fixed_t fdx, fdy;
	int i;
34242
	stroker.bounds = polygon->limits[0];
34242
	for (i = 1; i < polygon->num_limits; i++)
	     _cairo_box_add_box (&stroker.bounds, &polygon->limits[i]);
34242
	_cairo_stroke_style_max_distance_from_path (style, path, ctm, &dx, &dy);
34242
	fdx = _cairo_fixed_from_double (dx);
34242
	fdy = _cairo_fixed_from_double (dy);
34242
	stroker.bounds.p1.x -= fdx;
34242
	stroker.bounds.p2.x += fdx;
34242
	stroker.bounds.p1.y -= fdy;
34242
	stroker.bounds.p2.y += fdy;
    }
37758
    stroker.style = *style;
37758
    stroker.ctm = ctm;
37758
    stroker.ctm_inverse = ctm_inverse;
37758
    stroker.tolerance = tolerance;
37758
    stroker.half_line_width = style->line_width / 2.;
    /* If `CAIRO_LINE_JOIN_ROUND` is selected and a joint's `arc height`
     * is greater than `tolerance` then two segments are joined with
     * round-join, otherwise bevel-join is used.
     *
     * (See https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/372#note_1698225
     *  for an illustration.)
     *
     * `Arc height` is the distance from the center of arc's chord to
     * the center of the arc. It is also the difference of arc's radius
     * and the "distance from a point where segments are joined to the
     * chord" (distance to the chord). Arc's radius is the half of a line
     * width and the "distance to the chord" is equal to "half of a line width"
     * times `cos(half the angle between segment vectors)`. So
     *
     *     arc_height = w/2 - w/2 * cos(phi/2),
     *
     * where `w/2` is the "half of a line width".
     *
     * Using the double angle cosine formula we can express the `cos(phi/2)`
     * with just `cos(phi)` which is also the dot product of segments'
     * unit vectors.
     *
     *     cos(phi/2) = sqrt ( (1 + cos(phi)) / 2 );
     *     cos(phi/2) is in [0; 1] range, cannot be negative;
     *
     *     cos(phi) = a . b  = (ax * bx + ay * by),
     *
     * where `a` and `b` are unit vectors of the segments to be joined.
     *
     * Since `arc height` should be greater than the `tolerance` to produce
     * a round-join we can write
     *
     *     w/2 * (1 - cos(phi/2))  >  tolerance;
     *     1 - tolerance / (w/2)  >  cos(phi/2);    [!]
     *
     * which can be rewritten with the above double angle formula to
     *
     *     cos(phi)  <  2 * ( 1 - tolerance / (w/2) )^2 - 1,
     *
     * [!] Note that `w/2` is in [tolerance; +inf] range, since `cos(phi/2)`
     * cannot be negative. The left part of the above inequality is the
     * dot product and the right part is the `spline_cusp_tolerance`:
     *
     *     (ax * bx + ay * by) < spline_cusp_tolerance.
     *
     * In the code below only the `spline_cusp_tolerance` is calculated.
     * The dot product is calculated later, in the condition expression
     * itself. "Half of a line width" must be scaled with CTM for tolerance
     * condition to be properly met. Also, since `arch height` cannot exceed
     * the "half of a line width" and since `cos(phi/2)` cannot be negative,
     * when `tolerance` is greater than the "half of a line width" the
     * bevel-join should be produced.
     */
37758
    double scaled_hlw = hypot(stroker.half_line_width * ctm->xx,
37758
			      stroker.half_line_width * ctm->yx);
37758
    if (scaled_hlw <= tolerance) {
1011
	stroker.spline_cusp_tolerance = -1.0;
    } else {
36747
	stroker.spline_cusp_tolerance = 1 - tolerance / scaled_hlw;
36747
	stroker.spline_cusp_tolerance *= stroker.spline_cusp_tolerance;
36747
	stroker.spline_cusp_tolerance *= 2;
36747
	stroker.spline_cusp_tolerance -= 1;
    }
37758
    stroker.ctm_det_positive =
37758
	_cairo_matrix_compute_determinant (ctm) >= 0.0;
37758
    stroker.pen.num_vertices = 0;
37758
    if (path->has_curve_to ||
34911
	style->line_join == CAIRO_LINE_JOIN_ROUND ||
34308
	style->line_cap == CAIRO_LINE_CAP_ROUND) {
3483
	status = _cairo_pen_init (&stroker.pen,
				  stroker.half_line_width,
				  tolerance, ctm);
3483
	if (unlikely (status))
	    return status;
	/* If the line width is so small that the pen is reduced to a
	   single point, then we have nothing to do. */
3483
	if (stroker.pen.num_vertices <= 1)
	    return CAIRO_STATUS_SUCCESS;
    }
37758
    stroker.has_current_face = FALSE;
37758
    stroker.has_first_face = FALSE;
37758
    stroker.has_initial_sub_path = FALSE;
#if DEBUG
    remove ("contours.txt");
    remove ("polygons.txt");
    _cairo_contour_init (&stroker.path, 0);
#endif
37758
    _cairo_contour_init (&stroker.cw.contour, 1);
37758
    _cairo_contour_init (&stroker.ccw.contour, -1);
37758
    tolerance *= CAIRO_FIXED_ONE;
37758
    tolerance *= tolerance;
37758
    stroker.contour_tolerance = tolerance;
37758
    stroker.polygon = polygon;
37758
    status = _cairo_path_fixed_interpret (path,
					  move_to,
					  line_to,
					  curve_to,
					  close_path,
					  &stroker);
    /* Cap the start and end of the final sub path as needed */
37758
    if (likely (status == CAIRO_STATUS_SUCCESS))
37758
	add_caps (&stroker);
37758
    _cairo_contour_fini (&stroker.cw.contour);
37758
    _cairo_contour_fini (&stroker.ccw.contour);
37758
    if (stroker.pen.num_vertices)
3483
	_cairo_pen_fini (&stroker.pen);
#if DEBUG
    {
	FILE *file = fopen ("polygons.txt", "a");
	_cairo_debug_print_polygon (file, polygon);
	fclose (file);
    }
#endif
37758
    return status;
}