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 © 2003 University of Southern California
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is University of Southern
32
 * California.
33
 *
34
 * Contributor(s):
35
 *	Carl D. Worth <cworth@cworth.org>
36
 */
37

            
38
#include "cairoint.h"
39
#include "cairo-box-inline.h"
40
#include "cairo-error-private.h"
41
#include "cairo-path-fixed-private.h"
42

            
43
typedef struct _cairo_path_bounder {
44
    cairo_point_t current_point;
45
    cairo_bool_t has_extents;
46
    cairo_box_t extents;
47
} cairo_path_bounder_t;
48

            
49
static cairo_status_t
50
2793
_cairo_path_bounder_move_to (void *closure,
51
			     const cairo_point_t *point)
52
{
53
2793
    cairo_path_bounder_t *bounder = closure;
54

            
55
2793
    bounder->current_point = *point;
56

            
57
2793
    if (likely (bounder->has_extents)) {
58
	_cairo_box_add_point (&bounder->extents, point);
59
    } else {
60
2793
	bounder->has_extents = TRUE;
61
2793
	_cairo_box_set (&bounder->extents, point, point);
62
    }
63

            
64
2793
    return CAIRO_STATUS_SUCCESS;
65
}
66

            
67
static cairo_status_t
68
_cairo_path_bounder_line_to (void *closure,
69
			     const cairo_point_t *point)
70
{
71
    cairo_path_bounder_t *bounder = closure;
72

            
73
    bounder->current_point = *point;
74
    _cairo_box_add_point (&bounder->extents, point);
75

            
76
    return CAIRO_STATUS_SUCCESS;
77
}
78

            
79
static cairo_status_t
80
11172
_cairo_path_bounder_curve_to (void *closure,
81
			      const cairo_point_t *b,
82
			      const cairo_point_t *c,
83
			      const cairo_point_t *d)
84
{
85
11172
    cairo_path_bounder_t *bounder = closure;
86

            
87
11172
    _cairo_box_add_curve_to (&bounder->extents,
88
11172
			     &bounder->current_point,
89
			     b, c, d);
90
11172
    bounder->current_point = *d;
91

            
92
11172
    return CAIRO_STATUS_SUCCESS;
93
}
94

            
95
static cairo_status_t
96
_cairo_path_bounder_close_path (void *closure)
97
{
98
    return CAIRO_STATUS_SUCCESS;
99
}
100

            
101
cairo_bool_t
102
2793
_cairo_path_bounder_extents (const cairo_path_fixed_t *path,
103
			     cairo_box_t *extents)
104
{
105
    cairo_path_bounder_t bounder;
106
    cairo_status_t status;
107

            
108
2793
    bounder.has_extents = FALSE;
109
2793
    status = _cairo_path_fixed_interpret (path,
110
					  _cairo_path_bounder_move_to,
111
					  _cairo_path_bounder_line_to,
112
					  _cairo_path_bounder_curve_to,
113
					  _cairo_path_bounder_close_path,
114
					  &bounder);
115
2793
    assert (!status);
116

            
117
2793
    if (bounder.has_extents)
118
2793
	*extents = bounder.extents;
119

            
120
2793
    return bounder.has_extents;
121
}
122

            
123
void
124
2765
_cairo_path_fixed_approximate_clip_extents (const cairo_path_fixed_t *path,
125
					    cairo_rectangle_int_t *extents)
126
{
127
2765
    _cairo_path_fixed_approximate_fill_extents (path, extents);
128
2765
}
129

            
130
void
131
502678
_cairo_path_fixed_approximate_fill_extents (const cairo_path_fixed_t *path,
132
					    cairo_rectangle_int_t *extents)
133
{
134
502678
    _cairo_path_fixed_fill_extents (path, CAIRO_FILL_RULE_WINDING, 0, extents);
135
502678
}
136

            
137
void
138
503495
_cairo_path_fixed_fill_extents (const cairo_path_fixed_t	*path,
139
				cairo_fill_rule_t	 fill_rule,
140
				double			 tolerance,
141
				cairo_rectangle_int_t	*extents)
