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 © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
6 * Copyright © 2009 Chris Wilson
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.
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
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/
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.
31 * The Original Code is the cairo graphics library.
33 * The Initial Developer of the Original Code is University of Southern
37 * Carl D. Worth <cworth@cworth.org>
38 * Kristian Høgsberg <krh@redhat.com>
39 * Chris Wilson <chris@chris-wilson.co.uk>
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"
53 _cairo_clip_combine_with_surface (const cairo_clip_t
*clip
,
57 cairo_clip_path_t
*copy_path
;
58 cairo_clip_path_t
*clip_path
;
60 cairo_status_t status
= CAIRO_STATUS_SUCCESS
;
62 copy
= _cairo_clip_copy_with_translation (clip
, -dst_x
, -dst_y
);
63 copy_path
= copy
->path
;
67 status
= _cairo_surface_paint (dst
,
69 &_cairo_pattern_white
.base
,
74 if (_cairo_clip_is_region (copy
))
76 clip_path
= copy_path
;
77 while (status
== CAIRO_STATUS_SUCCESS
&& clip_path
) {
78 status
= _cairo_surface_fill (dst
,
80 &_cairo_pattern_white
.base
,
86 clip_path
= clip_path
->prev
;
89 copy
->path
= copy_path
;
90 _cairo_clip_destroy (copy
);
95 _cairo_path_fixed_add_box (cairo_path_fixed_t
*path
,
96 const cairo_box_t
*box
,
100 cairo_status_t status
;
102 status
= _cairo_path_fixed_move_to (path
, box
->p1
.x
+ fx
, box
->p1
.y
+ fy
);
103 if (unlikely (status
))
106 status
= _cairo_path_fixed_line_to (path
, box
->p2
.x
+ fx
, box
->p1
.y
+ fy
);
107 if (unlikely (status
))
110 status
= _cairo_path_fixed_line_to (path
, box
->p2
.x
+ fx
, box
->p2
.y
+ fy
);
111 if (unlikely (status
))
114 status
= _cairo_path_fixed_line_to (path
, box
->p1
.x
+ fx
, box
->p2
.y
+ fy
);
115 if (unlikely (status
))
118 return _cairo_path_fixed_close_path (path
);
122 _cairo_clip_get_surface (const cairo_clip_t
*clip
,
123 cairo_surface_t
*target
,
126 cairo_surface_t
*surface
;
127 cairo_status_t status
;
128 cairo_clip_t
*copy
, *region
;
129 cairo_clip_path_t
*copy_path
, *clip_path
;
131 if (clip
->num_boxes
) {
132 cairo_path_fixed_t path
;
135 surface
= _cairo_surface_create_scratch (target
,
138 clip
->extents
.height
,
139 CAIRO_COLOR_TRANSPARENT
);
140 if (unlikely (surface
->status
))
143 _cairo_path_fixed_init (&path
);
144 status
= CAIRO_STATUS_SUCCESS
;
145 for (i
= 0; status
== CAIRO_STATUS_SUCCESS
&& i
< clip
->num_boxes
; i
++) {
146 status
= _cairo_path_fixed_add_box (&path
, &clip
->boxes
[i
],
147 -_cairo_fixed_from_int (clip
->extents
.x
),
148 -_cairo_fixed_from_int (clip
->extents
.y
));
150 if (status
== CAIRO_STATUS_SUCCESS
)
151 status
= _cairo_surface_fill (surface
,
153 &_cairo_pattern_white
.base
,
155 CAIRO_FILL_RULE_WINDING
,
157 CAIRO_ANTIALIAS_DEFAULT
,
159 _cairo_path_fixed_fini (&path
);
160 if (unlikely (status
)) {
161 cairo_surface_destroy (surface
);
162 return _cairo_surface_create_in_error (status
);
165 surface
= _cairo_surface_create_scratch (target
,
168 clip
->extents
.height
,
170 if (unlikely (surface
->status
))
174 copy
= _cairo_clip_copy_with_translation (clip
,
177 copy_path
= copy
->path
;
181 if (! _cairo_clip_is_region (copy
))
182 region
= _cairo_clip_copy_region (copy
);
184 status
= CAIRO_STATUS_SUCCESS
;
185 clip_path
= copy_path
;
186 while (status
== CAIRO_STATUS_SUCCESS
&& clip_path
) {
187 status
= _cairo_surface_fill (surface
,
189 &_cairo_pattern_white
.base
,
191 clip_path
->fill_rule
,
192 clip_path
->tolerance
,
193 clip_path
->antialias
,
195 clip_path
= clip_path
->prev
;
198 copy
->path
= copy_path
;
199 _cairo_clip_destroy (copy
);
201 _cairo_clip_destroy (region
);
203 if (unlikely (status
)) {
204 cairo_surface_destroy (surface
);
205 return _cairo_surface_create_in_error (status
);
208 *tx
= clip
->extents
.x
;
209 *ty
= clip
->extents
.y
;
214 _cairo_clip_get_image (const cairo_clip_t
*clip
,
215 cairo_surface_t
*target
,
216 const cairo_rectangle_int_t
*extents
)
218 cairo_surface_t
*surface
;
219 cairo_status_t status
;
221 surface
= cairo_surface_create_similar_image (target
,
225 if (unlikely (surface
->status
))
228 status
= _cairo_surface_paint (surface
, CAIRO_OPERATOR_SOURCE
,
229 &_cairo_pattern_white
.base
, NULL
);
230 if (likely (status
== CAIRO_STATUS_SUCCESS
))
231 status
= _cairo_clip_combine_with_surface (clip
, surface
,
232 extents
->x
, extents
->y
);
234 if (unlikely (status
)) {
235 cairo_surface_destroy (surface
);
236 surface
= _cairo_surface_create_in_error (status
);