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 © 2005 Red Hat, Inc.
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
 */
38

            
39
#include "cairoint.h"
40

            
41
#include "cairo-box-inline.h"
42
#include "cairo-error-private.h"
43
#include "cairo-list-inline.h"
44
#include "cairo-path-fixed-private.h"
45
#include "cairo-slope-private.h"
46

            
47
static cairo_status_t
48
_cairo_path_fixed_add (cairo_path_fixed_t  *path,
49
		       cairo_path_op_t	    op,
50
		       const cairo_point_t *points,
51
		       int		    num_points);
52

            
53
static void
54
_cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
55
			   cairo_path_buf_t   *buf);
56

            
57
static cairo_path_buf_t *
58
_cairo_path_buf_create (int size_ops, int size_points);
59

            
60
static void
61
_cairo_path_buf_destroy (cairo_path_buf_t *buf);
62

            
63
static void
64
_cairo_path_buf_add_op (cairo_path_buf_t *buf,
65
			cairo_path_op_t   op);
66

            
67
static void
68
_cairo_path_buf_add_points (cairo_path_buf_t       *buf,
69
			    const cairo_point_t    *points,
70
			    int		            num_points);
71

            
72
void
73
779950
_cairo_path_fixed_init (cairo_path_fixed_t *path)
74
{
75
    VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
76

            
77
779950
    cairo_list_init (&path->buf.base.link);
78

            
79
779950
    path->buf.base.num_ops = 0;
80
779950
    path->buf.base.num_points = 0;
81
779950
    path->buf.base.size_ops = ARRAY_LENGTH (path->buf.op);
82
779950
    path->buf.base.size_points = ARRAY_LENGTH (path->buf.points);
83
779950
    path->buf.base.op = path->buf.op;
84
779950
    path->buf.base.points = path->buf.points;
85

            
86
779950
    path->current_point.x = 0;
87
779950
    path->current_point.y = 0;
88
779950
    path->last_move_point = path->current_point;
89

            
90
779950
    path->has_current_point = FALSE;
91
779950
    path->needs_move_to = TRUE;
92
779950
    path->has_extents = FALSE;
93
779950
    path->has_curve_to = FALSE;
94
779950
    path->stroke_is_rectilinear = TRUE;
95
779950
    path->fill_is_rectilinear = TRUE;
96
779950
    path->fill_maybe_region = TRUE;
97
779950
    path->fill_is_empty = TRUE;
98

            
99
779950
    path->extents.p1.x = path->extents.p1.y = 0;
100
779950
    path->extents.p2.x = path->extents.p2.y = 0;
101
779950
}
102

            
103
cairo_status_t
104
62699
_cairo_path_fixed_init_copy (cairo_path_fixed_t *path,
105
			     const cairo_path_fixed_t *other)
106
{
107
    cairo_path_buf_t *buf, *other_buf;
108
    unsigned int num_points, num_ops;
109

            
110
    VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
111

            
112
62699
    cairo_list_init (&path->buf.base.link);
113

            
114
62699
    path->buf.base.op = path->buf.op;
115
62699
    path->buf.base.points = path->buf.points;
116
62699
    path->buf.base.size_ops = ARRAY_LENGTH (path->buf.op);
117
62699
    path->buf.base.size_points = ARRAY_LENGTH (path->buf.points);
118

            
119
62699
    path->current_point = other->current_point;
120
62699
    path->last_move_point = other->last_move_point;
121

            
122
62699
    path->has_current_point = other->has_current_point;
123
62699
    path->needs_move_to = other->needs_move_to;
124
62699
    path->has_extents = other->has_extents;
125
62699
    path->has_curve_to = other->has_curve_to;
126
62699
    path->stroke_is_rectilinear = other->stroke_is_rectilinear;
127
62699
    path->fill_is_rectilinear = other->fill_is_rectilinear;
128
62699
    path->fill_maybe_region = other->fill_maybe_region;
129
62699
    path->fill_is_empty = other->fill_is_empty;
130

            
131
62699
    path->extents = other->extents;
132

            
133
62699
    path->buf.base.num_ops = other->buf.base.num_ops;
134
62699
    path->buf.base.num_points = other->buf.base.num_points;
135
62699
    memcpy (path->buf.op, other->buf.base.op,
136
62699
	    other->buf.base.num_ops * sizeof (other->buf.op[0]));
137
62699
    memcpy (path->buf.points, other->buf.points,
138
62699
	    other->buf.base.num_points * sizeof (other->buf.points[0]));
139

            
140
62699
    num_points = num_ops = 0;
141
62699
    for (other_buf = cairo_path_buf_next (cairo_path_head (other));
142
62807
	 other_buf != cairo_path_head (other);
143
108
	 other_buf = cairo_path_buf_next (other_buf))
144
    {
145
108
	num_ops    += other_buf->num_ops;
146
108
	num_points += other_buf->num_points;
147
    }
148

            
149
62699
    if (num_ops) {
150
36
	buf = _cairo_path_buf_create (num_ops, num_points);
151
36
	if (unlikely (buf == NULL)) {
152
	    _cairo_path_fixed_fini (path);
153
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
154
	}
155

            
156
36
	for (other_buf = cairo_path_buf_next (cairo_path_head (other));
157
144
	     other_buf != cairo_path_head (other);
158
108
	     other_buf = cairo_path_buf_next (other_buf))
159
	{
160
108
	    memcpy (buf->op + buf->num_ops, other_buf->op,
161
108
		    other_buf->num_ops * sizeof (buf->op[0]));
162
108
	    buf->num_ops += other_buf->num_ops;
163

            
164
108
	    memcpy (buf->points + buf->num_points, other_buf->points,
165
108
		    other_buf->num_points * sizeof (buf->points[0]));
166
108
	    buf->num_points += other_buf->num_points;
167
	}
168

            
169
36
	_cairo_path_fixed_add_buf (path, buf);
170
    }
171

            
172
62699
    return CAIRO_STATUS_SUCCESS;
173
}
174

            
175
uintptr_t
176
_cairo_path_fixed_hash (const cairo_path_fixed_t *path)
177
{
178
    uintptr_t hash = _CAIRO_HASH_INIT_VALUE;
179
    const cairo_path_buf_t *buf;
180
    unsigned int count;
181

            
182
    count = 0;
183
    cairo_path_foreach_buf_start (buf, path) {
184
	hash = _cairo_hash_bytes (hash, buf->op,
185
			          buf->num_ops * sizeof (buf->op[0]));
186
	count += buf->num_ops;
187
    } cairo_path_foreach_buf_end (buf, path);
188
    hash = _cairo_hash_bytes (hash, &count, sizeof (count));
189

            
190
    count = 0;
191
    cairo_path_foreach_buf_start (buf, path) {
192
	hash = _cairo_hash_bytes (hash, buf->points,
193
			          buf->num_points * sizeof (buf->points[0]));
194
	count += buf->num_points;
195
    } cairo_path_foreach_buf_end (buf, path);
196
    hash = _cairo_hash_bytes (hash, &count, sizeof (count));
197

            
198
    return hash;
199
}
200

            
201
unsigned long
202
_cairo_path_fixed_size (const cairo_path_fixed_t *path)
203
{
204
    const cairo_path_buf_t *buf;
205
    int num_points, num_ops;
206

            
207
    num_ops = num_points = 0;
208
    cairo_path_foreach_buf_start (buf, path) {
209
	num_ops    += buf->num_ops;
210
	num_points += buf->num_points;
211
    } cairo_path_foreach_buf_end (buf, path);
212

            
213
    return num_ops * sizeof (buf->op[0]) +
214
	   num_points * sizeof (buf->points[0]);
215
}
216

            
217
cairo_bool_t
218
1
_cairo_path_fixed_equal (const cairo_path_fixed_t *a,
219
			 const cairo_path_fixed_t *b)
