Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/cairo/src/cairo-path-stroke-traps.c
Line
Count
Source (jump to first uncovered line)
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 © 2013 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
#include "cairoint.h"
41
42
#include "cairo-box-inline.h"
43
#include "cairo-path-fixed-private.h"
44
#include "cairo-slope-private.h"
45
#include "cairo-stroke-dash-private.h"
46
#include "cairo-traps-private.h"
47
48
#include <float.h>
49
50
struct stroker {
51
    const cairo_stroke_style_t  *style;
52
53
    const cairo_matrix_t *ctm;
54
    const cairo_matrix_t *ctm_inverse;
55
    double spline_cusp_tolerance;
56
    double half_line_width;
57
    double tolerance;
58
    double ctm_determinant;
59
    cairo_bool_t ctm_det_positive;
60
    cairo_line_join_t line_join;
61
62
    cairo_traps_t *traps;
63
64
    cairo_pen_t pen;
65
66
    cairo_point_t first_point;
67
68
    cairo_bool_t has_initial_sub_path;
69
70
    cairo_bool_t has_current_face;
71
    cairo_stroke_face_t current_face;
72
73
    cairo_bool_t has_first_face;
74
    cairo_stroke_face_t first_face;
75
76
    cairo_stroker_dash_t dash;
77
78
    cairo_bool_t has_bounds;
79
    cairo_box_t tight_bounds;
80
    cairo_box_t line_bounds;
81
    cairo_box_t join_bounds;
82
};
83
84
static cairo_status_t
85
stroker_init (struct stroker    *stroker,
86
        const cairo_path_fixed_t  *path,
87
        const cairo_stroke_style_t  *style,
88
        const cairo_matrix_t  *ctm,
89
        const cairo_matrix_t  *ctm_inverse,
90
        double       tolerance,
91
        cairo_traps_t   *traps)
92
0
{
93
0
    cairo_status_t status;
94
95
0
    stroker->style = style;
96
0
    stroker->ctm = ctm;
97
0
    stroker->ctm_inverse = NULL;
98
0
    if (! _cairo_matrix_is_identity (ctm_inverse))
99
0
  stroker->ctm_inverse = ctm_inverse;
100
0
    stroker->line_join = style->line_join;
101
0
    stroker->half_line_width = style->line_width / 2.0;
102
0
    stroker->tolerance = tolerance;
103
0
    stroker->traps = traps;
104
105
    /* To test whether we need to join two segments of a spline using
106
     * a round-join or a bevel-join, we can inspect the angle between the
107
     * two segments. If the difference between the chord distance
108
     * (half-line-width times the cosine of the bisection angle) and the
109
     * half-line-width itself is greater than tolerance then we need to
110
     * inject a point.
111
     */
112
0
    stroker->spline_cusp_tolerance = 1 - tolerance / stroker->half_line_width;
113
0
    stroker->spline_cusp_tolerance *= stroker->spline_cusp_tolerance;
114
0
    stroker->spline_cusp_tolerance *= 2;
115
0
    stroker->spline_cusp_tolerance -= 1;
116
117
0
    stroker->ctm_determinant = _cairo_matrix_compute_determinant (stroker->ctm);
118
0
    stroker->ctm_det_positive = stroker->ctm_determinant >= 0.0;
119
120
0
    status = _cairo_pen_init (&stroker->pen,
121
0
                  stroker->half_line_width,
122
0
            tolerance, ctm);
123
0
    if (unlikely (status))
124
0
  return status;
125
126
0
    stroker->has_current_face = FALSE;
127
0
    stroker->has_first_face = FALSE;
128
0
    stroker->has_initial_sub_path = FALSE;
129
130
0
    _cairo_stroker_dash_init (&stroker->dash, style);
131
132
0
    stroker->has_bounds = traps->num_limits;
133
0
    if (stroker->has_bounds) {
134
  /* Extend the bounds in each direction to account for the maximum area
135
   * we might generate trapezoids, to capture line segments that are outside
136
   * of the bounds but which might generate rendering that's within bounds.
137
   */
138
0
  double dx, dy;
139
0
  cairo_fixed_t fdx, fdy;
140
141
0
  stroker->tight_bounds = traps->bounds;
142
143
0
  _cairo_stroke_style_max_distance_from_path (stroker->style, path,
144
0
                stroker->ctm, &dx, &dy);
145
146
0
  _cairo_stroke_style_max_line_distance_from_path (stroker->style, path,
147
0
               stroker->ctm, &dx, &dy);
148
149
0
  fdx = _cairo_fixed_from_double (dx);
150
0
  fdy = _cairo_fixed_from_double (dy);
151
152
0
  stroker->line_bounds = stroker->tight_bounds;
153
0
  stroker->line_bounds.p1.x -= fdx;
154
0
  stroker->line_bounds.p2.x += fdx;
155
0
  stroker->line_bounds.p1.y -= fdy;
156
0
  stroker->line_bounds.p2.y += fdy;
157
158
0
  _cairo_stroke_style_max_join_distance_from_path (stroker->style, path,
159
0
               stroker->ctm, &dx, &dy);
160
161
0
  fdx = _cairo_fixed_from_double (dx);
162
0
  fdy = _cairo_fixed_from_double (dy);
163
164
0
  stroker->join_bounds = stroker->tight_bounds;
165
0
  stroker->join_bounds.p1.x -= fdx;
166
0
  stroker->join_bounds.p2.x += fdx;
167
0
  stroker->join_bounds.p1.y -= fdy;
168
0
  stroker->join_bounds.p2.y += fdy;
169
0
    }
170
171
0
    return CAIRO_STATUS_SUCCESS;
172
0
}
173
174
static void
175
stroker_fini (struct stroker *stroker)
176
0
{
177
0
    _cairo_pen_fini (&stroker->pen);
178
0
}
179
180
static void
181
translate_point (cairo_point_t *point, cairo_point_t *offset)
182
0
{
183
0
    point->x += offset->x;
184
0
    point->y += offset->y;
185
0
}
186
187
static int
188
join_is_clockwise (const cairo_stroke_face_t *in,
189
       const cairo_stroke_face_t *out)
