233c2eb92e96beb0c3e5d39b39614d17f24ff4d3
[adg.git] / src / cpml / cpml-gobject.c
blob233c2eb92e96beb0c3e5d39b39614d17f24ff4d3
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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-gobject
23 * @Section_Id:GObject
24 * @title: GObject wrappers
25 * @short_description: Collection of #GBoxed wrappers for CPML structs.
27 * These wrappers are supposed to make bindings development easier.
28 * This file defines the wrappers and the functions needed for
29 * implementing a #GBoxed type, most notably the #GBoxedCopyFunc
30 * requested by g_boxed_type_register_static() (the #GBoxedFreeFunc
31 * will usually be g_free()).
33 * Since: 1.0
34 **/
37 #include "cpml-internal.h"
39 #include <glib-object.h>
40 #include <string.h>
42 #include "cpml-extents.h"
43 #include "cpml-segment.h"
44 #include "cpml-primitive.h"
46 #include "cpml-gobject.h"
49 GType
50 cpml_pair_get_type(void)
52 static GType pair_type = 0;
54 if (G_UNLIKELY(pair_type == 0))
55 pair_type = g_boxed_type_register_static("CpmlPair",
56 (GBoxedCopyFunc) cpml_pair_dup,
57 g_free);
59 return pair_type;
62 /**
63 * cpml_pair_dup:
64 * @pair: an #CpmlPair structure
66 * Duplicates @pair.
68 * Returns: (transfer full): the duplicate of @pair: must be freed with g_free() when no longer needed.
70 * Since: 1.0
71 **/
72 CpmlPair *
73 cpml_pair_dup(const CpmlPair *pair)
75 /* g_memdup() returns NULL if pair is NULL */
76 return g_memdup(pair, sizeof(CpmlPair));
80 GType
81 cpml_primitive_get_type(void)
83 static GType primitive_type = 0;
85 if (G_UNLIKELY(primitive_type == 0))
86 primitive_type = g_boxed_type_register_static("CpmlPrimitive",
87 (GBoxedCopyFunc) cpml_primitive_dup,
88 g_free);
90 return primitive_type;
93 /**
94 * cpml_primitive_dup:
95 * @primitive: an #CpmlPrimitive structure
97 * Duplicates @primitive. This function makes a shallow duplication of
98 * @primitives, that is the internal pointers of the resulting primitive
99 * struct refer to the same memory as the original @primitive. Check
100 * out cpml_primitive_deep_dup() if it is required also the content
101 * duplication.
103 * Returns: (transfer full): a shallow duplicate of @primitive: must be
104 * freed with g_free() when no longer needed.
106 * Since: 1.0
108 CpmlPrimitive *
109 cpml_primitive_dup(const CpmlPrimitive *primitive)
111 return g_memdup(primitive, sizeof(CpmlPrimitive));
115 * cpml_primitive_deep_dup:
116 * @primitive: an #CpmlPrimitive structure
118 * Duplicates @primitive. This function makes a deep duplication of
119 * @primitive, that is it duplicates also the definition data (both
120 * <structfield>org</structfield> and <structfield>data</structfield>).
122 * Furthermore, the new <structfield>segment</structfield> field will
123 * point to a fake duplicated segment with only its first primitive
124 * set (the first primitive of a segment should be a #CPML_MOVE).
125 * This is needed in order to let a #CPML_CLOSE work as expected.
127 * All the data is allocated in the same chunk of memory so freeing
128 * the returned pointer releases all the occupied memory.
130 * Returns: (transfer full): a deep duplicate of @primitive: must be
131 * freed with g_free() when no longer needed
133 * Since: 1.0
135 CpmlPrimitive *
136 cpml_primitive_deep_dup(const CpmlPrimitive *primitive)
138 const CpmlPrimitive *src;
139 CpmlPrimitive *dst;
140 gsize primitive_size, org_size, data_size, segment_size;
141 gchar *ptr;
143 src = primitive;
144 primitive_size = sizeof(CpmlPrimitive);
146 if (src->org != NULL)
147 org_size = sizeof(cairo_path_data_t);
148 else
149 org_size = 0;
151 if (src->data != NULL)
152 data_size = sizeof(cairo_path_data_t) * src->data->header.length;
153 else
154 data_size = 0;
156 if (src->segment != NULL && src->segment->data != NULL)
157 segment_size = sizeof(CpmlSegment) +
158 sizeof(cairo_path_data_t) * src->segment->data[0].header.length;
159 else
160 segment_size = 0;
162 dst = g_malloc(primitive_size + org_size + data_size + segment_size);
163 ptr = (gchar *) dst + primitive_size;
165 if (org_size > 0) {
166 dst->org = memcpy(ptr, src->org, org_size);
167 ptr += org_size;
168 } else {
169 dst->org = NULL;
172 if (data_size > 0) {
173 dst->data = memcpy(ptr, src->data, data_size);
174 ptr += data_size;
175 } else {
176 dst->data = NULL;
179 if (segment_size > 0) {
180 dst->segment = memcpy(ptr, src->segment, sizeof(CpmlSegment));
181 ptr += sizeof(CpmlSegment);
182 dst->segment->data = memcpy(ptr, src->segment->data,
183 sizeof(cairo_path_data_t) *
184 src->segment->data[0].header.length);
185 } else {
186 dst->segment = NULL;
189 return dst;
193 GType
194 cpml_segment_get_type(void)
196 static GType segment_type = 0;
198 if (G_UNLIKELY(segment_type == 0))
199 segment_type = g_boxed_type_register_static("CpmlSegment",
200 (GBoxedCopyFunc) cpml_segment_dup,
201 g_free);
203 return segment_type;
207 * cpml_segment_dup:
208 * @segment: an #CpmlSegment structure
210 * Duplicates @segment. This function makes a shallow duplication,
211 * that is the internal pointers of the resulting segment struct
212 * refer to the same memory as the original @segment. Check out
213 * cpml_segment_deep_dup() if it is required also the content
214 * duplication.
216 * Returns: (transfer full): a shallow duplicate of @segment: must be freed with g_free() when no longer needed.
218 * Since: 1.0
220 CpmlSegment *
221 cpml_segment_dup(const CpmlSegment *segment)
223 return g_memdup(segment, sizeof(CpmlSegment));
227 * cpml_segment_deep_dup:
228 * @segment: an #CpmlSegment structure
230 * Duplicates @segment. This function makes a deep duplication,
231 * that is it duplicates also the underlying data that defines
232 * the segment. The <structfield>path</structfield> field
233 * is set to %NULL as <structfield>data</structfield> is no
234 * more referring to the original cairo path.
236 * All the data is allocated in the same chunk of memory so freeing
237 * the returned pointer releases all the occupied memory.
239 * Returns: (transfer full): a deep duplicate of @segment: must be freed with g_free() when no longer needed.
241 * Since: 1.0
243 CpmlSegment *
244 cpml_segment_deep_dup(const CpmlSegment *segment)
246 CpmlSegment *dest;
247 int num_data;
248 gsize segment_size, data_size;
249 cairo_path_data_t *p_data;
251 g_return_val_if_fail(segment != NULL, NULL);
253 num_data = segment->num_data;
254 segment_size = sizeof(CpmlSegment);
255 data_size = segment->data ? sizeof(cairo_path_data_t) * num_data : 0;
256 dest = (CpmlSegment *) g_malloc(segment_size + data_size);
257 p_data = (cairo_path_data_t *) ((gchar *) dest + segment_size);
259 dest->path = NULL;
261 if (data_size > 0) {
262 dest->data = memcpy(p_data, segment->data, data_size);
263 dest->num_data = num_data;
264 } else {
265 dest->data = NULL;
266 dest->num_data = 0;
269 return dest;
273 * cpml_segment_deep_copy:
274 * @segment: an #CpmlSegment structure
275 * @src: the source segment to copy
277 * Makes a deep copy of @src to @segment. For a shallow copy, check out
278 * the cpml_segment_copy() API provided by the CPML library.
280 * This could seem a somewhat unusual operation because @segment should
281 * be "compatible" with @src: it is expected that they have the same
282 * <structfield>num_data</structfield> value. Anyway, it is convenient
283 * in some situation, such as when restoring the original data from a
284 * deep duplicated source:
286 * |[
287 * CpmlSegment *backup;
289 * backup = cpml_segment_deep_dup(&segment);
290 * // Now &segment can be modified
291 * ...
292 * cpml_segment_deep_copy(&segment, backup);
293 * g_free(backup);
294 * ]|
296 * The struct fields of @segment are left untouched and used only to
297 * check if it is compatible with @src.
299 * Since: 1.0
301 void
302 cpml_segment_deep_copy(CpmlSegment *segment, const CpmlSegment *src)
304 g_return_if_fail(segment != NULL);
305 g_return_if_fail(src != NULL);
306 g_return_if_fail(segment->num_data == src->num_data);
308 if (src->num_data > 0) {
309 size_t n = sizeof(cairo_path_data_t) * segment->num_data;
310 memcpy(segment->data, src->data, n);