Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/cairo/src/cairo-spline.c
Line
Count
Source
1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2002 University of Southern California
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it either under the terms of the GNU Lesser General Public
7
 * License version 2.1 as published by the Free Software Foundation
8
 * (the "LGPL") or, at your option, under the terms of the Mozilla
9
 * Public License Version 1.1 (the "MPL"). If you do not alter this
10
 * notice, a recipient may use your version of this file under either
11
 * the MPL or the LGPL.
12
 *
13
 * You should have received a copy of the LGPL along with this library
14
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16
 * You should have received a copy of the MPL along with this library
17
 * in the file COPYING-MPL-1.1
18
 *
19
 * The contents of this file are subject to the Mozilla Public License
20
 * Version 1.1 (the "License"); you may not use this file except in
21
 * compliance with the License. You may obtain a copy of the License at
22
 * http://www.mozilla.org/MPL/
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
 * the specific language governing rights and limitations.
27
 *
28
 * The Original Code is the cairo graphics library.
29
 *
30
 * The Initial Developer of the Original Code is University of Southern
31
 * California.
32
 *
33
 * Contributor(s):
34
 *  Carl D. Worth <cworth@cworth.org>
35
 */
36
37
#include "cairoint.h"
38
39
#include "cairo-box-inline.h"
40
#include "cairo-slope-private.h"
41
42
cairo_bool_t
43
_cairo_spline_intersects (const cairo_point_t *a,
44
        const cairo_point_t *b,
45
        const cairo_point_t *c,
46
        const cairo_point_t *d,
47
        const cairo_box_t *box)
48
0
{
49
0
    cairo_box_t bounds;
50
51
0
    if (_cairo_box_contains_point (box, a) ||
52
0
  _cairo_box_contains_point (box, b) ||
53
0
  _cairo_box_contains_point (box, c) ||
54
0
  _cairo_box_contains_point (box, d))
55
0
    {
56
0
  return TRUE;
57
0
    }
58
59
0
    bounds.p2 = bounds.p1 = *a;
60
0
    _cairo_box_add_point (&bounds, b);
61
0
    _cairo_box_add_point (&bounds, c);
62
0
    _cairo_box_add_point (&bounds, d);
63
64
0
    if (bounds.p2.x <= box->p1.x || bounds.p1.x >= box->p2.x ||
65
0
  bounds.p2.y <= box->p1.y || bounds.p1.y >= box->p2.y)
66
0
    {
67
0
  return FALSE;
68
0
    }
69
70
#if 0 /* worth refining? */
71
    bounds.p2 = bounds.p1 = *a;
72
    _cairo_box_add_curve_to (&bounds, b, c, d);
73
    if (bounds.p2.x <= box->p1.x || bounds.p1.x >= box->p2.x ||
74
  bounds.p2.y <= box->p1.y || bounds.p1.y >= box->p2.y)
75
    {
76
  return FALSE;
77
    }
78
#endif
79
80
0
    return TRUE;
81
0
}
82
83
cairo_bool_t
84
_cairo_spline_init (cairo_spline_t *spline,
85
        cairo_spline_add_point_func_t add_point_func,
86
        void *closure,
87
        const cairo_point_t *a, const cairo_point_t *b,
88
        const cairo_point_t *c, const cairo_point_t *d)
89
228
{
90
    /* If both tangents are zero, this is just a straight line */
91
228
    if (a->x == b->x && a->y == b->y && c->x == d->x && c->y == d->y)
92
0
  return FALSE;
93
94
228
    spline->add_point_func = add_point_func;
95
228
    spline->closure = closure;
96
97
228
    spline->knots.a = *a;
98
228
    spline->knots.b = *b;
99
228
    spline->knots.c = *c;
100
228
    spline->knots.d = *d;
101
102
228
    if (a->x != b->x || a->y != b->y)
103
228
  _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.b);
104
0
    else if (a->x != c->x || a->y != c->y)
105
0
  _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.c);
106
0
    else if (a->x != d->x || a->y != d->y)
107
0
  _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.d);
108
0
    else
109
0
  return FALSE;
110
111
228
    if (c->x != d->x || c->y != d->y)
112
228
  _cairo_slope_init (&spline->final_slope, &spline->knots.c, &spline->knots.d);
113
0
    else if (b->x != d->x || b->y != d->y)
114
0
  _cairo_slope_init (&spline->final_slope, &spline->knots.b, &spline->knots.d);
115
0
    else
