[docs] NEWS.xml updated
[adg.git] / adg / adg-segment.c
blobf6e13740ef598ee710ad410d2e2647dd527f5a64
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,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.
20 /**
21 * SECTION:segment
22 * @title: AdgSegment
23 * @short_description: A GObject based wrapper for the #CpmlSegment struct
25 * The AdgSegment is a wrapper typedef in #GType syntax of the
26 * #CpmlSegment struct. Furthermore, some dynamic memory functions
27 * are provided, such as shallow and deep duplication, as the CPML
28 * library does not have dynamic memory APIs.
29 **/
31 #include "adg-segment.h"
33 #include <string.h>
36 GType
37 adg_segment_get_type(void)
39 static int segment_type = 0;
41 if (G_UNLIKELY(segment_type == 0))
42 segment_type = g_boxed_type_register_static("AdgSegment",
43 (GBoxedCopyFunc) adg_segment_dup,
44 g_free);
46 return segment_type;
49 /**
50 * adg_segment_dup:
51 * @segment: an #AdgSegment structure
53 * Duplicates @segment. This function makes a shallow duplication,
54 * that is the internal pointers of the resulting segment struct
55 * refer to the same memory as the original @segment. Check out
56 * adg_segment_deep_dup() if it is required also the content
57 * duplication.
59 * Return value: a shallow duplicate of @segment: must be freed
60 * with g_free() when no longer needed.
61 **/
62 AdgSegment *
63 adg_segment_dup(const AdgSegment *segment)
65 return g_memdup(segment, sizeof(AdgSegment));
68 /**
69 * adg_segment_deep_dup:
70 * @segment: an #AdgSegment structure
72 * Duplicates @segment. This function makes a deep duplication,
73 * that is it duplicates also the underlying data that defines
74 * the segment. The <structfield>path</structfield> field
75 * is set to %NULL as <structfield>data</structfield> is no
76 * more referring to the original cairo path.
78 * All the data is allocated in the same chunk of memory so freeing
79 * the returned pointer releases all the occupied memory.
81 * Return value: a deep duplicate of @segment: must be freed
82 * with g_free() when no longer needed.
83 **/
84 AdgSegment *
85 adg_segment_deep_dup(const AdgSegment *segment)
87 AdgSegment *dest;
88 gsize segment_size, data_size;
89 cairo_path_data_t *p_data;
91 segment_size = sizeof(AdgSegment);
92 data_size = segment->data == NULL ?
93 0 : sizeof(cairo_path_data_t) * segment->num_data;
94 dest = (AdgSegment *) g_malloc(segment_size + data_size);
95 p_data = (cairo_path_data_t *) ((gchar *) dest + segment_size);
97 dest->path = NULL;
99 if (data_size > 0) {
100 dest->data = memcpy(p_data, segment->data, data_size);
101 dest->num_data = segment->num_data;
102 } else {
103 dest->data = NULL;
104 dest->num_data = 0;
107 return dest;