beta-0.89.2
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-clip-surface.c
blob85feaa649eb33e166c570f5335ca10ecc69f5c90
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
34 * California.
36 * Contributor(s):
37 * Carl D. Worth <cworth@cworth.org>
38 * Kristian Høgsberg <krh@redhat.com>
39 * Chris Wilson <chris@chris-wilson.co.uk>
42 #include "cairoint.h"
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"
52 cairo_status_t
53 _cairo_clip_combine_with_surface (const cairo_clip_t *clip,
54 cairo_surface_t *dst,
55 int dst_x, int dst_y)
57 cairo_clip_path_t *copy_path;
58 cairo_clip_path_t *clip_path;
59 cairo_clip_t *copy;
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;
64 copy->path = NULL;
66 if (copy->boxes) {
67 status = _cairo_surface_paint (dst,
68 CAIRO_OPERATOR_IN,
69 &_cairo_pattern_white.base,
70 copy);
73 clip = NULL;
74 if (_cairo_clip_is_region (copy))
75 clip = copy;
76 clip_path = copy_path;
77 while (status == CAIRO_STATUS_SUCCESS && clip_path) {
78 status = _cairo_surface_fill (dst,
79 CAIRO_OPERATOR_IN,
80 &_cairo_pattern_white.base,
81 &clip_path->path,
82 clip_path->fill_rule,
83 clip_path->tolerance,
84 clip_path->antialias,
85 clip);
86 clip_path = clip_path->prev;
89 copy->path = copy_path;
90 _cairo_clip_destroy (copy);
91 return status;
94 static cairo_status_t
95 _cairo_path_fixed_add_box (cairo_path_fixed_t *path,
96 const cairo_box_t *box,
97 cairo_fixed_t fx,
98 cairo_fixed_t fy)
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))
104 return status;
106 status = _cairo_path_fixed_line_to (path, box->p2.x + fx, box->p1.y + fy);
107 if (unlikely (status))
108 return status;
110 status = _cairo_path_fixed_line_to (path, box->p2.x + fx, box->p2.y + fy);
111 if (unlikely (status))
112 return status;
114 status = _cairo_path_fixed_line_to (path, box->p1.x + fx, box->p2.y + fy);
115 if (unlikely (status))
116 return status;
118 return _cairo_path_fixed_close_path (path);
121 cairo_surface_t *
122 _cairo_clip_get_surface (const cairo_clip_t *clip,
123 cairo_surface_t *target,
124 int *tx, int *ty)
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;
133 int i;
135 surface = _cairo_surface_create_scratch (target,
136 CAIRO_CONTENT_ALPHA,
137 clip->extents.width,
138 clip->extents.height,
139 CAIRO_COLOR_TRANSPARENT);
140 if (unlikely (surface->status))
141 return surface;
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,
152 CAIRO_OPERATOR_ADD,
153 &_cairo_pattern_white.base,
154 &path,
155 CAIRO_FILL_RULE_WINDING,
157 CAIRO_ANTIALIAS_DEFAULT,
158 NULL);
159 _cairo_path_fixed_fini (&path);
160 if (unlikely (status)) {
161 cairo_surface_destroy (surface);
162 return _cairo_surface_create_in_error (status);
164 } else {
165 surface = _cairo_surface_create_scratch (target,
166 CAIRO_CONTENT_ALPHA,
167 clip->extents.width,
168 clip->extents.height,
169 CAIRO_COLOR_WHITE);
170 if (unlikely (surface->status))
171 return surface;
174 copy = _cairo_clip_copy_with_translation (clip,
175 -clip->extents.x,
176 -clip->extents.y);
177 copy_path = copy->path;
178 copy->path = NULL;
180 region = copy;
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,
188 CAIRO_OPERATOR_IN,
189 &_cairo_pattern_white.base,
190 &clip_path->path,
191 clip_path->fill_rule,
192 clip_path->tolerance,
193 clip_path->antialias,
194 region);
195 clip_path = clip_path->prev;
198 copy->path = copy_path;
199 _cairo_clip_destroy (copy);
200 if (region != 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;
210 return surface;
213 cairo_surface_t *
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,
222 CAIRO_FORMAT_A8,
223 extents->width,
224 extents->height);
225 if (unlikely (surface->status))
226 return surface;
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);
239 return surface;