190
0
{
191
0
    return _cairo_slope_compare (&in->dev_vector, &out->dev_vector) < 0;
192
0
}
193
194
static int
195
slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
196
0
{
197
0
    double c = dx1 * dy2 - dx2 * dy1;
198
0
    if (c > 0) return 1;
199
0
    if (c < 0) return -1;
200
0
    return 0;
201
0
}
202
203
static cairo_bool_t
204
stroker_intersects_join (const struct stroker *stroker,
205
       const cairo_point_t *in,
206
       const cairo_point_t *out)
207
0
{
208
0
    cairo_line_t segment;
209
210
0
    if (! stroker->has_bounds)
211
0
  return TRUE;
212
213
0
    segment.p1 = *in;
214
0
    segment.p2 = *out;
215
0
    return _cairo_box_intersects_line_segment (&stroker->join_bounds, &segment);
216
0
}
217
218
static void
219
join (struct stroker *stroker,
220
      cairo_stroke_face_t *in,
221
      cairo_stroke_face_t *out)
222
0
{
223
0
    int clockwise = join_is_clockwise (out, in);
224
0
    cairo_point_t *inpt, *outpt;
225
226
0
    if (in->cw.x == out->cw.x &&
227
0
  in->cw.y == out->cw.y &&
228
0
  in->ccw.x == out->ccw.x &&
229
0
  in->ccw.y == out->ccw.y)
230
0
    {
231
0
  return;
232
0
    }
233
234
0
    if (clockwise) {
235
0
  inpt = &in->ccw;
236
0
  outpt = &out->ccw;
237
0
    } else {
238
0
  inpt = &in->cw;
239
0
  outpt = &out->cw;
240
0
    }
241
242
0
    if (! stroker_intersects_join (stroker, inpt, outpt))
243
0
      return;
244
245
0
    switch (stroker->line_join) {
246
0
    case CAIRO_LINE_JOIN_ROUND:
247
  /* construct a fan around the common midpoint */
248
0
  if ((in->dev_slope.x * out->dev_slope.x +
249
0
       in->dev_slope.y * out->dev_slope.y) < stroker->spline_cusp_tolerance)
250
0
  {
251
0
      int start, stop;
252
0
      cairo_point_t tri[3], edges[4];
253
0
      cairo_pen_t *pen = &stroker->pen;
254
255
0
      edges[0] = in->cw;
256
0
      edges[1] = in->ccw;
257
0
      tri[0] = in->point;
258
0
      tri[1] = *inpt;
259
0
      if (clockwise) {
260
0
    _cairo_pen_find_active_ccw_vertices (pen,
261
0
                 &in->dev_vector, &out->dev_vector,
262
0
                 &start, &stop);
263
0
    while (start != stop) {
264
0
        tri[2] = in->point;
265
0
        translate_point (&tri[2], &pen->vertices[start].point);
266
0
        edges[2] = in->point;
267
0
        edges[3] = tri[2];
268
0
        _cairo_traps_tessellate_triangle_with_edges (stroker->traps,
269
0
                 tri, edges);
270
0
        tri[1] = tri[2];
271
0
        edges[0] = edges[2];
272
0
        edges[1] = edges[3];
273
274
0
        if (start-- == 0)
275
0
      start += pen->num_vertices;
276
0
    }
277
0
      } else {
278
0
    _cairo_pen_find_active_cw_vertices (pen,
279
0
                &in->dev_vector, &out->dev_vector,
280
0
                &start, &stop);
281
0
    while (start != stop) {
282
0
        tri[2] = in->point;
283
0
        translate_point (&tri[2], &pen->vertices[start].point);
284
0
        edges[2] = in->point;
285
0
        edges[3] = tri[2];
286
0
        _cairo_traps_tessellate_triangle_with_edges (stroker->traps,
287
0
                 tri, edges);
288
0
        tri[1] = tri[2];
289
0
        edges[0] = edges[2];
290
0
        edges[1] = edges[3];
291
292
0
        if (++start == pen->num_vertices)
293
0
      start = 0;
294
0
    }
295
0
      }
296
0
      tri[2] = *outpt;
297
0
      edges[2] = out->cw;
298
0
      edges[3] = out->ccw;
299
0
      _cairo_traps_tessellate_triangle_with_edges (stroker->traps,
300
0
               tri, edges);
301
0
  } else {
302
0
      cairo_point_t t[] = { { in->point.x, in->point.y}, { inpt->x, inpt->y }, { outpt->x, outpt->y } };
303
0
      cairo_point_t e[] = { { in->cw.x, in->cw.y}, { in->ccw.x, in->ccw.y },
304
0
          { out->cw.x, out->cw.y}, { out->ccw.x, out->ccw.y } };
305
0
      _cairo_traps_tessellate_triangle_with_edges (stroker->traps, t, e);
306
0
  }
307
0
  break;
308
309
0
    case CAIRO_LINE_JOIN_MITER:
310
0
    default: {
311
  /* dot product of incoming slope vector with outgoing slope vector */
312
0
  double in_dot_out = (-in->usr_vector.x * out->usr_vector.x +
313
0
           -in->usr_vector.y * out->usr_vector.y);
314
0
  double ml = stroker->style->miter_limit;
315
316
  /* Check the miter limit -- lines meeting at an acute angle
317
   * can generate long miters, the limit converts them to bevel
318
   *
319
   * Consider the miter join formed when two line segments
320
   * meet at an angle psi:
321
   *
322
   *     /.\
323
   *    /. .\
324
   *   /./ \.\
325
   *  /./psi\.\
326
   *
327
   * We can zoom in on the right half of that to see:
328
   *
329
   *      |\
330
   *      | \ psi/2
331
   *      |  \
332
   *      |   \
333
   *      |    \
334
   *      |     \
335
   *    miter    \
336
   *   length     \
337
   *      |        \
338
   *      |        .\
339
   *      |    .     \
340
   *      |.   line   \
341
   *       \    width  \
342
   *        \           \
343
   *
344
   *
345
   * The right triangle in that figure, (the line-width side is
346
   * shown faintly with three '.' characters), gives us the
347
   * following expression relating miter length, angle and line
348
   * width:
349
   *
350
   *  1 /sin (psi/2) = miter_length / line_width
351
   *
352
   * The right-hand side of this relationship is the same ratio
353
   * in which the miter limit (ml) is expressed. We want to know
354
   * when the miter length is within the miter limit. That is
355
   * when the following condition holds:
356
   *
357
   *  1/sin(psi/2) <= ml
358
   *  1 <= ml sin(psi/2)
359
   *  1 <= ml² sin²(psi/2)
360
   *  2 <= ml² 2 sin²(psi/2)
361
   *        2·sin²(psi/2) = 1-cos(psi)
362
   *  2 <= ml² (1-cos(psi))
363
   *
364
   *        in · out = |in| |out| cos (psi)
365
   *
366
   * in and out are both unit vectors, so:
367
   *
368
   *        in · out = cos (psi)
369
   *
370
   *  2 <= ml² (1 - in · out)
371
   *
372
   */
373
0
  if (2 <= ml * ml * (1 - in_dot_out)) {
374
0
      double    x1, y1, x2, y2;
375
0
      double    mx, my;
376
0
      double    dx1, dx2, dy1, dy2;
377
0
      cairo_point_t outer;
378
0
      cairo_point_t quad[4];
379
0
      double    ix, iy;
380
0
      double    fdx1, fdy1, fdx2, fdy2;
381
0
      double    mdx, mdy;
382
383
      /*
384
       * we've got the points already transformed to device
385
       * space, but need to do some computation with them and
386
       * also need to transform the slope from user space to
387
       * device space
388
       */
389
      /* outer point of incoming line face */
390
0
      x1 = _cairo_fixed_to_double (inpt->x);
391
0
      y1 = _cairo_fixed_to_double (inpt->y);
392
0
      dx1 = in->usr_vector.x;
393
0
      dy1 = in->usr_vector.y;
394
0
      cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
395
396
      /* outer point of outgoing line face */
397
0
      x2 = _cairo_fixed_to_double (outpt->x);
398
0
      y2 = _cairo_fixed_to_double (outpt->y);
399
0
      dx2 = out->usr_vector.x;
400
0
      dy2 = out->usr_vector.y;
401
0
      cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
402
403
      /*
404
       * Compute the location of the outer corner of the miter.
405
       * That's pretty easy -- just the intersection of the two
406
       * outer edges.  We've got slopes and points on each
407
       * of those edges.  Compute my directly, then compute
408
       * mx by using the edge with the larger dy; that avoids
409
       * dividing by values close to zero.
410
       */
411
0
      my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
412
0
      (dx1 * dy2 - dx2 * dy1));
413
0
      if (fabs (dy1) >= fabs (dy2))
414
0
    mx = (my - y1) * dx1 / dy1 + x1;
415
0
      else
416
0
    mx = (my - y2) * dx2 / dy2 + x2;
417
418
      /*
419
       * When the two outer edges are nearly parallel, slight
420
       * perturbations in the position of the outer points of the lines
421
       * caused by representing them in fixed point form can cause the
422
       * intersection point of the miter to move a large amount. If
423
       * that moves the miter intersection from between the two faces,
424
       * then draw a bevel instead.
425
       */
426
427
0
      ix = _cairo_fixed_to_double (in->point.x);
428
0
      iy = _cairo_fixed_to_double (in->point.y);
429
430
      /* slope of one face */
431
0
      fdx1 = x1 - ix; fdy1 = y1 - iy;
432
433
      /* slope of the other face */
434
0
      fdx2 = x2 - ix; fdy2 = y2 - iy;
435
436
      /* slope from the intersection to the miter point */
437
0
      mdx = mx - ix; mdy = my - iy;
438
439
      /*
440
       * Make sure the miter point line lies between the two
441
       * faces by comparing the slopes
442
       */
443
0
      if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
444
0
    slope_compare_sgn (fdx2, fdy2, mdx, mdy))
445
0
      {
446
    /*
447
     * Draw the quadrilateral
448
     */
449
0
    outer.x = _cairo_fixed_from_double (mx);
450
0
    outer.y = _cairo_fixed_from_double (my);
451
452
0
    quad[0] = in->point;
453
0
    quad[1] = *inpt;
454
0
    quad[2] = outer;
455
0
    quad[3] = *outpt;
456
457
0
    _cairo_traps_tessellate_convex_quad (stroker->traps, quad);
458
0
    break;
459
0
      }
460
0
  }
461
0
    }
