fix build for --disable-gtk-doc
[swfdec.git] / swfdec / swfdec_rect.c
blobc6365c17f6e187dd630e8079f586baafbd47a824
1 /* Swfdec
2 * Copyright (C) 2003-2006 David Schleef <ds@schleef.org>
3 * 2005-2006 Eric Anholt <eric@anholt.net>
4 * 2006 Benjamin Otte <otte@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <math.h>
27 #include "swfdec_rect.h"
29 void
30 swfdec_rect_init_empty (SwfdecRect *rect)
32 g_return_if_fail (rect != NULL);
34 rect->x0 = 0;
35 rect->y0 = 0;
36 rect->x1 = 0;
37 rect->y1 = 0;
40 /**
41 * swfdec_rect_round:
42 * @dest: pointer to rect that will take the result
43 * @src: #SwfdecRect that should be rounded
45 * Puts the smallest rectangle in @dest that includes @src but only
46 * contains integer numbers
47 **/
48 void
49 swfdec_rect_round (SwfdecRect *dest, SwfdecRect *src)
51 g_return_if_fail (dest != NULL);
52 g_return_if_fail (src != NULL);
54 if (swfdec_rect_is_empty (src)) {
55 swfdec_rect_init_empty (dest);
56 return;
58 dest->x0 = floor (src->x0);
59 dest->y0 = floor (src->y0);
60 dest->x1 = ceil (src->x1);
61 dest->y1 = ceil (src->y1);
64 gboolean
65 swfdec_rect_intersect (SwfdecRect * dest, const SwfdecRect * a, const SwfdecRect * b)
67 SwfdecRect tmp;
69 g_return_val_if_fail (a != NULL, FALSE);
70 g_return_val_if_fail (b != NULL, FALSE);
71 if (dest == NULL)
72 dest = &tmp;
74 dest->x0 = MAX (a->x0, b->x0);
75 dest->y0 = MAX (a->y0, b->y0);
76 dest->x1 = MIN (a->x1, b->x1);
77 dest->y1 = MIN (a->y1, b->y1);
79 return !swfdec_rect_is_empty (dest);
82 /**
83 * swfdec_rect_union:
84 * @dest: destination rectangle
85 * @a: first source rectangle, may be emtpy
86 * @b: second source rectangle, may be empty
88 * Stores the union of @a and @b into @dest. The union is the smallest
89 * rectangle that includes both source rectangles. @a, @b and @dest may point
90 * to the same rectangle.
91 **/
92 void
93 swfdec_rect_union (SwfdecRect * dest, const SwfdecRect * a, const SwfdecRect * b)
95 g_return_if_fail (dest != NULL);
96 g_return_if_fail (a != NULL);
97 g_return_if_fail (b != NULL);
99 if (swfdec_rect_is_empty (a)) {
100 *dest = *b;
101 } else if (swfdec_rect_is_empty (b)) {
102 *dest = *a;
103 } else {
104 dest->x0 = MIN (a->x0, b->x0);
105 dest->y0 = MIN (a->y0, b->y0);
106 dest->x1 = MAX (a->x1, b->x1);
107 dest->y1 = MAX (a->y1, b->y1);
111 void
112 swfdec_rect_subtract (SwfdecRect *dest, const SwfdecRect *a, const SwfdecRect *b)
114 g_return_if_fail (dest != NULL);
115 g_return_if_fail (a != NULL);
116 g_return_if_fail (b != NULL);
118 /* FIXME: improve this */
119 if (swfdec_rect_is_empty (a)) {
120 swfdec_rect_init_empty (dest);
121 } else if (swfdec_rect_is_empty (b)) {
122 *dest = *a;
123 } else if (b->x0 <= a->x0 && b->x1 >= a->x1 &&
124 b->y0 <= a->y0 && b->y1 >= a->y1) {
125 swfdec_rect_init_empty (dest);
126 } else {
127 *dest = *a;
131 void
132 swfdec_rect_scale (SwfdecRect *dest, const SwfdecRect *src, double factor)
134 g_return_if_fail (dest != NULL);
135 g_return_if_fail (src != NULL);
137 dest->x0 = src->x0 * factor;
138 dest->x1 = src->x1 * factor;
139 dest->y0 = src->y0 * factor;
140 dest->y1 = src->y1 * factor;
143 gboolean
144 swfdec_rect_is_empty (const SwfdecRect * a)
146 return (a->x1 <= a->x0) || (a->y1 <= a->y0);
149 gboolean
150 swfdec_rect_contains (const SwfdecRect *rect, double x, double y)
152 return x >= rect->x0 &&
153 x < rect->x1 &&
154 y >= rect->y0 &&
155 y < rect->y1;
159 * swfdec_rect_transform:
160 * @dest: destination rectangle
161 * @src: source rectangle
162 * @matrix: matrix to apply to source
164 * Computes a rectangle that completely encloses the area that results from
165 * applying @matrix to @src.
167 void
168 swfdec_rect_transform (SwfdecRect *dest, const SwfdecRect *src, const cairo_matrix_t *matrix)
170 SwfdecRect tmp, tmp2;
172 g_return_if_fail (dest != NULL);
173 g_return_if_fail (src != NULL);
174 g_return_if_fail (matrix != NULL);
176 tmp = *src;
177 tmp2 = *src;
178 cairo_matrix_transform_point (matrix, &tmp.x0, &tmp.y0);
179 cairo_matrix_transform_point (matrix, &tmp.x1, &tmp.y1);
180 cairo_matrix_transform_point (matrix, &tmp2.x0, &tmp2.y1);
181 cairo_matrix_transform_point (matrix, &tmp2.x1, &tmp2.y0);
183 dest->x0 = MIN (MIN (tmp.x0, tmp.x1), MIN (tmp2.x0, tmp2.x1));
184 dest->y0 = MIN (MIN (tmp.y0, tmp.y1), MIN (tmp2.y0, tmp2.y1));
185 dest->x1 = MAX (MAX (tmp.x0, tmp.x1), MAX (tmp2.x0, tmp2.x1));
186 dest->y1 = MAX (MAX (tmp.y0, tmp.y1), MAX (tmp2.y0, tmp2.y1));
190 * swfdec_rect_inside:
191 * @outer: the supposed outer rectangle
192 * @inner: the supposed inner rectangle
194 * Checks if @outer completely includes the rectangle specified by @inner.
195 * If both rectangles are empty, TRUE is returned.
197 * Returns: TRUE if @outer includes @inner, FALSE otherwise
199 gboolean
200 swfdec_rect_inside (const SwfdecRect *outer, const SwfdecRect *inner)
202 /* empty includes empty */
203 if (swfdec_rect_is_empty (inner))
204 return TRUE;
205 /* if outer is empty, below will return FALSE */
206 return outer->x0 <= inner->x0 &&
207 outer->y0 <= inner->y0 &&
208 outer->x1 >= inner->x1 &&
209 outer->y1 >= inner->y1;
212 void
213 swfdec_rectangle_init_rect (SwfdecRectangle *rectangle, const SwfdecRect *rect)
215 g_return_if_fail (rectangle != NULL);
216 g_return_if_fail (rect != NULL);
218 rectangle->x = floor (rect->x0);
219 rectangle->y = floor (rect->y0);
220 rectangle->width = ceil (rect->x1) - rectangle->x;
221 rectangle->height = ceil (rect->y1) - rectangle->y;