s/2008,2009/2008,2009,2010/
[adg.git] / cpml / cpml-extents.c
blobc7e41dda870466624e26b60bb1bfabdc4e701075
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008,2009,2010 Nicola Fontana <ntd at entidi.it>
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 of the License, or (at your option) any later version.
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
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:cpml-extents
23 * @Section_Id:CpmlExtents
24 * @title: CpmlExtents
25 * @short_description: A rectangular area representing a bounding box
27 * The #CpmlExtents struct groups two pairs representing the rectangular
28 * area of a bounding box.
29 **/
31 /**
32 * CpmlExtents:
33 * @is_defined: set to %0 when these extents are undefined
34 * @org: the lowest x,y coordinates
35 * @size: the width (x) and height (y) of the extents
37 * A structure defining a bounding box area. These APIs expect the
38 * size of the extents to be always positives, so be careful while
39 * directly accessing the @size field.
40 **/
43 #include "cpml-internal.h"
44 #include "cpml-extents.h"
45 #include <string.h>
46 #include <math.h>
49 /**
50 * cpml_extents_copy:
51 * @extents: the destination #CpmlExtents
52 * @src: the source #CpmlExtents
54 * Copies @src in @extents.
56 * Returns: @extents
57 **/
58 CpmlExtents *
59 cpml_extents_copy(CpmlExtents *extents, const CpmlExtents *src)
61 return memcpy(extents, src, sizeof(CpmlExtents));
64 /**
65 * cpml_extents_from_cairo_text:
66 * @extents: the destination #CpmlExtents
67 * @cairo_extents: the source cairo_text_extents_t struct
69 * Converts @cairo_extents in a #CpmlExtents format and stores the
70 * result in @extents.
72 * Returns: @extents
73 **/
74 CpmlExtents *
75 cpml_extents_from_cairo_text(CpmlExtents *extents,
76 const cairo_text_extents_t *cairo_extents)
78 extents->is_defined = 1;
79 extents->org.x = cairo_extents->x_bearing;
80 extents->org.y = cairo_extents->y_bearing;
81 extents->size.x = cairo_extents->width;
82 extents->size.y = cairo_extents->height;
84 return extents;
87 /**
88 * cpml_extents_add:
89 * @extents: the destination #CpmlExtents
90 * @src: the extents to add
92 * Merges @extents and @src and store the result in @extents.
93 **/
94 void
95 cpml_extents_add(CpmlExtents *extents, const CpmlExtents *src)
97 CpmlPair pair;
99 if (src->is_defined == 0)
100 return;
102 pair.x = src->org.x + src->size.x;
103 pair.y = src->org.y + src->size.y;
105 cpml_extents_pair_add(extents, &src->org);
106 cpml_extents_pair_add(extents, &pair);
110 * cpml_extents_pair_add:
111 * @extents: the destination #CpmlExtents
112 * @src: the #AdgPair to add
114 * Extends @extents, if required, to include @src. If @extents is
115 * undefined, the origin of @extents is set to @src and its size
116 * will be (0,0).
118 void
119 cpml_extents_pair_add(CpmlExtents *extents, const CpmlPair *src)
121 if (extents->is_defined == 0) {
122 extents->is_defined = 1;
123 cpml_pair_copy(&extents->org, src);
124 extents->size.x = 0;
125 extents->size.y = 0;
126 return;
129 if (src->x < extents->org.x) {
130 extents->size.x += extents->org.x - src->x;
131 extents->org.x = src->x;
132 } else if (src->x > extents->org.x + extents->size.x) {
133 extents->size.x = src->x - extents->org.x;
136 if (src->y < extents->org.y) {
137 extents->size.y += extents->org.y - src->y;
138 extents->org.y = src->y;
139 } else if (src->y > extents->org.y + extents->size.y) {
140 extents->size.y = src->y - extents->org.y;
145 * cpml_extents_is_inside:
146 * @extents: the container #CpmlExtents
147 * @src: the subject #CpmlExtents
149 * Checks wheter @src is enterely contained by @extents. If @extents
150 * is undefined, %0 will be returned. If @src is undefined, %1 will
151 * be returned. The border of @extents is considered inside.
153 * Returns: %1 if @src is totally inside @extents, %0 otherwise
155 cairo_bool_t
156 cpml_extents_is_inside(const CpmlExtents *extents, const CpmlExtents *src)
158 CpmlPair pe, ps;
160 if (extents->is_defined == 0)
161 return 0;
163 if (src->is_defined == 0)
164 return 1;
166 cpml_pair_copy(&pe, &extents->org);
167 cpml_pair_copy(&ps, &src->org);
169 if (ps.x < pe.x || ps.y < pe.y)
170 return 0;
172 pe.x += extents->size.x;
173 pe.y += extents->size.y;
174 ps.x += extents->size.x;
175 ps.y += extents->size.y;
177 if (ps.x > pe.x || ps.y > pe.y)
178 return 0;
180 return 1;
184 * cpml_extents_pair_is_inside:
185 * @extents: the container #CpmlExtents
186 * @src: the subject #CpmlPair
188 * Checks wheter @src is inside @extents. If @extents is undefined,
189 * %0 will be returned. The border of @extents is considered inside.
191 * Returns: %1 if @src is inside @extents, %0 otherwise
193 cairo_bool_t
194 cpml_extents_pair_is_inside(const CpmlExtents *extents, const CpmlPair *src)
196 if (extents->is_defined == 0)
197 return 0;
199 if (src->x < extents->org.x || src->y < extents->org.y ||
200 src->x > extents->org.x + extents->size.x ||
201 src->y > extents->org.y + extents->size.y)
202 return 0;
204 return 1;
208 * cpml_extents_transform:
209 * @extents: the container #CpmlExtents
210 * @matrix: the transformation matrix
212 * Shortcut to apply a specific transformation matrix to @extents.
213 * It basically convert the <structfield>org</structfield> field
214 * with cairo_matrix_transform_point() and <structfield>size</structfield>
215 * with cairo_matrix_transform_distance().
217 void
218 cpml_extents_transform(CpmlExtents *extents, const cairo_matrix_t *matrix)
220 if (extents->is_defined == 0)
221 return;
223 cairo_matrix_transform_point(matrix, &extents->org.x, &extents->org.y);
224 cairo_matrix_transform_distance(matrix, &extents->size.x, &extents->size.y);