summaryrefslogtreecommitdiff
path: root/src/cairo-region.c
blob: 23a042fd709edab3557d96c413b297ec1f434299 (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
/* -*- 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>
 */

#include "cairoint.h"

void
_cairo_region_init (cairo_region_t *region)
{
    pixman_region32_init (&region->rgn);
}

void
_cairo_region_init_rect (cairo_region_t *region,
			 cairo_rectangle_int_t *rect)
{
    pixman_region32_init_rect (&region->rgn,
			     rect->x, rect->y,
			     rect->width, rect->height);
}

cairo_int_status_t
_cairo_region_init_boxes (cairo_region_t *region,
			  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_int_status_t status = CAIRO_STATUS_SUCCESS;
    int i;

    if (count > ARRAY_LENGTH(stack_pboxes)) {
	pboxes = _cairo_malloc_ab (count, sizeof(pixman_box32_t));
	if (pboxes == NULL)
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }

    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))
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);

    if (pboxes != stack_pboxes)
	free (pboxes);

    return status;
}

void
_cairo_region_fini (cairo_region_t *region)
{
    pixman_region32_fini (&region->rgn);
}

cairo_int_status_t
_cairo_region_copy (cairo_region_t *dst, cairo_region_t *src)
{
    if (!pixman_region32_copy (&dst->rgn, &src->rgn))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    return CAIRO_STATUS_SUCCESS;
}

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

cairo_int_status_t
_cairo_region_get_boxes (cairo_region_t *region, int *num_boxes, cairo_box_int_t **boxes)
{
    int nboxes;
    pixman_box32_t *pboxes;
    cairo_box_int_t *cboxes;
    int i;

    pboxes = pixman_region32_rectangles (&region->rgn, &nboxes);

    if (nboxes == 0) {
	*num_boxes = 0;
	*boxes = NULL;
	return CAIRO_STATUS_SUCCESS;
    }

    cboxes = _cairo_malloc_ab (nboxes, sizeof(cairo_box_int_t));
    if (cboxes == NULL)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

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

    *num_boxes = nboxes;
    *boxes = cboxes;

    return CAIRO_STATUS_SUCCESS;
}

void
_cairo_region_boxes_fini (cairo_region_t *region, cairo_box_int_t *boxes)
{
    free (boxes);
}

/**
 * _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 = 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_int_status_t
_cairo_region_subtract (cairo_region_t *dst, cairo_region_t *a, cairo_region_t *b)
{
    if (!pixman_region32_subtract (&dst->rgn, &a->rgn, &b->rgn))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    return CAIRO_STATUS_SUCCESS;
}

cairo_int_status_t
_cairo_region_intersect (cairo_region_t *dst, cairo_region_t *a, cairo_region_t *b)
{
    if (!pixman_region32_intersect (&dst->rgn, &a->rgn, &b->rgn))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    return CAIRO_STATUS_SUCCESS;
}

cairo_int_status_t
_cairo_region_union_rect (cairo_region_t *dst,
			  cairo_region_t *src,
			  cairo_rectangle_int_t *rect)
{
    if (!pixman_region32_union_rect (&dst->rgn, &src->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)
{
    return (cairo_bool_t) pixman_region32_not_empty (&region->rgn);
}

void
_cairo_region_translate (cairo_region_t *region,
			 int x, int y)
{
    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;

    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);
}