462
    /* fall through ... */
463
0
    case CAIRO_LINE_JOIN_BEVEL: {
464
0
  cairo_point_t t[] = { { in->point.x, in->point.y }, { inpt->x, inpt->y }, { outpt->x, outpt->y } };
465
0
  cairo_point_t e[] = { { in->cw.x, in->cw.y }, { in->ccw.x, in->ccw.y },
466
0
            { out->cw.x, out->cw.y }, { out->ccw.x, out->ccw.y } };
467
0
  _cairo_traps_tessellate_triangle_with_edges (stroker->traps, t, e);
468
0
  break;
469
0
    }
470
0
    }
471
0
}
472
473
static void
474
add_cap (struct stroker *stroker, cairo_stroke_face_t *f)
475
0
{
476
0
    switch (stroker->style->line_cap) {
477
0
    case CAIRO_LINE_CAP_ROUND: {
478
0
  int start, stop;
479
0
  cairo_slope_t in_slope, out_slope;
480
0
  cairo_point_t tri[3], edges[4];
481
0
  cairo_pen_t *pen = &stroker->pen;
482
483
0
  in_slope = f->dev_vector;
484
0
  out_slope.dx = -in_slope.dx;
485
0
  out_slope.dy = -in_slope.dy;
486
0
  _cairo_pen_find_active_cw_vertices (pen, &in_slope, &out_slope,
487
0
              &start, &stop);
488
0
  edges[0] = f->cw;
489
0
  edges[1] = f->ccw;
490
0
  tri[0] = f->point;
491
0
  tri[1] = f->cw;
492
0
  while (start != stop) {
493
0
      tri[2] = f->point;
494
0
      translate_point (&tri[2], &pen->vertices[start].point);
495
0
      edges[2] = f->point;
496
0
      edges[3] = tri[2];
497
0
      _cairo_traps_tessellate_triangle_with_edges (stroker->traps,
498
0
               tri, edges);
499
500
0
      tri[1] = tri[2];
501
0
      edges[0] = edges[2];
502
0
      edges[1] = edges[3];
503
0
      if (++start == pen->num_vertices)
504
0
    start = 0;
505
0
  }
506
0
  tri[2] = f->ccw;
507
0
  edges[2] = f->cw;
508
0
  edges[3] = f->ccw;
509
0
  _cairo_traps_tessellate_triangle_with_edges (stroker->traps,
510
0
                 tri, edges);
511
0
  break;
512
0
    }
513
514
0
    case CAIRO_LINE_CAP_SQUARE: {
515
0
  double dx, dy;
516
0
  cairo_slope_t fvector;
517
0
  cairo_point_t quad[4];
518
519
0
  dx = f->usr_vector.x;
520
0
  dy = f->usr_vector.y;
521
0
  dx *= stroker->half_line_width;
522
0
  dy *= stroker->half_line_width;
523
0
  cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
524
0
  fvector.dx = _cairo_fixed_from_double (dx);
525
0
  fvector.dy = _cairo_fixed_from_double (dy);
526
527
0
  quad[0] = f->cw;
528
0
  quad[1].x = f->cw.x + fvector.dx;
529
0
  quad[1].y = f->cw.y + fvector.dy;
530
0
  quad[2].x = f->ccw.x + fvector.dx;
531
0
  quad[2].y = f->ccw.y + fvector.dy;
532
0
  quad[3] = f->ccw;
533
534
0
  _cairo_traps_tessellate_convex_quad (stroker->traps, quad);
535
0
  break;
536
0
    }
537
538
0
    case CAIRO_LINE_CAP_BUTT:
539
0
    default:
540
0
  break;
541
0
    }
542
0
}
543
544
static void
545
add_leading_cap (struct stroker     *stroker,
546
     cairo_stroke_face_t *face)