220
{
221
    const cairo_path_buf_t *buf_a, *buf_b;
222
    const cairo_path_op_t *ops_a, *ops_b;
223
    const cairo_point_t *points_a, *points_b;
224
    int num_points_a, num_ops_a;
225
    int num_points_b, num_ops_b;
226

            
227
1
    if (a == b)
228
	return TRUE;
229

            
230
    /* use the flags to quickly differentiate based on contents */
231
1
    if (a->has_curve_to != b->has_curve_to)
232
    {
233
	return FALSE;
234
    }
235

            
236
1
    if (a->extents.p1.x != b->extents.p1.x ||
237
1
	a->extents.p1.y != b->extents.p1.y ||
238
1
	a->extents.p2.x != b->extents.p2.x ||
239
	a->extents.p2.y != b->extents.p2.y)
240
    {
241
1
	return FALSE;
242
    }
243

            
244
    num_ops_a = num_points_a = 0;
245
    cairo_path_foreach_buf_start (buf_a, a) {
246
	num_ops_a    += buf_a->num_ops;
247
	num_points_a += buf_a->num_points;
248
    } cairo_path_foreach_buf_end (buf_a, a);
249

            
250
    num_ops_b = num_points_b = 0;
251
    cairo_path_foreach_buf_start (buf_b, b) {
252
	num_ops_b    += buf_b->num_ops;
253
	num_points_b += buf_b->num_points;
254
    } cairo_path_foreach_buf_end (buf_b, b);
255

            
256
    if (num_ops_a == 0 && num_ops_b == 0)
257
	return TRUE;
258

            
259
    if (num_ops_a != num_ops_b || num_points_a != num_points_b)
260
	return FALSE;
261

            
262
    buf_a = cairo_path_head (a);
263
    num_points_a = buf_a->num_points;
264
    num_ops_a = buf_a->num_ops;
265
    ops_a = buf_a->op;
266
    points_a = buf_a->points;
267

            
268
    buf_b = cairo_path_head (b);
269
    num_points_b = buf_b->num_points;
270
    num_ops_b = buf_b->num_ops;
271
    ops_b = buf_b->op;
272
    points_b = buf_b->points;
273

            
274
    while (TRUE) {
275
	int num_ops = MIN (num_ops_a, num_ops_b);
276
	int num_points = MIN (num_points_a, num_points_b);
277

            
278
	if (memcmp (ops_a, ops_b, num_ops * sizeof (cairo_path_op_t)))
279
	    return FALSE;
280
	if (memcmp (points_a, points_b, num_points * sizeof (cairo_point_t)))
281
	    return FALSE;
282

            
283
	num_ops_a -= num_ops;
284
	ops_a += num_ops;
285
	num_points_a -= num_points;
286
	points_a += num_points;
287
	if (num_ops_a == 0 || num_points_a == 0) {
288
	    if (num_ops_a || num_points_a)
289
		return FALSE;
290

            
291
	    buf_a = cairo_path_buf_next (buf_a);
292
	    if (buf_a == cairo_path_head (a))
293
		break;
294

            
295
	    num_points_a = buf_a->num_points;
296
	    num_ops_a = buf_a->num_ops;
297
	    ops_a = buf_a->op;
298
	    points_a = buf_a->points;
299
	}
300

            
301
	num_ops_b -= num_ops;
302
	ops_b += num_ops;
303
	num_points_b -= num_points;
304
	points_b += num_points;
305
	if (num_ops_b == 0 || num_points_b == 0) {
306
	    if (num_ops_b || num_points_b)
307
		return FALSE;
308

            
309
	    buf_b = cairo_path_buf_next (buf_b);
310
	    if (buf_b == cairo_path_head (b))
311
		break;
312

            
313
	    num_points_b = buf_b->num_points;
314
	    num_ops_b = buf_b->num_ops;
315
	    ops_b = buf_b->op;
316
	    points_b = buf_b->points;
317
	}
318
    }
319

            
320
    return TRUE;
321
}
322

            
323
cairo_path_fixed_t *
324
657
_cairo_path_fixed_create (void)
325
{
326
    cairo_path_fixed_t	*path;
327

            
328
657
    path = _cairo_calloc (sizeof (cairo_path_fixed_t));
329
657
    if (!path) {
330
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
331
	return NULL;
332
    }
333

            
334
657
    _cairo_path_fixed_init (path);
335
657
    return path;
336
}
337

            
338
void
339
840863
_cairo_path_fixed_fini (cairo_path_fixed_t *path)
340
{
341
    cairo_path_buf_t *buf;
342

            
343
840863
    buf = cairo_path_buf_next (cairo_path_head (path));
344
1351718
    while (buf != cairo_path_head (path)) {
345
510855
	cairo_path_buf_t *this = buf;
346
510855
	buf = cairo_path_buf_next (buf);
347
510855
	_cairo_path_buf_destroy (this);
348
    }
349

            
350
    VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
351
840863
}
352

            
353
void
354
36
_cairo_path_fixed_destroy (cairo_path_fixed_t *path)
355
{
356
36
    _cairo_path_fixed_fini (path);
357
36
    free (path);
358
36
}
359

            
360
static cairo_path_op_t
361
275424828
_cairo_path_fixed_last_op (cairo_path_fixed_t *path)
362
{
363
    cairo_path_buf_t *buf;
364

            
365
275424828
    buf = cairo_path_tail (path);
366
275424828
    assert (buf->num_ops != 0);
367

            
368
275424828
    return buf->op[buf->num_ops - 1];
369
}
370

            
371
static inline const cairo_point_t *
372
82142148
_cairo_path_fixed_penultimate_point (cairo_path_fixed_t *path)
373
{
374
    cairo_path_buf_t *buf;
375

            
376
82142148
    buf = cairo_path_tail (path);
377
82142148
    if (likely (buf->num_points >= 2)) {
378
81634419
	return &buf->points[buf->num_points - 2];
379
    } else {
380
507729
	cairo_path_buf_t *prev_buf = cairo_path_buf_prev (buf);
381

            
382
507729
	assert (prev_buf->num_points >= 2 - buf->num_points);
383
507729
	return &prev_buf->points[prev_buf->num_points - (2 - buf->num_points)];
384
    }
385
}
386

            
387
static void
388
27562236
_cairo_path_fixed_drop_line_to (cairo_path_fixed_t *path)
389
{
390
    cairo_path_buf_t *buf;
391

            
392
27562236
    assert (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO);
393

            
394
27562236
    buf = cairo_path_tail (path);
395
27562236
    buf->num_points--;
396
27562236
    buf->num_ops--;
397
27562236
}
398

            
399
cairo_status_t
400
27578438
_cairo_path_fixed_move_to (cairo_path_fixed_t  *path,
401
			   cairo_fixed_t	x,
402
			   cairo_fixed_t	y)
