Updated: Sudoku no longer crashes
[moon.git] / cairo / src / cairo-path-bounds.c
blob70867e31ce5108644bb6ca0db2fda5fcdec84edb
1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2003 University of Southern California
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
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/
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.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is University of Southern
31 * California.
33 * Contributor(s):
34 * Carl D. Worth <cworth@cworth.org>
37 #include "cairoint.h"
39 typedef struct cairo_path_bounder {
40 cairo_point_t move_to_point;
41 cairo_bool_t has_move_to_point;
42 cairo_bool_t has_point;
44 cairo_fixed_t min_x;
45 cairo_fixed_t min_y;
46 cairo_fixed_t max_x;
47 cairo_fixed_t max_y;
48 } cairo_path_bounder_t;
50 static void
51 _cairo_path_bounder_init (cairo_path_bounder_t *bounder);
53 static void
54 _cairo_path_bounder_fini (cairo_path_bounder_t *bounder);
56 static void
57 _cairo_path_bounder_add_point (cairo_path_bounder_t *bounder, cairo_point_t *point);
59 static cairo_status_t
60 _cairo_path_bounder_move_to (void *closure, cairo_point_t *point);
62 static cairo_status_t
63 _cairo_path_bounder_line_to (void *closure, cairo_point_t *point);
65 static cairo_status_t
66 _cairo_path_bounder_close_path (void *closure);
68 static void
69 _cairo_path_bounder_init (cairo_path_bounder_t *bounder)
71 bounder->has_move_to_point = FALSE;
72 bounder->has_point = FALSE;
75 static void
76 _cairo_path_bounder_fini (cairo_path_bounder_t *bounder)
78 bounder->has_move_to_point = FALSE;
79 bounder->has_point = FALSE;
82 static void
83 _cairo_path_bounder_add_point (cairo_path_bounder_t *bounder, cairo_point_t *point)
85 if (bounder->has_point) {
86 if (point->x < bounder->min_x)
87 bounder->min_x = point->x;
89 if (point->y < bounder->min_y)
90 bounder->min_y = point->y;
92 if (point->x > bounder->max_x)
93 bounder->max_x = point->x;
95 if (point->y > bounder->max_y)
96 bounder->max_y = point->y;
97 } else {
98 bounder->min_x = point->x;
99 bounder->min_y = point->y;
100 bounder->max_x = point->x;
101 bounder->max_y = point->y;
103 bounder->has_point = TRUE;
107 static cairo_status_t
108 _cairo_path_bounder_move_to (void *closure, cairo_point_t *point)
110 cairo_path_bounder_t *bounder = closure;
112 bounder->move_to_point = *point;
113 bounder->has_move_to_point = TRUE;
115 return CAIRO_STATUS_SUCCESS;
118 static cairo_status_t
119 _cairo_path_bounder_line_to (void *closure, cairo_point_t *point)
121 cairo_path_bounder_t *bounder = closure;
123 if (bounder->has_move_to_point) {
124 _cairo_path_bounder_add_point (bounder,
125 &bounder->move_to_point);
126 bounder->has_move_to_point = FALSE;
129 _cairo_path_bounder_add_point (bounder, point);
131 return CAIRO_STATUS_SUCCESS;
134 static cairo_status_t
135 _cairo_path_bounder_close_path (void *closure)
137 return CAIRO_STATUS_SUCCESS;
140 /* XXX: Perhaps this should compute a PixRegion rather than 4 doubles */
141 cairo_status_t
142 _cairo_path_fixed_bounds (cairo_path_fixed_t *path,
143 double *x1, double *y1,
144 double *x2, double *y2,
145 double tolerance)
147 cairo_path_bounder_t bounder;
148 cairo_status_t status;
150 _cairo_path_bounder_init (&bounder);
152 status = _cairo_path_fixed_interpret_flat (path, CAIRO_DIRECTION_FORWARD,
153 _cairo_path_bounder_move_to,
154 _cairo_path_bounder_line_to,
155 _cairo_path_bounder_close_path,
156 &bounder,
157 tolerance);
159 if (status == CAIRO_STATUS_SUCCESS && bounder.has_point) {
160 *x1 = _cairo_fixed_to_double (bounder.min_x);
161 *y1 = _cairo_fixed_to_double (bounder.min_y);
162 *x2 = _cairo_fixed_to_double (bounder.max_x);
163 *y2 = _cairo_fixed_to_double (bounder.max_y);
164 } else {
165 *x1 = 0.0;
166 *y1 = 0.0;
167 *x2 = 0.0;
168 *y2 = 0.0;
171 _cairo_path_bounder_fini (&bounder);
173 return status;