tests: check adg_canvas_export()
[adg.git] / src / cpml / cpml-gobject.c
blob1ba30eb20c85b61639dcb27df3057ac8cca9e555
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2007-2015 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 boxed 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 the CPML primitives as #GBoxed type.
31 * Since: 1.0
32 **/
35 #include "cpml-internal.h"
37 #include <glib-object.h>
38 #include <string.h>
40 #include "cpml-extents.h"
41 #include "cpml-segment.h"
42 #include "cpml-primitive.h"
43 #include "cpml-curve.h"
45 #include "cpml-gobject.h"
48 GType
49 cpml_pair_get_type(void)
51 static GType pair_type = 0;
53 if (G_UNLIKELY(pair_type == 0))
54 pair_type = g_boxed_type_register_static("CpmlPair",
55 (GBoxedCopyFunc) cpml_pair_dup,
56 g_free);
58 return pair_type;
61 /**
62 * cpml_pair_dup:
63 * @pair: a #CpmlPair structure
65 * Duplicates @pair.
67 * Returns: (transfer full): the duplicate of @pair: must be freed with g_free() when no longer needed.
69 * Since: 1.0
70 **/
71 CpmlPair *
72 cpml_pair_dup(const CpmlPair *pair)
74 /* g_memdup() returns NULL if pair is NULL */
75 return g_memdup(pair, sizeof(CpmlPair));
79 GType
80 cpml_primitive_get_type(void)
82 static GType primitive_type = 0;
84 if (G_UNLIKELY(primitive_type == 0))
85 primitive_type = g_boxed_type_register_static("CpmlPrimitive",
86 (GBoxedCopyFunc) cpml_primitive_dup,
87 g_free);
89 return primitive_type;
92 /**
93 * cpml_primitive_dup:
94 * @primitive: a #CpmlPrimitive structure
96 * Duplicates @primitive. This function makes a shallow duplication of
97 * @primitives, that is the internal pointers of the resulting primitive
98 * struct refer to the same memory as the original @primitive. Check
99 * out cpml_primitive_deep_dup() if it is required also the content
100 * duplication.
102 * Returns: (transfer full): a shallow duplicate of @primitive: must be
103 * freed with g_free() when no longer needed.
105 * Since: 1.0
107 CpmlPrimitive *
108 cpml_primitive_dup(const CpmlPrimitive *primitive)
110 return g_memdup(primitive, sizeof(CpmlPrimitive));
114 * cpml_primitive_deep_dup:
115 * @primitive: a #CpmlPrimitive structure
117 * Duplicates @primitive. This function makes a deep duplication of
118 * @primitive, that is it duplicates also the definition data (both
119 * <structfield>org</structfield> and <structfield>data</structfield>).
121 * Furthermore, the new <structfield>segment</structfield> field will
122 * point to a fake duplicated segment with only its first primitive
123 * set (the first primitive of a segment should be a #CPML_MOVE).
124 * This is needed in order to let a #CPML_CLOSE work as expected.
126 * All the data is allocated in the same chunk of memory so freeing
127 * the returned pointer releases all the occupied memory.
129 * Returns: (transfer full): a deep duplicate of @primitive: must be
130 * freed with g_free() when no longer needed
132 * Since: 1.0
134 CpmlPrimitive *
135 cpml_primitive_deep_dup(const CpmlPrimitive *primitive)
137 const CpmlPrimitive *src;
138 CpmlPrimitive *dst;
139 gsize primitive_size, org_size, data_size, segment_size;
140 gchar *ptr;
142 g_return_val_if_fail(primitive != NULL, NULL);
144 src = primitive;
145 primitive_size = sizeof(CpmlPrimitive);
147 if (src->org != NULL)
148 org_size = sizeof(cairo_path_data_t);
149 else
150 org_size = 0;
152 if (src->data != NULL)
153 data_size = sizeof(cairo_path_data_t) * src->data->header.length;
154 else
155 data_size = 0;
157 if (src->segment != NULL && src->segment->data != NULL)
158 segment_size = sizeof(CpmlSegment) +
159 sizeof(cairo_path_data_t) * src->segment->data[0].header.length;
160 else
161 segment_size = 0;
163 dst = g_malloc(primitive_size + org_size + data_size + segment_size);
164 ptr = (gchar *) dst + primitive_size;
166 if (org_size > 0) {
167 dst->org = memcpy(ptr, src->org, org_size);
168 ptr += org_size;
169 } else {
170 dst->org = NULL;
173 if (data_size > 0) {
174 dst->data = memcpy(ptr, src->data, data_size);
175 ptr += data_size;
176 } else {
177 dst->data = NULL;
180 if (segment_size > 0) {
181 dst->segment = memcpy(ptr, src->segment, sizeof(CpmlSegment));
182 ptr += sizeof(CpmlSegment);
183 dst->segment->data = memcpy(ptr, src->segment->data,
184 sizeof(cairo_path_data_t) *
185 src->segment->data[0].header.length);
186 } else {
187 dst->segment = NULL;
190 return dst;
194 GType
195 cpml_segment_get_type(void)
197 static GType segment_type = 0;
199 if (G_UNLIKELY(segment_type == 0))
200 segment_type = g_boxed_type_register_static("CpmlSegment",
201 (GBoxedCopyFunc) cpml_segment_dup,
202 g_free);
204 return segment_type;
208 * cpml_segment_dup:
209 * @segment: a #CpmlSegment structure
211 * Duplicates @segment. This function makes a shallow duplication,
212 * that is the internal pointers of the resulting segment struct
213 * refer to the same memory as the original @segment. Check out
214 * cpml_segment_deep_dup() if it is required also the content
215 * duplication.
217 * Returns: (transfer full): a shallow duplicate of @segment: must be freed with g_free() when no longer needed.
219 * Since: 1.0
221 CpmlSegment *
222 cpml_segment_dup(const CpmlSegment *segment)
224 return g_memdup(segment, sizeof(CpmlSegment));
228 * cpml_segment_deep_dup:
229 * @segment: a #CpmlSegment structure
231 * Duplicates @segment. This function makes a deep duplication,
232 * that is it duplicates also the underlying data that defines
233 * the segment. The <structfield>path</structfield> field
234 * is set to <constant>NULL</constant> as
235 * <structfield>data</structfield> is no more referring to the
236 * original cairo path.
238 * All the data is allocated in the same chunk of memory so freeing
239 * the returned pointer releases all the occupied memory.
241 * Returns: (transfer full): a deep duplicate of @segment: must be freed with g_free() when no longer needed.
243 * Since: 1.0
245 CpmlSegment *
246 cpml_segment_deep_dup(const CpmlSegment *segment)
248 CpmlSegment *dest;
249 int num_data;
250 gsize segment_size, data_size;
251 cairo_path_data_t *p_data;
253 g_return_val_if_fail(segment != NULL, NULL);
255 num_data = segment->num_data;
256 segment_size = sizeof(CpmlSegment);
257 data_size = segment->data ? sizeof(cairo_path_data_t) * num_data : 0;
258 dest = (CpmlSegment *) g_malloc(segment_size + data_size);
259 p_data = (cairo_path_data_t *) ((gchar *) dest + segment_size);
261 dest->path = NULL;
263 if (data_size > 0) {
264 dest->data = memcpy(p_data, segment->data, data_size);
265 dest->num_data = num_data;
266 } else {
267 dest->data = NULL;
268 dest->num_data = 0;
271 return dest;
274 GType
275 cpml_curve_offset_algorithm_get_type(void)
277 static GType etype = 0;
278 if (G_UNLIKELY(etype == 0)) {
279 static const GEnumValue values[] = {
280 { CPML_CURVE_OFFSET_ALGORITHM_NONE, "CPML_CURVE_OFFSET_ALGORITHM_NONE", "none" },
281 { CPML_CURVE_OFFSET_ALGORITHM_DEFAULT, "CPML_CURVE_OFFSET_ALGORITHM_DEFAULT", "default" },
282 { CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT, "CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT", "handcraft" },
283 { CPML_CURVE_OFFSET_ALGORITHM_BAIOCA, "CPML_CURVE_OFFSET_ALGORITHM_BAIOCA", "baioca" },
284 { 0, NULL, NULL }
287 etype = g_enum_register_static("CpmlCurveOffsetAlgorithm", values);
290 return etype;
293 GType
294 cpml_primitive_type_get_type(void)
296 static GType etype = 0;
297 if (G_UNLIKELY(etype == 0)) {
298 static const GEnumValue values[] = {
299 { CPML_MOVE, "CPML_MOVE", "move" },
300 { CPML_LINE, "CPML_LINE", "line" },
301 { CPML_ARC, "CPML_ARC", "arc" },
302 { CPML_CURVE, "CPML_CURVE", "curve" },
303 { CPML_CLOSE, "CPML_CLOSE", "close" },
304 { 0, NULL, NULL }
307 etype = g_enum_register_static("CpmlPrimitiveType", values);
310 return etype;