403
{
404
27578438
    _cairo_path_fixed_new_sub_path (path);
405

            
406
27578438
    path->has_current_point = TRUE;
407
27578438
    path->current_point.x = x;
408
27578438
    path->current_point.y = y;
409
27578438
    path->last_move_point = path->current_point;
410

            
411
27578438
    return CAIRO_STATUS_SUCCESS;
412
}
413

            
414
static cairo_status_t
415
110867983
_cairo_path_fixed_move_to_apply (cairo_path_fixed_t  *path)
416
{
417
110867983
    if (likely (! path->needs_move_to))
418
83360322
	return CAIRO_STATUS_SUCCESS;
419

            
420
27507661
    path->needs_move_to = FALSE;
421

            
422
27507661
    if (path->has_extents) {
423
26764541
	_cairo_box_add_point (&path->extents, &path->current_point);
424
    } else {
425
743120
	_cairo_box_set (&path->extents, &path->current_point, &path->current_point);
426
743120
	path->has_extents = TRUE;
427
    }
428

            
429
27507661
    if (path->fill_maybe_region) {
430
1212750
	path->fill_maybe_region = _cairo_fixed_is_integer (path->current_point.x) &&
431
457511
				  _cairo_fixed_is_integer (path->current_point.y);
432
    }
433

            
434
27507661
    path->last_move_point = path->current_point;
435

            
436
27507661
    return _cairo_path_fixed_add (path, CAIRO_PATH_OP_MOVE_TO, &path->current_point, 1);
437
}
438

            
439
void
440
27579969
_cairo_path_fixed_new_sub_path (cairo_path_fixed_t *path)
441
{
442
27579969
    if (! path->needs_move_to) {
443
	/* If the current subpath doesn't need_move_to, it contains at least one command */
444
112182
	if (path->fill_is_rectilinear) {
445
	    /* Implicitly close for fill */
446
1815
	    path->fill_is_rectilinear = path->current_point.x == path->last_move_point.x ||
447
708
					path->current_point.y == path->last_move_point.y;
448
1107
	    path->fill_maybe_region &= path->fill_is_rectilinear;
449
	}
450
112182
	path->needs_move_to = TRUE;
451
    }
452

            
453
27579969
    path->has_current_point = FALSE;
454
27579969
}
455

            
456
cairo_status_t
457
12
_cairo_path_fixed_rel_move_to (cairo_path_fixed_t *path,
458
			       cairo_fixed_t	   dx,
459
			       cairo_fixed_t	   dy)
460
{
461
12
    if (unlikely (! path->has_current_point))
462
3
	return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
463

            
464
9
    return _cairo_path_fixed_move_to (path,
465
9
				      path->current_point.x + dx,
466
9
				      path->current_point.y + dy);
467

            
468
}
469

            
470
cairo_status_t
471
110347579
_cairo_path_fixed_line_to (cairo_path_fixed_t *path,
472
			   cairo_fixed_t	x,
473
			   cairo_fixed_t	y)
474
{
475
    cairo_status_t status;
476
    cairo_point_t point;
477

            
478
110347579
    point.x = x;
479
110347579
    point.y = y;
480

            
481
    /* When there is not yet a current point, the line_to operation
482
     * becomes a move_to instead. Note: We have to do this by
483
     * explicitly calling into _cairo_path_fixed_move_to to ensure
484
     * that the last_move_point state is updated properly.
485
     */
486
110347579
    if (! path->has_current_point)
487
3112
	return _cairo_path_fixed_move_to (path, point.x, point.y);
488

            
489
110344467
    status = _cairo_path_fixed_move_to_apply (path);
490
110344467
    if (unlikely (status))
491
	return status;
492

            
493
    /* If the previous op was but the initial MOVE_TO and this segment
494
     * is degenerate, then we can simply skip this point. Note that
495
     * a move-to followed by a degenerate line-to is a valid path for
496
     * stroking, but at all other times is simply a degenerate segment.
497
     */
498
110344467
    if (_cairo_path_fixed_last_op (path) != CAIRO_PATH_OP_MOVE_TO) {
499
82840070
	if (x == path->current_point.x && y == path->current_point.y)
500
700659
	    return CAIRO_STATUS_SUCCESS;
501
    }
502

            
503
    /* If the previous op was also a LINE_TO with the same gradient,
504
     * then just change its end-point rather than adding a new op.
505
     */
506
109643808
    if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO) {
507
	const cairo_point_t *p;
508

            
509
82137268
	p = _cairo_path_fixed_penultimate_point (path);
510
82137268
	if (p->x == path->current_point.x && p->y == path->current_point.y) {
511
	    /* previous line element was degenerate, replace */
512
203013
	    _cairo_path_fixed_drop_line_to (path);
513
	} else {
514
	    cairo_slope_t prev, self;
515

            
516
81934255
	    _cairo_slope_init (&prev, p, &path->current_point);
517
81934255
	    _cairo_slope_init (&self, &path->current_point, &point);
518
82145896
	    if (_cairo_slope_equal (&prev, &self) &&
519
		/* cannot trim anti-parallel segments whilst stroking */
520
211641
		! _cairo_slope_backwards (&prev, &self))
521
	    {
522
6357
		_cairo_path_fixed_drop_line_to (path);
523
		/* In this case the flags might be more restrictive than
524
		 * what we actually need.
525
		 * When changing the flags definition we should check if
526
		 * changing the line_to point can affect them.
527
		*/
528
	    }
529
	}
530
    }
531

            
532
109643808
    if (path->stroke_is_rectilinear) {
533
5644317
	path->stroke_is_rectilinear = path->current_point.x == x ||
534
1777279
				      path->current_point.y == y;
535
3867038
	path->fill_is_rectilinear &= path->stroke_is_rectilinear;
536
3867038
	path->fill_maybe_region &= path->fill_is_rectilinear;
537
3867038
	if (path->fill_maybe_region) {
538
3239950
	    path->fill_maybe_region = _cairo_fixed_is_integer (x) &&
539
1619405
				      _cairo_fixed_is_integer (y);
540
	}
541
3867038
	if (path->fill_is_empty) {
542
783986
	    path->fill_is_empty = path->current_point.x == x &&
543
40668
				  path->current_point.y == y;
544
	}
545
    }
546

            
547
109643808
    path->current_point = point;
548

            
549
109643808
    _cairo_box_add_point (&path->extents, &point);
550

            
551
109643808
    return _cairo_path_fixed_add (path, CAIRO_PATH_OP_LINE_TO, &point, 1);
552
}
553

            
554
cairo_status_t
555
81561120
_cairo_path_fixed_rel_line_to (cairo_path_fixed_t *path,
556
			       cairo_fixed_t	   dx,
557
			       cairo_fixed_t	   dy)
558
{
559
81561120
    if (unlikely (! path->has_current_point))
560
3
	return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
561

            
562
81561117
    return _cairo_path_fixed_line_to (path,
563
81561117
				      path->current_point.x + dx,
564
81561117
				      path->current_point.y + dy);
565
}
566

            
567
cairo_status_t
568
523537
_cairo_path_fixed_curve_to (cairo_path_fixed_t	*path,
569
			    cairo_fixed_t x0, cairo_fixed_t y0,
570
			    cairo_fixed_t x1, cairo_fixed_t y1,
571
			    cairo_fixed_t x2, cairo_fixed_t y2)
