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);
64 return CAIRO_STATUS_NULL_POINTER;
65 copy_path = copy->path;
69 status = _cairo_surface_paint (dst,
71 &_cairo_pattern_white.base,
76 if (_cairo_clip_is_region (copy))
78 clip_path = copy_path;
79 while (status == CAIRO_STATUS_SUCCESS && clip_path) {
80 status = _cairo_surface_fill (dst,
82 &_cairo_pattern_white.base,
88 clip_path = clip_path->prev;
91 copy->path = copy_path;
92 _cairo_clip_destroy (copy);
97 _cairo_path_fixed_add_box (cairo_path_fixed_t *path,
98 const cairo_box_t *box,
102 cairo_status_t status;
104 status = _cairo_path_fixed_move_to (path, box->p1.x + fx, box->p1.y + fy);
105 if (unlikely (status))
108 status = _cairo_path_fixed_line_to (path, box->p2.x + fx, box->p1.y + fy);
109 if (unlikely (status))
112 status = _cairo_path_fixed_line_to (path, box->p2.x + fx, box->p2.y + fy);
113 if (unlikely (status))
116 status = _cairo_path_fixed_line_to (path, box->p1.x + fx, box->p2.y + fy);
117 if (unlikely (status))
120 return _cairo_path_fixed_close_path (path);
124 _cairo_clip_get_surface (const cairo_clip_t *clip,
125 cairo_surface_t *target,
128 cairo_surface_t *surface;
129 cairo_status_t status;
130 cairo_clip_t *copy, *region;
131 cairo_clip_path_t *copy_path, *clip_path;
133 if (clip->num_boxes) {
134 cairo_path_fixed_t path;
137 surface = _cairo_surface_create_similar_solid (target,
140 clip->extents.height,
141 CAIRO_COLOR_TRANSPARENT);
142 if (unlikely (surface->status))
145 _cairo_path_fixed_init (&path);
146 status = CAIRO_STATUS_SUCCESS;
147 for (i = 0; status == CAIRO_STATUS_SUCCESS && i < clip->num_boxes; i++) {
148 status = _cairo_path_fixed_add_box (&path, &clip->boxes[i],
149 -_cairo_fixed_from_int (clip->extents.x),
150 -_cairo_fixed_from_int (clip->extents.y));
152 if (status == CAIRO_STATUS_SUCCESS)
153 status = _cairo_surface_fill (surface,
155 &_cairo_pattern_white.base,
157 CAIRO_FILL_RULE_WINDING,
159 CAIRO_ANTIALIAS_DEFAULT,
161 _cairo_path_fixed_fini (&path);
162 if (unlikely (status)) {
163 cairo_surface_destroy (surface);
164 return _cairo_surface_create_in_error (status);
167 surface = _cairo_surface_create_similar_solid (target,
170 clip->extents.height,
172 if (unlikely (surface->status))
176 copy = _cairo_clip_copy_with_translation (clip,
180 cairo_surface_destroy (surface);
181 status = CAIRO_STATUS_NULL_POINTER;
182 return _cairo_surface_create_in_error (status);
184 copy_path = copy->path;
188 if (! _cairo_clip_is_region (copy))
189 region = _cairo_clip_copy_region (copy);
191 status = CAIRO_STATUS_SUCCESS;
192 clip_path = copy_path;
193 while (status == CAIRO_STATUS_SUCCESS && clip_path) {
194 status = _cairo_surface_fill (surface,
196 &_cairo_pattern_white.base,
198 clip_path->fill_rule,
199 clip_path->tolerance,
200 clip_path->antialias,
202 clip_path = clip_path->prev;
205 copy->path = copy_path;
206 _cairo_clip_destroy (copy);
208 _cairo_clip_destroy (region);
210 if (unlikely (status)) {
211 cairo_surface_destroy (surface);
212 return _cairo_surface_create_in_error (status);
215 *tx = clip->extents.x;
216 *ty = clip->extents.y;
221 _cairo_clip_get_image (const cairo_clip_t *clip,
222 cairo_surface_t *target,
223 const cairo_rectangle_int_t *extents)
225 cairo_surface_t *surface;
226 cairo_status_t status;
228 surface = cairo_surface_create_similar_image (target,
232 if (unlikely (surface->status))
235 status = _cairo_surface_paint (surface, CAIRO_OPERATOR_SOURCE,
236 &_cairo_pattern_white.base, NULL);
237 if (likely (status == CAIRO_STATUS_SUCCESS))
238 status = _cairo_clip_combine_with_surface (clip, surface,
239 extents->x, extents->y);
241 if (unlikely (status)) {
242 cairo_surface_destroy (surface);
243 surface = _cairo_surface_create_in_error (status);