1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/*
3
 * Copyright © 2002 Keith Packard
4
 * Copyright © 2007 Red Hat, Inc.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Keith Packard
32
 *
33
 * Contributor(s):
34
 *	Keith R. Packard <keithp@keithp.com>
35
 *	Carl D. Worth <cworth@cworth.org>
36
 *
37
 * 2002-07-15: Converted from XRenderCompositeDoublePoly to #cairo_trap_t. Carl D. Worth
38
 */
39

            
40
#include "cairoint.h"
41

            
42
#include "cairo-box-inline.h"
43
#include "cairo-boxes-private.h"
44
#include "cairo-error-private.h"
45
#include "cairo-line-private.h"
46
#include "cairo-region-private.h"
47
#include "cairo-slope-private.h"
48
#include "cairo-traps-private.h"
49
#include "cairo-spans-private.h"
50

            
51
/* private functions */
52

            
53
void
54
825
_cairo_traps_init (cairo_traps_t *traps)
55
{
56
    VG (VALGRIND_MAKE_MEM_UNDEFINED (traps, sizeof (cairo_traps_t)));
57

            
58
825
    traps->status = CAIRO_STATUS_SUCCESS;
59

            
60
825
    traps->maybe_region = 1;
61
825
    traps->is_rectilinear = 0;
62
825
    traps->is_rectangular = 0;
63

            
64
825
    traps->num_traps = 0;
65

            
66
825
    traps->traps_size = ARRAY_LENGTH (traps->traps_embedded);
67
825
    traps->traps = traps->traps_embedded;
68

            
69
825
    traps->num_limits = 0;
70
825
    traps->has_intersections = FALSE;
71
825
}
72

            
73
void
74
9
_cairo_traps_limit (cairo_traps_t	*traps,
75
		    const cairo_box_t	*limits,
76
		    int			 num_limits)
77
{
78
    int i;
79

            
80
9
    traps->limits = limits;
81
9
    traps->num_limits = num_limits;
82

            
83
9
    traps->bounds = limits[0];
84
27
    for (i = 1; i < num_limits; i++)
85
18
	_cairo_box_add_box (&traps->bounds, &limits[i]);
86
9
}
87

            
88
void
89
6
_cairo_traps_init_with_clip (cairo_traps_t *traps,
90
			     const cairo_clip_t *clip)
91
{
92
6
    _cairo_traps_init (traps);
93
6
    if (clip)
94
6
	_cairo_traps_limit (traps, clip->boxes, clip->num_boxes);
95
6
}
96

            
97
void
98
555
_cairo_traps_clear (cairo_traps_t *traps)
99
{
100
555
    traps->status = CAIRO_STATUS_SUCCESS;
101

            
102
555
    traps->maybe_region = 1;
103
555
    traps->is_rectilinear = 0;
104
555
    traps->is_rectangular = 0;
105

            
106
555
    traps->num_traps = 0;
107
555
    traps->has_intersections = FALSE;
108
555
}
109

            
110
void
111
825
_cairo_traps_fini (cairo_traps_t *traps)
112
{
113
825
    if (traps->traps != traps->traps_embedded)
114
270
	free (traps->traps);
115

            
116
    VG (VALGRIND_MAKE_MEM_UNDEFINED (traps, sizeof (cairo_traps_t)));
117
825
}
118

            
119
/* make room for at least one more trap */
120
static cairo_bool_t
121
408
_cairo_traps_grow (cairo_traps_t *traps)
122
{
123
    cairo_trapezoid_t *new_traps;
124
408
    int new_size = 4 * traps->traps_size;
125

            
126
    if (CAIRO_INJECT_FAULT ()) {
127
	traps->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
128
	return FALSE;
129
    }
130

            
131
408
    if (traps->traps == traps->traps_embedded) {
132
270
	new_traps = _cairo_malloc_ab (new_size, sizeof (cairo_trapezoid_t));
133
270
	if (new_traps != NULL)
134
270
	    memcpy (new_traps, traps->traps, sizeof (traps->traps_embedded));
135
    } else {
136
276
	new_traps = _cairo_realloc_ab (traps->traps,
137
	                               new_size, sizeof (cairo_trapezoid_t));
138
    }
139

            
140
408
    if (unlikely (new_traps == NULL)) {
141
	traps->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
142
	return FALSE;
143
    }
144

            
145
408
    traps->traps = new_traps;
146
408
    traps->traps_size = new_size;
147
408
    return TRUE;
148
}
149

            
150
void
151
31281
_cairo_traps_add_trap (cairo_traps_t *traps,
152
		       cairo_fixed_t top, cairo_fixed_t bottom,
153
		       const cairo_line_t *left,
154
		       const cairo_line_t *right)
155
{
156
    cairo_trapezoid_t *trap;
157

            
158
31281
    assert (left->p1.y != left->p2.y);
159
31281
    assert (right->p1.y != right->p2.y);
160
31281
    assert (bottom > top);
161

            
162
31281
    if (unlikely (traps->num_traps == traps->traps_size)) {
163
408
	if (unlikely (! _cairo_traps_grow (traps)))
164
	    return;
165
    }
166

            
167
31281
    trap = &traps->traps[traps->num_traps++];
168
31281
    trap->top = top;
169
31281
    trap->bottom = bottom;
170
31281
    trap->left = *left;
171
31281
    trap->right = *right;
172
}
173

            
174
static void
175
1218
_cairo_traps_add_clipped_trap (cairo_traps_t *traps,
176
			       cairo_fixed_t _top, cairo_fixed_t _bottom,
177
			       const cairo_line_t *_left,
178
			       const cairo_line_t *_right)
