[CPML] Minor docs correction in extents management
[adg.git] / src / cpml / cpml-extents.c
blob746577166c196610b1437409516dbb5d281cda43
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008,2009,2010,2011 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_equal:
89 * @extents: the first extents to compare
90 * @src: the second extents to compare
92 * Compares @extents to @src and returns 1 if the extents are equals.
93 * Two %NULL or two undefined extents are considered equal, athough
94 * %NULL extents are not equal to undefined extents.
96 * Returns: 1 if @extents is equal to @src, 0 otherwise
97 **/
98 cairo_bool_t
99 cpml_extents_equal(const CpmlExtents *extents, const CpmlExtents *src)
101 if (extents == NULL && src == NULL)
102 return 1;
104 if (extents == NULL || src == NULL)
105 return 0;
107 if (!extents->is_defined && !src->is_defined)
108 return 1;
110 return extents->is_defined == src->is_defined &&
111 cpml_pair_equal(&extents->org, &src->org) &&
112 cpml_pair_equal(&extents->size, &src->size) ? 1 : 0;
116 * cpml_extents_add:
117 * @extents: the destination #CpmlExtents
118 * @src: the extents to add
120 * Merges @extents and @src and store the result in @extents.
122 void
123 cpml_extents_add(CpmlExtents *extents, const CpmlExtents *src)
125 CpmlPair pair;
127 if (src->is_defined == 0)
128 return;
130 pair.x = src->org.x + src->size.x;
131 pair.y = src->org.y + src->size.y;
133 cpml_extents_pair_add(extents, &src->org);
134 cpml_extents_pair_add(extents, &pair);
138 * cpml_extents_pair_add:
139 * @extents: the destination #CpmlExtents
140 * @src: the #AdgPair to add
142 * Extends @extents, if required, to include @src. If @extents is
143 * undefined, the origin of @extents is set to @src and its size
144 * will be (0,0).
146 void
147 cpml_extents_pair_add(CpmlExtents *extents, const CpmlPair *src)
149 if (extents->is_defined == 0) {
150 extents->is_defined = 1;
151 cpml_pair_copy(&extents->org, src);
152 extents->size.x = 0;
153 extents->size.y = 0;
154 return;
157 if (src->x < extents->org.x) {
158 extents->size.x += extents->org.x - src->x;
159 extents->org.x = src->x;
160 } else if (src->x > extents->org.x + extents->size.x) {
161 extents->size.x = src->x - extents->org.x;
164 if (src->y < extents->org.y) {
165 extents->size.y += extents->org.y - src->y;
166 extents->org.y = src->y;
167 } else if (src->y > extents->org.y + extents->size.y) {
168 extents->size.y = src->y - extents->org.y;
173 * cpml_extents_is_inside:
174 * @extents: the container #CpmlExtents
175 * @src: the subject #CpmlExtents
177 * Checks wheter @src is enterely contained by @extents. If @extents
178 * is undefined, %0 will be returned. If @src is undefined, %1 will
179 * be returned. The border of @extents is considered inside.
181 * Returns: %1 if @src is totally inside @extents, %0 otherwise
183 cairo_bool_t
184 cpml_extents_is_inside(const CpmlExtents *extents, const CpmlExtents *src)
186 CpmlPair pe, ps;
188 if (extents->is_defined == 0)
189 return 0;
191 if (src->is_defined == 0)
192 return 1;
194 cpml_pair_copy(&pe, &extents->org);
195 cpml_pair_copy(&ps, &src->org);
197 if (ps.x < pe.x || ps.y < pe.y)
198 return 0;
200 pe.x += extents->size.x;
201 pe.y += extents->size.y;
202 ps.x += extents->size.x;
203 ps.y += extents->size.y;
205 if (ps.x > pe.x || ps.y > pe.y)
206 return 0;
208 return 1;
212 * cpml_extents_pair_is_inside:
213 * @extents: the container #CpmlExtents
214 * @src: the subject #CpmlPair
216 * Checks wheter @src is inside @extents. If @extents is undefined,
217 * %0 will be returned. The border of @extents is considered inside.
219 * Returns: %1 if @src is inside @extents, %0 otherwise
221 cairo_bool_t
222 cpml_extents_pair_is_inside(const CpmlExtents *extents, const CpmlPair *src)
224 if (extents->is_defined == 0)
225 return 0;
227 if (src->x < extents->org.x || src->y < extents->org.y ||
228 src->x > extents->org.x + extents->size.x ||
229 src->y > extents->org.y + extents->size.y)
230 return 0;
232 return 1;
236 * cpml_extents_transform:
237 * @extents: the container #CpmlExtents
238 * @matrix: the transformation matrix
240 * Shortcut to apply a specific transformation matrix to @extents.
241 * It basically convert the <structfield>org</structfield> field
242 * with cairo_matrix_transform_point() and <structfield>size</structfield>
243 * with cairo_matrix_transform_distance().
245 void
246 cpml_extents_transform(CpmlExtents *extents, const cairo_matrix_t *matrix)
248 if (extents->is_defined == 0)
249 return;
251 cairo_matrix_transform_point(matrix, &extents->org.x, &extents->org.y);
252 cairo_matrix_transform_distance(matrix, &extents->size.x, &extents->size.y);