[ADG] Moved include dependencies from .h to .c
[adg.git] / src / adg / adg-primitive.c
bloba03391219f50415956ce71f53970896731bde983
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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-primitive
23 * @Section_Id:AdgPrimitive
24 * @title: AdgPrimitive
25 * @short_description: A wrapper for #CpmlPrimitive
27 * AdgPrimitive is a wrapper in #GType syntax of the #CpmlPrimitive struct.
28 **/
30 /**
31 * AdgPrimitive:
33 * Another name for #CpmlPrimitive: check its documentation for the
34 * fields description and visibility details.
35 **/
38 #include "adg-internal.h"
39 #include <string.h>
42 GType
43 adg_primitive_get_type(void)
45 static int primitive_type = 0;
47 if (G_UNLIKELY(primitive_type == 0))
48 primitive_type = g_boxed_type_register_static("AdgPrimitive",
49 (GBoxedCopyFunc) adg_primitive_dup,
50 g_free);
52 return primitive_type;
55 /**
56 * adg_primitive_dup:
57 * @primitive: an #AdgPrimitive structure
59 * Duplicates @primitive. This function makes a shallow duplication of
60 * @primitives, that is the internal pointers of the resulting primitive
61 * struct refer to the same memory as the original @primitive. Check
62 * out adg_primitive_deep_dup() if it is required also the content
63 * duplication.
65 * Returns: a shallow duplicate of @primitive: must be freed
66 * with g_free() when no longer needed.
67 **/
68 AdgPrimitive *
69 adg_primitive_dup(const AdgPrimitive *primitive)
71 return g_memdup(primitive, sizeof(AdgPrimitive));
74 /**
75 * adg_primitive_deep_dup:
76 * @primitive: an #AdgPrimitive structure
78 * Duplicates @primitive. This function makes a deep duplication of
79 * @primitive, that is it duplicates also the definition data (both
80 * <structfield>org</structfield> and <structfield>data</structfield>).
82 * Furthermore, the new <structfield>segment</structfield> field will
83 * point to a fake duplicated segment with only its first primitive
84 * set (the first primitive of a segment should be a #CPML_MOVE).
85 * This is needed in order to let a #CPML_CLOSE work as expected.
87 * All the data is allocated in the same chunk of memory so freeing
88 * the returned pointer releases all the occupied memory.
90 * Returns: 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 const AdgPrimitive *src;
97 AdgPrimitive *dst;
98 gsize primitive_size, org_size, data_size, segment_size;
99 gchar *ptr;
101 src = primitive;
102 primitive_size = sizeof(AdgPrimitive);
104 if (src->org != NULL)
105 org_size = sizeof(cairo_path_data_t);
106 else
107 org_size = 0;
109 if (src->data != NULL)
110 data_size = sizeof(cairo_path_data_t) * src->data->header.length;
111 else
112 data_size = 0;
114 if (src->segment != NULL && src->segment->data != NULL)
115 segment_size = sizeof(CpmlSegment) +
116 sizeof(cairo_path_data_t) * src->segment->data[0].header.length;
117 else
118 segment_size = 0;
120 dst = g_malloc(primitive_size + org_size + data_size + segment_size);
121 ptr = (gchar *) dst + primitive_size;
123 if (org_size > 0) {
124 dst->org = memcpy(ptr, src->org, org_size);
125 ptr += org_size;
126 } else {
127 dst->org = NULL;
130 if (data_size > 0) {
131 dst->data = memcpy(ptr, src->data, data_size);
132 ptr += data_size;
133 } else {
134 dst->data = NULL;
137 if (segment_size > 0) {
138 dst->segment = memcpy(ptr, src->segment, sizeof(CpmlSegment));
139 ptr += sizeof(CpmlSegment);
140 dst->segment->data = memcpy(ptr, src->segment->data,
141 sizeof(cairo_path_data_t) *
142 src->segment->data[0].header.length);
143 } else {
144 dst->segment = NULL;
147 return dst;