/work/workdir/UnpackedTarball/cairo/src/cairo-rectangle.c
Line | Count | Source (jump to first uncovered line) |
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 © 2002 University of Southern California |
5 | | * Copyright © 2005 Red Hat, Inc. |
6 | | * Copyright © 2006 Red Hat, Inc. |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or |
9 | | * modify it either under the terms of the GNU Lesser General Public |
10 | | * License version 2.1 as published by the Free Software Foundation |
11 | | * (the "LGPL") or, at your option, under the terms of the Mozilla |
12 | | * Public License Version 1.1 (the "MPL"). If you do not alter this |
13 | | * notice, a recipient may use your version of this file under either |
14 | | * the MPL or the LGPL. |
15 | | * |
16 | | * You should have received a copy of the LGPL along with this library |
17 | | * in the file COPYING-LGPL-2.1; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA |
19 | | * You should have received a copy of the MPL along with this library |
20 | | * in the file COPYING-MPL-1.1 |
21 | | * |
22 | | * The contents of this file are subject to the Mozilla Public License |
23 | | * Version 1.1 (the "License"); you may not use this file except in |
24 | | * compliance with the License. You may obtain a copy of the License at |
25 | | * http://www.mozilla.org/MPL/ |
26 | | * |
27 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY |
28 | | * OF ANY KIND, either express or implied. See the LGPL or the MPL for |
29 | | * the specific language governing rights and limitations. |
30 | | * |
31 | | * The Original Code is the cairo graphics library. |
32 | | * |
33 | | * The Initial Developer of the Original Code is University of Southern |
34 | | * California. |
35 | | * |
36 | | * Contributor(s): |
37 | | * Carl D. Worth <cworth@cworth.org> |
38 | | */ |
39 | | |
40 | | #include "cairoint.h" |
41 | | |
42 | | #include "cairo-box-inline.h" |
43 | | |
44 | | const cairo_rectangle_int_t _cairo_empty_rectangle = { 0, 0, 0, 0 }; |
45 | | const cairo_rectangle_int_t _cairo_unbounded_rectangle = { |
46 | | CAIRO_RECT_INT_MIN, CAIRO_RECT_INT_MIN, |
47 | | CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN, |
48 | | CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN, |
49 | | }; |
50 | | |
51 | | cairo_private void |
52 | | _cairo_box_from_doubles (cairo_box_t *box, |
53 | | double *x1, double *y1, |
54 | | double *x2, double *y2) |
55 | 0 | { |
56 | 0 | box->p1.x = _cairo_fixed_from_double (*x1); |
57 | 0 | box->p1.y = _cairo_fixed_from_double (*y1); |
58 | 0 | box->p2.x = _cairo_fixed_from_double (*x2); |
59 | 0 | box->p2.y = _cairo_fixed_from_double (*y2); |
60 | 0 | } |
61 | | |
62 | | cairo_private void |
63 | | _cairo_box_to_doubles (const cairo_box_t *box, |
64 | | double *x1, double *y1, |
65 | | double *x2, double *y2) |
66 | 0 | { |
67 | 0 | *x1 = _cairo_fixed_to_double (box->p1.x); |
68 | 0 | *y1 = _cairo_fixed_to_double (box->p1.y); |
69 | 0 | *x2 = _cairo_fixed_to_double (box->p2.x); |
70 | 0 | *y2 = _cairo_fixed_to_double (box->p2.y); |
71 | 0 | } |
72 | | |
73 | | void |
74 | | _cairo_box_from_rectangle (cairo_box_t *box, |
75 | | const cairo_rectangle_int_t *rect) |
76 | 1.09k | { |
77 | 1.09k | box->p1.x = _cairo_fixed_from_int (rect->x); |
78 | 1.09k | box->p1.y = _cairo_fixed_from_int (rect->y); |
79 | 1.09k | box->p2.x = _cairo_fixed_from_int (rect->x + rect->width); |
80 | 1.09k | box->p2.y = _cairo_fixed_from_int (rect->y + rect->height); |
81 | 1.09k | } |
82 | | |
83 | | void |
84 | | _cairo_boxes_get_extents (const cairo_box_t *boxes, |
85 | | int num_boxes, |
86 | | cairo_box_t *extents) |
87 | 14.5k | { |
88 | 14.5k | assert (num_boxes > 0); |
89 | 14.5k | *extents = *boxes; |
90 | 14.5k | while (--num_boxes) |
91 | 0 | _cairo_box_add_box (extents, ++boxes); |
92 | 14.5k | } |
93 | | |
94 | | /* XXX We currently have a confusing mix of boxes and rectangles as |
95 | | * exemplified by this function. A #cairo_box_t is a rectangular area |
96 | | * represented by the coordinates of the upper left and lower right |
97 | | * corners, expressed in fixed point numbers. A #cairo_rectangle_int_t is |
98 | | * also a rectangular area, but represented by the upper left corner |
99 | | * and the width and the height, as integer numbers. |
100 | | * |
101 | | * This function converts a #cairo_box_t to a #cairo_rectangle_int_t by |
102 | | * increasing the area to the nearest integer coordinates. We should |
103 | | * standardize on #cairo_rectangle_fixed_t and #cairo_rectangle_int_t, and |
104 | | * this function could be renamed to the more reasonable |
105 | | * _cairo_rectangle_fixed_round. |
106 | | */ |
107 | | |
108 | | void |
109 | | _cairo_box_round_to_rectangle (const cairo_box_t *box, |
110 | | cairo_rectangle_int_t *rectangle) |
111 | 9.36M | { |
112 | 9.36M | rectangle->x = _cairo_fixed_integer_floor (box->p1.x); |
113 | 9.36M | rectangle->y = _cairo_fixed_integer_floor (box->p1.y); |
114 | 9.36M | rectangle->width = _cairo_fixed_integer_ceil (box->p2.x) - rectangle->x; |
115 | 9.36M | rectangle->height = _cairo_fixed_integer_ceil (box->p2.y) - rectangle->y; |
116 | 9.36M | } |
117 | | |
118 | | cairo_bool_t |
119 | | _cairo_rectangle_intersect (cairo_rectangle_int_t *dst, |
120 | | const cairo_rectangle_int_t *src) |
121 | 19.0M | { |
122 | 19.0M | int x1, y1, x2, y2; |
123 | | |
124 | 19.0M | x1 = MAX (dst->x, src->x); |
125 | 19.0M | y1 = MAX (dst->y, src->y); |
126 | | /* Beware the unsigned promotion, fortunately we have bits to spare |
127 | | * as (CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN) < UINT_MAX |
128 | | */ |
129 | 19.0M | x2 = MIN (dst->x + (int) dst->width, src->x + (int) src->width); |
130 | 19.0M | y2 = MIN (dst->y + (int) dst->height, src->y + (int) src->height); |
131 | | |
132 | 19.0M | if (x1 >= x2 || y1 >= y2) { |
133 | 7.27M | dst->x = 0; |
134 | 7.27M | dst->y = 0; |
135 | 7.27M | dst->width = 0; |
136 | 7.27M | dst->height = 0; |
137 | | |
138 | 7.27M | return FALSE; |
139 | 11.7M | } else { |
140 | 11.7M | dst->x = x1; |
141 | 11.7M | dst->y = y1; |
142 | 11.7M | dst->width = x2 - x1; |
143 | 11.7M | dst->height = y2 - y1; |
144 | | |
145 | 11.7M | return TRUE; |
146 | 11.7M | } |
147 | 19.0M | } |
148 | | |
149 | | /* Extends the dst rectangle to also contain src. |
150 | | * If one of the rectangles is empty, the result is undefined |
151 | | */ |
152 | | void |
153 | | _cairo_rectangle_union (cairo_rectangle_int_t *dst, |
154 | | const cairo_rectangle_int_t *src) |
155 | 0 | { |
156 | 0 | int x1, y1, x2, y2; |
157 | |
|
158 | 0 | x1 = MIN (dst->x, src->x); |
159 | 0 | y1 = MIN (dst->y, src->y); |
160 | | /* Beware the unsigned promotion, fortunately we have bits to spare |
161 | | * as (CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN) < UINT_MAX |
162 | | */ |
163 | 0 | x2 = MAX (dst->x + (int) dst->width, src->x + (int) src->width); |
164 | 0 | y2 = MAX (dst->y + (int) dst->height, src->y + (int) src->height); |
165 | |
|
166 | 0 | dst->x = x1; |
167 | 0 | dst->y = y1; |
168 | 0 | dst->width = x2 - x1; |
169 | 0 | dst->height = y2 - y1; |
170 | 0 | } |
171 | | |
172 | 0 | #define P1x (line->p1.x) |
173 | 0 | #define P1y (line->p1.y) |
174 | 0 | #define P2x (line->p2.x) |
175 | 0 | #define P2y (line->p2.y) |
176 | 0 | #define B1x (box->p1.x) |
177 | 0 | #define B1y (box->p1.y) |
178 | 0 | #define B2x (box->p2.x) |
179 | 0 | #define B2y (box->p2.y) |
180 | | |
181 | | /* |
182 | | * Check whether any part of line intersects box. This function essentially |
183 | | * computes whether the ray starting at line->p1 in the direction of line->p2 |
184 | | * intersects the box before it reaches p2. Normally, this is done |
185 | | * by dividing by the lengths of the line projected onto each axis. Because |
186 | | * we're in fixed point, this function does a bit more work to avoid having to |
187 | | * do the division -- we don't care about the actual intersection point, so |
188 | | * it's of no interest to us. |
189 | | */ |
190 | | |
191 | | cairo_bool_t |
192 | | _cairo_box_intersects_line_segment (const cairo_box_t *box, cairo_line_t *line) |
193 | 0 | { |
194 | 0 | cairo_fixed_t t1=0, t2=0, t3=0, t4=0; |
195 | 0 | cairo_int64_t t1y, t2y, t3x, t4x; |
196 | |
|
197 | 0 | cairo_fixed_t xlen, ylen; |
198 | |
|
199 | 0 | if (_cairo_box_contains_point (box, &line->p1) || |
200 | 0 | _cairo_box_contains_point (box, &line->p2)) |
201 | 0 | return TRUE; |
202 | | |
203 | 0 | xlen = P2x - P1x; |
204 | 0 | ylen = P2y - P1y; |
205 | |
|
206 | 0 | if (xlen) { |
207 | 0 | if (xlen > 0) { |
208 | 0 | t1 = B1x - P1x; |
209 | 0 | t2 = B2x - P1x; |
210 | 0 | } else { |
211 | 0 | t1 = P1x - B2x; |
212 | 0 | t2 = P1x - B1x; |
213 | 0 | xlen = - xlen; |
214 | 0 | } |
215 | |
|
216 | 0 | if ((t1 < 0 || t1 > xlen) && |
217 | 0 | (t2 < 0 || t2 > xlen)) |
218 | 0 | return FALSE; |
219 | 0 | } else { |
220 | | /* Fully vertical line -- check that X is in bounds */ |
221 | 0 | if (P1x < B1x || P1x > B2x) |
222 | 0 | return FALSE; |
223 | 0 | } |
224 | | |
225 | 0 | if (ylen) { |
226 | 0 | if (ylen > 0) { |
227 | 0 | t3 = B1y - P1y; |
228 | 0 | t4 = B2y - P1y; |
229 | 0 | } else { |
230 | 0 | t3 = P1y - B2y; |
231 | 0 | t4 = P1y - B1y; |
232 | 0 | ylen = - ylen; |
233 | 0 | } |
234 | |
|
235 | 0 | if ((t3 < 0 || t3 > ylen) && |
236 | 0 | (t4 < 0 || t4 > ylen)) |
237 | 0 | return FALSE; |
238 | 0 | } else { |
239 | | /* Fully horizontal line -- check Y */ |
240 | 0 | if (P1y < B1y || P1y > B2y) |
241 | 0 | return FALSE; |
242 | 0 | } |
243 | | |
244 | | /* If we had a horizontal or vertical line, then it's already been checked */ |
245 | 0 | if (P1x == P2x || P1y == P2y) |
246 | 0 | return TRUE; |
247 | | |
248 | | /* Check overlap. Note that t1 < t2 and t3 < t4 here. */ |
249 | 0 | t1y = _cairo_int32x32_64_mul (t1, ylen); |
250 | 0 | t2y = _cairo_int32x32_64_mul (t2, ylen); |
251 | 0 | t3x = _cairo_int32x32_64_mul (t3, xlen); |
252 | 0 | t4x = _cairo_int32x32_64_mul (t4, xlen); |
253 | |
|
254 | 0 | if (_cairo_int64_lt(t1y, t4x) && |
255 | 0 | _cairo_int64_lt(t3x, t2y)) |
256 | 0 | return TRUE; |
257 | | |
258 | 0 | return FALSE; |
259 | 0 | } |
260 | | |
261 | | static cairo_status_t |
262 | | _cairo_box_add_spline_point (void *closure, |
263 | | const cairo_point_t *point, |
264 | | const cairo_slope_t *tangent) |
265 | 16.8k | { |
266 | 16.8k | _cairo_box_add_point (closure, point); |
267 | | |
268 | 16.8k | return CAIRO_STATUS_SUCCESS; |
269 | 16.8k | } |
270 | | |
271 | | /* assumes a has been previously added */ |
272 | | void |
273 | | _cairo_box_add_curve_to (cairo_box_t *extents, |
274 | | const cairo_point_t *a, |
275 | | const cairo_point_t *b, |
276 | | const cairo_point_t *c, |
277 | | const cairo_point_t *d) |
278 | 67.3k | { |
279 | 67.3k | _cairo_box_add_point (extents, d); |
280 | 67.3k | if (!_cairo_box_contains_point (extents, b) || |
281 | 67.3k | !_cairo_box_contains_point (extents, c)) |
282 | 4.75k | { |
283 | 4.75k | cairo_status_t status; |
284 | | |
285 | 4.75k | status = _cairo_spline_bound (_cairo_box_add_spline_point, |
286 | 4.75k | extents, a, b, c, d); |
287 | 4.75k | assert (status == CAIRO_STATUS_SUCCESS); |
288 | 4.75k | } |
289 | 67.3k | } |
290 | | |
291 | | void |
292 | | _cairo_rectangle_int_from_double (cairo_rectangle_int_t *recti, |
293 | | const cairo_rectangle_t *rectf) |
294 | 0 | { |
295 | 0 | recti->x = floor (rectf->x); |
296 | 0 | recti->y = floor (rectf->y); |
297 | 0 | recti->width = ceil (rectf->x + rectf->width) - floor (rectf->x); |
298 | 0 | recti->height = ceil (rectf->y + rectf->height) - floor (rectf->y); |
299 | 0 | } |