9c7222426d3753a7a3cc55642faff09bc64d651b
[framework/graphics/cairo.git] / src / cairo-path-bounds.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 © 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 _cairo_path_bounder_move_to (void *closure,
51                              const cairo_point_t *point)
52 {
53     cairo_path_bounder_t *bounder = closure;
54
55     bounder->current_point = *point;
56
57     if (likely (bounder->has_extents)) {
58         _cairo_box_add_point (&bounder->extents, point);
59     } else {
60         bounder->has_extents = TRUE;
61         _cairo_box_set (&bounder->extents, point, point);
62     }
63
64     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 _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     cairo_path_bounder_t *bounder = closure;
86
87     _cairo_box_add_curve_to (&bounder->extents,
88                              &bounder->current_point,
89                              b, c, d);
90     bounder->current_point = *d;
91
92     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 _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     bounder.has_extents = FALSE;
109     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     assert (!status);
116
117     if (bounder.has_extents)
118         *extents = bounder.extents;
119
120     return bounder.has_extents;
121 }
122
123 void
124 _cairo_path_fixed_approximate_clip_extents (const cairo_path_fixed_t *path,
125                                             cairo_rectangle_int_t *extents)
126 {
127     _cairo_path_fixed_approximate_fill_extents (path, extents);
128 }
129
130 void
131 _cairo_path_fixed_approximate_fill_extents (const cairo_path_fixed_t *path,
132                                             cairo_rectangle_int_t *extents)
133 {
134     _cairo_path_fixed_fill_extents (path, CAIRO_FILL_RULE_WINDING, 0, extents);
135 }
136
137 void
138 _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     if (path->extents.p1.x < path->extents.p2.x &&
144         path->extents.p1.y < path->extents.p2.y) {
145         _cairo_box_round_to_rectangle (&path->extents, extents);
146     } else {
147         extents->x = extents->y = 0;
148         extents->width = extents->height = 0;
149     }
150 }
151
152 /* Adjusts the fill extents (above) by the device-space pen.  */
153 void
154 _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_rectangle_int_t *extents)
158 {
159     if (path->has_extents) {
160         cairo_box_t box_extents;
161         double dx, dy;
162
163         _cairo_stroke_style_max_distance_from_path (style, path, ctm, &dx, &dy);
164
165         box_extents = path->extents;
166         box_extents.p1.x -= _cairo_fixed_from_double (dx);
167         box_extents.p1.y -= _cairo_fixed_from_double (dy);
168         box_extents.p2.x += _cairo_fixed_from_double (dx);
169         box_extents.p2.y += _cairo_fixed_from_double (dy);
170
171         _cairo_box_round_to_rectangle (&box_extents, extents);
172     } else {
173         extents->x = extents->y = 0;
174         extents->width = extents->height = 0;
175     }
176 }
177
178 cairo_status_t
179 _cairo_path_fixed_stroke_extents (const cairo_path_fixed_t      *path,
180                                   const cairo_stroke_style_t    *stroke_style,
181                                   const cairo_matrix_t          *ctm,
182                                   const cairo_matrix_t          *ctm_inverse,
183                                   double                         tolerance,
184                                   cairo_rectangle_int_t         *extents)
185 {
186     cairo_polygon_t polygon;
187     cairo_status_t status;
188
189     _cairo_polygon_init (&polygon, NULL, 0);
190     status = _cairo_path_fixed_stroke_to_polygon (path,
191                                                   stroke_style,
192                                                   ctm, ctm_inverse,
193                                                   tolerance,
194                                                   &polygon);
195     _cairo_box_round_to_rectangle (&polygon.extents, extents);
196     _cairo_polygon_fini (&polygon);
197
198     return status;
199 }
200
201 cairo_bool_t
202 _cairo_path_fixed_extents (const cairo_path_fixed_t *path,
203                            cairo_box_t *box)
204 {
205     *box = path->extents;
206     return path->has_extents;
207 }