142
{
143
503495
    if (path->extents.p1.x < path->extents.p2.x &&
144
503486
	path->extents.p1.y < path->extents.p2.y) {
145
503474
	_cairo_box_round_to_rectangle (&path->extents, extents);
146
    } else {
147
21
	extents->x = extents->y = 0;
148
21
	extents->width = extents->height = 0;
149
    }
150
503495
}
151

            
152
/* Adjusts the fill extents (above) by the device-space pen.  */
153
void
154
40753
_cairo_path_fixed_approximate_stroke_extents (const cairo_path_fixed_t *path,
155
					      const cairo_stroke_style_t *style,
156
					      const cairo_matrix_t *ctm,
157
					      cairo_bool_t is_vector,
158
					      cairo_rectangle_int_t *extents)
159
{
160
40753
    if (path->has_extents) {
161
	cairo_box_t box_extents;
162
	double dx, dy;
163

            
164
40705
	_cairo_stroke_style_max_distance_from_path (style, path, ctm, &dx, &dy);
165
40705
	if (is_vector)
166
	{
167
	    /* When calculating extents for vector surfaces, ensure lines thinner
168
	     * than the fixed point resolution are not optimized away. */
169
697
	    double min = _cairo_fixed_to_double (CAIRO_FIXED_EPSILON*2);
170
697
	    if (dx < min)
171
		dx = min;
172

            
173
697
	    if (dy < min)
174
		dy = min;
175
	}
176

            
177
40705
	box_extents = path->extents;
178
40705
	box_extents.p1.x -= _cairo_fixed_from_double (dx);
179
40705
	box_extents.p1.y -= _cairo_fixed_from_double (dy);
180
40705
	box_extents.p2.x += _cairo_fixed_from_double (dx);
181
40705
	box_extents.p2.y += _cairo_fixed_from_double (dy);
182

            
183
40705
	_cairo_box_round_to_rectangle (&box_extents, extents);
184
    } else {
185
48
	extents->x = extents->y = 0;
186
48
	extents->width = extents->height = 0;
187
    }
188
40753
}
189

            
190
cairo_status_t
191
672
_cairo_path_fixed_stroke_extents (const cairo_path_fixed_t	*path,
192
				  const cairo_stroke_style_t	*stroke_style,
193
				  const cairo_matrix_t		*ctm,
194
				  const cairo_matrix_t		*ctm_inverse,
195
				  double			 tolerance,
196
				  cairo_rectangle_int_t		*extents)
197
{
198
    cairo_polygon_t polygon;
199
    cairo_status_t status;
200
    cairo_stroke_style_t style;
201

            
202
    /* When calculating extents for vector surfaces, ensure lines thinner
203
     * than one point are not optimized away. */
204
672
    double min_line_width = _cairo_matrix_transformed_circle_major_axis (ctm_inverse, 1.0);
205
672
    if (stroke_style->line_width < min_line_width)
206
    {
207
282
	style = *stroke_style;
208
282
	style.line_width = min_line_width;
209
282
	stroke_style = &style;
210
    }
211

            
212
672
    _cairo_polygon_init (&polygon, NULL, 0);
213
672
    status = _cairo_path_fixed_stroke_to_polygon (path,
214
						  stroke_style,
215
						  ctm, ctm_inverse,
216
						  tolerance,
217
						  &polygon);
218
672
    _cairo_box_round_to_rectangle (&polygon.extents, extents);
219
672
    _cairo_polygon_fini (&polygon);
220

            
221
672
    return status;
222
}
223

            
224
cairo_bool_t
225
108
_cairo_path_fixed_extents (const cairo_path_fixed_t *path,
226
			   cairo_box_t *box)
227
{
228
108
    *box = path->extents;
229
108
    return path->has_extents;
230
}