116
0
  return FALSE; /* just treat this as a straight-line from a -> d */
117
118
    /* XXX if the initial, final and vector are all equal, this is just a line */
119
120
228
    return TRUE;
121
228
}
122
123
static cairo_status_t
124
_cairo_spline_add_point (cairo_spline_t *spline,
125
       const cairo_point_t *point,
126
       const cairo_point_t *knot)
127
356
{
128
356
    cairo_point_t *prev;
129
356
    cairo_slope_t slope;
130
131
356
    prev = &spline->last_point;
132
356
    if (prev->x == point->x && prev->y == point->y)
133
228
  return CAIRO_STATUS_SUCCESS;
134
135
128
    _cairo_slope_init (&slope, point, knot);
136
137
128
    spline->last_point = *point;
138
128
    return spline->add_point_func (spline->closure, point, &slope);
139
356
}
140
141
static void
142
_lerp_half (const cairo_point_t *a, const cairo_point_t *b, cairo_point_t *result)
143
768
{
144
    /* The midpoint lies between the two points, so the result fits in a
145
     * cairo_fixed_t even when the distance between them does not. */
146
768
    result->x = (cairo_fixed_t) ((int64_t) a->x + (((int64_t) b->x - a->x) >> 1));
147
768
    result->y = (cairo_fixed_t) ((int64_t) a->y + (((int64_t) b->y - a->y) >> 1));
148
768
}
149
150
static void
151
_de_casteljau (cairo_spline_knots_t *s1, cairo_spline_knots_t *s2)
152
128
{
153
128
    cairo_point_t ab, bc, cd;
154
128
    cairo_point_t abbc, bccd;
155
128
    cairo_point_t final;
156
157
128
    _lerp_half (&s1->a, &s1->b, &ab);
158
128
    _lerp_half (&s1->b, &s1->c, &bc);
159
128
    _lerp_half (&s1->c, &s1->d, &cd);
160
128
    _lerp_half (&ab, &bc, &abbc);
161
128
    _lerp_half (&bc, &cd, &bccd);
162
128
    _lerp_half (&abbc, &bccd, &final);
163
164
128
    s2->a = final;
165
128
    s2->b = bccd;
166
128
    s2->c = cd;
167
128
    s2->d = s1->d;
168
169
128
    s1->b = ab;
170
128
    s1->c = abbc;
171
128
    s1->d = final;
172
128
}
173
174
/* Return an upper bound on the error (squared) that could result from
175
 * approximating a spline as a line segment connecting the two endpoints. */
176
static double
177
_cairo_spline_error_squared (const cairo_spline_knots_t *knots)
178
484
{
179
484
    double bdx, bdy, berr;
180
484
    double cdx, cdy, cerr;
181
182
    /* We are going to compute the distance (squared) between each of the b
183
     * and c control points and the segment a-b. The maximum of these two
184
     * distances will be our approximation error. */
185
186
484
    bdx = _cairo_fixed_to_double (knots->b.x) - _cairo_fixed_to_double (knots->a.x);
187
484
    bdy = _cairo_fixed_to_double (knots->b.y) - _cairo_fixed_to_double (knots->a.y);
188
189
484
    cdx = _cairo_fixed_to_double (knots->c.x) - _cairo_fixed_to_double (knots->a.x);
190
484
    cdy = _cairo_fixed_to_double (knots->c.y) - _cairo_fixed_to_double (knots->a.y);
191
192
484
    if (knots->a.x != knots->d.x || knots->a.y != knots->d.y) {
193
  /* Intersection point (px):
194
   *     px = p1 + u(p2 - p1)
195
   *     (p - px) ∙ (p2 - p1) = 0
196
   * Thus:
197
   *     u = ((p - p1) ∙ (p2 - p1)) / ∥p2 - p1∥²;
198
   */
199
200
484
  double dx, dy, u, v;
201
202
484
  dx = _cairo_fixed_to_double (knots->d.x) - _cairo_fixed_to_double (knots->a.x);
203
484
  dy = _cairo_fixed_to_double (knots->d.y) - _cairo_fixed_to_double (knots->a.y);
204
484
   v = dx * dx + dy * dy;
205
206
484
  u = bdx * dx + bdy * dy;
207
484
  if (u <= 0) {
208
      /* bdx -= 0;
209
       * bdy -= 0;
210
       */
211
484
  } else if (u >= v) {
212
0
      bdx -= dx;
213
0
      bdy -= dy;
214
484
  } else {
215
484
      bdx -= u/v * dx;
216
484
      bdy -= u/v * dy;
217
484
  }
218
219
484
  u = cdx * dx + cdy * dy;
220
484
  if (u <= 0) {
221
      /* cdx -= 0;
222
       * cdy -= 0;
223
       */
224
484
  } else if (u >= v) {
225
0
      cdx -= dx;
226
0
      cdy -= dy;
227
484
  } else {
228
484
      cdx -= u/v * dx;
229
484
      cdy -= u/v * dy;
230
484
  }
231
484
    }
232
233
484
    berr = bdx * bdx + bdy * bdy;
234
484
    cerr = cdx * cdx + cdy * cdy;
235
484
    if (berr > cerr)
236
224
  return berr;
237
260
    else
238
260
  return cerr;
239
484
}
240
241
static cairo_status_t
242
_cairo_spline_decompose_into (cairo_spline_knots_t *s1,
243
            double tolerance_squared,
244
            cairo_spline_t *result)