572
{
573
    cairo_status_t status;
574
    cairo_point_t point[3];
575

            
576
    /* If this curves does not move, replace it with a line-to.
577
     * This frequently happens with rounded-rectangles and r==0.
578
    */
579
523537
    if (path->current_point.x == x2 && path->current_point.y == y2) {
580
45
	if (x1 == x2 && x0 == x2 && y1 == y2 && y0 == y2)
581
21
	    return _cairo_path_fixed_line_to (path, x2, y2);
582

            
583
	/* We may want to check for the absence of a cusp, in which case
584
	 * we can also replace the curve-to with a line-to.
585
	 */
586
    }
587

            
588
    /* make sure subpaths are started properly */
589
523516
    if (! path->has_current_point) {
590
6
	status = _cairo_path_fixed_move_to (path, x0, y0);
591
6
	assert (status == CAIRO_STATUS_SUCCESS);
592
    }
593

            
594
523516
    status = _cairo_path_fixed_move_to_apply (path);
595
523516
    if (unlikely (status))
596
	return status;
597

            
598
    /* If the previous op was a degenerate LINE_TO, drop it. */
599
523516
    if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO) {
600
	const cairo_point_t *p;
601

            
602
4880
	p = _cairo_path_fixed_penultimate_point (path);
603
4880
	if (p->x == path->current_point.x && p->y == path->current_point.y) {
604
	    /* previous line element was degenerate, replace */
605
2908
	    _cairo_path_fixed_drop_line_to (path);
606
	}
607
    }
608

            
609
523516
    point[0].x = x0; point[0].y = y0;
610
523516
    point[1].x = x1; point[1].y = y1;
611
523516
    point[2].x = x2; point[2].y = y2;
612

            
613
523516
    _cairo_box_add_curve_to (&path->extents, &path->current_point,
614
			     &point[0], &point[1], &point[2]);
615

            
616
523516
    path->current_point = point[2];
617
523516
    path->has_curve_to = TRUE;
618
523516
    path->stroke_is_rectilinear = FALSE;
619
523516
    path->fill_is_rectilinear = FALSE;
620
523516
    path->fill_maybe_region = FALSE;
621
523516
    path->fill_is_empty = FALSE;
622

            
623
523516
    return _cairo_path_fixed_add (path, CAIRO_PATH_OP_CURVE_TO, point, 3);
624
}
625

            
626
cairo_status_t
627
780
_cairo_path_fixed_rel_curve_to (cairo_path_fixed_t *path,
628
				cairo_fixed_t dx0, cairo_fixed_t dy0,
629
				cairo_fixed_t dx1, cairo_fixed_t dy1,
630
				cairo_fixed_t dx2, cairo_fixed_t dy2)
631
{
632
780
    if (unlikely (! path->has_current_point))
633
3
	return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
634

            
635
777
    return _cairo_path_fixed_curve_to (path,
636
777
				       path->current_point.x + dx0,
637
777
				       path->current_point.y + dy0,
638

            
639
777
				       path->current_point.x + dx1,
640
777
				       path->current_point.y + dy1,
641

            
642
777
				       path->current_point.x + dx2,
643
777
				       path->current_point.y + dy2);
644
}
645

            
646
cairo_status_t
647
27351131
_cairo_path_fixed_close_path (cairo_path_fixed_t *path)
648
{
649
    cairo_status_t status;
650

            
651
27351131
    if (! path->has_current_point)
652
330
	return CAIRO_STATUS_SUCCESS;
653

            
654
    /*
655
     * Add a line_to, to compute flags and solve any degeneracy.
656
     * It will be removed later (if it was actually added).
657
     */
658
27350801
    status = _cairo_path_fixed_line_to (path,
659
					path->last_move_point.x,
660
					path->last_move_point.y);
661
27350801
    if (unlikely (status))
662
	return status;
663

            
664
    /*
665
     * If the command used to close the path is a line_to, drop it.
666
     * We must check that last command is actually a line_to,
667
     * because the path could have been closed with a curve_to (and
668
     * the previous line_to not added as it would be degenerate).
669
     */
670
27350801
    if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO)
671
27349958
	    _cairo_path_fixed_drop_line_to (path);
672

            
673
27350801
    path->needs_move_to = TRUE; /* After close_path, add an implicit move_to */
674

            
675
27350801
    return _cairo_path_fixed_add (path, CAIRO_PATH_OP_CLOSE_PATH, NULL, 0);
676
}
677

            
678
cairo_bool_t
679
39180
_cairo_path_fixed_get_current_point (cairo_path_fixed_t *path,
680
				     cairo_fixed_t	*x,
681
				     cairo_fixed_t	*y)
682
{
683
39180
    if (! path->has_current_point)
684
	return FALSE;
685

            
686
39180
    *x = path->current_point.x;
687
39180
    *y = path->current_point.y;
688

            
689
39180
    return TRUE;
690
}
691

            
692
static cairo_status_t
693
165025786
_cairo_path_fixed_add (cairo_path_fixed_t   *path,
694
		       cairo_path_op_t	     op,
695
		       const cairo_point_t  *points,
696
		       int		     num_points)
697
{
698
165025786
    cairo_path_buf_t *buf = cairo_path_tail (path);
699

            
700
165025786
    if (buf->num_ops + 1 > buf->size_ops ||
701
164922364
	buf->num_points + num_points > buf->size_points)
702
    {
703
511542
	buf = _cairo_path_buf_create (buf->num_ops * 2, buf->num_points * 2);
704
511542
	if (unlikely (buf == NULL))
705
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
706

            
707
511542
	_cairo_path_fixed_add_buf (path, buf);
708
    }
709

            
710
    if (WATCH_PATH) {
711
	const char *op_str[] = {
712
	    "move-to",
713
	    "line-to",
714
	    "curve-to",
715
	    "close-path",
716
	};
717
	char buf[1024];
718
	int len = 0;
719
	int i;
720

            
721
	len += snprintf (buf + len, sizeof (buf), "[");
722
	for (i = 0; i < num_points; i++) {
723
	    if (i != 0)
724
		len += snprintf (buf + len, sizeof (buf), " ");
725
	    len += snprintf (buf + len, sizeof (buf), "(%f, %f)",
726
			     _cairo_fixed_to_double (points[i].x),
727
			     _cairo_fixed_to_double (points[i].y));
728
	}
729
	len += snprintf (buf + len, sizeof (buf), "]");
730

            
731
#define STRINGIFYFLAG(x)  (path->x ? #x " " : "")
732
	fprintf (stderr,
733
		 "_cairo_path_fixed_add (%s, %s) [%s%s%s%s%s%s%s%s]\n",
734
		 op_str[(int) op], buf,
735
		 STRINGIFYFLAG(has_current_point),
736
		 STRINGIFYFLAG(needs_move_to),
737
		 STRINGIFYFLAG(has_extents),
738
		 STRINGIFYFLAG(has_curve_to),
739
		 STRINGIFYFLAG(stroke_is_rectilinear),
740
		 STRINGIFYFLAG(fill_is_rectilinear),
741
		 STRINGIFYFLAG(fill_is_empty),
742
		 STRINGIFYFLAG(fill_maybe_region)
743
		 );
744
#undef STRINGIFYFLAG
745
    }
746

            
747
165025786
    _cairo_path_buf_add_op (buf, op);
748
165025786
    _cairo_path_buf_add_points (buf, points, num_points);
749

            
750
165025786
    return CAIRO_STATUS_SUCCESS;
751
}
752

            
753
static void
754
511578
_cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
755
			   cairo_path_buf_t   *buf)