547
0
{
548
0
    cairo_stroke_face_t reversed;
549
0
    cairo_point_t t;
550
551
0
    reversed = *face;
552
553
    /* The initial cap needs an outward facing vector. Reverse everything */
554
0
    reversed.usr_vector.x = -reversed.usr_vector.x;
555
0
    reversed.usr_vector.y = -reversed.usr_vector.y;
556
0
    reversed.dev_vector.dx = -reversed.dev_vector.dx;
557
0
    reversed.dev_vector.dy = -reversed.dev_vector.dy;
558
0
    t = reversed.cw;
559
0
    reversed.cw = reversed.ccw;
560
0
    reversed.ccw = t;
561
562
0
    add_cap (stroker, &reversed);
563
0
}
564
565
static void
566
add_trailing_cap (struct stroker *stroker, cairo_stroke_face_t *face)
567
0
{
568
0
    add_cap (stroker, face);
569
0
}
570
571
static inline double
572
normalize_slope (double *dx, double *dy)
573
0
{
574
0
    double dx0 = *dx, dy0 = *dy;
575
576
0
    if (dx0 == 0.0 && dy0 == 0.0)
577
0
  return 0;
578
579
0
    if (dx0 == 0.0) {
580
0
  *dx = 0.0;
581
0
  if (dy0 > 0.0) {
582
0
      *dy = 1.0;
583
0
      return dy0;
584
0
  } else {
585
0
      *dy = -1.0;
586
0
      return -dy0;
587
0
  }
588
0
    } else if (dy0 == 0.0) {
589
0
  *dy = 0.0;
590
0
  if (dx0 > 0.0) {
591
0
      *dx = 1.0;
592
0
      return dx0;
593
0
  } else {
594
0
      *dx = -1.0;
595
0
      return -dx0;
596
0
  }
597
0
    } else {
598
0
  double mag = hypot (dx0, dy0);
599
0
  *dx = dx0 / mag;
600
0
  *dy = dy0 / mag;
601
0
  return mag;
602
0
    }
603
0
}
604
605
static void
606
compute_face (const cairo_point_t *point,
607
        const cairo_slope_t *dev_slope,
608
        struct stroker *stroker,
609
        cairo_stroke_face_t *face)