245
484
{
246
484
    cairo_spline_knots_t s2;
247
484
    cairo_status_t status;
248
249
484
    if (_cairo_spline_error_squared (s1) < tolerance_squared)
250
356
  return _cairo_spline_add_point (result, &s1->a, &s1->b);
251
252
128
    _de_casteljau (s1, &s2);
253
254
128
    status = _cairo_spline_decompose_into (s1, tolerance_squared, result);
255
128
    if (unlikely (status))
256
0
  return status;
257
258
128
    return _cairo_spline_decompose_into (&s2, tolerance_squared, result);
259
128
}
260
261
cairo_status_t
262
_cairo_spline_decompose (cairo_spline_t *spline, double tolerance)
263
228
{
264
228
    cairo_spline_knots_t s1;
265
228
    cairo_status_t status;
266
267
228
    s1 = spline->knots;
268
228
    spline->last_point = s1.a;
269
228
    status = _cairo_spline_decompose_into (&s1, tolerance * tolerance, spline);
270
228
    if (unlikely (status))
271
0
  return status;
272
273
228
    return spline->add_point_func (spline->closure,
274
228
           &spline->knots.d, &spline->final_slope);
275
228
}
276
277
/* Note: this function is only good for computing bounds in device space. */
278
cairo_status_t
279
_cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
280
         void *closure,
281
         const cairo_point_t *p0, const cairo_point_t *p1,
282
         const cairo_point_t *p2, const cairo_point_t *p3)
