Moved AdgPair to CpmlPair
[adg.git] / src / adg / adg-segment.c
blobd018abfe6f2bff855e554e1525c7b7f52a6c107c
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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:adg-segment
23 * @Section_Id:AdgSegment
24 * @title: AdgSegment
25 * @short_description: A wrapper for #CpmlSegment
27 * AdgSegment is a wrapper in #GType syntax of the #CpmlSegment struct.
28 * Furthermore, some dynamic memory functions are provided, such as
29 * shallow and deep duplication functions.
31 * Since: 1.0
32 **/
34 /**
35 * AdgSegment:
37 * Another name for #CpmlSegment: check its documentation for the
38 * fields description and visibility details.
40 * Since: 1.0
41 **/
44 #include "adg-internal.h"
45 #include "adg-segment.h"
46 #include <string.h>
49 GType
50 adg_segment_get_type(void)
52 static GType segment_type = 0;
54 if (G_UNLIKELY(segment_type == 0))
55 segment_type = g_boxed_type_register_static("AdgSegment",
56 (GBoxedCopyFunc) adg_segment_dup,
57 g_free);
59 return segment_type;
62 /**
63 * adg_segment_dup:
64 * @segment: an #AdgSegment structure
66 * Duplicates @segment. This function makes a shallow duplication,
67 * that is the internal pointers of the resulting segment struct
68 * refer to the same memory as the original @segment. Check out
69 * adg_segment_deep_dup() if it is required also the content
70 * duplication.
72 * Returns: (transfer full): a shallow duplicate of @segment: must be freed with g_free() when no longer needed.
74 * Since: 1.0
75 **/
76 AdgSegment *
77 adg_segment_dup(const AdgSegment *segment)
79 return g_memdup(segment, sizeof(AdgSegment));
82 /**
83 * adg_segment_deep_dup:
84 * @segment: an #AdgSegment structure
86 * Duplicates @segment. This function makes a deep duplication,
87 * that is it duplicates also the underlying data that defines
88 * the segment. The <structfield>path</structfield> field
89 * is set to %NULL as <structfield>data</structfield> is no
90 * more referring to the original cairo path.
92 * All the data is allocated in the same chunk of memory so freeing
93 * the returned pointer releases all the occupied memory.
95 * Returns: (transfer full): a deep duplicate of @segment: must be freed with g_free() when no longer needed.
97 * Since: 1.0
98 **/
99 AdgSegment *
100 adg_segment_deep_dup(const AdgSegment *segment)
102 AdgSegment *dest;
103 int num_data;
104 gsize segment_size, data_size;
105 cairo_path_data_t *p_data;
107 g_return_val_if_fail(segment != NULL, NULL);
109 num_data = segment->num_data;
110 segment_size = sizeof(AdgSegment);
111 data_size = segment->data ? sizeof(cairo_path_data_t) * num_data : 0;
112 dest = (AdgSegment *) g_malloc(segment_size + data_size);
113 p_data = (cairo_path_data_t *) ((gchar *) dest + segment_size);
115 dest->path = NULL;
117 if (data_size > 0) {
118 dest->data = memcpy(p_data, segment->data, data_size);
119 dest->num_data = num_data;
120 } else {
121 dest->data = NULL;
122 dest->num_data = 0;
125 return dest;
129 * adg_segment_deep_copy:
130 * @segment: an #AdgSegment structure
131 * @src: the source segment to copy
133 * Makes a deep copy of @src to @segment. For a shallow copy, check out
134 * the cpml_segment_copy() API provided by the CPML library.
136 * This could seem a somewhat unusual operation because @segment should
137 * be "compatible" with @src: it is expected that they have the same
138 * <structfield>num_data</structfield> value. Anyway, it is convenient
139 * in some situation, such as when restoring the original data from a
140 * deep duplicated source:
142 * |[
143 * AdgSegment *backup;
145 * backup = adg_segment_deep_dup(&segment);
146 * // Now &segment can be modified
147 * ...
148 * adg_segment_deep_copy(&segment, backup);
149 * g_free(backup);
150 * ]|
152 * The struct fields of @segment are left untouched and used only to
153 * check if it is compatible with @src.
155 * Since: 1.0
157 void
158 adg_segment_deep_copy(AdgSegment *segment, const AdgSegment *src)
160 g_return_if_fail(segment != NULL);
161 g_return_if_fail(src != NULL);
162 g_return_if_fail(segment->num_data == src->num_data);
164 if (src->num_data > 0) {
165 size_t n = sizeof(cairo_path_data_t) * segment->num_data;
166 memcpy(segment->data, src->data, n);