756
{
757
511578
    cairo_list_add_tail (&buf->link, &cairo_path_head (path)->link);
758
511578
}
759

            
760
COMPILE_TIME_ASSERT (sizeof (cairo_path_op_t) == 1);
761
static cairo_path_buf_t *
762
511578
_cairo_path_buf_create (int size_ops, int size_points)
763
{
764
    cairo_path_buf_t *buf;
765

            
766
    /* adjust size_ops to ensure that buf->points is naturally aligned */
767
511578
    size_ops += sizeof (double) - ((sizeof (cairo_path_buf_t) + size_ops) % sizeof (double));
768
511578
    buf = _cairo_malloc_ab_plus_c (size_points, sizeof (cairo_point_t), size_ops + sizeof (cairo_path_buf_t));
769
511578
    if (buf) {
770
511578
	buf->num_ops = 0;
771
511578
	buf->num_points = 0;
772
511578
	buf->size_ops = size_ops;
773
511578
	buf->size_points = size_points;
774

            
775
511578
	buf->op = (cairo_path_op_t *) (buf + 1);
776
511578
	buf->points = (cairo_point_t *) (buf->op + size_ops);
777
    }
778

            
779
511578
    return buf;
780
}
781

            
782
static void
783
510855
_cairo_path_buf_destroy (cairo_path_buf_t *buf)
784
{
785
510855
    free (buf);
786
510855
}
787

            
788
static void
789
165025786
_cairo_path_buf_add_op (cairo_path_buf_t *buf,
790
			cairo_path_op_t	  op)
791
{
792
165025786
    buf->op[buf->num_ops++] = op;
793
165025786
}
794

            
795
static void
796
165025786
_cairo_path_buf_add_points (cairo_path_buf_t       *buf,
797
			    const cairo_point_t    *points,
798
			    int		            num_points)
799
{
800
165025786
    if (num_points == 0)
801
27350801
	return;
802

            
803
137674985
    memcpy (buf->points + buf->num_points,
804
	    points,
805
	    sizeof (points[0]) * num_points);
806
137674985
    buf->num_points += num_points;
807
}
808

            
809
cairo_status_t
810
207408
_cairo_path_fixed_interpret (const cairo_path_fixed_t		*path,
811
			     cairo_path_fixed_move_to_func_t	*move_to,
812
			     cairo_path_fixed_line_to_func_t	*line_to,
813
			     cairo_path_fixed_curve_to_func_t	*curve_to,
814
			     cairo_path_fixed_close_path_func_t	*close_path,
815
			     void				*closure)
816
{
817
    const cairo_path_buf_t *buf;
818
    cairo_status_t status;
819

            
820
207408
    cairo_path_foreach_buf_start (buf, path) {
821
717315
	const cairo_point_t *points = buf->points;
822
	unsigned int i;
823

            
824
134349270
	for (i = 0; i < buf->num_ops; i++) {
825
133631955
	    switch (buf->op[i]) {
826
26730560
	    case CAIRO_PATH_OP_MOVE_TO:
827
26730560
		status = (*move_to) (closure, &points[0]);
828
26730560
		points += 1;
829
26730560
		break;
830
80257867
	    case CAIRO_PATH_OP_LINE_TO:
831
80257867
		status = (*line_to) (closure, &points[0]);
832
80257867
		points += 1;
833
80257867
		break;
834
125866
	    case CAIRO_PATH_OP_CURVE_TO:
835
125866
		status = (*curve_to) (closure, &points[0], &points[1], &points[2]);
836
125866
		points += 3;
837
125866
		break;
838
	    default:
839
		ASSERT_NOT_REACHED;
840
	    case CAIRO_PATH_OP_CLOSE_PATH:
841
26517662
		status = (*close_path) (closure);
842
26517662
		break;
843
	    }
844

            
845
133631955
	    if (unlikely (status))
846
		return status;
847
	}
848
717315
    } cairo_path_foreach_buf_end (buf, path);
849

            
850
207408
    if (path->needs_move_to && path->has_current_point)
851
108451
	return (*move_to) (closure, &path->current_point);
852

            
853
98957
    return CAIRO_STATUS_SUCCESS;
854
}
855

            
856
typedef struct _cairo_path_fixed_append_closure {
857
    cairo_point_t	    offset;
858
    cairo_path_fixed_t	    *path;
859
} cairo_path_fixed_append_closure_t;
860

            
861
static cairo_status_t
862
38661
_append_move_to (void		 *abstract_closure,
863
		 const cairo_point_t  *point)
864
{
865
38661
    cairo_path_fixed_append_closure_t	*closure = abstract_closure;
866

            
867
77322
    return _cairo_path_fixed_move_to (closure->path,
868
38661
				      point->x + closure->offset.x,
869
38661
				      point->y + closure->offset.y);
870
}
871

            
872
static cairo_status_t
873
112254
_append_line_to (void		 *abstract_closure,
874
		 const cairo_point_t *point)
875
{
876
112254
    cairo_path_fixed_append_closure_t	*closure = abstract_closure;
877

            
878
224508
    return _cairo_path_fixed_line_to (closure->path,
879
112254
				      point->x + closure->offset.x,
880
112254
				      point->y + closure->offset.y);
881
}
882

            
883
static cairo_status_t
884
6228
_append_curve_to (void	  *abstract_closure,
885
		  const cairo_point_t *p0,
886
		  const cairo_point_t *p1,
887
		  const cairo_point_t *p2)
888
{
889
6228
    cairo_path_fixed_append_closure_t	*closure = abstract_closure;
890

            
891
12456
    return _cairo_path_fixed_curve_to (closure->path,
892
6228
				       p0->x + closure->offset.x,
893
6228
				       p0->y + closure->offset.y,
894
6228
				       p1->x + closure->offset.x,
895
6228
				       p1->y + closure->offset.y,
896
6228
				       p2->x + closure->offset.x,
897
6228
				       p2->y + closure->offset.y);
898
}
899

            
900
static cairo_status_t
901
37578
_append_close_path (void *abstract_closure)
902
{
903
37578
    cairo_path_fixed_append_closure_t	*closure = abstract_closure;
904

            
905
37578
    return _cairo_path_fixed_close_path (closure->path);
906
}
907

            
908
cairo_status_t
909
1203
_cairo_path_fixed_append (cairo_path_fixed_t		    *path,
910
			  const cairo_path_fixed_t	    *other,
911
			  cairo_fixed_t			     tx,
912
			  cairo_fixed_t			     ty)
913
{
914
    cairo_path_fixed_append_closure_t closure;
915

            
916
1203
    closure.path = path;
917
1203
    closure.offset.x = tx;
918
1203
    closure.offset.y = ty;
919

            
920
1203
    return _cairo_path_fixed_interpret (other,
921
					_append_move_to,
922
					_append_line_to,
923
					_append_curve_to,
924
					_append_close_path,
925
					&closure);
926
}
927

            
928
static void
929
50361
_cairo_path_fixed_offset_and_scale (cairo_path_fixed_t *path,
930
				    cairo_fixed_t offx,
931
				    cairo_fixed_t offy,
932
				    cairo_fixed_t scalex,
933
				    cairo_fixed_t scaley)