610
0
{
611
0
    double face_dx, face_dy;
612
0
    cairo_point_t offset_ccw, offset_cw;
613
0
    double slope_dx, slope_dy;
614
615
0
    slope_dx = _cairo_fixed_to_double (dev_slope->dx);
616
0
    slope_dy = _cairo_fixed_to_double (dev_slope->dy);
617
0
    face->length = normalize_slope (&slope_dx, &slope_dy);
618
0
    face->dev_slope.x = slope_dx;
619
0
    face->dev_slope.y = slope_dy;
620
621
    /*
622
     * rotate to get a line_width/2 vector along the face, note that
623
     * the vector must be rotated the right direction in device space,
624
     * but by 90° in user space. So, the rotation depends on
625
     * whether the ctm reflects or not, and that can be determined
626
     * by looking at the determinant of the matrix.
627
     */
628
0
    if (stroker->ctm_inverse) {
629
0
  cairo_matrix_transform_distance (stroker->ctm_inverse, &slope_dx, &slope_dy);
630
0
  normalize_slope (&slope_dx, &slope_dy);
631
632
0
  if (stroker->ctm_det_positive) {
633
0
      face_dx = - slope_dy * stroker->half_line_width;
634
0
      face_dy = slope_dx * stroker->half_line_width;
635
0
  } else {
636
0
      face_dx = slope_dy * stroker->half_line_width;
637
0
      face_dy = - slope_dx * stroker->half_line_width;
638
0
  }
639
640
  /* back to device space */
641
0
  cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
642
0
    } else {
643
0
  face_dx = - slope_dy * stroker->half_line_width;
644
0
  face_dy = slope_dx * stroker->half_line_width;
645
0
    }
646
647
0
    offset_ccw.x = _cairo_fixed_from_double (face_dx);
648
0
    offset_ccw.y = _cairo_fixed_from_double (face_dy);
649
0
    offset_cw.x = -offset_ccw.x;
650
0
    offset_cw.y = -offset_ccw.y;
651
652
0
    face->ccw = *point;
653
0
    translate_point (&face->ccw, &offset_ccw);
654
655
0
    face->point = *point;
656
657
0
    face->cw = *point;
658
0
    translate_point (&face->cw, &offset_cw);
659
660
0
    face->usr_vector.x = slope_dx;
661
0
    face->usr_vector.y = slope_dy;
662
663
0
    face->dev_vector = *dev_slope;
664
0
}
665
666
static void
667
add_caps (struct stroker *stroker)
668
0
{
669
    /* check for a degenerative sub_path */
670
0
    if (stroker->has_initial_sub_path &&
671
0
  !stroker->has_first_face &&
672
0
  !stroker->has_current_face &&
673
0
  stroker->style->line_cap == CAIRO_LINE_CAP_ROUND)
674
0
    {
675
  /* pick an arbitrary slope to use */
676
0
  cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
677
0
  cairo_stroke_face_t face;
678
679
  /* arbitrarily choose first_point
680
   * first_point and current_point should be the same */
681
0
  compute_face (&stroker->first_point, &slope, stroker, &face);
682
683
0
  add_leading_cap (stroker, &face);
684
0
  add_trailing_cap (stroker, &face);
685
0
    }
686
687
0
    if (stroker->has_first_face)
688
0
  add_leading_cap (stroker, &stroker->first_face);
689
690
0
    if (stroker->has_current_face)
691
0
  add_trailing_cap (stroker, &stroker->current_face);
692
0
}
693
694
static cairo_bool_t
695
stroker_intersects_edge (const struct stroker *stroker,
696
       const cairo_stroke_face_t *start,
697
       const cairo_stroke_face_t *end)
698
0
{
699
0
    cairo_box_t box;
700
701
0
    if (! stroker->has_bounds)
702
0
  return TRUE;
703
704
0
    if (_cairo_box_contains_point (&stroker->tight_bounds, &start->cw))
705
0
  return TRUE;
706
0
    box.p2 = box.p1 = start->cw;
707
708
0
    if (_cairo_box_contains_point (&stroker->tight_bounds, &start->ccw))
709
0
  return TRUE;
710
0
    _cairo_box_add_point (&box, &start->ccw);
711
712
0
    if (_cairo_box_contains_point (&stroker->tight_bounds, &end->cw))
713
0
  return TRUE;
714
0
    _cairo_box_add_point (&box, &end->cw);
715
716
0
    if (_cairo_box_contains_point (&stroker->tight_bounds, &end->ccw))
717
0
  return TRUE;
718
0
    _cairo_box_add_point (&box, &end->ccw);
719
720
0
    return (box.p2.x > stroker->tight_bounds.p1.x &&
721
0
      box.p1.x < stroker->tight_bounds.p2.x &&
722
0
      box.p2.y > stroker->tight_bounds.p1.y &&
723
0
      box.p1.y < stroker->tight_bounds.p2.y);
724
0
}
725
726
static void
727
add_sub_edge (struct stroker *stroker,
728
        const cairo_point_t *p1, const cairo_point_t *p2,
729
        const cairo_slope_t *dev_slope,
730
        cairo_stroke_face_t *start, cairo_stroke_face_t *end)
