1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Chris Wilson
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Red Hat, Inc.
31  *
32  * Contributor(s):
33  *      Chris Wilson <chris@chris-wilson.co.uk>
34  */
35 
36 #include "cairoint.h"
37 
38 #include "cairo-surface-clipper-private.h"
39 
40 /* A collection of routines to facilitate vector surface clipping */
41 
42 static cairo_status_t
_cairo_surface_clipper_intersect_clip_path_recursive(cairo_surface_clipper_t * clipper,cairo_clip_path_t * clip_path)43 _cairo_surface_clipper_intersect_clip_path_recursive (cairo_surface_clipper_t *clipper,
44 						      cairo_clip_path_t *clip_path)
45 {
46     cairo_status_t status;
47 
48     if (clip_path->prev != NULL) {
49 	status =
50 	    _cairo_surface_clipper_intersect_clip_path_recursive (clipper,
51 								  clip_path->prev);
52 	if (unlikely (status))
53 	    return status;
54     }
55 
56     return clipper->intersect_clip_path (clipper,
57 					 &clip_path->path,
58 					 clip_path->fill_rule,
59 					 clip_path->tolerance,
60 					 clip_path->antialias);
61 }
62 
63 cairo_status_t
_cairo_surface_clipper_set_clip(cairo_surface_clipper_t * clipper,cairo_clip_t * clip)64 _cairo_surface_clipper_set_clip (cairo_surface_clipper_t *clipper,
65 				 cairo_clip_t *clip)
66 {
67     cairo_status_t status;
68     cairo_bool_t clear;
69 
70     /* XXX as we cache a reference to the path, and compare every time,
71      * we may in future need to install a notification if the clip->path
72      * is every modified (e.g. cairo_clip_translate).
73      */
74 
75     if (clip == NULL && clipper->clip.path == NULL)
76 	return CAIRO_STATUS_SUCCESS;
77 
78     if (clip != NULL && clipper->clip.path != NULL &&
79 	_cairo_clip_equal (clip, &clipper->clip))
80     {
81 	return CAIRO_STATUS_SUCCESS;
82     }
83 
84     /* all clipped out state should never propagate this far */
85     assert (clip == NULL || clip->path != NULL);
86 
87     /* Check whether this clip is a continuation of the previous.
88      * If not, we have to remove the current clip and rebuild.
89      */
90     clear = clip == NULL || clip->path->prev != clipper->clip.path;
91 
92     _cairo_clip_reset (&clipper->clip);
93     _cairo_clip_init_copy (&clipper->clip, clip);
94 
95     if (clear) {
96 	clipper->is_clipped = FALSE;
97 	status = clipper->intersect_clip_path (clipper, NULL, 0, 0, 0);
98 	if (unlikely (status))
99 	    return status;
100 
101 	if (clip != NULL && clip->path != NULL) {
102 	    status =
103 		_cairo_surface_clipper_intersect_clip_path_recursive (clipper,
104 								      clip->path);
105 	    clipper->is_clipped = TRUE;
106 	}
107     } else {
108 	cairo_clip_path_t *path = clip->path;
109 
110 	clipper->is_clipped = TRUE;
111 	status = clipper->intersect_clip_path (clipper,
112 					       &path->path,
113 					       path->fill_rule,
114 					       path->tolerance,
115 					       path->antialias);
116     }
117 
118     return status;
119 }
120 
121 void
_cairo_surface_clipper_init(cairo_surface_clipper_t * clipper,cairo_surface_clipper_intersect_clip_path_func_t func)122 _cairo_surface_clipper_init (cairo_surface_clipper_t *clipper,
123 			     cairo_surface_clipper_intersect_clip_path_func_t func)
124 {
125     _cairo_clip_init (&clipper->clip);
126     clipper->is_clipped = FALSE;
127     clipper->intersect_clip_path = func;
128 }
129 
130 void
_cairo_surface_clipper_reset(cairo_surface_clipper_t * clipper)131 _cairo_surface_clipper_reset (cairo_surface_clipper_t *clipper)
132 {
133     _cairo_clip_reset (&clipper->clip);
134     clipper->is_clipped = FALSE;
135 }
136