179
{
180
    /* Note: With the goofy trapezoid specification, (where an
181
     * arbitrary two points on the lines can specified for the left
182
     * and right edges), these limit checks would not work in
183
     * general. For example, one can imagine a trapezoid entirely
184
     * within the limits, but with two points used to specify the left
185
     * edge entirely to the right of the limits.  Fortunately, for our
186
     * purposes, cairo will never generate such a crazy
187
     * trapezoid. Instead, cairo always uses for its points the
188
     * extreme positions of the edge that are visible on at least some
189
     * trapezoid. With this constraint, it's impossible for both
190
     * points to be outside the limits while the relevant edge is
191
     * entirely inside the limits.
192
     */
193
1218
    if (traps->num_limits) {
194
1218
	const cairo_box_t *b = &traps->bounds;
195
1218
	cairo_fixed_t top = _top, bottom = _bottom;
196
1218
	cairo_line_t left = *_left, right = *_right;
197

            
198
	/* Trivially reject if trapezoid is entirely to the right or
199
	 * to the left of the limits. */
200
1218
	if (left.p1.x >= b->p2.x && left.p2.x >= b->p2.x)
201
48
	    return;
202

            
203
1218
	if (right.p1.x <= b->p1.x && right.p2.x <= b->p1.x)
204
	    return;
205

            
206
	/* And reject if the trapezoid is entirely above or below */
207
1218
	if (top >= b->p2.y || bottom <= b->p1.y)
208
	    return;
209

            
210
	/* Otherwise, clip the trapezoid to the limits. We only clip
211
	 * where an edge is entirely outside the limits. If we wanted
212
	 * to be more clever, we could handle cases where a trapezoid
213
	 * edge intersects the edge of the limits, but that would
214
	 * require slicing this trapezoid into multiple trapezoids,
215
	 * and I'm not sure the effort would be worth it. */
216
1218
	if (top < b->p1.y)
217
6
	    top = b->p1.y;
218

            
219
1218
	if (bottom > b->p2.y)
220
6
	    bottom = b->p2.y;
221

            
222
1218
	if (left.p1.x <= b->p1.x && left.p2.x <= b->p1.x)
223
	    left.p1.x = left.p2.x = b->p1.x;
224

            
225
1218
	if (right.p1.x >= b->p2.x && right.p2.x >= b->p2.x)
226
	    right.p1.x = right.p2.x = b->p2.x;
227

            
228
	/* Trivial discards for empty trapezoids that are likely to
229
	 * be produced by our tessellators (most notably convex_quad
230
	 * when given a simple rectangle).
231
	 */
232
1218
	if (top >= bottom)
233
48
	    return;
234

            
235
	/* cheap colinearity check */
236
1170
	if (right.p1.x <= left.p1.x && right.p1.y == left.p1.y &&
237
354
	    right.p2.x <= left.p2.x && right.p2.y == left.p2.y)
238
	    return;
239

            
240
1170
	_cairo_traps_add_trap (traps, top, bottom, &left, &right);
241
    } else
242
	_cairo_traps_add_trap (traps, _top, _bottom, _left, _right);
243
}
244

            
245
static int
246
984
_compare_point_fixed_by_y (const void *av, const void *bv)
247
{
248
984
    const cairo_point_t	*a = av, *b = bv;
249
984
    int ret = a->y - b->y;
250
984
    if (ret == 0)
251
18
	ret = a->x - b->x;
252
984
    return ret;
253
}
254

            
255
void
256
246
_cairo_traps_tessellate_convex_quad (cairo_traps_t *traps,
257
				     const cairo_point_t q[4])
