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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
/* -*- 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"
static const cairo_region_t _cairo_region_nil = {
CAIRO_STATUS_NO_MEMORY, /* status */
};
/**
* _cairo_region_set_error:
* @region: a region
* @status: a status value indicating an error
*
* Atomically sets region->status to @status and calls _cairo_error;
* Does nothing if status is %CAIRO_STATUS_SUCCESS or any of the internal
* status values.
*
* All assignments of an error status to region->status should happen
* through _cairo_region_set_error(). Note that due to the nature of
* the atomic operation, it is not safe to call this function on the
* nil objects.
*
* The purpose of this function is to allow the user to set a
* breakpoint in _cairo_error() to generate a stack trace for when the
* user causes cairo to detect an error.
*
* Return value: the error status.
**/
static cairo_status_t
_cairo_region_set_error (cairo_region_t *region,
cairo_status_t status)
{
if (! _cairo_status_is_error (status))
return status;
/* Don't overwrite an existing error. This preserves the first
* error, which is the most significant. */
_cairo_status_set_error (®ion->status, status);
return _cairo_error (status);
}
void
_cairo_region_init (cairo_region_t *region)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (region, sizeof (cairo_region_t)));
region->status = CAIRO_STATUS_SUCCESS;
pixman_region32_init (®ion->rgn);
}
void
_cairo_region_init_rectangle (cairo_region_t *region,
const cairo_rectangle_int_t *rectangle)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (region, sizeof (cairo_region_t)));
region->status = CAIRO_STATUS_SUCCESS;
pixman_region32_init_rect (®ion->rgn,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
}
void
_cairo_region_fini (cairo_region_t *region)
{
pixman_region32_fini (®ion->rgn);
VG (VALGRIND_MAKE_MEM_NOACCESS (region, sizeof (cairo_region_t)));
}
/**
* cairo_region_create:
*
* Allocates a new empty region object.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_create (void)
{
cairo_region_t *region;
region = _cairo_malloc (sizeof (cairo_region_t));
if (region == NULL)
return (cairo_region_t *) &_cairo_region_nil;
region->status = CAIRO_STATUS_SUCCESS;
pixman_region32_init (®ion->rgn);
return region;
}
slim_hidden_def (cairo_region_create);
cairo_region_t *
cairo_region_create_rectangles (cairo_rectangle_int_t *rects,
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 = rects[i].x;
pboxes[i].y1 = rects[i].y;
pboxes[i].x2 = rects[i].x + rects[i].width;
pboxes[i].y2 = rects[i].y + rects[i].height;
}
if (! pixman_region32_init_rects (®ion->rgn, pboxes, count)) {
free (region);
region = (cairo_region_t *)&_cairo_region_nil;
}
if (pboxes != stack_pboxes)
free (pboxes);
return region;
}
slim_hidden_def (cairo_region_create_rectangles);
/**
* cairo_region_create_rectangle:
* @rectangle: a #cairo_rectangle_int_t
*
* Allocates a new region object containing @rectangle.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_create_rectangle (const cairo_rectangle_int_t *rectangle)
{
cairo_region_t *region;
region = _cairo_malloc (sizeof (cairo_region_t));
if (region == NULL)
return (cairo_region_t *) &_cairo_region_nil;
region->status = CAIRO_STATUS_SUCCESS;
pixman_region32_init_rect (®ion->rgn,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
return region;
}
slim_hidden_def (cairo_region_create_rectangle);
/**
* cairo_region_copy:
* @original: a #cairo_region_t
*
* Allocates a new region object copying the area from @original.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
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->status)
return copy;
if (! pixman_region32_copy (©->rgn, &original->rgn)) {
cairo_region_destroy (copy);
return (cairo_region_t *) &_cairo_region_nil;
}
return copy;
}
slim_hidden_def (cairo_region_copy);
/**
* cairo_region_destroy:
* @region: a #cairo_region_t
*
* Destroys a #cairo_region_t object created with
* cairo_region_create(), cairo_region_copy(), or
* or cairo_region_create_rectangle().
*
* Since: 1.10
**/
void
cairo_region_destroy (cairo_region_t *region)
{
if (region == (cairo_region_t *) &_cairo_region_nil)
return;
pixman_region32_fini (®ion->rgn);
free (region);
}
slim_hidden_def (cairo_region_destroy);
/**
* cairo_region_num_rectangles:
* @region: a #cairo_region_t
*
* Returns the number of rectangles contained in @region.
*
* Return value: The number of rectangles contained in @region.
*
* Since: 1.10
**/
int
cairo_region_num_rectangles (cairo_region_t *region)
{
if (region->status)
return 0;
return pixman_region32_n_rects (®ion->rgn);
}
slim_hidden_def (cairo_region_num_rectangles);
/**
* cairo_region_get_rectangle:
* @region: a #cairo_region_t
* @nth: a number indicating which rectangle should be returned
* @rectangle: return location for a #cairo_rectangle_int_t
*
* Stores the @nth rectangle from the region in @rectangle.
*
* Since: 1.10
**/
void
cairo_region_get_rectangle (cairo_region_t *region,
int nth,
cairo_rectangle_int_t *rectangle)
{
pixman_box32_t *pbox;
if (region->status) {
rectangle->x = rectangle->y = 0;
rectangle->width = rectangle->height = 0;
return;
}
pbox = pixman_region32_rectangles (®ion->rgn, NULL) + nth;
rectangle->x = pbox->x1;
rectangle->y = pbox->y1;
rectangle->width = pbox->x2 - pbox->x1;
rectangle->height = pbox->y2 - pbox->y1;
}
slim_hidden_def (cairo_region_get_rectangle);
/**
* cairo_region_get_extents:
* @region: a #cairo_region_t
* @rectangle: rectangle into which to store the extents
*
* Gets the bounding rectangle of @region as a #cairo_rectangle_int_t
*
* Since: 1.10
**/
void
cairo_region_get_extents (cairo_region_t *region,
cairo_rectangle_int_t *extents)
{
pixman_box32_t *pextents;
if (region->status) {
extents->x = extents->y = 0;
extents->width = extents->height = 0;
return;
}
pextents = pixman_region32_extents (®ion->rgn);
extents->x = pextents->x1;
extents->y = pextents->y1;
extents->width = pextents->x2 - pextents->x1;
extents->height = pextents->y2 - pextents->y1;
}
slim_hidden_def (cairo_region_get_extents);
/**
* cairo_region_status:
* @region: a #cairo_region_t
*
* Checks whether an error has previous occured for this
* region object.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_status (cairo_region_t *region)
{
return region->status;
}
slim_hidden_def (cairo_region_status);
/**
* cairo_region_subtract:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Subtracts @other from @dst and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_subtract (cairo_region_t *dst, cairo_region_t *other)
{
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
if (! pixman_region32_subtract (&dst->rgn, &dst->rgn, &other->rgn))
return _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_region_subtract);
/**
* cairo_region_subtract_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Subtracts @rectangle from @dst and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_subtract_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
if (! pixman_region32_subtract (&dst->rgn, &dst->rgn, ®ion))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_subtract_rectangle);
/**
* cairo_region_intersect:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Computes the intersection of @dst with @other and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_intersect (cairo_region_t *dst, cairo_region_t *other)
{
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
if (! pixman_region32_intersect (&dst->rgn, &dst->rgn, &other->rgn))
return _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_region_intersect);
/**
* cairo_region_intersect_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Computes the intersection of @dst with @rectangle and places the
* result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_intersect_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
if (! pixman_region32_intersect (&dst->rgn, &dst->rgn, ®ion))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_intersect_rectangle);
/**
* cairo_region_union:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Computes the union of @dst with @other and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_union (cairo_region_t *dst,
cairo_region_t *other)
{
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
if (! pixman_region32_union (&dst->rgn, &dst->rgn, &other->rgn))
return _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_region_union);
/**
* cairo_region_union_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Computes the union of @dst with @rectangle and places the result in @dst.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_union_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
if (! pixman_region32_union (&dst->rgn, &dst->rgn, ®ion))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_union_rectangle);
/**
* cairo_region_is_empty:
* @region: a #cairo_region_t
*
* Checks whether @region is empty.
*
* Return value: %TRUE if @region is empty, %FALSE if it isn't.
*
* Since: 1.10
**/
cairo_bool_t
cairo_region_is_empty (cairo_region_t *region)
{
if (region->status)
return TRUE;
return ! pixman_region32_not_empty (®ion->rgn);
}
slim_hidden_def (cairo_region_is_empty);
/**
* cairo_region_translate:
* @region: a #cairo_region_t
* @dx: Amount to translate in the x direction
* @dy: Amount to translate in the y direction
*
* Translates @region by (@dx, @dy).
*
* Since: 1.10
**/
void
cairo_region_translate (cairo_region_t *region,
int dx, int dy)
{
if (region->status)
return;
pixman_region32_translate (®ion->rgn, dx, dy);
}
slim_hidden_def (cairo_region_translate);
/**
* cairo_region_contains_rectangle:
* @region: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Checks whether @rectangle is inside, outside or partially contained
* in @region
*
* Return value:
* %CAIRO_REGION_OVERLAP_IN if @rectangle is entirely inside @region,
* %CAIRO_REGION_OVERLAP_OUT if @rectangle is entirely outside @region, or
* %CAIRO_REGION_OVERLAP_PART if @rectangle is partially inside and partially outside @region.
*
* Since: 1.10
**/
cairo_region_overlap_t
cairo_region_contains_rectangle (cairo_region_t *region,
const cairo_rectangle_int_t *rectangle)
{
pixman_box32_t pbox;
pixman_region_overlap_t poverlap;
if (region->status)
return CAIRO_REGION_OVERLAP_OUT;
pbox.x1 = rectangle->x;
pbox.y1 = rectangle->y;
pbox.x2 = rectangle->x + rectangle->width;
pbox.y2 = rectangle->y + rectangle->height;
poverlap = pixman_region32_contains_rectangle (®ion->rgn, &pbox);
switch (poverlap) {
default:
case PIXMAN_REGION_OUT: return CAIRO_REGION_OVERLAP_OUT;
case PIXMAN_REGION_IN: return CAIRO_REGION_OVERLAP_IN;
case PIXMAN_REGION_PART: return CAIRO_REGION_OVERLAP_PART;
}
}
slim_hidden_def (cairo_region_contains_rectangle);
/**
* cairo_region_contains_point:
* @region: a #cairo_region_t
* @x: the x coordinate of a point
* @y: the y coordinate of a point
*
* Checks whether (@x, @y) is contained in @region.
*
* Return value: %TRUE if (@x, @y) is contained in @region, %FALSE if it is not.
*
* Since: 1.10
**/
cairo_bool_t
cairo_region_contains_point (cairo_region_t *region,
int x, int y)
{
pixman_box32_t box;
if (region->status)
return FALSE;
return pixman_region32_contains_point (®ion->rgn, x, y, &box);
}
slim_hidden_def (cairo_region_contains_point);
|