934
{
935
    cairo_path_buf_t *buf;
936
    unsigned int i;
937

            
938
50361
    if (scalex == CAIRO_FIXED_ONE && scaley == CAIRO_FIXED_ONE) {
939
1380
	_cairo_path_fixed_translate (path, offx, offy);
940
1380
	return;
941
    }
942

            
943
48981
    path->last_move_point.x = _cairo_fixed_mul (scalex, path->last_move_point.x) + offx;
944
48981
    path->last_move_point.y = _cairo_fixed_mul (scaley, path->last_move_point.y) + offy;
945
48981
    path->current_point.x   = _cairo_fixed_mul (scalex, path->current_point.x) + offx;
946
48981
    path->current_point.y   = _cairo_fixed_mul (scaley, path->current_point.y) + offy;
947

            
948
48981
    path->fill_maybe_region = TRUE;
949

            
950
48981
    cairo_path_foreach_buf_start (buf, path) {
951
398103
	 for (i = 0; i < buf->num_points; i++) {
952
349122
	     if (scalex != CAIRO_FIXED_ONE)
953
349122
		 buf->points[i].x = _cairo_fixed_mul (buf->points[i].x, scalex);
954
349122
	     buf->points[i].x += offx;
955

            
956
349122
	     if (scaley != CAIRO_FIXED_ONE)
957
349122
		 buf->points[i].y = _cairo_fixed_mul (buf->points[i].y, scaley);
958
349122
	     buf->points[i].y += offy;
959

            
960
349122
	    if (path->fill_maybe_region) {
961
155909
		path->fill_maybe_region = _cairo_fixed_is_integer (buf->points[i].x) &&
962
62534
					  _cairo_fixed_is_integer (buf->points[i].y);
963
	    }
964
	 }
965
48981
    } cairo_path_foreach_buf_end (buf, path);
966

            
967
48981
    path->fill_maybe_region &= path->fill_is_rectilinear;
968

            
969
48981
    path->extents.p1.x = _cairo_fixed_mul (scalex, path->extents.p1.x) + offx;
970
48981
    path->extents.p2.x = _cairo_fixed_mul (scalex, path->extents.p2.x) + offx;
971
48981
    if (scalex < 0) {
972
11880
	cairo_fixed_t t = path->extents.p1.x;
973
11880
	path->extents.p1.x = path->extents.p2.x;
974
11880
	path->extents.p2.x = t;
975
    }
976

            
977
48981
    path->extents.p1.y = _cairo_fixed_mul (scaley, path->extents.p1.y) + offy;
978
48981
    path->extents.p2.y = _cairo_fixed_mul (scaley, path->extents.p2.y) + offy;
979
48981
    if (scaley < 0) {
980
11880
	cairo_fixed_t t = path->extents.p1.y;
981
11880
	path->extents.p1.y = path->extents.p2.y;
982
11880
	path->extents.p2.y = t;
983
    }
984
}
985

            
986
void
987
2777
_cairo_path_fixed_translate (cairo_path_fixed_t *path,
988
			     cairo_fixed_t offx,
989
			     cairo_fixed_t offy)
990
{
991
    cairo_path_buf_t *buf;
992
    unsigned int i;
993

            
994
2777
    if (offx == 0 && offy == 0)
995
845
	return;
996

            
997
1932
    path->last_move_point.x += offx;
998
1932
    path->last_move_point.y += offy;
999
1932
    path->current_point.x += offx;
1932
    path->current_point.y += offy;
1932
    path->fill_maybe_region = TRUE;
1932
    cairo_path_foreach_buf_start (buf, path) {
12189
	for (i = 0; i < buf->num_points; i++) {
10257
	    buf->points[i].x += offx;
10257
	    buf->points[i].y += offy;
10257
	    if (path->fill_maybe_region) {
4449
		path->fill_maybe_region = _cairo_fixed_is_integer (buf->points[i].x) &&
1935
					  _cairo_fixed_is_integer (buf->points[i].y);
	    }
	 }
1932
    } cairo_path_foreach_buf_end (buf, path);
1932
    path->fill_maybe_region &= path->fill_is_rectilinear;
1932
    path->extents.p1.x += offx;
1932
    path->extents.p1.y += offy;
1932
    path->extents.p2.x += offx;
1932
    path->extents.p2.y += offy;
}
static inline void
125373
_cairo_path_fixed_transform_point (cairo_point_t *p,
				   const cairo_matrix_t *matrix)
{
    double dx, dy;
125373
    dx = _cairo_fixed_to_double (p->x);
125373
    dy = _cairo_fixed_to_double (p->y);
125373
    cairo_matrix_transform_point (matrix, &dx, &dy);
125373
    p->x = _cairo_fixed_from_double (dx);
125373
    p->y = _cairo_fixed_from_double (dy);
125373
}
/**
 * _cairo_path_fixed_transform:
 * @path: a #cairo_path_fixed_t to be transformed
 * @matrix: a #cairo_matrix_t
 *
 * Transform the fixed-point path according to the given matrix.
 * There is a fast path for the case where @matrix has no rotation
 * or shear.
 **/
void
62697
_cairo_path_fixed_transform (cairo_path_fixed_t	*path,
			     const cairo_matrix_t     *matrix)
{
    cairo_box_t extents;
    cairo_point_t point;
    cairo_path_buf_t *buf;
    unsigned int i;
62697
    if (matrix->yx == 0.0 && matrix->xy == 0.0) {
	/* Fast path for the common case of scale+transform */
50361
	_cairo_path_fixed_offset_and_scale (path,
50361
					    _cairo_fixed_from_double (matrix->x0),
50361
					    _cairo_fixed_from_double (matrix->y0),
50361
					    _cairo_fixed_from_double (matrix->xx),
50361
					    _cairo_fixed_from_double (matrix->yy));
50361
	return;
    }
12336
    _cairo_path_fixed_transform_point (&path->last_move_point, matrix);
12336
    _cairo_path_fixed_transform_point (&path->current_point, matrix);
12336
    buf = cairo_path_head (path);
12336
    if (buf->num_points == 0)
	return;
12336
    extents = path->extents;
12336
    point = buf->points[0];
12336
    _cairo_path_fixed_transform_point (&point, matrix);
12336
    _cairo_box_set (&path->extents, &point, &point);
12336
    cairo_path_foreach_buf_start (buf, path) {
100701
	for (i = 0; i < buf->num_points; i++) {
88365
	    _cairo_path_fixed_transform_point (&buf->points[i], matrix);
88365
	    _cairo_box_add_point (&path->extents, &buf->points[i]);
	}
12336
    } cairo_path_foreach_buf_end (buf, path);
12336
    if (path->has_curve_to) {
	cairo_bool_t is_tight;
2793
	_cairo_matrix_transform_bounding_box_fixed (matrix, &extents, &is_tight);
2793
	if (!is_tight) {
	    cairo_bool_t has_extents;
2793
	    has_extents = _cairo_path_bounder_extents (path, &extents);
2793
	    assert (has_extents);
	}
2793
	path->extents = extents;
    }
    /* flags might become more strict than needed */
12336
    path->stroke_is_rectilinear = FALSE;
12336
    path->fill_is_rectilinear = FALSE;
12336
    path->fill_is_empty = FALSE;
12336
    path->fill_maybe_region = FALSE;
}
/* Closure for path flattening */
typedef struct cairo_path_flattener {
    double tolerance;
    cairo_point_t current_point;
    cairo_path_fixed_move_to_func_t	*move_to;
    cairo_path_fixed_line_to_func_t	*line_to;
    cairo_path_fixed_close_path_func_t	*close_path;
    void *closure;
} cpf_t;
static cairo_status_t
24
_cpf_move_to (void *closure,
	      const cairo_point_t *point)
{
24
    cpf_t *cpf = closure;
24
    cpf->current_point = *point;
24
    return cpf->move_to (cpf->closure, point);
}
static cairo_status_t
258
_cpf_line_to (void *closure,
	      const cairo_point_t *point)
{
258
    cpf_t *cpf = closure;
258
    cpf->current_point = *point;
258
    return cpf->line_to (cpf->closure, point);
}
static cairo_status_t
240
_cpf_add_point (void *closure,
		const cairo_point_t *point,
		const cairo_slope_t *tangent)
{
240
    return _cpf_line_to (closure, point);
};
static cairo_status_t
24
_cpf_curve_to (void		*closure,
	       const cairo_point_t	*p1,
	       const cairo_point_t	*p2,
	       const cairo_point_t	*p3)
{
24
    cpf_t *cpf = closure;
    cairo_spline_t spline;
24
    cairo_point_t *p0 = &cpf->current_point;
24
    if (! _cairo_spline_init (&spline,
			      _cpf_add_point,
			      cpf,
			      p0, p1, p2, p3))
    {
	return _cpf_line_to (closure, p3);
    }
24
    cpf->current_point = *p3;
24
    return _cairo_spline_decompose (&spline, cpf->tolerance);
}
static cairo_status_t
12
_cpf_close_path (void *closure)
{
12
    cpf_t *cpf = closure;
12
    return cpf->close_path (cpf->closure);
}
cairo_status_t
30
_cairo_path_fixed_interpret_flat (const cairo_path_fixed_t		*path,
				  cairo_path_fixed_move_to_func_t	*move_to,
				  cairo_path_fixed_line_to_func_t	*line_to,
				  cairo_path_fixed_close_path_func_t	*close_path,
				  void					*closure,
				  double				tolerance)
{
    cpf_t flattener;
30
    if (! path->has_curve_to) {
18
	return _cairo_path_fixed_interpret (path,
					    move_to,
					    line_to,
					    NULL,
					    close_path,
					    closure);
    }
12
    flattener.tolerance = tolerance;
12
    flattener.move_to = move_to;
12
    flattener.line_to = line_to;
12
    flattener.close_path = close_path;
12
    flattener.closure = closure;
12
    return _cairo_path_fixed_interpret (path,
					_cpf_move_to,
					_cpf_line_to,
					_cpf_curve_to,
					_cpf_close_path,
					&flattener);
}
static inline void
927046
_canonical_box (cairo_box_t *box,
		const cairo_point_t *p1,
		const cairo_point_t *p2)
{
927046
    if (p1->x <= p2->x) {
873307
	box->p1.x = p1->x;
873307
	box->p2.x = p2->x;
    } else {
53739
	box->p1.x = p2->x;
53739
	box->p2.x = p1->x;
    }
927046
    if (p1->y <= p2->y) {
898012
	box->p1.y = p1->y;
898012
	box->p2.y = p2->y;
    } else {
29034
	box->p1.y = p2->y;
29034
	box->p2.y = p1->y;
    }
927046
}
static inline cairo_bool_t
931972
_path_is_quad (const cairo_path_fixed_t *path)
{
931972
    const cairo_path_buf_t *buf = cairo_path_head (path);
    /* Do we have the right number of ops? */
931972
    if (buf->num_ops < 4 || buf->num_ops > 6)
5205
	return FALSE;
    /* Check whether the ops are those that would be used for a rectangle */
926767
    if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
926767
	buf->op[1] != CAIRO_PATH_OP_LINE_TO ||
926767
	buf->op[2] != CAIRO_PATH_OP_LINE_TO ||
926767
	buf->op[3] != CAIRO_PATH_OP_LINE_TO)
    {
	return FALSE;
    }
    /* we accept an implicit close for filled paths */