258
{
259
    int a, b, c, d;
260
    int i;
261
    cairo_slope_t ab, ad;
262
    cairo_bool_t b_left_of_d;
263
    cairo_line_t left;
264
    cairo_line_t right;
265

            
266
    /* Choose a as a point with minimal y */
267
246
    a = 0;
268
984
    for (i = 1; i < 4; i++)
269
738
	if (_compare_point_fixed_by_y (&q[i], &q[a]) < 0)
270
282
	    a = i;
271

            
272
    /* b and d are adjacent to a, while c is opposite */
273
246
    b = (a + 1) % 4;
274
246
    c = (a + 2) % 4;
275
246
    d = (a + 3) % 4;
276

            
277
    /* Choose between b and d so that b.y is less than d.y */
278
246
    if (_compare_point_fixed_by_y (&q[d], &q[b]) < 0) {
279
138
	b = (a + 3) % 4;
280
138
	d = (a + 1) % 4;
281
    }
282

            
283
    /* Without freedom left to choose anything else, we have four
284
     * cases to tessellate.
285
     *
286
     * First, we have to determine the Y-axis sort of the four
287
     * vertices, (either abcd or abdc). After that we need to determine
288
     * which edges will be "left" and which will be "right" in the
289
     * resulting trapezoids. This can be determined by computing a
290
     * slope comparison of ab and ad to determine if b is left of d or
291
     * not.
292
     *
293
     * Note that "left of" here is in the sense of which edges should
294
     * be the left vs. right edges of the trapezoid. In particular, b
295
     * left of d does *not* mean that b.x is less than d.x.
296
     *
297
     * This should hopefully be made clear in the lame ASCII art
298
     * below. Since the same slope comparison is used in all cases, we
299
     * compute it before testing for the Y-value sort. */
300

            
301
    /* Note: If a == b then the ab slope doesn't give us any
302
     * information. In that case, we can replace it with the ac (or
303
     * equivalenly the bc) slope which gives us exactly the same
304
     * information we need. At worst the names of the identifiers ab
305
     * and b_left_of_d are inaccurate in this case, (would be ac, and
306
     * c_left_of_d). */
307
246
    if (q[a].x == q[b].x && q[a].y == q[b].y)
308
	_cairo_slope_init (&ab, &q[a], &q[c]);
309
    else
310
246
	_cairo_slope_init (&ab, &q[a], &q[b]);
311

            
312
246
    _cairo_slope_init (&ad, &q[a], &q[d]);
313

            
314
246
    b_left_of_d = _cairo_slope_compare (&ab, &ad) > 0;
315

            
316
246
    if (q[c].y <= q[d].y) {
317
12
	if (b_left_of_d) {
318
	    /* Y-sort is abcd and b is left of d, (slope(ab) > slope (ad))
319
	     *
320
	     *                      top bot left right
321
	     *        _a  a  a
322
	     *      / /  /|  |\      a.y b.y  ab   ad
323
	     *     b /  b |  b \
324
	     *    / /   | |   \ \    b.y c.y  bc   ad
325
	     *   c /    c |    c \
326
	     *  | /      \|     \ \  c.y d.y  cd   ad
327
	     *  d         d       d
328
	     */
329
	    left.p1  = q[a]; left.p2  = q[b];
330
	    right.p1 = q[a]; right.p2 = q[d];
331
	    _cairo_traps_add_clipped_trap (traps, q[a].y, q[b].y, &left, &right);
332
	    left.p1  = q[b]; left.p2  = q[c];
333
	    _cairo_traps_add_clipped_trap (traps, q[b].y, q[c].y, &left, &right);
334
	    left.p1  = q[c]; left.p2  = q[d];
335
	    _cairo_traps_add_clipped_trap (traps, q[c].y, q[d].y, &left, &right);
336
	} else {
337
	    /* Y-sort is abcd and b is right of d, (slope(ab) <= slope (ad))
338
	     *
339
	     *       a  a  a_
340
	     *      /|  |\  \ \     a.y b.y  ad  ab
341
	     *     / b  | b  \ b
342
	     *    / /   | |   \ \   b.y c.y  ad  bc
343
	     *   / c    | c    \ c
344
	     *  / /     |/      \ | c.y d.y  ad  cd
345
	     *  d       d         d
346
	     */
347
12
	    left.p1  = q[a]; left.p2  = q[d];
348
12
	    right.p1 = q[a]; right.p2 = q[b];
349
12
	    _cairo_traps_add_clipped_trap (traps, q[a].y, q[b].y, &left, &right);
350
12
	    right.p1 = q[b]; right.p2 = q[c];
351
12
	    _cairo_traps_add_clipped_trap (traps, q[b].y, q[c].y, &left, &right);
352
12
	    right.p1 = q[c]; right.p2 = q[d];
353
12
	    _cairo_traps_add_clipped_trap (traps, q[c].y, q[d].y, &left, &right);
354
	}
355
    } else {
356
234
	if (b_left_of_d) {
357
	    /* Y-sort is abdc and b is left of d, (slope (ab) > slope (ad))
358
	     *
359
	     *        a   a     a
360
	     *       //  / \    |\     a.y b.y  ab  ad
361
	     *     /b/  b   \   b \
362
	     *    / /    \   \   \ \   b.y d.y  bc  ad
363
	     *   /d/      \   d   \ d
364
	     *  //         \ /     \|  d.y c.y  bc  dc
365
	     *  c           c       c
366
	     */
367
108
	    left.p1  = q[a]; left.p2  = q[b];
368
108
	    right.p1 = q[a]; right.p2 = q[d];
369
108
	    _cairo_traps_add_clipped_trap (traps, q[a].y, q[b].y, &left, &right);
370
108
	    left.p1  = q[b]; left.p2  = q[c];
371
108
	    _cairo_traps_add_clipped_trap (traps, q[b].y, q[d].y, &left, &right);
372
108
	    right.p1 = q[d]; right.p2 = q[c];
373
108
	    _cairo_traps_add_clipped_trap (traps, q[d].y, q[c].y, &left, &right);
374
	} else {
375
	    /* Y-sort is abdc and b is right of d, (slope (ab) <= slope (ad))
376
	     *
377
	     *      a     a   a
378
	     *     /|    / \  \\       a.y b.y  ad  ab
379
	     *    / b   /   b  \b\
380
	     *   / /   /   /    \ \    b.y d.y  ad  bc
381
	     *  d /   d   /	 \d\
382
	     *  |/     \ /         \\  d.y c.y  dc  bc
383
	     *  c       c	   c
384
	     */
385
126
	    left.p1  = q[a]; left.p2  = q[d];
386
126
	    right.p1 = q[a]; right.p2 = q[b];
387
126
	    _cairo_traps_add_clipped_trap (traps, q[a].y, q[b].y, &left, &right);
388
126
	    right.p1 = q[b]; right.p2 = q[c];
389
126
	    _cairo_traps_add_clipped_trap (traps, q[b].y, q[d].y, &left, &right);
390
126
	    left.p1  = q[d]; left.p2  = q[c];
391
126
	    _cairo_traps_add_clipped_trap (traps, q[d].y, q[c].y, &left, &right);
392
	}
393
    }
394
246
}
395

            
396
480
static void add_tri (cairo_traps_t *traps,
397
		     int y1, int y2,
398
		     const cairo_line_t *left,
399
		     const cairo_line_t *right)
400
{
401
480
    if (y2 < y1) {
402
234
	int tmp = y1;
403
234
	y1 = y2;
404
234
	y2 = tmp;
405
    }
406

            
407
480
    if (_cairo_lines_compare_at_y (left, right, y1) > 0) {
408
240
	const cairo_line_t *tmp = left;
409
240
	left = right;
410
240
	right = tmp;
411
    }
412

            
413
480
    _cairo_traps_add_clipped_trap (traps, y1, y2, left, right);
414
480
}
415

            
416
void
417
240
_cairo_traps_tessellate_triangle_with_edges (cairo_traps_t *traps,
418
					     const cairo_point_t t[3],
419
					     const cairo_point_t edges[4])
