1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2008 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 Chris Wilson.
31
 *
32
 * Contributor(s):
33
 *	Chris Wilson <chris@chris-wilson.co.uk>
34
 */
35

            
36
#include "cairoint.h"
37
#include "cairo-path-fixed-private.h"
38

            
39
typedef struct cairo_in_fill {
40
    double tolerance;
41
    cairo_bool_t on_edge;
42
    int winding;
43

            
44
    cairo_fixed_t x, y;
45

            
46
    cairo_bool_t has_current_point;
47
    cairo_point_t current_point;
48
    cairo_point_t first_point;
49
} cairo_in_fill_t;
50

            
51
static void
52
142
_cairo_in_fill_init (cairo_in_fill_t	*in_fill,
53
		     double		 tolerance,
54
		     double		 x,
55
		     double		 y)
56
{
57
142
    in_fill->on_edge = FALSE;
58
142
    in_fill->winding = 0;
59
142
    in_fill->tolerance = tolerance;
60

            
61
142
    in_fill->x = _cairo_fixed_from_double (x);
62
142
    in_fill->y = _cairo_fixed_from_double (y);
63

            
64
142
    in_fill->has_current_point = FALSE;
65
142
    in_fill->current_point.x = 0;
66
142
    in_fill->current_point.y = 0;
67
142
}
68

            
69
static void
70
142
_cairo_in_fill_fini (cairo_in_fill_t *in_fill)
71
{
72
142
}
73

            
74
static int
75
335
edge_compare_for_y_against_x (const cairo_point_t *p1,
76
			      const cairo_point_t *p2,
77
			      cairo_fixed_t y,
78
			      cairo_fixed_t x)
79
{
80
    cairo_fixed_t adx, ady;
81
    cairo_fixed_t dx, dy;
82
    cairo_int64_t L, R;
83

            
84
335
    adx = p2->x - p1->x;
85
335
    dx = x - p1->x;
86

            
87
335
    if (adx == 0)
88
2
	return -dx;
89
333
    if ((adx ^ dx) < 0)
90
	return adx;
91

            
92
333
    dy = y - p1->y;
93
333
    ady = p2->y - p1->y;
94

            
95
333
    L = _cairo_int32x32_64_mul (dy, adx);
96
333
    R = _cairo_int32x32_64_mul (dx, ady);
97

            
98
333
    return _cairo_int64_cmp (L, R);
99
}
100

            
101
static void
102
1353
_cairo_in_fill_add_edge (cairo_in_fill_t *in_fill,
103
			 const cairo_point_t *p1,
104
			 const cairo_point_t *p2)
105
{
106
    int dir;
107

            
108
1353
    if (in_fill->on_edge)
109
113
	return;
110

            
111
    /* count the number of edge crossing to -∞ */
112

            
113
1240
    dir = 1;
114
1240
    if (p2->y < p1->y) {
115
	const cairo_point_t *tmp;
116

            
117
438
	tmp = p1;
118
438
	p1 = p2;
119
438
	p2 = tmp;
120

            
121
438
	dir = -1;
122
    }
123

            
124
    /* First check whether the query is on an edge */
125
1240
    if ((p1->x == in_fill->x && p1->y == in_fill->y) ||
126
1237
	(p2->x == in_fill->x && p2->y == in_fill->y) ||
127
1233
	(! (p2->y < in_fill->y || p1->y > in_fill->y ||
128
327
	   (p1->x > in_fill->x && p2->x > in_fill->x) ||
129
464
	   (p1->x < in_fill->x && p2->x < in_fill->x)) &&
130
205
	 edge_compare_for_y_against_x (p1, p2, in_fill->y, in_fill->x) == 0))
131
    {
132
29
	in_fill->on_edge = TRUE;
133
29
	return;
134
    }
135

            
136
    /* edge is entirely above or below, note the shortening rule */
137
1211
    if (p2->y <= in_fill->y || p1->y > in_fill->y)
138
1007
	return;
139

            
140
    /* edge lies wholly to the right */
141
204
    if (p1->x >= in_fill->x && p2->x >= in_fill->x)
142
45
	return;
143

            
144
289
    if ((p1->x <= in_fill->x && p2->x <= in_fill->x) ||
145
130
	edge_compare_for_y_against_x (p1, p2, in_fill->y, in_fill->x) < 0)
146
    {
147
95
	in_fill->winding += dir;
148
    }
149
}
150

            
151
static cairo_status_t
152
477
_cairo_in_fill_move_to (void *closure,
153
			const cairo_point_t *point)
154
{
155
477
    cairo_in_fill_t *in_fill = closure;
156

            
157
    /* implicit close path */
158
477
    if (in_fill->has_current_point) {
159
	_cairo_in_fill_add_edge (in_fill,
160
				 &in_fill->current_point,
161
				 &in_fill->first_point);
162
    }
163

            
164
477
    in_fill->first_point = *point;
165
477
    in_fill->current_point = *point;
166
477
    in_fill->has_current_point = TRUE;
167

            
168
477
    return CAIRO_STATUS_SUCCESS;
169
}
170

            
171
static cairo_status_t
172
876
_cairo_in_fill_line_to (void *closure,
173
			const cairo_point_t *point)