926767
    if (buf->num_ops > 4) {
	/* Now, there are choices. The rectangle might end with a LINE_TO
	 * (to the original point), but this isn't required. If it
	 * doesn't, then it must end with a CLOSE_PATH. */
926761
	if (buf->op[4] == CAIRO_PATH_OP_LINE_TO) {
	    if (buf->points[4].x != buf->points[0].x ||
		buf->points[4].y != buf->points[0].y)
		return FALSE;
926761
	} else if (buf->op[4] != CAIRO_PATH_OP_CLOSE_PATH) {
	    return FALSE;
	}
926761
	if (buf->num_ops == 6) {
	    /* A trailing CLOSE_PATH or MOVE_TO is ok */
	    if (buf->op[5] != CAIRO_PATH_OP_MOVE_TO &&
		buf->op[5] != CAIRO_PATH_OP_CLOSE_PATH)
		return FALSE;
	}
    }
926767
    return TRUE;
}
static inline cairo_bool_t
926767
_points_form_rect (const cairo_point_t *points)
{
926767
    if (points[0].y == points[1].y &&
902041
	points[1].x == points[2].x &&
902041
	points[2].y == points[3].y &&
902041
	points[3].x == points[0].x)
902041
	return TRUE;
24726
    if (points[0].x == points[1].x &&
24726
	points[1].y == points[2].y &&
24726
	points[2].x == points[3].x &&
24726
	points[3].y == points[0].y)
24726
	return TRUE;
    return FALSE;
}
/*
 * Check whether the given path contains a single rectangle.
 */
cairo_bool_t
1042129
_cairo_path_fixed_is_box (const cairo_path_fixed_t *path,
			  cairo_box_t *box)
{
    const cairo_path_buf_t *buf;
1042129
    if (! path->fill_is_rectilinear)
110157
	return FALSE;
931972
    if (! _path_is_quad (path))
5205
	return FALSE;
926767
    buf = cairo_path_head (path);
926767
    if (_points_form_rect (buf->points)) {
926767
	_canonical_box (box, &buf->points[0], &buf->points[2]);
926767
	return TRUE;
    }
    return FALSE;
}
/* Determine whether two lines A->B and C->D intersect based on the 
 * algorithm described here: http://paulbourke.net/geometry/pointlineplane/ */
static inline cairo_bool_t
_lines_intersect_or_are_coincident (cairo_point_t a,
				    cairo_point_t b,
				    cairo_point_t c,
				    cairo_point_t d)
{
    cairo_int64_t numerator_a, numerator_b, denominator;
    cairo_bool_t denominator_negative;
    denominator = _cairo_int64_sub (_cairo_int32x32_64_mul (d.y - c.y, b.x - a.x),
				    _cairo_int32x32_64_mul (d.x - c.x, b.y - a.y));
    numerator_a = _cairo_int64_sub (_cairo_int32x32_64_mul (d.x - c.x, a.y - c.y),
				    _cairo_int32x32_64_mul (d.y - c.y, a.x - c.x));
    numerator_b = _cairo_int64_sub (_cairo_int32x32_64_mul (b.x - a.x, a.y - c.y),
				    _cairo_int32x32_64_mul (b.y - a.y, a.x - c.x));
    if (_cairo_int64_is_zero (denominator)) {
	/* If the denominator and numerators are both zero,
	 * the lines are coincident. */
	if (_cairo_int64_is_zero (numerator_a) && _cairo_int64_is_zero (numerator_b))
	    return TRUE;
	/* Otherwise, a zero denominator indicates the lines are
	*  parallel and never intersect. */
	return FALSE;
    }
    /* The lines intersect if both quotients are between 0 and 1 (exclusive). */
     /* We first test whether either quotient is a negative number. */
    denominator_negative = _cairo_int64_negative (denominator);
    if (_cairo_int64_negative (numerator_a) ^ denominator_negative)
	return FALSE;
    if (_cairo_int64_negative (numerator_b) ^ denominator_negative)
	return FALSE;
    /* A zero quotient indicates an "intersection" at an endpoint, which
     * we aren't considering a true intersection. */
    if (_cairo_int64_is_zero (numerator_a) || _cairo_int64_is_zero (numerator_b))
	return FALSE;
    /* If the absolute value of the numerator is larger than or equal to the
     * denominator the result of the division would be greater than or equal
     * to one. */
    if (! denominator_negative) {
        if (! _cairo_int64_lt (numerator_a, denominator) ||
	    ! _cairo_int64_lt (numerator_b, denominator))
	    return FALSE;
    } else {
        if (! _cairo_int64_lt (denominator, numerator_a) ||
	    ! _cairo_int64_lt (denominator, numerator_b))
	    return FALSE;
    }
    return TRUE;
}
cairo_bool_t
_cairo_path_fixed_is_simple_quad (const cairo_path_fixed_t *path)
{
    const cairo_point_t *points;
    if (! _path_is_quad (path))
	return FALSE;
    points = cairo_path_head (path)->points;
    if (_points_form_rect (points))
	return TRUE;
    if (_lines_intersect_or_are_coincident (points[0], points[1],
					    points[3], points[2]))
	return FALSE;
    if (_lines_intersect_or_are_coincident (points[0], points[3],
					    points[1], points[2]))
	return FALSE;
    return TRUE;
}
cairo_bool_t
1896
_cairo_path_fixed_is_stroke_box (const cairo_path_fixed_t *path,
				 cairo_box_t *box)
{
1896
    const cairo_path_buf_t *buf = cairo_path_head (path);
1896
    if (! path->fill_is_rectilinear)
3
	return FALSE;
    /* Do we have the right number of ops? */
1893
    if (buf->num_ops != 5)
1614
	return FALSE;
    /* Check whether the ops are those that would be used for a rectangle */
279
    if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
279
	buf->op[1] != CAIRO_PATH_OP_LINE_TO ||
279
	buf->op[2] != CAIRO_PATH_OP_LINE_TO ||
279
	buf->op[3] != CAIRO_PATH_OP_LINE_TO ||
279
	buf->op[4] != CAIRO_PATH_OP_CLOSE_PATH)
    {
	return FALSE;
    }
    /* Ok, we may have a box, if the points line up */
