[AdgLDim] Using the new AdgEntity APIs
[adg.git] / cpml / cpml-extents.c
blobc9966b5a766e221b657a431b089a1272fe440488
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008, 2009 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: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 area of a
28 * bounding box. The <fieldname>org</fieldname> field holds the lowest
29 * (x, y) coordinates while <fieldname>size</fieldname> holds the x and
30 * y extents of the area. The additional <fieldname>is_defined</fieldname>
31 * boolean flag can be checked to know if #CpmlExtents has been computed
32 * (when equal to %1) or not (when <fieldname>is_defined</fieldname> is %0).
33 **/
35 /**
36 * CpmlExtents:
37 * @is_defined: %1 if these extents should be considered, %0 otherwise
38 * @org: the lowest x,y coordinates
39 * @size: the width (x) and height (y) of the extents
41 * A structure defining a bounding box area.
42 **/
45 #include "cpml-extents.h"
47 #include <stdlib.h>
48 #include <string.h>
49 #include <math.h>
52 /**
53 * cpml_extents_copy:
54 * @extents: the destination #CpmlExtents
55 * @src: the source #CpmlExtents
57 * Copies @src in @extents.
59 * Return value: @extents
60 **/
61 CpmlExtents *
62 cpml_extents_copy(CpmlExtents *extents, const CpmlExtents *src)
64 return memcpy(extents, src, sizeof(CpmlExtents));
67 /**
68 * cpml_extents_from_cairo_text:
69 * @extents: the destination #CpmlExtents
70 * @cairo_extents: the source cairo_text_extents_t struct
72 * Converts @cairo_extents in a #CpmlExtents format and stores the
73 * result in @extents.
75 * Returns: @extents
76 **/
77 CpmlExtents *
78 cpml_extents_from_cairo_text(CpmlExtents *extents,
79 const cairo_text_extents_t *cairo_extents)
81 extents->is_defined = 1;
82 extents->org.x = cairo_extents->x_bearing;
83 extents->org.y = cairo_extents->y_bearing;
84 extents->size.x = cairo_extents->width;
85 extents->size.y = cairo_extents->height;
87 return extents;
90 /**
91 * cpml_extents_add:
92 * @extents: the destination #CpmlExtents
93 * @src: the extents to add
95 * Merges @extents and @src and store the result in @extents.
96 **/
97 void
98 cpml_extents_add(CpmlExtents *extents, const CpmlExtents *src)
100 if (src->is_defined == 0)
101 return;
103 if (extents->is_defined == 0) {
104 cpml_extents_copy(extents, src);
105 return;
108 extents->is_defined = 1;
110 if (src->org.x < extents->org.x) {
111 extents->size.x += extents->org.x - src->org.x;
112 extents->org.x = src->org.x;
115 if (src->org.y < extents->org.y) {
116 extents->size.y += extents->org.y - src->org.y;
117 extents->org.y = src->org.y;
120 if (src->org.x + src->size.x > extents->org.x + extents->size.x)
121 extents->size.x = src->org.x + src->size.x - extents->org.x;
123 if (src->org.y + src->size.y > extents->org.y + extents->size.y)
124 extents->size.y = src->org.y + src->size.y - extents->org.y;