731
0
{
732
0
    cairo_point_t rectangle[4];
733
734
0
    compute_face (p1, dev_slope, stroker, start);
735
736
0
    *end = *start;
737
0
    end->point = *p2;
738
0
    rectangle[0].x = p2->x - p1->x;
739
0
    rectangle[0].y = p2->y - p1->y;
740
0
    translate_point (&end->ccw, &rectangle[0]);
741
0
    translate_point (&end->cw, &rectangle[0]);
742
743
0
    if (p1->x == p2->x && p1->y == p2->y)
744
0
  return;
745
746
0
    if (! stroker_intersects_edge (stroker, start, end))
747
0
  return;
748
749
0
    rectangle[0] = start->cw;
750
0
    rectangle[1] = start->ccw;
751
0
    rectangle[2] = end->ccw;
752
0
    rectangle[3] = end->cw;
753
754
0
    _cairo_traps_tessellate_convex_quad (stroker->traps, rectangle);
755
0
}
756
757
static cairo_status_t
758
move_to (void *closure, const cairo_point_t *point)
759
0
{
760
0
    struct stroker *stroker = closure;
761
762
    /* Cap the start and end of the previous sub path as needed */
763
0
    add_caps (stroker);
764
765
0
    stroker->first_point = *point;
766
0
    stroker->current_face.point = *point;
767
768
0
    stroker->has_first_face = FALSE;
769
0
    stroker->has_current_face = FALSE;
770
0
    stroker->has_initial_sub_path = FALSE;
771
772
0
    return CAIRO_STATUS_SUCCESS;
773
0
}
774
775
static cairo_status_t
776
move_to_dashed (void *closure, const cairo_point_t *point)
777
0
{
778
    /* reset the dash pattern for new sub paths */
779
0
    struct stroker *stroker = closure;
780
781
0
    _cairo_stroker_dash_start (&stroker->dash);
782
0
    return move_to (closure, point);
783
0
}
784
785
static cairo_status_t
786
line_to (void *closure, const cairo_point_t *point)
787
0
{
788
0
    struct stroker *stroker = closure;
789
0
    cairo_stroke_face_t start, end;
790
0
    const cairo_point_t *p1 = &stroker->current_face.point;
791
0
    const cairo_point_t *p2 = point;
792
0
    cairo_slope_t dev_slope;
793
794
0
    stroker->has_initial_sub_path = TRUE;
795
796
0
    if (p1->x == p2->x && p1->y == p2->y)
797
0
  return CAIRO_STATUS_SUCCESS;
798
799
0
    _cairo_slope_init (&dev_slope, p1, p2);
800
0
    add_sub_edge (stroker, p1, p2, &dev_slope, &start, &end);
801
802
0
    if (stroker->has_current_face) {
803
  /* Join with final face from previous segment */
804
0
  join (stroker, &stroker->current_face, &start);
805
0
    } else if (!stroker->has_first_face) {
806
  /* Save sub path's first face in case needed for closing join */
807
0
  stroker->first_face = start;
808
0
  stroker->has_first_face = TRUE;
809
0
    }
810
0
    stroker->current_face = end;
811
0
    stroker->has_current_face = TRUE;
812
813
0
    return CAIRO_STATUS_SUCCESS;
814
0
}
815
816
/*
817
 * Dashed lines.  Cap each dash end, join around turns when on
818
 */
