s/2010 N/2010,2011 N/
[adg.git] / src / cpml / cpml-extents.c
blob1fad2c3a6b2c632ee1f23f62fa3ed7f218b872bb
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 undefined extents are considered equal.
95 * Returns: 1 if @extents is equal to @src, 0 otherwise
96 **/
97 cairo_bool_t
98 cpml_extents_equal(const CpmlExtents *extents, const CpmlExtents *src)
100 if (extents == NULL && src == NULL)
101 return 1;
103 if (extents == NULL || src == NULL)
104 return 0;
106 if (!extents->is_defined && !src->is_defined)
107 return 1;
109 return extents->is_defined == src->is_defined &&
110 cpml_pair_equal(&extents->org, &src->org) &&
111 cpml_pair_equal(&extents->size, &src->size) ? 1 : 0;
115 * cpml_extents_add:
116 * @extents: the destination #CpmlExtents
117 * @src: the extents to add
119 * Merges @extents and @src and store the result in @extents.
121 void
122 cpml_extents_add(CpmlExtents *extents, const CpmlExtents *src)
124 CpmlPair pair;
126 if (src->is_defined == 0)
127 return;
129 pair.x = src->org.x + src->size.x;
130 pair.y = src->org.y + src->size.y;
132 cpml_extents_pair_add(extents, &src->org);
133 cpml_extents_pair_add(extents, &pair);
137 * cpml_extents_pair_add:
138 * @extents: the destination #CpmlExtents
139 * @src: the #AdgPair to add
141 * Extends @extents, if required, to include @src. If @extents is
142 * undefined, the origin of @extents is set to @src and its size
143 * will be (0,0).
145 void
146 cpml_extents_pair_add(CpmlExtents *extents, const CpmlPair *src)
148 if (extents->is_defined == 0) {
149 extents->is_defined = 1;
150 cpml_pair_copy(&extents->org, src);
151 extents->size.x = 0;
152 extents->size.y = 0;
153 return;
156 if (src->x < extents->org.x) {
157 extents->size.x += extents->org.x - src->x;
158 extents->org.x = src->x;
159 } else if (src->x > extents->org.x + extents->size.x) {
160 extents->size.x = src->x - extents->org.x;
163 if (src->y < extents->org.y) {
164 extents->size.y += extents->org.y - src->y;
165 extents->org.y = src->y;
166 } else if (src->y > extents->org.y + extents->size.y) {
167 extents->size.y = src->y - extents->org.y;
172 * cpml_extents_is_inside:
173 * @extents: the container #CpmlExtents
174 * @src: the subject #CpmlExtents
176 * Checks wheter @src is enterely contained by @extents. If @extents
177 * is undefined, %0 will be returned. If @src is undefined, %1 will
178 * be returned. The border of @extents is considered inside.
180 * Returns: %1 if @src is totally inside @extents, %0 otherwise
182 cairo_bool_t
183 cpml_extents_is_inside(const CpmlExtents *extents, const CpmlExtents *src)
185 CpmlPair pe, ps;
187 if (extents->is_defined == 0)
188 return 0;
190 if (src->is_defined == 0)
191 return 1;
193 cpml_pair_copy(&pe, &extents->org);
194 cpml_pair_copy(&ps, &src->org);
196 if (ps.x < pe.x || ps.y < pe.y)
197 return 0;
199 pe.x += extents->size.x;
200 pe.y += extents->size.y;
201 ps.x += extents->size.x;
202 ps.y += extents->size.y;
204 if (ps.x > pe.x || ps.y > pe.y)
205 return 0;
207 return 1;
211 * cpml_extents_pair_is_inside:
212 * @extents: the container #CpmlExtents
213 * @src: the subject #CpmlPair
215 * Checks wheter @src is inside @extents. If @extents is undefined,
216 * %0 will be returned. The border of @extents is considered inside.
218 * Returns: %1 if @src is inside @extents, %0 otherwise
220 cairo_bool_t
221 cpml_extents_pair_is_inside(const CpmlExtents *extents, const CpmlPair *src)
223 if (extents->is_defined == 0)
224 return 0;
226 if (src->x < extents->org.x || src->y < extents->org.y ||
227 src->x > extents->org.x + extents->size.x ||
228 src->y > extents->org.y + extents->size.y)
229 return 0;
231 return 1;
235 * cpml_extents_transform:
236 * @extents: the container #CpmlExtents
237 * @matrix: the transformation matrix
239 * Shortcut to apply a specific transformation matrix to @extents.
240 * It basically convert the <structfield>org</structfield> field
241 * with cairo_matrix_transform_point() and <structfield>size</structfield>
242 * with cairo_matrix_transform_distance().
244 void
245 cpml_extents_transform(CpmlExtents *extents, const cairo_matrix_t *matrix)
247 if (extents->is_defined == 0)
248 return;
250 cairo_matrix_transform_point(matrix, &extents->org.x, &extents->org.y);
251 cairo_matrix_transform_distance(matrix, &extents->size.x, &extents->size.y);