283
0
{
284
0
    double x0, x1, x2, x3;
285
0
    double y0, y1, y2, y3;
286
0
    double a, b, c;
287
0
    double t[4];
288
0
    int t_num = 0, i;
289
0
    cairo_status_t status;
290
291
0
    x0 = _cairo_fixed_to_double (p0->x);
292
0
    y0 = _cairo_fixed_to_double (p0->y);
293
0
    x1 = _cairo_fixed_to_double (p1->x);
294
0
    y1 = _cairo_fixed_to_double (p1->y);
295
0
    x2 = _cairo_fixed_to_double (p2->x);
296
0
    y2 = _cairo_fixed_to_double (p2->y);
297
0
    x3 = _cairo_fixed_to_double (p3->x);
298
0
    y3 = _cairo_fixed_to_double (p3->y);
299
300
    /* The spline can be written as a polynomial of the four points:
301
     *
302
     *   (1-t)³p0 + 3t(1-t)²p1 + 3t²(1-t)p2 + t³p3
303
     *
304
     * for 0≤t≤1.  Now, the X and Y components of the spline follow the
305
     * same polynomial but with x and y replaced for p.  To find the
306
     * bounds of the spline, we just need to find the X and Y bounds.
307
     * To find the bound, we take the derivative and equal it to zero,
308
     * and solve to find the t's that give the extreme points.
309
     *
310
     * Here is the derivative of the curve, sorted on t:
311
     *
312
     *   3t²(-p0+3p1-3p2+p3) + 2t(3p0-6p1+3p2) -3p0+3p1
313
     *
314
     * Let:
315
     *
316
     *   a = -p0+3p1-3p2+p3
317
     *   b =  p0-2p1+p2
318
     *   c = -p0+p1
319
     *
320
     * Gives:
321
     *
322
     *   a.t² + 2b.t + c = 0
323
     *
324
     * With:
325
     *
326
     *   delta = b*b - a*c
327
     *
328
     * the extreme points are at -c/2b if a is zero, at (-b±√delta)/a if
329
     * delta is positive, and at -b/a if delta is zero.
330
     */
331
332
0
#define ADD(t0) \
333
0
    { \
334
0
  double _t0 = (t0); \
335
0
  if (0 < _t0 && _t0 < 1) \
336
0
      t[t_num++] = _t0; \
337
0
    }
338
339
0
#define FIND_EXTREMES(a,b,c) \
340
0
    { \
341
0
  if (a == 0) { \
342
0
      if (b != 0) \
343
0
    ADD (-c / (2*b)); \
344
0
  } else { \
345
0
      double b2 = b * b; \
346
0
      double delta = b2 - a * c; \
347
0
      if (delta > 0) { \
348
0
    cairo_bool_t feasible; \
349
0
    double _2ab = 2 * a * b; \
350
    /* We are only interested in solutions t that satisfy 0<t<1 \
351
     * here.  We do some checks to avoid sqrt if the solutions \
352
     * are not in that range.  The checks can be derived from: \
353
     * \
354
     *   0 < (-b±√delta)/a < 1 \
355
     */ \
356
0
    if (_2ab >= 0) \
357
0
        feasible = delta > b2 && delta < a*a + b2 + _2ab; \
358
0
    else if (-b / a >= 1) \
359
0
        feasible = delta < b2 && delta > a*a + b2 + _2ab; \
360
0
    else \
361
0
        feasible = delta < b2 || delta < a*a + b2 + _2ab; \
362
0
          \
363
0
    if (unlikely (feasible)) { \
364
0
        double sqrt_delta = sqrt (delta); \
365
0
        ADD ((-b - sqrt_delta) / a); \
366
0
        ADD ((-b + sqrt_delta) / a); \
367
0
    } \
368
0
      } else if (delta == 0) { \
369
0
    ADD (-b / a); \
370
0
      } \
371
0
  } \
372
0
    }
373
374
    /* Find X extremes */
375
0
    a = -x0 + 3*x1 - 3*x2 + x3;
376
0
    b =  x0 - 2*x1 + x2;
377
0
    c = -x0 + x1;
378
0
    FIND_EXTREMES (a, b, c);
379
380
    /* Find Y extremes */
381
0
    a = -y0 + 3*y1 - 3*y2 + y3;
382
0
    b =  y0 - 2*y1 + y2;
383
0
    c = -y0 + y1;
384
0
    FIND_EXTREMES (a, b, c);
385
386
0
    status = add_point_func (closure, p0, NULL);
387
0
    if (unlikely (status))
388
0
  return status;
389
390
0
    for (i = 0; i < t_num; i++) {
391
0
  cairo_point_t p;
392
0
  double x, y;
393
0
        double t_1_0, t_0_1;
394
0
        double t_2_0, t_0_2;
395
0
        double t_3_0, t_2_1_3, t_1_2_3, t_0_3;
396
397
0
        t_1_0 = t[i];          /*      t  */
398
0
        t_0_1 = 1 - t_1_0;     /* (1 - t) */
399
400
0
        t_2_0 = t_1_0 * t_1_0; /*      t  *      t  */
401
0
        t_0_2 = t_0_1 * t_0_1; /* (1 - t) * (1 - t) */
402
403
0
        t_3_0   = t_2_0 * t_1_0;     /*      t  *      t  *      t      */
404
0
        t_2_1_3 = t_2_0 * t_0_1 * 3; /*      t  *      t  * (1 - t) * 3 */
405
0
        t_1_2_3 = t_1_0 * t_0_2 * 3; /*      t  * (1 - t) * (1 - t) * 3 */
406
0
        t_0_3   = t_0_1 * t_0_2;     /* (1 - t) * (1 - t) * (1 - t)     */
407
408
        /* Bezier polynomial */
409
0
        x = x0 * t_0_3
410
0
          + x1 * t_1_2_3
411
0
          + x2 * t_2_1_3
412
0
          + x3 * t_3_0;
413
0
        y = y0 * t_0_3
414
0
          + y1 * t_1_2_3
415
0
          + y2 * t_2_1_3
416
0
          + y3 * t_3_0;
417
418
0
  p.x = _cairo_fixed_from_double (x);
419
0
  p.y = _cairo_fixed_from_double (y);
420
0
  status = add_point_func (closure, &p, NULL);
421
0
  if (unlikely (status))
422
0
      return status;
423
0
    }
424
425
0
    return add_point_func (closure, p3, NULL);
426
0
}