819
static cairo_status_t
820
line_to_dashed (void *closure, const cairo_point_t *point)
821
0
{
822
0
    struct stroker *stroker = closure;
823
0
    double mag, remain, step_length = 0;
824
0
    double slope_dx, slope_dy;
825
0
    double dx2, dy2;
826
0
    cairo_stroke_face_t sub_start, sub_end;
827
0
    const cairo_point_t *p1 = &stroker->current_face.point;
828
0
    const cairo_point_t *p2 = point;
829
0
    cairo_slope_t dev_slope;
830
0
    cairo_line_t segment;
831
0
    cairo_bool_t fully_in_bounds;
832
833
0
    stroker->has_initial_sub_path = stroker->dash.dash_starts_on;
834
835
0
    if (p1->x == p2->x && p1->y == p2->y)
836
0
  return CAIRO_STATUS_SUCCESS;
837
838
0
    fully_in_bounds = TRUE;
839
0
    if (stroker->has_bounds &&
840
0
  (! _cairo_box_contains_point (&stroker->join_bounds, p1) ||
841
0
   ! _cairo_box_contains_point (&stroker->join_bounds, p2)))
842
0
    {
843
0
  fully_in_bounds = FALSE;
844
0
    }
845
846
0
    _cairo_slope_init (&dev_slope, p1, p2);
847
848
0
    slope_dx = _cairo_fixed_to_double (p2->x - p1->x);
849
0
    slope_dy = _cairo_fixed_to_double (p2->y - p1->y);
850
851
0
    if (stroker->ctm_inverse)
852
0
  cairo_matrix_transform_distance (stroker->ctm_inverse, &slope_dx, &slope_dy);
853
0
    mag = normalize_slope (&slope_dx, &slope_dy);
854
0
    if (mag <= DBL_EPSILON)
855
0
  return CAIRO_STATUS_SUCCESS;
856
857
0
    remain = mag;
858
0
    segment.p1 = *p1;
859
0
    while (remain) {
860
0
  step_length = MIN (stroker->dash.dash_remain, remain);
861
0
  remain -= step_length;
862
0
  dx2 = slope_dx * (mag - remain);
863
0
  dy2 = slope_dy * (mag - remain);
864
0
  cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
865
0
  segment.p2.x = _cairo_fixed_from_double (dx2) + p1->x;
866
0
  segment.p2.y = _cairo_fixed_from_double (dy2) + p1->y;
867
868
0
  if (stroker->dash.dash_on &&
869
0
      (fully_in_bounds ||
870
0
       (! stroker->has_first_face && stroker->dash.dash_starts_on) ||
871
0
       _cairo_box_intersects_line_segment (&stroker->join_bounds, &segment)))
872
0
  {
873
0
      add_sub_edge (stroker,
874
0
        &segment.p1, &segment.p2,
875
0
        &dev_slope,
876
0
        &sub_start, &sub_end);
877
878
0
      if (stroker->has_current_face) {
879
    /* Join with final face from previous segment */
880
0
    join (stroker, &stroker->current_face, &sub_start);
881
882
0
    stroker->has_current_face = FALSE;
883
0
      } else if (! stroker->has_first_face && stroker->dash.dash_starts_on) {
884
    /* Save sub path's first face in case needed for closing join */
885
0
    stroker->first_face = sub_start;
886
0
    stroker->has_first_face = TRUE;
887
0
      } else {
888
    /* Cap dash start if not connecting to a previous segment */
889
0
    add_leading_cap (stroker, &sub_start);
890
0
      }
891
892
0
      if (remain) {
893
    /* Cap dash end if not at end of segment */
894
0
    add_trailing_cap (stroker, &sub_end);
895
0
      } else {
896
0
    stroker->current_face = sub_end;
897
0
    stroker->has_current_face = TRUE;
898
0
      }
899
0
  } else {
900
0
      if (stroker->has_current_face) {
901
    /* Cap final face from previous segment */
902
0
    add_trailing_cap (stroker, &stroker->current_face);
903
904
0
    stroker->has_current_face = FALSE;
905
0
      }
906
0
  }
907
908
0
  _cairo_stroker_dash_step (&stroker->dash, step_length);
909
0
  segment.p1 = segment.p2;
910
0
    }
911
912
0
    if (stroker->dash.dash_on && ! stroker->has_current_face) {
913
  /* This segment ends on a transition to dash_on, compute a new face
914
   * and add cap for the beginning of the next dash_on step.
915
   *
916
   * Note: this will create a degenerate cap if this is not the last line
917
   * in the path. Whether this behaviour is desirable or not is debatable.
918
   * On one side these degenerate caps can not be reproduced with regular
919
   * path stroking.
920
   * On the other hand, Acroread 7 also produces the degenerate caps.
921
   */
922
0
  compute_face (point, &dev_slope, stroker, &stroker->current_face);
923
924
0
  add_leading_cap (stroker, &stroker->current_face);
925
926
0
  stroker->has_current_face = TRUE;
927
0
    } else
928
0
  stroker->current_face.point = *point;
929
930
0
    return CAIRO_STATUS_SUCCESS;
931
0
}
932
933
static cairo_status_t
934
add_point (void *closure,
935
     const cairo_point_t *point,
936
     const cairo_slope_t *tangent)
937
0
{
938
0
    return line_to_dashed (closure, point);
939
0
};
940
941
static cairo_status_t
942
spline_to (void *closure,
943
     const cairo_point_t *point,
944
     const cairo_slope_t *tangent)
945
0
{
946
0
    struct stroker *stroker = closure;
947
0
    cairo_stroke_face_t face;
948
949
0
    if ((tangent->dx | tangent->dy) == 0) {
950
0
  cairo_point_t t;
951
952
0
  face = stroker->current_face;
953
954
0
  face.usr_vector.x = -face.usr_vector.x;
955
0
  face.usr_vector.y = -face.usr_vector.y;
956
0
  face.dev_slope.x = -face.dev_slope.x;
957
0
  face.dev_slope.y = -face.dev_slope.y;
958
0
  face.dev_vector.dx = -face.dev_vector.dx;
959
0
  face.dev_vector.dy = -face.dev_vector.dy;
960
961
0
  t = face.cw;
962
0
  face.cw = face.ccw;
963
0
  face.ccw = t;
964
965
0
  join (stroker, &stroker->current_face, &face);
966
0
    } else {
967
0
  cairo_point_t rectangle[4];
968
969
0
  compute_face (&stroker->current_face.point, tangent, stroker, &face);
970
0
  join (stroker, &stroker->current_face, &face);
971
972
0
  rectangle[0] = face.cw;
973
0
  rectangle[1] = face.ccw;
974
975
0
  rectangle[2].x = point->x - face.point.x;
976
0
  rectangle[2].y = point->y - face.point.y;
977
0
  face.point = *point;
978
0
  translate_point (&face.ccw, &rectangle[2]);
979
0
  translate_point (&face.cw, &rectangle[2]);
980
981
0
  rectangle[2] = face.ccw;
982
0
  rectangle[3] = face.cw;
983
984
0
  _cairo_traps_tessellate_convex_quad (stroker->traps, rectangle);
985
0
    }
986
987
0
    stroker->current_face = face;
988
989
0
    return CAIRO_STATUS_SUCCESS;
990
0
}
991
992
static cairo_status_t
993
curve_to (void *closure,
994
    const cairo_point_t *b,
995
    const cairo_point_t *c,
996
    const cairo_point_t *d)
997
0
{
998
0
    struct stroker *stroker = closure;
999
0
    cairo_line_join_t line_join_save;
1000
0
    cairo_spline_t spline;
1001
0
    cairo_stroke_face_t face;
1002
0
    cairo_status_t status;
1003
1004
0
    if (stroker->has_bounds &&
1005
0
  ! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
1006
0
            &stroker->line_bounds))
1007
0
  return line_to (closure, d);
