From 57548e3eddb3ba1017593657c89a30196086a07c Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Fri, 3 Jul 2009 23:06:34 +0200 Subject: [PATCH] [AdgPrimitive] Added GObject type for CpmlPrimitive Wrapper for CpmlPrimtitive that adds also some dynamic memory methods, as the CPML library does not dynamic allocation (the "manipulation" golden rule). --- adg/Makefile.am | 2 + adg/adg-primitive.c | 116 +++++++++++++++++++++++++++++++++++++++++ adg/{adg.h => adg-primitive.h} | 48 ++++++++--------- adg/adg.h | 1 + 4 files changed, 142 insertions(+), 25 deletions(-) create mode 100644 adg/adg-primitive.c copy adg/{adg.h => adg-primitive.h} (57%) diff --git a/adg/Makefile.am b/adg/Makefile.am index cd1e1923..ecde55b1 100644 --- a/adg/Makefile.am +++ b/adg/Makefile.am @@ -23,6 +23,7 @@ adg_h_sources= adg.h \ adg-path.h \ adg-pattern.h \ adg-point.h \ + adg-primitive.h \ adg-rotable.h \ adg-stroke.h \ adg-style.h \ @@ -71,6 +72,7 @@ adg_c_sources= adg-adim.c \ adg-path.c \ adg-pattern.c \ adg-point.c \ + adg-primitive.c \ adg-rotable.c \ adg-stroke.c \ adg-style.c \ diff --git a/adg/adg-primitive.c b/adg/adg-primitive.c new file mode 100644 index 00000000..989c2a82 --- /dev/null +++ b/adg/adg-primitive.c @@ -0,0 +1,116 @@ +/* ADG - Automatic Drawing Generation + * Copyright (C) 2007,2008,2009 Nicola Fontana + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +/** + * SECTION:primitive + * @title: AdgPrimitive + * @short_description: A GObject based wrapper for the #CpmlPrimitive struct + * + * The AdgPrimitive is a wrapper typedef in #GType syntax of the + * #CpmlPrimitive struct. Furthermore, some dynamic memory functions + * are provided, such as shallow and deep duplication, as the CPML + * library does not have dynamic memory APIs. + **/ + +#include "adg-primitive.h" + +#include + + +GType +adg_primitive_get_type(void) +{ + static int primitive_type = 0; + + if (G_UNLIKELY(primitive_type == 0)) + primitive_type = g_boxed_type_register_static("AdgPrimitive", + (GBoxedCopyFunc) adg_primitive_dup, + g_free); + + return primitive_type; +} + +/** + * adg_primitive_dup: + * @primitive: an #AdgPrimitive structure + * + * Duplicates @primitive. This function makes a shallow duplication of + * @primitives, that is the internal pointers of the resulting primitive + * struct refer to the same memory as the original @primitive. Check + * out adg_primitive_deep_dup() if it is required also the content + * duplication. + * + * Return value: a shallow duplicate of @primitive: must be freed + * with g_free() when no longer needed. + **/ +AdgPrimitive * +adg_primitive_dup(const AdgPrimitive *primitive) +{ + return g_memdup(primitive, sizeof(AdgPrimitive)); +} + +/** + * adg_primitive_deep_dup: + * @primitive: an #AdgPrimitive structure + * + * Duplicates @primitive. This function makes a deep duplication of + * @primitive, that is it duplicates also the definition data (both + * org and data). + * The segment field instead is set to + * %NULL as the parent segment is not duplicated. + * + * All the data is allocated in the same chunk of memory so freeing + * the returned pointer releases all the occupied memory. + * + * Return value: a deep duplicate of @primitive: must be freed + * with g_free() when no longer needed. + **/ +AdgPrimitive * +adg_primitive_deep_dup(const AdgPrimitive *primitive) +{ + AdgPrimitive *dest; + gsize primitive_size, org_size, data_size; + cairo_path_data_t *p_data; + + primitive_size = sizeof(AdgPrimitive); + org_size = primitive->org == NULL ? 0 : sizeof(cairo_path_data_t); + data_size = primitive->data == NULL ? + 0 : sizeof(cairo_path_data_t) * primitive->data->header.length; + dest = (AdgPrimitive *) g_malloc(primitive_size + org_size + data_size); + p_data = (cairo_path_data_t *) ((gchar *) dest + primitive_size); + + dest->segment = NULL; + + if (primitive->org != NULL) { + dest->org = p_data; + memcpy(p_data, primitive->org, org_size); + ++ p_data; + } else { + dest->org = NULL; + } + + if (primitive->data != NULL) { + dest->data = p_data; + memcpy(p_data, primitive->data, data_size); + } else { + dest->data = NULL; + } + + return dest; +} diff --git a/adg/adg.h b/adg/adg-primitive.h similarity index 57% copy from adg/adg.h copy to adg/adg-primitive.h index 96739ad5..d8cf33d2 100644 --- a/adg/adg.h +++ b/adg/adg-primitive.h @@ -18,28 +18,26 @@ */ -#ifndef __ADG_H__ -#define __ADG_H__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#endif /* __ADG_H__ */ +#ifndef __ADG_PRIMITIVE_H__ +#define __ADG_PRIMITIVE_H__ + +#include +#include + + +G_BEGIN_DECLS + +#define ADG_TYPE_PRIMITIVE (adg_primitive_get_type()) + + +typedef CpmlPrimitive AdgPrimitive; + + +GType adg_primitive_get_type (void) G_GNUC_CONST; +AdgPrimitive * adg_primitive_dup (const AdgPrimitive *primitive); +AdgPrimitive * adg_primitive_deep_dup (const AdgPrimitive *primitive); + +G_END_DECLS + + +#endif /* __ADG_PRIMITIVE_H__ */ diff --git a/adg/adg.h b/adg/adg.h index 96739ad5..a347fdc9 100644 --- a/adg/adg.h +++ b/adg/adg.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include -- 2.11.4.GIT