tizen 2.3.1 release
[framework/graphics/cairo.git] / src / cairo-clip-surface.c
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
3  *
4  * Copyright © 2002 University of Southern California
5  * Copyright © 2005 Red Hat, Inc.
6  * Copyright © 2009 Chris Wilson
7  *
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.
15  *
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
21  *
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/
26  *
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.
30  *
31  * The Original Code is the cairo graphics library.
32  *
33  * The Initial Developer of the Original Code is University of Southern
34  * California.
35  *
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>
40  */
41
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"
51
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)
56 {
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;
61
62     copy = _cairo_clip_copy_with_translation (clip, -dst_x, -dst_y);
63     if (copy == NULL)
64         return CAIRO_STATUS_NULL_POINTER;
65     copy_path = copy->path;
66     copy->path = NULL;
67
68     if (copy->boxes) {
69         status = _cairo_surface_paint (dst,
70                                        CAIRO_OPERATOR_IN,
71                                        &_cairo_pattern_white.base,
72                                        copy);
73     }
74
75     clip = NULL;
76     if (_cairo_clip_is_region (copy))
77         clip = copy;
78     clip_path = copy_path;
79     while (status == CAIRO_STATUS_SUCCESS && clip_path) {
80         status = _cairo_surface_fill (dst,
81                                       CAIRO_OPERATOR_IN,
82                                       &_cairo_pattern_white.base,
83                                       &clip_path->path,
84                                       clip_path->fill_rule,
85                                       clip_path->tolerance,
86                                       clip_path->antialias,
87                                       clip);
88         clip_path = clip_path->prev;
89     }
90
91     copy->path = copy_path;
92     _cairo_clip_destroy (copy);
93     return status;
94 }
95
96 static cairo_status_t
97 _cairo_path_fixed_add_box (cairo_path_fixed_t *path,
98                            const cairo_box_t *box,
99                            cairo_fixed_t fx,
100                            cairo_fixed_t fy)
101 {
102     cairo_status_t status;
103
104     status = _cairo_path_fixed_move_to (path, box->p1.x + fx, box->p1.y + fy);
105     if (unlikely (status))
106         return status;
107
108     status = _cairo_path_fixed_line_to (path, box->p2.x + fx, box->p1.y + fy);
109     if (unlikely (status))
110         return status;
111
112     status = _cairo_path_fixed_line_to (path, box->p2.x + fx, box->p2.y + fy);
113     if (unlikely (status))
114         return status;
115
116     status = _cairo_path_fixed_line_to (path, box->p1.x + fx, box->p2.y + fy);
117     if (unlikely (status))
118         return status;
119
120     return _cairo_path_fixed_close_path (path);
121 }
122
123 cairo_surface_t *
124 _cairo_clip_get_surface (const cairo_clip_t *clip,
125                          cairo_surface_t *target,
126                          int *tx, int *ty)
127 {
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;
132
133     if (clip->num_boxes) {
134         cairo_path_fixed_t path;
135         int i;
136
137         surface = _cairo_surface_create_similar_solid (target,
138                                                        CAIRO_CONTENT_ALPHA,
139                                                        clip->extents.width,
140                                                        clip->extents.height,
141                                                        CAIRO_COLOR_TRANSPARENT);
142         if (unlikely (surface->status))
143             return surface;
144
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));
151         }
152         if (status == CAIRO_STATUS_SUCCESS)
153             status = _cairo_surface_fill (surface,
154                                           CAIRO_OPERATOR_ADD,
155                                           &_cairo_pattern_white.base,
156                                           &path,
157                                           CAIRO_FILL_RULE_WINDING,
158                                           1.,
159                                           CAIRO_ANTIALIAS_DEFAULT,
160                                           NULL);
161         _cairo_path_fixed_fini (&path);
162         if (unlikely (status)) {
163             cairo_surface_destroy (surface);
164             return _cairo_surface_create_in_error (status);
165         }
166     } else {
167         surface = _cairo_surface_create_similar_solid (target,
168                                                        CAIRO_CONTENT_ALPHA,
169                                                        clip->extents.width,
170                                                        clip->extents.height,
171                                                        CAIRO_COLOR_WHITE);
172         if (unlikely (surface->status))
173             return surface;
174     }
175
176     copy = _cairo_clip_copy_with_translation (clip,
177                                               -clip->extents.x,
178                                               -clip->extents.y);
179     if (copy == NULL) {
180         cairo_surface_destroy (surface);
181         status = CAIRO_STATUS_NULL_POINTER;
182         return _cairo_surface_create_in_error (status);
183     }
184     copy_path = copy->path;
185     copy->path = NULL;
186
187     region = copy;
188     if (! _cairo_clip_is_region (copy))
189         region = _cairo_clip_copy_region (copy);
190
191     status = CAIRO_STATUS_SUCCESS;
192     clip_path = copy_path;
193     while (status == CAIRO_STATUS_SUCCESS && clip_path) {
194         status = _cairo_surface_fill (surface,
195                                       CAIRO_OPERATOR_IN,
196                                       &_cairo_pattern_white.base,
197                                       &clip_path->path,
198                                       clip_path->fill_rule,
199                                       clip_path->tolerance,
200                                       clip_path->antialias,
201                                       region);
202         clip_path = clip_path->prev;
203     }
204
205     copy->path = copy_path;
206     _cairo_clip_destroy (copy);
207     if (region != copy)
208         _cairo_clip_destroy (region);
209
210     if (unlikely (status)) {
211         cairo_surface_destroy (surface);
212         return _cairo_surface_create_in_error (status);
213     }
214
215     *tx = clip->extents.x;
216     *ty = clip->extents.y;
217     return surface;
218 }
219
220 cairo_surface_t *
221 _cairo_clip_get_image (const cairo_clip_t *clip,
222                        cairo_surface_t *target,
223                        const cairo_rectangle_int_t *extents)
224 {
225     cairo_surface_t *surface;
226     cairo_status_t status;
227
228     surface = cairo_surface_create_similar_image (target,
229                                                   CAIRO_FORMAT_A8,
230                                                   extents->width,
231                                                   extents->height);
232     if (unlikely (surface->status))
233         return surface;
234
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);
240
241     if (unlikely (status)) {
242         cairo_surface_destroy (surface);
243         surface = _cairo_surface_create_in_error (status);
244     }
245
246     return surface;
247 }