s/2008,2009/2008,2009,2010/
[adg.git] / adg / adg-primitive.c
blobe80405486109260674ef5afbd4f2d30d0171d821
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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 "adg-primitive.h"
40 #include <string.h>
43 GType
44 adg_primitive_get_type(void)
46 static int primitive_type = 0;
48 if (G_UNLIKELY(primitive_type == 0))
49 primitive_type = g_boxed_type_register_static("AdgPrimitive",
50 (GBoxedCopyFunc) adg_primitive_dup,
51 g_free);
53 return primitive_type;
56 /**
57 * adg_primitive_dup:
58 * @primitive: an #AdgPrimitive structure
60 * Duplicates @primitive. This function makes a shallow duplication of
61 * @primitives, that is the internal pointers of the resulting primitive
62 * struct refer to the same memory as the original @primitive. Check
63 * out adg_primitive_deep_dup() if it is required also the content
64 * duplication.
66 * Returns: a shallow duplicate of @primitive: must be freed
67 * with g_free() when no longer needed.
68 **/
69 AdgPrimitive *
70 adg_primitive_dup(const AdgPrimitive *primitive)
72 return g_memdup(primitive, sizeof(AdgPrimitive));
75 /**
76 * adg_primitive_deep_dup:
77 * @primitive: an #AdgPrimitive structure
79 * Duplicates @primitive. This function makes a deep duplication of
80 * @primitive, that is it duplicates also the definition data (both
81 * <structfield>org</structfield> and <structfield>data</structfield>).
83 * Furthermore, the new <structfield>segment</structfield> field will
84 * point to a fake duplicated segment with only its first primitive
85 * set (the first primitive of a segment should be a #CPML_MOVE).
86 * This is needed in order to let a #CPML_CLOSE work as expected.
88 * All the data is allocated in the same chunk of memory so freeing
89 * the returned pointer releases all the occupied memory.
91 * Returns: a deep duplicate of @primitive: must be freed
92 * with g_free() when no longer needed.
93 **/
94 AdgPrimitive *
95 adg_primitive_deep_dup(const AdgPrimitive *primitive)
97 const AdgPrimitive *src;
98 AdgPrimitive *dst;
99 gsize primitive_size, org_size, data_size, segment_size;
100 gchar *ptr;
102 src = primitive;
103 primitive_size = sizeof(AdgPrimitive);
105 if (src->org != NULL)
106 org_size = sizeof(cairo_path_data_t);
107 else
108 org_size = 0;
110 if (src->data != NULL)
111 data_size = sizeof(cairo_path_data_t) * src->data->header.length;
112 else
113 data_size = 0;
115 if (src->segment != NULL && src->segment->data != NULL)
116 segment_size = sizeof(CpmlSegment) +
117 sizeof(cairo_path_data_t) * src->segment->data[0].header.length;
118 else
119 segment_size = 0;
121 dst = g_malloc(primitive_size + org_size + data_size + segment_size);
122 ptr = (gchar *) dst + primitive_size;
124 if (org_size > 0) {
125 dst->org = memcpy(ptr, src->org, org_size);
126 ptr += org_size;
127 } else {
128 dst->org = NULL;
131 if (data_size > 0) {
132 dst->data = memcpy(ptr, src->data, data_size);
133 ptr += data_size;
134 } else {
135 dst->data = NULL;
138 if (segment_size > 0) {
139 dst->segment = memcpy(ptr, src->segment, sizeof(CpmlSegment));
140 ptr += sizeof(CpmlSegment);
141 dst->segment->data = memcpy(ptr, src->segment->data,
142 sizeof(cairo_path_data_t) *
143 src->segment->data[0].header.length);
144 } else {
145 dst->segment = NULL;
148 return dst;