docs: updated copyright to 2012
[adg.git] / src / adg / adg-primitive.c
blobc6ba876d962d33fca05149d67f9d02756b3de1a2
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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.
29 * Since: 1.0
30 **/
32 /**
33 * AdgPrimitive:
35 * Another name for #CpmlPrimitive: check its documentation for the
36 * fields description and visibility details.
38 * Since: 1.0
39 **/
42 #include "adg-internal.h"
43 #include <string.h>
46 GType
47 adg_primitive_get_type(void)
49 static int primitive_type = 0;
51 if (G_UNLIKELY(primitive_type == 0))
52 primitive_type = g_boxed_type_register_static("AdgPrimitive",
53 (GBoxedCopyFunc) adg_primitive_dup,
54 g_free);
56 return primitive_type;
59 /**
60 * adg_primitive_dup:
61 * @primitive: an #AdgPrimitive structure
63 * Duplicates @primitive. This function makes a shallow duplication of
64 * @primitives, that is the internal pointers of the resulting primitive
65 * struct refer to the same memory as the original @primitive. Check
66 * out adg_primitive_deep_dup() if it is required also the content
67 * duplication.
69 * Returns: (transfer full): a shallow duplicate of @primitive: must be
70 * freed with g_free() when no longer needed.
72 * Since: 1.0
73 **/
74 AdgPrimitive *
75 adg_primitive_dup(const AdgPrimitive *primitive)
77 return g_memdup(primitive, sizeof(AdgPrimitive));
80 /**
81 * adg_primitive_deep_dup:
82 * @primitive: an #AdgPrimitive structure
84 * Duplicates @primitive. This function makes a deep duplication of
85 * @primitive, that is it duplicates also the definition data (both
86 * <structfield>org</structfield> and <structfield>data</structfield>).
88 * Furthermore, the new <structfield>segment</structfield> field will
89 * point to a fake duplicated segment with only its first primitive
90 * set (the first primitive of a segment should be a #CPML_MOVE).
91 * This is needed in order to let a #CPML_CLOSE work as expected.
93 * All the data is allocated in the same chunk of memory so freeing
94 * the returned pointer releases all the occupied memory.
96 * Returns: (transfer full): a deep duplicate of @primitive: must be
97 * freed with g_free() when no longer needed
99 * Since: 1.0
101 AdgPrimitive *
102 adg_primitive_deep_dup(const AdgPrimitive *primitive)
104 const AdgPrimitive *src;
105 AdgPrimitive *dst;
106 gsize primitive_size, org_size, data_size, segment_size;
107 gchar *ptr;
109 src = primitive;
110 primitive_size = sizeof(AdgPrimitive);
112 if (src->org != NULL)
113 org_size = sizeof(cairo_path_data_t);
114 else
115 org_size = 0;
117 if (src->data != NULL)
118 data_size = sizeof(cairo_path_data_t) * src->data->header.length;
119 else
120 data_size = 0;
122 if (src->segment != NULL && src->segment->data != NULL)
123 segment_size = sizeof(CpmlSegment) +
124 sizeof(cairo_path_data_t) * src->segment->data[0].header.length;
125 else
126 segment_size = 0;
128 dst = g_malloc(primitive_size + org_size + data_size + segment_size);
129 ptr = (gchar *) dst + primitive_size;
131 if (org_size > 0) {
132 dst->org = memcpy(ptr, src->org, org_size);
133 ptr += org_size;
134 } else {
135 dst->org = NULL;
138 if (data_size > 0) {
139 dst->data = memcpy(ptr, src->data, data_size);
140 ptr += data_size;
141 } else {
142 dst->data = NULL;
145 if (segment_size > 0) {
146 dst->segment = memcpy(ptr, src->segment, sizeof(CpmlSegment));
147 ptr += sizeof(CpmlSegment);
148 dst->segment->data = memcpy(ptr, src->segment->data,
149 sizeof(cairo_path_data_t) *
150 src->segment->data[0].header.length);
151 } else {
152 dst->segment = NULL;
155 return dst;