420
{
421
    cairo_line_t lines[3];
422

            
423
240
    if (edges[0].y <= edges[1].y) {
424
126
	    lines[0].p1 = edges[0];
425
126
	    lines[0].p2 = edges[1];
426
    } else {
427
114
	    lines[0].p1 = edges[1];
428
114
	    lines[0].p2 = edges[0];
429
    }
430

            
431
240
    if (edges[2].y <= edges[3].y) {
432
126
	    lines[1].p1 = edges[2];
433
126
	    lines[1].p2 = edges[3];
434
    } else {
435
114
	    lines[1].p1 = edges[3];
436
114
	    lines[1].p2 = edges[2];
437
    }
438

            
439
240
    if (t[1].y == t[2].y) {
440
	add_tri (traps, t[0].y, t[1].y, &lines[0], &lines[1]);
441
	return;
442
    }
443

            
444
240
    if (t[1].y <= t[2].y) {
445
120
	    lines[2].p1 = t[1];
446
120
	    lines[2].p2 = t[2];
447
    } else {
448
120
	    lines[2].p1 = t[2];
449
120
	    lines[2].p2 = t[1];
450
    }
451

            
452
240
    if (((t[1].y - t[0].y) < 0) ^ ((t[2].y - t[0].y) < 0)) {
453
12
	add_tri (traps, t[0].y, t[1].y, &lines[0], &lines[2]);
454
12
	add_tri (traps, t[0].y, t[2].y, &lines[1], &lines[2]);
455
228
    } else if (abs(t[1].y - t[0].y) < abs(t[2].y - t[0].y)) {
456
114
	add_tri (traps, t[0].y, t[1].y, &lines[0], &lines[1]);
457
114
	add_tri (traps, t[1].y, t[2].y, &lines[2], &lines[1]);
458
    } else {
459
114
	add_tri (traps, t[0].y, t[2].y, &lines[1], &lines[0]);
460
114
	add_tri (traps, t[1].y, t[2].y, &lines[2], &lines[0]);
461
    }
462
}
463

            
464
/**
465
 * _cairo_traps_init_boxes:
466
 * @traps: a #cairo_traps_t
467
 * @box: an array box that will each be converted to a single trapezoid
468
 *       to store in @traps.
469
 *
470
 * Initializes a #cairo_traps_t to contain an array of rectangular
471
 * trapezoids.
472
 **/
473
cairo_status_t
474
63
_cairo_traps_init_boxes (cairo_traps_t	    *traps,
475
		         const cairo_boxes_t *boxes)
476
{
477
    cairo_trapezoid_t *trap;
478
    const struct _cairo_boxes_chunk *chunk;
479

            
480
63
    _cairo_traps_init (traps);
481

            
482
63
    while (traps->traps_size < boxes->num_boxes) {
483
	if (unlikely (! _cairo_traps_grow (traps))) {
484
	    _cairo_traps_fini (traps);
485
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
486
	}
487
    }
488

            
489
63
    traps->num_traps = boxes->num_boxes;
490
63
    traps->is_rectilinear = TRUE;
491
63
    traps->is_rectangular = TRUE;
492
63
    traps->maybe_region = boxes->is_pixel_aligned;
493

            
494
63
    trap = &traps->traps[0];
495
126
    for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
496
	const cairo_box_t *box;
497
	int i;
498

            
499
63
	box = chunk->base;
500
183
	for (i = 0; i < chunk->count; i++) {
501
120
	    trap->top    = box->p1.y;
502
120
	    trap->bottom = box->p2.y;
503

            
504
120
	    trap->left.p1   = box->p1;
505
120
	    trap->left.p2.x = box->p1.x;
506
120
	    trap->left.p2.y = box->p2.y;
507

            
508
120
	    trap->right.p1.x = box->p2.x;
509
120
	    trap->right.p1.y = box->p1.y;
510
120
	    trap->right.p2   = box->p2;
511

            
512
120
	    box++, trap++;
513
	}
514
    }
515

            
516
63
    return CAIRO_STATUS_SUCCESS;
517
}
518

            
519
cairo_status_t
520
_cairo_traps_tessellate_rectangle (cairo_traps_t *traps,
521
				   const cairo_point_t *top_left,
522
				   const cairo_point_t *bottom_right)
