add blend mode tests
[swfdec.git] / swfdec / swfdec_rectangle.c
blob622f15afad08720aa50301020cc0f6b4417e9011
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_rectangle.h"
26 /**
27 * SECTION:SwfdecRectangle
28 * @title: SwfdecRectangle
29 * @short_description: handling regions on the screen
31 * This section describes how regions are handled in Swfdec. Regions are
32 * important when tracking which parts of the screen have been invalidated and
33 * need to be repainted. See SwfdecPlayer::invalidate for an example.
36 /**
37 * SwfdecRectangle:
38 * @x: x coordinate of top-left point
39 * @y: y coordinate of top-left point
40 * @width: width of rectangle or 0 for empty
41 * @height: height of rectangle or 0 for empty
43 * This structure represents a rectangular region. It is identical to the
44 * #GdkRectangle structure, so you can cast freely between them. The only
45 * difference is that Gdk does not allow empty rectangles, while Swfdec does.
46 * You can use swfdec_rectangle_is_empty() to check for this.
49 static gpointer
50 swfdec_rectangle_copy (gpointer src)
52 return g_memdup (src, sizeof (SwfdecRectangle));
55 GType
56 swfdec_rectangle_get_type (void)
58 static GType type = 0;
60 if (!type)
61 type = g_boxed_type_register_static ("SwfdecRectangle",
62 swfdec_rectangle_copy, g_free);
64 return type;
67 /**
68 * swfdec_rectangle_init_empty:
69 * @rectangle: rectangle to initialize
71 * Initializes the rectangle as empty.
72 **/
73 void
74 swfdec_rectangle_init_empty (SwfdecRectangle *rectangle)
76 rectangle->x = rectangle->y = rectangle->width = rectangle->height = 0;
79 /**
80 * swfdec_rectangle_is_empty:
81 * @rectangle: rectangle to check
83 * Checks if the given @rectangle is empty.
85 * Returns: %TRUE if the rectangle is emtpy
86 **/
87 gboolean
88 swfdec_rectangle_is_empty (const SwfdecRectangle *rectangle)
90 g_return_val_if_fail (rectangle != NULL, FALSE);
92 return rectangle->width <= 0 || rectangle->height <= 0;
95 /**
96 * swfdec_rectangle_intersect:
97 * @dest: the rectangle to take the result or %NULL
98 * @a: first rectangle to intersect
99 * @b: second rectangle to intersect
101 * Intersects the rectangles @a and @b and puts the result into @dest. It is
102 * allowed if @dest is the same as @a or @b.
104 * Returns: %TRUE if the intersection is not empty.
106 gboolean
107 swfdec_rectangle_intersect (SwfdecRectangle *dest, const SwfdecRectangle *a,
108 const SwfdecRectangle *b)
110 SwfdecRectangle tmp;
112 g_return_val_if_fail (a != NULL, FALSE);
113 g_return_val_if_fail (b != NULL, FALSE);
115 tmp.x = MAX (a->x, b->x);
116 tmp.y = MAX (a->y, b->y);
117 tmp.width = MIN (a->x + a->width, b->x + b->width) - tmp.x;
118 tmp.height = MIN (a->y + a->height, b->y + b->height) - tmp.y;
120 if (tmp.width <= 0 && tmp.height <= 0) {
121 if (dest)
122 dest->x = dest->y = dest->width = dest->height = 0;
123 return FALSE;
126 if (dest != NULL)
127 *dest = tmp;
128 return TRUE;
132 * swfdec_rectangle_union:
133 * @dest: destination to take the union
134 * @a: first rectangle to union
135 * @b: second rectangle to union
137 * Computes the smallest rectangle that contains both @a and @b and puts it in
138 * @dest.
140 void
141 swfdec_rectangle_union (SwfdecRectangle *dest, const SwfdecRectangle *a,
142 const SwfdecRectangle *b)
144 int x, y;
146 g_return_if_fail (dest != NULL);
147 g_return_if_fail (a != NULL);
148 g_return_if_fail (b != NULL);
150 if (swfdec_rectangle_is_empty (a)) {
151 *dest = *b;
152 return;
153 } else if (swfdec_rectangle_is_empty (b)) {
154 *dest = *a;
155 return;
157 x = MIN (a->x, b->x);
158 y = MIN (a->y, b->y);
159 dest->width = MAX (a->x + a->width, b->x + b->width) - x;
160 dest->height = MAX (a->y + a->height, b->y + b->height) - y;
161 dest->x = x;
162 dest->y = y;
166 * swfdec_rectangle_contains:
167 * @container: the supposedly bigger rectangle
168 * @content: the supposedly smaller rectangle
170 * Checks if @container contains the whole rectangle @content.
172 * Returns: %TRUE if @container contains @content.
174 gboolean
175 swfdec_rectangle_contains (const SwfdecRectangle *container, const SwfdecRectangle *content)
177 g_return_val_if_fail (container != NULL, FALSE);
178 g_return_val_if_fail (content != NULL, FALSE);
180 return container->x <= content->x &&
181 container->y <= content->y &&
182 container->x + container->width >= content->x + content->width &&
183 container->y + container->height >= content->y + content->height;
187 * swfdec_rectangle_contains_point:
188 * @rectangle: a rectangle
189 * @x: x coordinate of point to check
190 * @y: y coordinate of point to check
192 * Checks if the given point is inside the given rectangle.
194 * Returns: %TRUE if the point is inside the rectangle
196 gboolean
197 swfdec_rectangle_contains_point (const SwfdecRectangle *rectangle, int x, int y)
199 g_return_val_if_fail (rectangle != NULL, FALSE);
201 return rectangle->x <= x && rectangle->y <= y &&
202 rectangle->x + rectangle->width > x && rectangle->y + rectangle->height > y;