summaryrefslogtreecommitdiff
path: root/src/cairo-region.c
blob: 8224582d2c2f7b175d5ceebe489a8b12c3593163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2005 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Red Hat, Inc.
 *
 * Contributor(s):
 *	Owen Taylor <otaylor@redhat.com>
 *      Vladimir Vukicevic <vladimir@pobox.com>
 *      Søren Sandmann <sandmann@daimi.au.dk>
 */

#include "cairoint.h"

const cairo_region_t _cairo_region_nil = {
    CAIRO_STATUS_NO_MEMORY,		/* status */
};

cairo_region_t *
_cairo_region_create (void)
{
    cairo_region_t *region = _cairo_malloc (sizeof (cairo_region_t));

    if (!region)
	return (cairo_region_t *)&_cairo_region_nil;

    region->status = CAIRO_STATUS_SUCCESS;

    pixman_region32_init (&region->rgn);

    return region;
}

cairo_region_t *
_cairo_region_create_rect (cairo_rectangle_int_t *rect)
{
    cairo_region_t *region = _cairo_malloc (sizeof (cairo_region_t));

    if (!region)
	return (cairo_region_t *)&_cairo_region_nil;
    
    region->status = CAIRO_STATUS_SUCCESS;
    
    pixman_region32_init_rect (&region->rgn,
			       rect->x, rect->y,
			       rect->width, rect->height);

    return region;
}

cairo_region_t *
_cairo_region_create_boxes (cairo_box_int_t *boxes,
			    int count)
{
    pixman_box32_t stack_pboxes[CAIRO_STACK_ARRAY_LENGTH (pixman_box32_t)];
    pixman_box32_t *pboxes = stack_pboxes;
    cairo_region_t *region;
    int i;

    region = _cairo_malloc (sizeof (cairo_region_t));

    if (!region)
	return (cairo_region_t *)&_cairo_region_nil;

    region->status = CAIRO_STATUS_SUCCESS;
    
    if (count > ARRAY_LENGTH (stack_pboxes)) {
	pboxes = _cairo_malloc_ab (count, sizeof (pixman_box32_t));

	if (unlikely (pboxes == NULL)) {
	    free (region);
	    return (cairo_region_t *)&_cairo_region_nil;
	}
    }

    for (i = 0; i < count; i++) {
	pboxes[i].x1 = boxes[i].p1.x;
	pboxes[i].y1 = boxes[i].p1.y;
	pboxes[i].x2 = boxes[i].p2.x;
	pboxes[i].y2 = boxes[i].p2.y;
    }

    if (! pixman_region32_init_rects (&region->rgn, pboxes, count)) {
	free (region);

	region = (cairo_region_t *)&_cairo_region_nil;
    }
    
    if (pboxes != stack_pboxes)
	free (pboxes);

    return region;
}

void
_cairo_region_destroy (cairo_region_t *region)
{
    if (region->status)
	return;
    
    pixman_region32_fini (&region->rgn);
    free (region);
}

cairo_region_t *
_cairo_region_copy (cairo_region_t *original)
{
    cairo_region_t *copy;
    
    if (original->status)
	return (cairo_region_t *)&_cairo_region_nil;

    copy = _cairo_region_create ();
    if (!copy)
	return (cairo_region_t *)&_cairo_region_nil;

    if (!pixman_region32_copy (&copy->rgn, &original->rgn)) {
	_cairo_region_destroy (copy);

	return (cairo_region_t *)&_cairo_region_nil;
    }

    return copy;
}

int
_cairo_region_num_boxes (cairo_region_t *region)
{
    if (region->status)
	return 0;
    
    return pixman_region32_n_rects (&region->rgn);
}

cairo_private void
_cairo_region_get_box (cairo_region_t *region,
		       int nth_box,
		       cairo_box_int_t *box)
{
    pixman_box32_t *pbox;

    if (region->status)
	return;
    
    pbox = pixman_region32_rectangles (&region->rgn, NULL) + nth_box;

    box->p1.x = pbox->x1;
    box->p1.y = pbox->y1;
    box->p2.x = pbox->x2;
    box->p2.y = pbox->y2;
}

/**
 * _cairo_region_get_extents:
 * @region: a #cairo_region_t
 * @rect: rectangle into which to store the extents
 *
 * Gets the bounding box of a region as a #cairo_rectangle_int_t
 **/
void
_cairo_region_get_extents (cairo_region_t *region,
			   cairo_rectangle_int_t *extents)
{
    pixman_box32_t *pextents;

    if (region->status || !extents)
	return;

    pextents = pixman_region32_extents (&region->rgn);

    extents->x = pextents->x1;
    extents->y = pextents->y1;
    extents->width = pextents->x2 - pextents->x1;
    extents->height = pextents->y2 - pextents->y1;
}

cairo_status_t
_cairo_region_status (cairo_region_t *region)
{
    return region->status;
}

void
_cairo_region_clear (cairo_region_t *region)
{
    if (region->status)
	return;

    pixman_region32_fini (&region->rgn);
    pixman_region32_init (&region->rgn);
}

cairo_status_t
_cairo_region_subtract (cairo_region_t *dst, cairo_region_t *other)
{
    if (dst->status)
	return dst->status;

    if (other->status)
	return other->status;
    
    if (!pixman_region32_subtract (&dst->rgn, &dst->rgn, &other->rgn))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    return CAIRO_STATUS_SUCCESS;
}

cairo_status_t
_cairo_region_intersect (cairo_region_t *dst, cairo_region_t *other)
{
    if (dst->status)
	return dst->status;

    if (other->status)
	return other->status;
    
    if (!pixman_region32_intersect (&dst->rgn, &dst->rgn, &other->rgn))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    return CAIRO_STATUS_SUCCESS;
}

cairo_status_t
_cairo_region_union_rect (cairo_region_t *dst,
			  cairo_rectangle_int_t *rect)
{
    if (!pixman_region32_union_rect (&dst->rgn, &dst->rgn,
				     rect->x, rect->y,
				     rect->width, rect->height))
    {
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }

    return CAIRO_STATUS_SUCCESS;
}

cairo_bool_t
_cairo_region_not_empty (cairo_region_t *region)
{
    if (region->status)
	return FALSE;
    
    return (cairo_bool_t) pixman_region32_not_empty (&region->rgn);
}

void
_cairo_region_translate (cairo_region_t *region,
			 int x, int y)
{
    if (region->status)
	return;
    
    pixman_region32_translate (&region->rgn, x, y);
}

pixman_region_overlap_t
_cairo_region_contains_rectangle (cairo_region_t *region,
				  const cairo_rectangle_int_t *rect)
{
    pixman_box32_t pbox;

    if (region->status)
	return PIXMAN_REGION_OUT;
    
    pbox.x1 = rect->x;
    pbox.y1 = rect->y;
    pbox.x2 = rect->x + rect->width;
    pbox.y2 = rect->y + rect->height;

    return pixman_region32_contains_rectangle (&region->rgn, &pbox);
}