523
{
524
    cairo_line_t left;
525
    cairo_line_t right;
526
    cairo_fixed_t top, bottom;
527

            
528
    if (top_left->y == bottom_right->y)
529
	return CAIRO_STATUS_SUCCESS;
530

            
531
    if (top_left->x == bottom_right->x)
532
	return CAIRO_STATUS_SUCCESS;
533

            
534
     left.p1.x =  left.p2.x = top_left->x;
535
     left.p1.y = right.p1.y = top_left->y;
536
    right.p1.x = right.p2.x = bottom_right->x;
537
     left.p2.y = right.p2.y = bottom_right->y;
538

            
539
     top = top_left->y;
540
     bottom = bottom_right->y;
541

            
542
    if (traps->num_limits) {
543
	cairo_bool_t reversed;
544
	int n;
545

            
546
	if (top >= traps->bounds.p2.y || bottom <= traps->bounds.p1.y)
547
	    return CAIRO_STATUS_SUCCESS;
548

            
549
	/* support counter-clockwise winding for rectangular tessellation */
550
	reversed = top_left->x > bottom_right->x;
551
	if (reversed) {
552
	    right.p1.x = right.p2.x = top_left->x;
553
	    left.p1.x = left.p2.x = bottom_right->x;
554
	}
555

            
556
	if (left.p1.x >= traps->bounds.p2.x || right.p1.x <= traps->bounds.p1.x)
557
	    return CAIRO_STATUS_SUCCESS;
558

            
559
	for (n = 0; n < traps->num_limits; n++) {
560
	    const cairo_box_t *limits = &traps->limits[n];
561
	    cairo_line_t _left, _right;
562
	    cairo_fixed_t _top, _bottom;
563

            
564
	    if (top >= limits->p2.y)
565
		continue;
566
	    if (bottom <= limits->p1.y)
567
		continue;
568

            
569
	    /* Trivially reject if trapezoid is entirely to the right or
570
	     * to the left of the limits. */
571
	    if (left.p1.x >= limits->p2.x)
572
		continue;
573
	    if (right.p1.x <= limits->p1.x)
574
		continue;
575

            
576
	    /* Otherwise, clip the trapezoid to the limits. */
577
	    _top = top;
578
	    if (_top < limits->p1.y)
579
		_top = limits->p1.y;
580

            
581
	    _bottom = bottom;
582
	    if (_bottom > limits->p2.y)
583
		_bottom = limits->p2.y;
584

            
585
	    if (_bottom <= _top)
586
		continue;
587

            
588
	    _left = left;
589
	    if (_left.p1.x < limits->p1.x) {
590
		_left.p1.x = limits->p1.x;
591
		_left.p1.y = limits->p1.y;
592
		_left.p2.x = limits->p1.x;
593
		_left.p2.y = limits->p2.y;
594
	    }
595

            
596
	    _right = right;
597
	    if (_right.p1.x > limits->p2.x) {
598
		_right.p1.x = limits->p2.x;
599
		_right.p1.y = limits->p1.y;
600
		_right.p2.x = limits->p2.x;
601
		_right.p2.y = limits->p2.y;
602
	    }
603

            
604
	    if (left.p1.x >= right.p1.x)
605
		continue;
606

            
607
	    if (reversed)
608
		_cairo_traps_add_trap (traps, _top, _bottom, &_right, &_left);
609
	    else
610
		_cairo_traps_add_trap (traps, _top, _bottom, &_left, &_right);
611
	}
612
    } else {
613
	_cairo_traps_add_trap (traps, top, bottom, &left, &right);
614
    }
615

            
616
    return traps->status;
617
}
618

            
619
void
620
_cairo_traps_translate (cairo_traps_t *traps, int x, int y)
621
{
622
    cairo_fixed_t xoff, yoff;
623
    cairo_trapezoid_t *t;
624
    int i;
625

            
626
    /* Ugh. The cairo_composite/(Render) interface doesn't allow
627
       an offset for the trapezoids. Need to manually shift all
628
       the coordinates to align with the offset origin of the
629
       intermediate surface. */
630

            
631
    xoff = _cairo_fixed_from_int (x);
632
    yoff = _cairo_fixed_from_int (y);
633

            
634
    for (i = 0, t = traps->traps; i < traps->num_traps; i++, t++) {
635
	t->top += yoff;
636
	t->bottom += yoff;
637
	t->left.p1.x += xoff;
638
	t->left.p1.y += yoff;
639
	t->left.p2.x += xoff;
640
	t->left.p2.y += yoff;
641
	t->right.p1.x += xoff;
642
	t->right.p1.y += yoff;
643
	t->right.p2.x += xoff;
644
	t->right.p2.y += yoff;
645
    }
646
}
647

            
648
void
649
_cairo_trapezoid_array_translate_and_scale (cairo_trapezoid_t *offset_traps,
650
                                            cairo_trapezoid_t *src_traps,
651
                                            int num_traps,
652
                                            double tx, double ty,
653
                                            double sx, double sy)
654
{
655
    int i;
656
    cairo_fixed_t xoff = _cairo_fixed_from_double (tx);
657
    cairo_fixed_t yoff = _cairo_fixed_from_double (ty);
658

            
659
    if (sx == 1.0 && sy == 1.0) {
660
        for (i = 0; i < num_traps; i++) {
661
            offset_traps[i].top = src_traps[i].top + yoff;
662
            offset_traps[i].bottom = src_traps[i].bottom + yoff;
663
            offset_traps[i].left.p1.x = src_traps[i].left.p1.x + xoff;
664
            offset_traps[i].left.p1.y = src_traps[i].left.p1.y + yoff;
665
            offset_traps[i].left.p2.x = src_traps[i].left.p2.x + xoff;
666
            offset_traps[i].left.p2.y = src_traps[i].left.p2.y + yoff;
667
            offset_traps[i].right.p1.x = src_traps[i].right.p1.x + xoff;
668
            offset_traps[i].right.p1.y = src_traps[i].right.p1.y + yoff;
669
            offset_traps[i].right.p2.x = src_traps[i].right.p2.x + xoff;
670
            offset_traps[i].right.p2.y = src_traps[i].right.p2.y + yoff;
671
        }
672
    } else {
673
        cairo_fixed_t xsc = _cairo_fixed_from_double (sx);
674
        cairo_fixed_t ysc = _cairo_fixed_from_double (sy);
675

            
676
        for (i = 0; i < num_traps; i++) {
677
            offset_traps[i].top = _cairo_fixed_mul (src_traps[i].top + yoff, ysc);
678
            offset_traps[i].bottom = _cairo_fixed_mul (src_traps[i].bottom + yoff, ysc);
679
            offset_traps[i].left.p1.x = _cairo_fixed_mul (src_traps[i].left.p1.x + xoff, xsc);
680
            offset_traps[i].left.p1.y = _cairo_fixed_mul (src_traps[i].left.p1.y + yoff, ysc);
681
            offset_traps[i].left.p2.x = _cairo_fixed_mul (src_traps[i].left.p2.x + xoff, xsc);
682
            offset_traps[i].left.p2.y = _cairo_fixed_mul (src_traps[i].left.p2.y + yoff, ysc);
683
            offset_traps[i].right.p1.x = _cairo_fixed_mul (src_traps[i].right.p1.x + xoff, xsc);
684
            offset_traps[i].right.p1.y = _cairo_fixed_mul (src_traps[i].right.p1.y + yoff, ysc);
685
            offset_traps[i].right.p2.x = _cairo_fixed_mul (src_traps[i].right.p2.x + xoff, xsc);
686
            offset_traps[i].right.p2.y = _cairo_fixed_mul (src_traps[i].right.p2.y + yoff, ysc);
687
        }
688
    }
689
}
690

            
691
static cairo_bool_t
692
3
_cairo_trap_contains (cairo_trapezoid_t *t, cairo_point_t *pt)
693
{
694
    cairo_slope_t slope_left, slope_pt, slope_right;
695

            
696
3
    if (t->top > pt->y)
697
	return FALSE;
698
3
    if (t->bottom < pt->y)
699
	return FALSE;
700

            
701
3
    _cairo_slope_init (&slope_left, &t->left.p1, &t->left.p2);
702
3
    _cairo_slope_init (&slope_pt, &t->left.p1, pt);
703

            
704
3
    if (_cairo_slope_compare (&slope_left, &slope_pt) < 0)
705
3
	return FALSE;
706

            
707
    _cairo_slope_init (&slope_right, &t->right.p1, &t->right.p2);
708
    _cairo_slope_init (&slope_pt, &t->right.p1, pt);
709

            
710
    if (_cairo_slope_compare (&slope_pt, &slope_right) < 0)
711
	return FALSE;
712

            
713
    return TRUE;
714
}
715

            
716
cairo_bool_t
717
3
_cairo_traps_contain (const cairo_traps_t *traps,
718
		      double x, double y)