279
    if (buf->points[0].y == buf->points[1].y &&
279
	buf->points[1].x == buf->points[2].x &&
279
	buf->points[2].y == buf->points[3].y &&
279
	buf->points[3].x == buf->points[0].x)
    {
279
	_canonical_box (box, &buf->points[0], &buf->points[2]);
279
	return TRUE;
    }
    if (buf->points[0].x == buf->points[1].x &&
	buf->points[1].y == buf->points[2].y &&
	buf->points[2].x == buf->points[3].x &&
	buf->points[3].y == buf->points[0].y)
    {
	_canonical_box (box, &buf->points[0], &buf->points[2]);
	return TRUE;
    }
    return FALSE;
}
/*
 * Check whether the given path contains a single rectangle
 * that is logically equivalent to:
 * <informalexample><programlisting>
 *   cairo_move_to (cr, x, y);
 *   cairo_rel_line_to (cr, width, 0);
 *   cairo_rel_line_to (cr, 0, height);
 *   cairo_rel_line_to (cr, -width, 0);
 *   cairo_close_path (cr);
 * </programlisting></informalexample>
 */
cairo_bool_t
120
_cairo_path_fixed_is_rectangle (const cairo_path_fixed_t *path,
				cairo_box_t        *box)
{
    const cairo_path_buf_t *buf;
120
    if (! _cairo_path_fixed_is_box (path, box))
41
	return FALSE;
    /* This check is valid because the current implementation of
     * _cairo_path_fixed_is_box () only accepts rectangles like:
     * move,line,line,line[,line|close[,close|move]]. */
79
    buf = cairo_path_head (path);
79
    if (buf->num_ops > 4)
79
	return TRUE;
    return FALSE;
}
void
2770
_cairo_path_fixed_iter_init (cairo_path_fixed_iter_t *iter,
			     const cairo_path_fixed_t *path)
{
2770
    iter->first = iter->buf = cairo_path_head (path);
2770
    iter->n_op = 0;
2770
    iter->n_point = 0;
2770
}
static cairo_bool_t
944737
_cairo_path_fixed_iter_next_op (cairo_path_fixed_iter_t *iter)
{
944737
    if (++iter->n_op >= iter->buf->num_ops) {
6295
	iter->buf = cairo_path_buf_next (iter->buf);
6295
	if (iter->buf == iter->first) {
2740
	    iter->buf = NULL;
2740
	    return FALSE;
	}
3555
	iter->n_op = 0;
3555
	iter->n_point = 0;
    }
941997
    return TRUE;
}
cairo_bool_t
272814
_cairo_path_fixed_iter_is_fill_box (cairo_path_fixed_iter_t *_iter,
				    cairo_box_t *box)
{
    cairo_point_t points[5];
    cairo_path_fixed_iter_t iter;
272814
    if (_iter->buf == NULL)
2728
	return FALSE;
270086
    iter = *_iter;
270086
    if (iter.n_op == iter.buf->num_ops && ! _cairo_path_fixed_iter_next_op (&iter))
	return FALSE;
    /* Check whether the ops are those that would be used for a rectangle */
270086
    if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_MOVE_TO)
	return FALSE;
270086
    points[0] = iter.buf->points[iter.n_point++];
270086
    if (! _cairo_path_fixed_iter_next_op (&iter))
	return FALSE;
270086
    if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_LINE_TO)
9
	return FALSE;
270077
    points[1] = iter.buf->points[iter.n_point++];
270077
    if (! _cairo_path_fixed_iter_next_op (&iter))
12
	return FALSE;
    /* a horizontal/vertical closed line is also a degenerate rectangle */
270065
    switch (iter.buf->op[iter.n_op]) {
202800
    case CAIRO_PATH_OP_CLOSE_PATH:
202800
	_cairo_path_fixed_iter_next_op (&iter); /* fall through */
202800
    case CAIRO_PATH_OP_MOVE_TO: /* implicit close */
202800
	box->p1 = box->p2 = points[0];
202800
	*_iter = iter;
202800
	return TRUE;
    default:
	return FALSE;
67265
    case CAIRO_PATH_OP_LINE_TO:
67265
	break;
    }
67265
    points[2] = iter.buf->points[iter.n_point++];
67265
    if (! _cairo_path_fixed_iter_next_op (&iter))
	return FALSE;
67265
    if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_LINE_TO)
	return FALSE;
67265
    points[3] = iter.buf->points[iter.n_point++];
    /* Now, there are choices. The rectangle might end with a LINE_TO
     * (to the original point), but this isn't required. If it
     * doesn't, then it must end with a CLOSE_PATH (which may be implicit). */
67265
    if (! _cairo_path_fixed_iter_next_op (&iter)) {
	/* implicit close due to fill */
67265
    } else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_LINE_TO) {
21
	points[4] = iter.buf->points[iter.n_point++];
21
	if (points[4].x != points[0].x || points[4].y != points[0].y)
21
	    return FALSE;
	_cairo_path_fixed_iter_next_op (&iter);
67244
    } else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_CLOSE_PATH) {
67244
	_cairo_path_fixed_iter_next_op (&iter);
    } else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_MOVE_TO) {
	/* implicit close-path due to new-sub-path */
    } else {
	return FALSE;
    }
    /* Ok, we may have a box, if the points line up */
67244
    if (points[0].y == points[1].y &&
67235
	points[1].x == points[2].x &&
67235
	points[2].y == points[3].y &&
67235
	points[3].x == points[0].x)
    {
67235
	box->p1 = points[0];
67235
	box->p2 = points[2];
67235
	*_iter = iter;
67235
	return TRUE;
    }
9
    if (points[0].x == points[1].x &&
9
	points[1].y == points[2].y &&
9
	points[2].x == points[3].x &&
9
	points[3].y == points[0].y)
    {
9
	box->p1 = points[1];
9
	box->p2 = points[3];
9
	*_iter = iter;
9
	return TRUE;
    }
    return FALSE;
}
cairo_bool_t
2770
_cairo_path_fixed_iter_at_end (const cairo_path_fixed_iter_t *iter)
{
2770
    if (iter->buf == NULL)
2728
	return TRUE;
42
    return iter->n_op == iter->buf->num_ops;
}