Updated: Sudoku no longer crashes
[moon.git] / cairo / src / cairo-region.c
blob23a042fd709edab3557d96c413b297ec1f434299
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 © 2005 Red Hat, Inc.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
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/
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.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Red Hat, Inc.
33 * Contributor(s):
34 * Owen Taylor <otaylor@redhat.com>
35 * Vladimir Vukicevic <vladimir@pobox.com>
38 #include "cairoint.h"
40 void
41 _cairo_region_init (cairo_region_t *region)
43 pixman_region32_init (&region->rgn);
46 void
47 _cairo_region_init_rect (cairo_region_t *region,
48 cairo_rectangle_int_t *rect)
50 pixman_region32_init_rect (&region->rgn,
51 rect->x, rect->y,
52 rect->width, rect->height);
55 cairo_int_status_t
56 _cairo_region_init_boxes (cairo_region_t *region,
57 cairo_box_int_t *boxes,
58 int count)
60 pixman_box32_t stack_pboxes[CAIRO_STACK_ARRAY_LENGTH (pixman_box32_t)];
61 pixman_box32_t *pboxes = stack_pboxes;
62 cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
63 int i;
65 if (count > ARRAY_LENGTH(stack_pboxes)) {
66 pboxes = _cairo_malloc_ab (count, sizeof(pixman_box32_t));
67 if (pboxes == NULL)
68 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
71 for (i = 0; i < count; i++) {
72 pboxes[i].x1 = boxes[i].p1.x;
73 pboxes[i].y1 = boxes[i].p1.y;
74 pboxes[i].x2 = boxes[i].p2.x;
75 pboxes[i].y2 = boxes[i].p2.y;
78 if (!pixman_region32_init_rects (&region->rgn, pboxes, count))
79 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
81 if (pboxes != stack_pboxes)
82 free (pboxes);
84 return status;
87 void
88 _cairo_region_fini (cairo_region_t *region)
90 pixman_region32_fini (&region->rgn);
93 cairo_int_status_t
94 _cairo_region_copy (cairo_region_t *dst, cairo_region_t *src)
96 if (!pixman_region32_copy (&dst->rgn, &src->rgn))
97 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
99 return CAIRO_STATUS_SUCCESS;
103 _cairo_region_num_boxes (cairo_region_t *region)
105 return pixman_region32_n_rects (&region->rgn);
108 cairo_int_status_t
109 _cairo_region_get_boxes (cairo_region_t *region, int *num_boxes, cairo_box_int_t **boxes)
111 int nboxes;
112 pixman_box32_t *pboxes;
113 cairo_box_int_t *cboxes;
114 int i;
116 pboxes = pixman_region32_rectangles (&region->rgn, &nboxes);
118 if (nboxes == 0) {
119 *num_boxes = 0;
120 *boxes = NULL;
121 return CAIRO_STATUS_SUCCESS;
124 cboxes = _cairo_malloc_ab (nboxes, sizeof(cairo_box_int_t));
125 if (cboxes == NULL)
126 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
128 for (i = 0; i < nboxes; i++) {
129 cboxes[i].p1.x = pboxes[i].x1;
130 cboxes[i].p1.y = pboxes[i].y1;
131 cboxes[i].p2.x = pboxes[i].x2;
132 cboxes[i].p2.y = pboxes[i].y2;
135 *num_boxes = nboxes;
136 *boxes = cboxes;
138 return CAIRO_STATUS_SUCCESS;
141 void
142 _cairo_region_boxes_fini (cairo_region_t *region, cairo_box_int_t *boxes)
144 free (boxes);
148 * _cairo_region_get_extents:
149 * @region: a #cairo_region_t
150 * @rect: rectangle into which to store the extents
152 * Gets the bounding box of a region as a #cairo_rectangle_int_t
154 void
155 _cairo_region_get_extents (cairo_region_t *region, cairo_rectangle_int_t *extents)
157 pixman_box32_t *pextents = pixman_region32_extents (&region->rgn);
159 extents->x = pextents->x1;
160 extents->y = pextents->y1;
161 extents->width = pextents->x2 - pextents->x1;
162 extents->height = pextents->y2 - pextents->y1;
165 cairo_int_status_t
166 _cairo_region_subtract (cairo_region_t *dst, cairo_region_t *a, cairo_region_t *b)
168 if (!pixman_region32_subtract (&dst->rgn, &a->rgn, &b->rgn))
169 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
171 return CAIRO_STATUS_SUCCESS;
174 cairo_int_status_t
175 _cairo_region_intersect (cairo_region_t *dst, cairo_region_t *a, cairo_region_t *b)
177 if (!pixman_region32_intersect (&dst->rgn, &a->rgn, &b->rgn))
178 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
180 return CAIRO_STATUS_SUCCESS;
183 cairo_int_status_t
184 _cairo_region_union_rect (cairo_region_t *dst,
185 cairo_region_t *src,
186 cairo_rectangle_int_t *rect)
188 if (!pixman_region32_union_rect (&dst->rgn, &src->rgn,
189 rect->x, rect->y,
190 rect->width, rect->height))
191 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
193 return CAIRO_STATUS_SUCCESS;
196 cairo_bool_t
197 _cairo_region_not_empty (cairo_region_t *region)
199 return (cairo_bool_t) pixman_region32_not_empty (&region->rgn);
202 void
203 _cairo_region_translate (cairo_region_t *region,
204 int x, int y)
206 pixman_region32_translate (&region->rgn, x, y);
209 pixman_region_overlap_t
210 _cairo_region_contains_rectangle (cairo_region_t *region,
211 const cairo_rectangle_int_t *rect)
213 pixman_box32_t pbox;
215 pbox.x1 = rect->x;
216 pbox.y1 = rect->y;
217 pbox.x2 = rect->x + rect->width;
218 pbox.y2 = rect->y + rect->height;
220 return pixman_region32_contains_rectangle (&region->rgn, &pbox);