719
{
720
    int i;
721
    cairo_point_t point;
722

            
723
3
    point.x = _cairo_fixed_from_double (x);
724
3
    point.y = _cairo_fixed_from_double (y);
725

            
726
6
    for (i = 0; i < traps->num_traps; i++) {
727
3
	if (_cairo_trap_contains (&traps->traps[i], &point))
728
	    return TRUE;
729
    }
730

            
731
3
    return FALSE;
732
}
733

            
734
static cairo_fixed_t
735
636
_line_compute_intersection_x_for_y (const cairo_line_t *line,
736
				    cairo_fixed_t y)
737
{
738
636
    return _cairo_edge_compute_intersection_x_for_y (&line->p1, &line->p2, y);
739
}
740

            
741
void
742
171
_cairo_traps_extents (const cairo_traps_t *traps,
743
		      cairo_box_t *extents)
744
{
745
    int i;
746

            
747
171
    if (traps->num_traps == 0) {
748
48
	extents->p1.x = extents->p1.y = 0;
749
48
	extents->p2.x = extents->p2.y = 0;
750
48
	return;
751
    }
752

            
753
123
    extents->p1.x = extents->p1.y = INT32_MAX;
754
123
    extents->p2.x = extents->p2.y = INT32_MIN;
755

            
756
5190
    for (i = 0; i < traps->num_traps; i++) {
757
5067
	const cairo_trapezoid_t *trap =  &traps->traps[i];
758

            
759
5067
	if (trap->top < extents->p1.y)
760
261
	    extents->p1.y = trap->top;
761
5067
	if (trap->bottom > extents->p2.y)
762
2247
	    extents->p2.y = trap->bottom;
763

            
764
5067
	if (trap->left.p1.x < extents->p1.x) {
765
201
	    cairo_fixed_t x = trap->left.p1.x;
766
201
	    if (trap->top != trap->left.p1.y) {
767
30
		x = _line_compute_intersection_x_for_y (&trap->left,
768
30
							trap->top);
769
30
		if (x < extents->p1.x)
770
24
		    extents->p1.x = x;
771
	    } else
772
171
		extents->p1.x = x;
773
	}
774
5067
	if (trap->left.p2.x < extents->p1.x) {
775
942
	    cairo_fixed_t x = trap->left.p2.x;
776
942
	    if (trap->bottom != trap->left.p2.y) {
777
372
		x = _line_compute_intersection_x_for_y (&trap->left,
778
372
							trap->bottom);
779
372
		if (x < extents->p1.x)
780
180
		    extents->p1.x = x;
781
	    } else
782
570
		extents->p1.x = x;
783
	}
784

            
785
5067
	if (trap->right.p1.x > extents->p2.x) {
786
324
	    cairo_fixed_t x = trap->right.p1.x;
787
324
	    if (trap->top != trap->right.p1.y) {
788
198
		x = _line_compute_intersection_x_for_y (&trap->right,
789
198
							trap->top);
790
198
		if (x > extents->p2.x)
791
48
		    extents->p2.x = x;
792
	    } else
793
126
		extents->p2.x = x;
794
	}
795
5067
	if (trap->right.p2.x > extents->p2.x) {
796
291
	    cairo_fixed_t x = trap->right.p2.x;
797
291
	    if (trap->bottom != trap->right.p2.y) {
798
36
		x = _line_compute_intersection_x_for_y (&trap->right,
799
36
							trap->bottom);
800
36
		if (x > extents->p2.x)
801
33
		    extents->p2.x = x;
802
	    } else
803
255
		extents->p2.x = x;
804
	}
805
    }
806
}
807

            
808
static cairo_bool_t
809
_mono_edge_is_vertical (const cairo_line_t *line)
810
{
811
    return _cairo_fixed_integer_round_down (line->p1.x) == _cairo_fixed_integer_round_down (line->p2.x);
812
}
813

            
814
static cairo_bool_t
815
_traps_are_pixel_aligned (cairo_traps_t *traps,
816
			  cairo_antialias_t antialias)
817
{
818
    int i;
819

            
820
    if (antialias == CAIRO_ANTIALIAS_NONE) {
821
	for (i = 0; i < traps->num_traps; i++) {
822
	    if (! _mono_edge_is_vertical (&traps->traps[i].left)   ||
823
		! _mono_edge_is_vertical (&traps->traps[i].right))
824
	    {
825
		traps->maybe_region = FALSE;
826
		return FALSE;
827
	    }
828
	}
829
    } else {
830
	for (i = 0; i < traps->num_traps; i++) {
831
	    if (traps->traps[i].left.p1.x != traps->traps[i].left.p2.x   ||
832
		traps->traps[i].right.p1.x != traps->traps[i].right.p2.x ||
833
		! _cairo_fixed_is_integer (traps->traps[i].top)          ||
834
		! _cairo_fixed_is_integer (traps->traps[i].bottom)       ||
835
		! _cairo_fixed_is_integer (traps->traps[i].left.p1.x)    ||
836
		! _cairo_fixed_is_integer (traps->traps[i].right.p1.x))
837
	    {
838
		traps->maybe_region = FALSE;
839
		return FALSE;
840
	    }
841
	}
842
    }
843

            
844
    return TRUE;
845
}
846

            
847
/**
848
 * _cairo_traps_extract_region:
849
 * @traps: a #cairo_traps_t
850
 * @region: a #cairo_region_t
851
 *
852
 * Determines if a set of trapezoids are exactly representable as a
853
 * cairo region.  If so, the passed-in region is initialized to
854
 * the area representing the given traps.  It should be finalized
855
 * with cairo_region_fini().  If not, %CAIRO_INT_STATUS_UNSUPPORTED
856
 * is returned.
857
 *
858
 * Return value: %CAIRO_STATUS_SUCCESS, %CAIRO_INT_STATUS_UNSUPPORTED
859
 * or %CAIRO_STATUS_NO_MEMORY
860
 **/