1008
1009
0
    if (! _cairo_spline_init (&spline, spline_to, stroker,
1010
0
            &stroker->current_face.point, b, c, d))
1011
0
  return line_to (closure, d);
1012
1013
0
    compute_face (&stroker->current_face.point, &spline.initial_slope,
1014
0
      stroker, &face);
1015
1016
0
    if (stroker->has_current_face) {
1017
  /* Join with final face from previous segment */
1018
0
  join (stroker, &stroker->current_face, &face);
1019
0
    } else {
1020
0
  if (! stroker->has_first_face) {
1021
      /* Save sub path's first face in case needed for closing join */
1022
0
      stroker->first_face = face;
1023
0
      stroker->has_first_face = TRUE;
1024
0
  }
1025
0
  stroker->has_current_face = TRUE;
1026
0
    }
1027
0
    stroker->current_face = face;
1028
1029
    /* Temporarily modify the stroker to use round joins to guarantee
1030
     * smooth stroked curves. */
1031
0
    line_join_save = stroker->line_join;
1032
0
    stroker->line_join = CAIRO_LINE_JOIN_ROUND;
1033
1034
0
    status = _cairo_spline_decompose (&spline, stroker->tolerance);
1035
1036
0
    stroker->line_join = line_join_save;
1037
1038
0
    return status;
1039
0
}
1040
1041
static cairo_status_t
1042
curve_to_dashed (void *closure,
1043
     const cairo_point_t *b,
1044
     const cairo_point_t *c,
1045
     const cairo_point_t *d)
1046
0
{
1047
0
    struct stroker *stroker = closure;
1048
0
    cairo_spline_t spline;
1049
0
    cairo_line_join_t line_join_save;
1050
0
    cairo_spline_add_point_func_t func;
1051
0
    cairo_status_t status;
1052
1053
0
    func = add_point;
1054
1055
0
    if (stroker->has_bounds &&
1056
0
  ! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
1057
0
            &stroker->line_bounds))
1058
0
  return func (closure, d, NULL);
1059
1060
0
    if (! _cairo_spline_init (&spline, func, stroker,
1061
0
            &stroker->current_face.point, b, c, d))
1062
0
  return func (closure, d, NULL);
1063
1064
    /* Temporarily modify the stroker to use round joins to guarantee
1065
     * smooth stroked curves. */
1066
0
    line_join_save = stroker->line_join;
1067
0
    stroker->line_join = CAIRO_LINE_JOIN_ROUND;
1068
1069
0
    status = _cairo_spline_decompose (&spline, stroker->tolerance);
1070
1071
0
    stroker->line_join = line_join_save;
1072
1073
0
    return status;
1074
0
}
1075
1076
static cairo_status_t
1077
_close_path (struct stroker *stroker)
1078
0
{
1079
0
    if (stroker->has_first_face && stroker->has_current_face) {
1080
  /* Join first and final faces of sub path */
1081
0
  join (stroker, &stroker->current_face, &stroker->first_face);
1082
0
    } else {
1083
  /* Cap the start and end of the sub path as needed */
1084
0
  add_caps (stroker);
1085
0
    }
1086
1087
0
    stroker->has_initial_sub_path = FALSE;
1088
0
    stroker->has_first_face = FALSE;
1089
0
    stroker->has_current_face = FALSE;
1090
0
    return CAIRO_STATUS_SUCCESS;
1091
0
}
1092
1093
static cairo_status_t
1094
close_path (void *closure)
1095
0
{
1096
0
    struct stroker *stroker = closure;
1097
0
    cairo_status_t status;
1098
1099
0
    status = line_to (stroker, &stroker->first_point);
1100
0
    if (unlikely (status))
1101
0
  return status;
1102
1103
0
    return _close_path (stroker);
1104
0
}
1105
1106
static cairo_status_t
1107
close_path_dashed (void *closure)
1108
0
{
1109
0
    struct stroker *stroker = closure;
1110
0
    cairo_status_t status;
1111
1112
0
    status = line_to_dashed (stroker, &stroker->first_point);
1113
0
    if (unlikely (status))
1114
0
  return status;
1115
1116
0
    return _close_path (stroker);
1117
0
}
1118
1119
cairo_int_status_t
1120
_cairo_path_fixed_stroke_to_traps (const cairo_path_fixed_t *path,
1121
           const cairo_stroke_style_t *style,
1122
           const cairo_matrix_t   *ctm,
1123
           const cairo_matrix_t   *ctm_inverse,
1124
           double      tolerance,
1125
           cairo_traps_t    *traps)
1126
0
{
1127
0
    struct stroker stroker;
1128
0
    cairo_status_t status;
1129
1130
0
    status = stroker_init (&stroker, path, style,
1131
0
         ctm, ctm_inverse, tolerance,
1132
0
         traps);
1133
0
    if (unlikely (status))
1134
0
  return status;
1135
1136
0
    if (stroker.dash.dashed)
1137
0
  status = _cairo_path_fixed_interpret (path,
1138
0
                move_to_dashed,
1139
0
                line_to_dashed,
1140
0
                curve_to_dashed,
1141
0
                close_path_dashed,
1142
0
                &stroker);
1143
0
    else
1144
0
  status = _cairo_path_fixed_interpret (path,
1145
0
                move_to,
1146
0
                line_to,
1147
0
                curve_to,
1148
0
                close_path,
1149
0
                &stroker);
1150
0
    assert(status == CAIRO_STATUS_SUCCESS);
1151
0
    add_caps (&stroker);
1152
1153
0
    stroker_fini (&stroker);
1154
1155
0
    return traps->status;
1156
0
}