[AdgArrowStyle] Hidden private struct
[adg.git] / adg / adg-primitive.c
blob5ff155e4a6d8170b360217bf4c79fe3ba169e5b0
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.
21 /**
22 * SECTION:primitive
23 * @title: AdgPrimitive
24 * @short_description: A GObject based wrapper for the #CpmlPrimitive struct
26 * The AdgPrimitive is a wrapper typedef in #GType syntax of the
27 * #CpmlPrimitive struct. Furthermore, some dynamic memory functions
28 * are provided, such as shallow and deep duplication, as the CPML
29 * library does not have dynamic memory APIs.
30 **/
32 /**
33 * AdgPrimitive:
35 * Another name for the #CpmlPrimitive type: check its documentation
36 * for fields description.
37 **/
40 #include "adg-primitive.h"
42 #include <string.h>
45 GType
46 adg_primitive_get_type(void)
48 static int primitive_type = 0;
50 if (G_UNLIKELY(primitive_type == 0))
51 primitive_type = g_boxed_type_register_static("AdgPrimitive",
52 (GBoxedCopyFunc) adg_primitive_dup,
53 g_free);
55 return primitive_type;
58 /**
59 * adg_primitive_dup:
60 * @primitive: an #AdgPrimitive structure
62 * Duplicates @primitive. This function makes a shallow duplication of
63 * @primitives, that is the internal pointers of the resulting primitive
64 * struct refer to the same memory as the original @primitive. Check
65 * out adg_primitive_deep_dup() if it is required also the content
66 * duplication.
68 * Return value: a shallow duplicate of @primitive: must be freed
69 * with g_free() when no longer needed.
70 **/
71 AdgPrimitive *
72 adg_primitive_dup(const AdgPrimitive *primitive)
74 return g_memdup(primitive, sizeof(AdgPrimitive));
77 /**
78 * adg_primitive_deep_dup:
79 * @primitive: an #AdgPrimitive structure
81 * Duplicates @primitive. This function makes a deep duplication of
82 * @primitive, that is it duplicates also the definition data (both
83 * <structfield>org</structfield> and <structfield>data</structfield>).
84 * The <structfield>segment</structfield> field instead is set to
85 * %NULL as the parent segment is not duplicated.
87 * All the data is allocated in the same chunk of memory so freeing
88 * the returned pointer releases all the occupied memory.
90 * Return value: a deep duplicate of @primitive: must be freed
91 * with g_free() when no longer needed.
92 **/
93 AdgPrimitive *
94 adg_primitive_deep_dup(const AdgPrimitive *primitive)
96 AdgPrimitive *dest;
97 gsize primitive_size, org_size, data_size;
98 cairo_path_data_t *p_data;
100 primitive_size = sizeof(AdgPrimitive);
101 org_size = primitive->org == NULL ? 0 : sizeof(cairo_path_data_t);
102 data_size = primitive->data == NULL ?
103 0 : sizeof(cairo_path_data_t) * primitive->data->header.length;
104 dest = (AdgPrimitive *) g_malloc(primitive_size + org_size + data_size);
105 p_data = (cairo_path_data_t *) ((gchar *) dest + primitive_size);
107 dest->segment = NULL;
109 if (primitive->org != NULL) {
110 dest->org = p_data;
111 memcpy(p_data, primitive->org, org_size);
112 ++ p_data;
113 } else {
114 dest->org = NULL;
117 if (data_size > 0)
118 dest->data = memcpy(p_data, primitive->data, data_size);
119 else
120 dest->data = NULL;
122 return dest;