861
cairo_int_status_t
862
_cairo_traps_extract_region (cairo_traps_t   *traps,
863
			     cairo_antialias_t antialias,
864
			     cairo_region_t **region)
865
{
866
    cairo_rectangle_int_t stack_rects[CAIRO_STACK_ARRAY_LENGTH (cairo_rectangle_int_t)];
867
    cairo_rectangle_int_t *rects = stack_rects;
868
    cairo_int_status_t status;
869
    int i, rect_count;
870

            
871
    /* we only treat this a hint... */
872
    if (antialias != CAIRO_ANTIALIAS_NONE && ! traps->maybe_region)
873
	return CAIRO_INT_STATUS_UNSUPPORTED;
874

            
875
    if (! _traps_are_pixel_aligned (traps, antialias)) {
876
	traps->maybe_region = FALSE;
877
	return CAIRO_INT_STATUS_UNSUPPORTED;
878
    }
879

            
880
    if (traps->num_traps > ARRAY_LENGTH (stack_rects)) {
881
	rects = _cairo_malloc_ab (traps->num_traps, sizeof (cairo_rectangle_int_t));
882

            
883
	if (unlikely (rects == NULL))
884
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
885
    }
886

            
887
    rect_count = 0;
888
    for (i = 0; i < traps->num_traps; i++) {
889
	int x1, y1, x2, y2;
890

            
891
	if (antialias == CAIRO_ANTIALIAS_NONE) {
892
	    x1 = _cairo_fixed_integer_round_down (traps->traps[i].left.p1.x);
893
	    y1 = _cairo_fixed_integer_round_down (traps->traps[i].top);
894
	    x2 = _cairo_fixed_integer_round_down (traps->traps[i].right.p1.x);
895
	    y2 = _cairo_fixed_integer_round_down (traps->traps[i].bottom);
896
	} else {
897
	    x1 = _cairo_fixed_integer_part (traps->traps[i].left.p1.x);
898
	    y1 = _cairo_fixed_integer_part (traps->traps[i].top);
899
	    x2 = _cairo_fixed_integer_part (traps->traps[i].right.p1.x);
900
	    y2 = _cairo_fixed_integer_part (traps->traps[i].bottom);
901
	}
902

            
903
	if (x2 > x1 && y2 > y1) {
904
	    rects[rect_count].x = x1;
905
	    rects[rect_count].y = y1;
906
	    rects[rect_count].width  = x2 - x1;
907
	    rects[rect_count].height = y2 - y1;
908
	    rect_count++;
909
	}
910
    }
911

            
912

            
913
    *region = cairo_region_create_rectangles (rects, rect_count);
914
    status = (*region)->status;
915

            
916
    if (rects != stack_rects)
917
	free (rects);
918

            
919
    return status;
920
}
921

            
922
cairo_bool_t
923
3
_cairo_traps_to_boxes (cairo_traps_t *traps,
924
		       cairo_antialias_t antialias,
925
		       cairo_boxes_t *boxes)
