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
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
6 * Copyright © 2009 Chris Wilson
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
31 * The Original Code is the cairo graphics library.
33 * The Initial Developer of the Original Code is University of Southern
37 * Carl D. Worth <cworth@cworth.org>
38 * Kristian Høgsberg <krh@redhat.com>
39 * Chris Wilson <chris@chris-wilson.co.uk>
44 #include "cairo-box-inline.h"
45 #include "cairo-clip-inline.h"
46 #include "cairo-clip-private.h"
47 #include "cairo-error-private.h"
48 #include "cairo-freed-pool-private.h"
49 #include "cairo-gstate-private.h"
50 #include "cairo-path-fixed-private.h"
51 #include "cairo-pattern-private.h"
52 #include "cairo-composite-rectangles-private.h"
53 #include "cairo-region-private.h"
69 _cairo_clip_contains_rectangle_box (const cairo_clip_t *clip,
70 const cairo_rectangle_int_t *rect,
71 const cairo_box_t *box)
75 /* clip == NULL means no clip, so the clip contains everything */
79 if (_cairo_clip_is_all_clipped (clip))
82 /* If we have a non-trivial path, just say no */
86 if (! _cairo_rectangle_contains_rectangle (&clip->extents, rect))
89 if (clip->num_boxes == 0)
92 /* Check for a clip-box that wholly contains the rectangle */
93 for (i = 0; i < clip->num_boxes; i++) {
94 if (box->p1.x >= clip->boxes[i].p1.x &&
95 box->p1.y >= clip->boxes[i].p1.y &&
96 box->p2.x <= clip->boxes[i].p2.x &&
97 box->p2.y <= clip->boxes[i].p2.y)
107 _cairo_clip_contains_box (const cairo_clip_t *clip,
108 const cairo_box_t *box)
110 cairo_rectangle_int_t rect;
112 _cairo_box_round_to_rectangle (box, &rect);
113 return _cairo_clip_contains_rectangle_box(clip, &rect, box);
117 _cairo_clip_contains_rectangle (const cairo_clip_t *clip,
118 const cairo_rectangle_int_t *rect)
122 box.p1.x = _cairo_fixed_from_int (rect->x);
123 box.p1.y = _cairo_fixed_from_int (rect->y);
124 box.p2.x = _cairo_fixed_from_int (rect->x + rect->width);
125 box.p2.y = _cairo_fixed_from_int (rect->y + rect->height);
127 return _cairo_clip_contains_rectangle_box (clip, rect, &box);
131 _cairo_clip_intersect_rectilinear_path (cairo_clip_t *clip,
132 const cairo_path_fixed_t *path,
133 cairo_fill_rule_t fill_rule,
134 cairo_antialias_t antialias)
136 cairo_status_t status;
139 _cairo_boxes_init (&boxes);
140 status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
144 if (likely (status == CAIRO_STATUS_SUCCESS && boxes.num_boxes))
145 clip = _cairo_clip_intersect_boxes (clip, &boxes);
147 clip = _cairo_clip_set_all_clipped (clip);
148 _cairo_boxes_fini (&boxes);
153 static cairo_clip_t *
154 _cairo_clip_intersect_rectangle_box (cairo_clip_t *clip,
155 const cairo_rectangle_int_t *r,
156 const cairo_box_t *box)
158 cairo_box_t extents_box;
159 cairo_bool_t changed = FALSE;
163 clip = _cairo_clip_create ();
165 return _cairo_clip_set_all_clipped (clip);
168 if (clip->num_boxes == 0) {
169 clip->boxes = &clip->embedded_box;
170 clip->boxes[0] = *box;
172 if (clip->path == NULL) {
175 if (! _cairo_rectangle_intersect (&clip->extents, r))
176 clip = _cairo_clip_set_all_clipped (clip);
178 if (clip->path == NULL)
179 clip->is_region = _cairo_box_is_pixel_aligned (box);
183 /* Does the new box wholly subsume the clip? Perform a cheap check
184 * for the common condition of a single clip rectangle.
186 if (clip->num_boxes == 1 &&
187 clip->boxes[0].p1.x >= box->p1.x &&
188 clip->boxes[0].p1.y >= box->p1.y &&
189 clip->boxes[0].p2.x <= box->p2.x &&
190 clip->boxes[0].p2.y <= box->p2.y)
195 for (i = j = 0; i < clip->num_boxes; i++) {
196 cairo_box_t *b = &clip->boxes[j];
201 if (box->p1.x > b->p1.x)
202 b->p1.x = box->p1.x, changed = TRUE;
203 if (box->p2.x < b->p2.x)
204 b->p2.x = box->p2.x, changed = TRUE;
206 if (box->p1.y > b->p1.y)
207 b->p1.y = box->p1.y, changed = TRUE;
208 if (box->p2.y < b->p2.y)
209 b->p2.y = box->p2.y, changed = TRUE;
211 j += b->p2.x > b->p1.x && b->p2.y > b->p1.y;
215 if (clip->num_boxes == 0)
216 return _cairo_clip_set_all_clipped (clip);
221 extents_box = clip->boxes[0];
222 for (i = 1; i < clip->num_boxes; i++) {
223 if (clip->boxes[i].p1.x < extents_box.p1.x)
224 extents_box.p1.x = clip->boxes[i].p1.x;
226 if (clip->boxes[i].p1.y < extents_box.p1.y)
227 extents_box.p1.y = clip->boxes[i].p1.y;
229 if (clip->boxes[i].p2.x > extents_box.p2.x)
230 extents_box.p2.x = clip->boxes[i].p2.x;
232 if (clip->boxes[i].p2.y > extents_box.p2.y)
233 extents_box.p2.y = clip->boxes[i].p2.y;
236 if (clip->path == NULL) {
237 _cairo_box_round_to_rectangle (&extents_box, &clip->extents);
239 cairo_rectangle_int_t extents_rect;
241 _cairo_box_round_to_rectangle (&extents_box, &extents_rect);
242 if (! _cairo_rectangle_intersect (&clip->extents, &extents_rect))
243 return _cairo_clip_set_all_clipped (clip);
247 cairo_region_destroy (clip->region);
251 clip->is_region = FALSE;
256 _cairo_clip_intersect_box (cairo_clip_t *clip,
257 const cairo_box_t *box)
259 cairo_rectangle_int_t r;
261 _cairo_box_round_to_rectangle (box, &r);
262 if (r.width == 0 || r.height == 0)
263 return _cairo_clip_set_all_clipped (clip);
265 return _cairo_clip_intersect_rectangle_box (clip, &r, box);
269 _cairo_clip_intersect_boxes (cairo_clip_t *clip,
270 const cairo_boxes_t *boxes)
272 cairo_boxes_t clip_boxes;
274 cairo_rectangle_int_t extents;
276 if (_cairo_clip_is_all_clipped (clip))
279 if (boxes->num_boxes == 0)
280 return _cairo_clip_set_all_clipped (clip);
282 if (boxes->num_boxes == 1)
283 return _cairo_clip_intersect_box (clip, boxes->chunks.base);
286 clip = _cairo_clip_create ();
288 if (clip->num_boxes) {
289 _cairo_boxes_init_for_array (&clip_boxes, clip->boxes, clip->num_boxes);
290 if (unlikely (_cairo_boxes_intersect (&clip_boxes, boxes, &clip_boxes))) {
291 clip = _cairo_clip_set_all_clipped (clip);
295 if (clip->boxes != &clip->embedded_box)
302 if (boxes->num_boxes == 0) {
303 clip = _cairo_clip_set_all_clipped (clip);
305 } else if (boxes->num_boxes == 1) {
306 clip->boxes = &clip->embedded_box;
307 clip->boxes[0] = boxes->chunks.base[0];
310 clip->boxes = _cairo_boxes_to_array (boxes, &clip->num_boxes, TRUE);
312 _cairo_boxes_extents (boxes, &limits);
314 _cairo_box_round_to_rectangle (&limits, &extents);
315 if (clip->path == NULL)
316 clip->extents = extents;
317 else if (! _cairo_rectangle_intersect (&clip->extents, &extents))
318 clip = _cairo_clip_set_all_clipped (clip);
321 cairo_region_destroy (clip->region);
324 clip->is_region = FALSE;
327 if (boxes == &clip_boxes)
328 _cairo_boxes_fini (&clip_boxes);
334 _cairo_clip_intersect_rectangle (cairo_clip_t *clip,
335 const cairo_rectangle_int_t *r)
339 if (_cairo_clip_is_all_clipped (clip))
342 if (r->width == 0 || r->height == 0)
343 return _cairo_clip_set_all_clipped (clip);
345 box.p1.x = _cairo_fixed_from_int (r->x);
346 box.p1.y = _cairo_fixed_from_int (r->y);
347 box.p2.x = _cairo_fixed_from_int (r->x + r->width);
348 box.p2.y = _cairo_fixed_from_int (r->y + r->height);
350 return _cairo_clip_intersect_rectangle_box (clip, r, &box);
359 cairo_point_t current_point;
360 cairo_point_t last_move_to;
364 _add_clipped_edge (struct reduce *r,
365 const cairo_point_t *p1,
366 const cairo_point_t *p2,
371 x = _cairo_edge_compute_intersection_x_for_y (p1, p2, y1);
372 if (x < r->extents.p1.x)
375 x = _cairo_edge_compute_intersection_x_for_y (p1, p2, y2);
376 if (x > r->extents.p2.x)
379 if (y1 < r->extents.p1.y)
380 r->extents.p1.y = y1;
382 if (y2 > r->extents.p2.y)
383 r->extents.p2.y = y2;
389 _add_edge (struct reduce *r,
390 const cairo_point_t *p1,
391 const cairo_point_t *p2)
405 if (bottom < r->limit.p1.y || top > r->limit.p2.y)
409 const cairo_point_t *t = p1;
414 if (p2->x <= r->limit.p1.x || p1->x >= r->limit.p2.x)
417 for (n = 0; n < r->clip->num_boxes; n++) {
418 const cairo_box_t *limits = &r->clip->boxes[n];
420 if (bottom < limits->p1.y || top > limits->p2.y)
423 if (p2->x <= limits->p1.x || p1->x >= limits->p2.x)
426 if (p1->x >= limits->p1.x && p2->x <= limits->p1.x) {
432 p1_y = _cairo_edge_compute_intersection_y_for_x (p1, p2,
434 p2_y = _cairo_edge_compute_intersection_y_for_x (p1, p2,
450 if (top_y < limits->p1.y)
451 top_y = limits->p1.y;
453 if (bot_y > limits->p2.y)
454 bot_y = limits->p2.y;
456 _add_clipped_edge (r, p1, p2, top_y, bot_y);
460 static cairo_status_t
461 _reduce_line_to (void *closure,
462 const cairo_point_t *point)
464 struct reduce *r = closure;
466 _add_edge (r, &r->current_point, point);
467 r->current_point = *point;
469 return CAIRO_STATUS_SUCCESS;
472 static cairo_status_t
473 _reduce_close (void *closure)
475 struct reduce *r = closure;
477 return _reduce_line_to (r, &r->last_move_to);
480 static cairo_status_t
481 _reduce_move_to (void *closure,
482 const cairo_point_t *point)
484 struct reduce *r = closure;
485 cairo_status_t status;
487 /* close current subpath */
488 status = _reduce_close (closure);
490 /* make sure that the closure represents a degenerate path */
491 r->current_point = *point;
492 r->last_move_to = *point;
497 static cairo_clip_t *
498 _cairo_clip_reduce_to_boxes (cairo_clip_t *clip)
501 cairo_clip_path_t *clip_path;
502 cairo_status_t status;
505 if (clip->path == NULL)
509 r.extents.p1.x = r.extents.p1.y = INT_MAX;
510 r.extents.p2.x = r.extents.p2.y = INT_MIN;
513 r.limit.p1.x = _cairo_fixed_from_int (clip->extents.x);
514 r.limit.p1.y = _cairo_fixed_from_int (clip->extents.y);
515 r.limit.p2.x = _cairo_fixed_from_int (clip->extents.x + clip->extents.width);
516 r.limit.p2.y = _cairo_fixed_from_int (clip->extents.y + clip->extents.height);
518 clip_path = clip->path;
520 r.current_point.x = 0;
521 r.current_point.y = 0;
522 r.last_move_to = r.current_point;
524 status = _cairo_path_fixed_interpret_flat (&clip_path->path,
529 clip_path->tolerance);
530 assert (status == CAIRO_STATUS_SUCCESS);
532 } while ((clip_path = clip_path->prev));
535 _cairo_clip_path_destroy (clip->path);
539 return _cairo_clip_intersect_box (clip, &r.extents);
543 _cairo_clip_reduce_to_rectangle (const cairo_clip_t *clip,
544 const cairo_rectangle_int_t *r)
548 if (_cairo_clip_is_all_clipped (clip))
549 return (cairo_clip_t *) clip;
551 if (_cairo_clip_contains_rectangle (clip, r))
552 return _cairo_clip_intersect_rectangle (NULL, r);
554 copy = _cairo_clip_copy_intersect_rectangle (clip, r);
555 if (_cairo_clip_is_all_clipped (copy))
558 return _cairo_clip_reduce_to_boxes (copy);
562 _cairo_clip_reduce_for_composite (const cairo_clip_t *clip,
563 cairo_composite_rectangles_t *extents)
565 const cairo_rectangle_int_t *r;
567 r = extents->is_bounded ? &extents->bounded : &extents->unbounded;
568 return _cairo_clip_reduce_to_rectangle (clip, r);
572 _cairo_clip_from_boxes (const cairo_boxes_t *boxes)
575 cairo_clip_t *clip = _cairo_clip_create ();
577 return _cairo_clip_set_all_clipped (clip);
580 if(boxes->num_boxes == 1) {
581 clip->boxes = &clip->embedded_box;
582 clip->boxes[0] = boxes->chunks.base[0];
585 clip->boxes = _cairo_boxes_to_array (boxes, &clip->num_boxes, TRUE);
586 if (clip->boxes == NULL)
587 return _cairo_clip_set_all_clipped (clip);
590 _cairo_boxes_extents (boxes, &extents);
591 _cairo_box_round_to_rectangle (&extents, &clip->extents);