Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-clip-region.c
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  * Copyright © 2009 Chris Wilson
7  *
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.
15  *
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
21  *
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/
26  *
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.
30  *
31  * The Original Code is the cairo graphics library.
32  *
33  * The Initial Developer of the Original Code is University of Southern
34  * California.
35  *
36  * Contributor(s):
37  *      Carl D. Worth <cworth@cworth.org>
38  *      Kristian Høgsberg <krh@redhat.com>
39  *      Chris Wilson <chris@chris-wilson.co.uk>
40  */
41
42 #include "cairoint.h"
43 #include "cairo-clip-private.h"
44 #include "cairo-error-private.h"
45 #include "cairo-freed-pool-private.h"
46 #include "cairo-gstate-private.h"
47 #include "cairo-path-fixed-private.h"
48 #include "cairo-pattern-private.h"
49 #include "cairo-composite-rectangles-private.h"
50 #include "cairo-region-private.h"
51
52 static void
53 _cairo_clip_extract_region (cairo_clip_t *clip)
54 {
55     cairo_rectangle_int_t stack_rects[CAIRO_STACK_ARRAY_LENGTH (cairo_rectangle_int_t)];
56     cairo_rectangle_int_t *r = stack_rects;
57     cairo_bool_t is_region;
58     int i;
59
60     if (clip->num_boxes == 0)
61         return;
62
63     if (clip->num_boxes > ARRAY_LENGTH (stack_rects)) {
64         r = _cairo_malloc_ab (clip->num_boxes, sizeof (cairo_rectangle_int_t));
65         if (r == NULL){
66             _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
67             return;
68         }
69     }
70
71     is_region = clip->path == NULL;
72     for (i = 0; i < clip->num_boxes; i++) {
73         cairo_box_t *b = &clip->boxes[i];
74         if (is_region)
75             is_region =
76                 _cairo_fixed_is_integer (b->p1.x | b->p1.y |  b->p2.x | b->p2.y);
77         r[i].x = _cairo_fixed_integer_floor (b->p1.x);
78         r[i].y = _cairo_fixed_integer_floor (b->p1.y);
79         r[i].width  = _cairo_fixed_integer_ceil (b->p2.x) - r[i].x;
80         r[i].height = _cairo_fixed_integer_ceil (b->p2.y) - r[i].y;
81     }
82     clip->is_region = is_region;
83
84     clip->region = cairo_region_create_rectangles (r, i);
85
86     if (r != stack_rects)
87         free (r);
88 }
89
90 cairo_region_t *
91 _cairo_clip_get_region (const cairo_clip_t *clip)
92 {
93     if (clip == NULL)
94         return NULL;
95
96     if (clip->region == NULL)
97         _cairo_clip_extract_region ((cairo_clip_t *) clip);
98
99     return clip->region;
100 }
101
102 cairo_bool_t
103 _cairo_clip_is_region (const cairo_clip_t *clip)
104 {
105     if (clip == NULL)
106         return TRUE;
107
108     if (clip->is_region)
109         return TRUE;
110
111     /* XXX Geometric reduction? */
112
113     if (clip->path)
114         return FALSE;
115
116     if (clip->num_boxes == 0)
117         return TRUE;
118
119     if (clip->region == NULL)
120         _cairo_clip_extract_region ((cairo_clip_t *) clip);
121
122     return clip->is_region;
123 }