926
{
927
    int i;
928

            
929
3
    for (i = 0; i < traps->num_traps; i++) {
930
3
	if (traps->traps[i].left.p1.x  != traps->traps[i].left.p2.x ||
931
	    traps->traps[i].right.p1.x != traps->traps[i].right.p2.x)
932
3
	    return FALSE;
933
    }
934

            
935
    _cairo_boxes_init (boxes);
936

            
937
    boxes->num_boxes    = traps->num_traps;
938
    boxes->chunks.base  = (cairo_box_t *) traps->traps;
939
    boxes->chunks.count = traps->num_traps;
940
    boxes->chunks.size  = traps->num_traps;
941

            
942
    if (antialias != CAIRO_ANTIALIAS_NONE) {
943
	for (i = 0; i < traps->num_traps; i++) {
944
	    /* Note the traps and boxes alias so we need to take the local copies first. */
945
	    cairo_fixed_t x1 = traps->traps[i].left.p1.x;
946
	    cairo_fixed_t x2 = traps->traps[i].right.p1.x;
947
	    cairo_fixed_t y1 = traps->traps[i].top;
948
	    cairo_fixed_t y2 = traps->traps[i].bottom;
949

            
950
	    boxes->chunks.base[i].p1.x = x1;
951
	    boxes->chunks.base[i].p1.y = y1;
952
	    boxes->chunks.base[i].p2.x = x2;
953
	    boxes->chunks.base[i].p2.y = y2;
954

            
955
	    if (boxes->is_pixel_aligned) {
956
		boxes->is_pixel_aligned =
957
		    _cairo_fixed_is_integer (x1) && _cairo_fixed_is_integer (y1) &&
958
		    _cairo_fixed_is_integer (x2) && _cairo_fixed_is_integer (y2);
959
	    }
960
	}
961
    } else {
962
	boxes->is_pixel_aligned = TRUE;
963

            
964
	for (i = 0; i < traps->num_traps; i++) {
965
	    /* Note the traps and boxes alias so we need to take the local copies first. */
966
	    cairo_fixed_t x1 = traps->traps[i].left.p1.x;
967
	    cairo_fixed_t x2 = traps->traps[i].right.p1.x;
968
	    cairo_fixed_t y1 = traps->traps[i].top;
969
	    cairo_fixed_t y2 = traps->traps[i].bottom;
970

            
971
	    /* round down here to match Pixman's behavior when using traps. */
972
	    boxes->chunks.base[i].p1.x = _cairo_fixed_round_down (x1);
973
	    boxes->chunks.base[i].p1.y = _cairo_fixed_round_down (y1);
974
	    boxes->chunks.base[i].p2.x = _cairo_fixed_round_down (x2);
975
	    boxes->chunks.base[i].p2.y = _cairo_fixed_round_down (y2);
976
	}
977
    }
978

            
979
    return TRUE;
980
}
981

            
982
/* moves trap points such that they become the actual corners of the trapezoid */
983
static void
984
14775
_sanitize_trap (cairo_trapezoid_t *t)
985
{
986
14775
    cairo_trapezoid_t s = *t;
987

            
988
#define FIX(lr, tb, p) \
989
    if (t->lr.p.y != t->tb) { \
990
        t->lr.p.x = s.lr.p2.x + _cairo_fixed_mul_div_floor (s.lr.p1.x - s.lr.p2.x, s.tb - s.lr.p2.y, s.lr.p1.y - s.lr.p2.y); \
991
        t->lr.p.y = s.tb; \
992
    }
993
14775
    FIX (left,  top,    p1);
994
14775
    FIX (left,  bottom, p2);
995
14775
    FIX (right, top,    p1);
996
14775
    FIX (right, bottom, p2);
997
14775
}
998

            
999
cairo_private cairo_status_t
213
_cairo_traps_path (const cairo_traps_t *traps,
		   cairo_path_fixed_t  *path)
{
    int i;
14988
    for (i = 0; i < traps->num_traps; i++) {
	cairo_status_t status;
14775
	cairo_trapezoid_t trap = traps->traps[i];
14775
	if (trap.top == trap.bottom)
	    continue;
14775
	_sanitize_trap (&trap);
14775
	status = _cairo_path_fixed_move_to (path, trap.left.p1.x, trap.top);
14775
	if (unlikely (status)) return status;
14775
	status = _cairo_path_fixed_line_to (path, trap.right.p1.x, trap.top);
14775
	if (unlikely (status)) return status;
14775
	status = _cairo_path_fixed_line_to (path, trap.right.p2.x, trap.bottom);
14775
	if (unlikely (status)) return status;
14775
	status = _cairo_path_fixed_line_to (path, trap.left.p2.x, trap.bottom);
14775
	if (unlikely (status)) return status;
14775
	status = _cairo_path_fixed_close_path (path);
14775
	if (unlikely (status)) return status;
    }
213
    return CAIRO_STATUS_SUCCESS;
}
void
_cairo_debug_print_traps (FILE *file, const cairo_traps_t *traps)
{
    cairo_box_t extents;
    int n;
#if 0
    if (traps->has_limits) {
	printf ("%s: limits=(%d, %d, %d, %d)\n",
		filename,
		traps->limits.p1.x, traps->limits.p1.y,
		traps->limits.p2.x, traps->limits.p2.y);
    }
#endif
    _cairo_traps_extents (traps, &extents);
    fprintf (file, "extents=(%d, %d, %d, %d)\n",
	     extents.p1.x, extents.p1.y,
	     extents.p2.x, extents.p2.y);
    for (n = 0; n < traps->num_traps; n++) {
	fprintf (file, "%d %d L:(%d, %d), (%d, %d) R:(%d, %d), (%d, %d)\n",
		 traps->traps[n].top,
		 traps->traps[n].bottom,
		 traps->traps[n].left.p1.x,
		 traps->traps[n].left.p1.y,
		 traps->traps[n].left.p2.x,
		 traps->traps[n].left.p2.y,
		 traps->traps[n].right.p1.x,
		 traps->traps[n].right.p1.y,
		 traps->traps[n].right.p2.x,
		 traps->traps[n].right.p2.y);
    }
}
struct cairo_trap_renderer {
    cairo_span_renderer_t base;
    cairo_traps_t *traps;
};
static cairo_status_t
span_to_traps (void *abstract_renderer, int y, int h,
	       const cairo_half_open_span_t *spans, unsigned num_spans)
{
    struct cairo_trap_renderer *r = abstract_renderer;
    cairo_fixed_t top, bot;
    if (num_spans == 0)
	return CAIRO_STATUS_SUCCESS;
    top = _cairo_fixed_from_int (y);
    bot = _cairo_fixed_from_int (y + h);
    do {
	if (spans[0].coverage) {
	    cairo_fixed_t x0 = _cairo_fixed_from_int(spans[0].x);
	    cairo_fixed_t x1 = _cairo_fixed_from_int(spans[1].x);
	    cairo_line_t left = { { x0, top }, { x0, bot } },
			 right = { { x1, top }, { x1, bot } };
	    _cairo_traps_add_trap (r->traps, top, bot, &left, &right);
	}
	spans++;
    } while (--num_spans > 1);
    return CAIRO_STATUS_SUCCESS;
}
cairo_int_status_t
_cairo_rasterise_polygon_to_traps (cairo_polygon_t			*polygon,
				   cairo_fill_rule_t			 fill_rule,
				   cairo_antialias_t			 antialias,
				   cairo_traps_t *traps)
{
    struct cairo_trap_renderer renderer;
    cairo_scan_converter_t *converter;
    cairo_int_status_t status;
    cairo_rectangle_int_t r;
    TRACE ((stderr, "%s: fill_rule=%d, antialias=%d\n",
	    __FUNCTION__, fill_rule, antialias));
    assert(antialias == CAIRO_ANTIALIAS_NONE);
    renderer.traps = traps;
    renderer.base.render_rows = span_to_traps;
    _cairo_box_round_to_rectangle (&polygon->extents, &r);
    converter = _cairo_mono_scan_converter_create (r.x, r.y,
						   r.x + r.width,
						   r.y + r.height,
						   fill_rule);
    status = _cairo_mono_scan_converter_add_polygon (converter, polygon);
    if (likely (status == CAIRO_INT_STATUS_SUCCESS))
	status = converter->generate (converter, &renderer.base);
    converter->destroy (converter);
    return status;
}