174
{
175
876
    cairo_in_fill_t *in_fill = closure;
176

            
177
876
    if (in_fill->has_current_point)
178
876
	_cairo_in_fill_add_edge (in_fill, &in_fill->current_point, point);
179

            
180
876
    in_fill->current_point = *point;
181
876
    in_fill->has_current_point = TRUE;
182

            
183
876
    return CAIRO_STATUS_SUCCESS;
184
}
185

            
186
static cairo_status_t
187
480
_cairo_in_fill_add_point (void *closure,
188
                          const cairo_point_t *point,
189
                          const cairo_slope_t *tangent)
190
{
191
480
    return _cairo_in_fill_line_to (closure, point);
192
};
193

            
194
static cairo_status_t
195
50
_cairo_in_fill_curve_to (void *closure,
196
			 const cairo_point_t *b,
197
			 const cairo_point_t *c,
198
			 const cairo_point_t *d)
199
{
200
50
    cairo_in_fill_t *in_fill = closure;
201
    cairo_spline_t spline;
202
    cairo_fixed_t top, bot, left;
203

            
204
    /* first reject based on bbox */
205
50
    bot = top = in_fill->current_point.y;
206
50
    if (b->y < top) top = b->y;
207
50
    if (b->y > bot) bot = b->y;
208
50
    if (c->y < top) top = c->y;
209
50
    if (c->y > bot) bot = c->y;
210
50
    if (d->y < top) top = d->y;
211
50
    if (d->y > bot) bot = d->y;
212
50
    if (bot < in_fill->y || top > in_fill->y) {
213
	in_fill->current_point = *d;
214
	return CAIRO_STATUS_SUCCESS;
215
    }
216

            
217
50
    left = in_fill->current_point.x;
218
50
    if (b->x < left) left = b->x;
219
50
    if (c->x < left) left = c->x;
220
50
    if (d->x < left) left = d->x;
221
50
    if (left > in_fill->x) {
222
	in_fill->current_point = *d;
223
	return CAIRO_STATUS_SUCCESS;
224
    }
225

            
226
    /* XXX Investigate direct inspection of the inflections? */
227
50
    if (! _cairo_spline_init (&spline,
228
			      _cairo_in_fill_add_point,
229
			      in_fill,
230
50
			      &in_fill->current_point, b, c, d))
231
    {
232
	return CAIRO_STATUS_SUCCESS;
233
    }
234

            
235
50
    return _cairo_spline_decompose (&spline, in_fill->tolerance);
236
}
237

            
238
static cairo_status_t
239
477
_cairo_in_fill_close_path (void *closure)
240
{
241
477
    cairo_in_fill_t *in_fill = closure;
242

            
243
477
    if (in_fill->has_current_point) {
244
477
	_cairo_in_fill_add_edge (in_fill,
245
477
				 &in_fill->current_point,
246
477
				 &in_fill->first_point);
247

            
248
477
	in_fill->has_current_point = FALSE;
249
    }
250

            
251
477
    return CAIRO_STATUS_SUCCESS;
252
}
253

            
254
cairo_bool_t
255
145
_cairo_path_fixed_in_fill (const cairo_path_fixed_t	*path,
256
			   cairo_fill_rule_t	 fill_rule,
257
			   double		 tolerance,
258
			   double		 x,
259
			   double		 y)
260
{
261
    cairo_in_fill_t in_fill;
262
    cairo_status_t status;
263
    cairo_bool_t is_inside;
264

            
265
145
    if (_cairo_path_fixed_fill_is_empty (path))
266
3
	return FALSE;
267

            
268
142
    _cairo_in_fill_init (&in_fill, tolerance, x, y);
269

            
270
142
    status = _cairo_path_fixed_interpret (path,
271
					  _cairo_in_fill_move_to,
272
					  _cairo_in_fill_line_to,
273
					  _cairo_in_fill_curve_to,
274
					  _cairo_in_fill_close_path,
275
					  &in_fill);
276
142
    assert (status == CAIRO_STATUS_SUCCESS);
277

            
278
142
    _cairo_in_fill_close_path (&in_fill);
279

            
280
142
    if (in_fill.on_edge) {
281
29
	is_inside = TRUE;
282
113
    } else switch (fill_rule) {
283
4
    case CAIRO_FILL_RULE_EVEN_ODD:
284
4
	is_inside = in_fill.winding & 1;
285
4
	break;
286
109
    case CAIRO_FILL_RULE_WINDING:
287
109
	is_inside = in_fill.winding != 0;
288
109
	break;
289
    default:
290
	ASSERT_NOT_REACHED;
291
	is_inside = FALSE;
292
	break;
293
    }
294

            
295
142
    _cairo_in_fill_fini (&in_fill);
296

            
297
142
    return is_inside;
298
}