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.
22 * SECTION:cpml-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.
35 #include "cpml-internal.h"
37 #include <glib-object.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"
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
,
63 * @pair: a #CpmlPair structure
67 * Returns: (transfer full): the duplicate of @pair: must be freed with g_free() when no longer needed.
72 cpml_pair_dup(const CpmlPair
*pair
)
74 /* g_memdup() returns NULL if pair is NULL */
75 return g_memdup(pair
, sizeof(CpmlPair
));
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
,
89 return primitive_type
;
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
102 * Returns: (transfer full): a shallow duplicate of @primitive: must be
103 * freed with g_free() when no longer needed.
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
135 cpml_primitive_deep_dup(const CpmlPrimitive
*primitive
)
137 const CpmlPrimitive
*src
;
139 gsize primitive_size
, org_size
, data_size
, segment_size
;
142 g_return_val_if_fail(primitive
!= NULL
, NULL
);
145 primitive_size
= sizeof(CpmlPrimitive
);
147 if (src
->org
!= NULL
)
148 org_size
= sizeof(cairo_path_data_t
);
152 if (src
->data
!= NULL
)
153 data_size
= sizeof(cairo_path_data_t
) * src
->data
->header
.length
;
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
;
163 dst
= g_malloc(primitive_size
+ org_size
+ data_size
+ segment_size
);
164 ptr
= (gchar
*) dst
+ primitive_size
;
167 dst
->org
= memcpy(ptr
, src
->org
, org_size
);
174 dst
->data
= memcpy(ptr
, src
->data
, data_size
);
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
);
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
,
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
217 * Returns: (transfer full): a shallow duplicate of @segment: must be freed with g_free() when no longer needed.
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.
246 cpml_segment_deep_dup(const CpmlSegment
*segment
)
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
);
264 dest
->data
= memcpy(p_data
, segment
->data
, data_size
);
265 dest
->num_data
= num_data
;
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" },
287 etype
= g_enum_register_static("CpmlCurveOffsetAlgorithm", values
);
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" },
307 etype
= g_enum_register_static("CpmlPrimitiveType", values
);