[lasem/mml-attrs: 1/5] [Mathml] Remove lsmdomattributes.[ch]



commit b37e7a9f3607152d83c88fd2208cb08912fbbac0
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Sun Nov 15 16:24:06 2009 +0100

    [Mathml] Remove lsmdomattributes.[ch]
    
    Move the content to lsmmathmlattributes.[ch]

 src/Makefile.am           |    2 -
 src/lsmdomattributes.c    |  527 ---------------------------------------------
 src/lsmdomattributes.h    |  166 --------------
 src/lsmmathmlattributes.c |  501 ++++++++++++++++++++++++++++++++++++++++++
 src/lsmmathmlattributes.h |  138 ++++++++++++-
 5 files changed, 638 insertions(+), 696 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8c97298..1f145d5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -31,7 +31,6 @@ liblasem_la_SOURCES =				\
 	lsmdomelement.c				\
 	lsmdomcharacterdata.c			\
 	lsmdomtext.c				\
-	lsmdomattributes.c			\
 	lsmdomview.c				\
 	lsmdomparser.c				\
 	lsmdomimplementation.c			\
@@ -114,7 +113,6 @@ LASEM_HDRS = \
 	lsmdomelement.h				\
 	lsmdomcharacterdata.h			\
 	lsmdomtext.h				\
-	lsmdomattributes.h			\
 	lsmdomview.h				\
 	lsmdomparser.h				\
 	lsmdomimplementation.h			\
diff --git a/src/lsmmathmlattributes.c b/src/lsmmathmlattributes.c
index 49a9b3a..8cfcdbc 100644
--- a/src/lsmmathmlattributes.c
+++ b/src/lsmmathmlattributes.c
@@ -21,6 +21,7 @@
 
 #include <lsmmathmlattributes.h>
 #include <lsmmathmlstyle.h>
+#include <lsmdebug.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -29,6 +30,506 @@
 #include <glib-object.h>
 #include <math.h>
 
+typedef struct {
+	ptrdiff_t bag_offset;
+	const LsmDomAttributeBagClass *bag_class;
+} LsmDomAttributeBagInfos;
+
+typedef struct {
+	ptrdiff_t attribute_offset;
+	const LsmDomAttributeClass *attribute_class;
+	LsmDomAttributeBagInfos *bag_infos;
+} LsmDomAttributeInfos;
+
+LsmDomAttributeMap *
+lsm_dom_attribute_map_new (void)
+{
+	LsmDomAttributeMap *map;
+
+	map = g_new0 (LsmDomAttributeMap, 1);
+	g_return_val_if_fail (map != NULL,  NULL);
+
+	map->attribute_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
+	map->bag_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
+
+	return map;
+}
+
+LsmDomAttributeMap *
+lsm_dom_attribute_map_duplicate (const LsmDomAttributeMap *from)
+{
+	LsmDomAttributeMap *map;
+	GHashTableIter iter;
+	void *key;
+	void *value;
+
+	map = lsm_dom_attribute_map_new ();
+	g_return_val_if_fail (map != NULL, NULL);
+
+	g_hash_table_iter_init (&iter, from->attribute_hash);
+	while (g_hash_table_iter_next (&iter, &key, &value))
+		g_hash_table_insert (map->attribute_hash, key, value);
+
+	g_hash_table_iter_init (&iter, from->bag_hash);
+	while (g_hash_table_iter_next (&iter, &key, &value))
+		g_hash_table_insert (map->bag_hash, key, value);
+
+	return map;
+}
+
+void
+lsm_dom_attribute_map_free (LsmDomAttributeMap *map)
+{
+	g_return_if_fail (map != NULL);
+
+	g_hash_table_unref (map->attribute_hash);
+	g_hash_table_unref (map->bag_hash);
+	g_free (map);
+}
+
+void
+lsm_dom_attribute_map_add_bag_attribute  (LsmDomAttributeMap *map,
+				       const char *name,
+				       ptrdiff_t attribute_offset,
+				       const LsmDomAttributeClass *attribute_class,
+				       ptrdiff_t bag_offset,
+				       const LsmDomAttributeBagClass *bag_class)
+{
+	LsmDomAttributeInfos *attribute_infos;
+	LsmDomAttributeBagInfos *bag_infos;
+
+	g_return_if_fail (map != NULL);
+	g_return_if_fail (name != NULL);
+	g_return_if_fail (attribute_offset >= 0);
+
+	if (g_hash_table_lookup (map->attribute_hash, name) != NULL) {
+		g_warning ("[LsmDomAttributeMap::add_attribute] %s already defined", name);
+		return;
+	}
+
+	attribute_infos = g_new (LsmDomAttributeInfos, 1);
+	attribute_infos->attribute_offset = attribute_offset;
+	attribute_infos->attribute_class = attribute_class;
+
+	g_hash_table_insert (map->attribute_hash, (char *) name, attribute_infos);
+
+	if (bag_class != NULL) {
+		bag_infos = g_hash_table_lookup (map->bag_hash, bag_class);
+		if (bag_infos == NULL) {
+			bag_infos = g_new (LsmDomAttributeBagInfos, 1);
+			bag_infos->bag_offset = bag_offset;
+			bag_infos->bag_class = bag_class;
+
+			g_hash_table_insert (map->bag_hash, (void *) bag_class, bag_infos);
+		}
+		attribute_infos->bag_infos = bag_infos;
+	} else
+		attribute_infos->bag_infos = NULL;
+
+}
+
+void
+lsm_dom_attribute_map_add_attribute_full (LsmDomAttributeMap *map,
+					  const char *name,
+					  ptrdiff_t offset,
+					  const LsmDomAttributeClass *attribute_class)
+{
+	lsm_dom_attribute_map_add_bag_attribute (map, name, offset, attribute_class, 0, NULL);
+}
+
+void
+lsm_dom_attribute_map_add_attribute_full_width_default (LsmDomAttributeMap *map,
+						     const char *name,
+						     ptrdiff_t offset,
+						     const LsmDomAttributeClass *attribute_class)
+{
+	lsm_dom_attribute_map_add_bag_attribute (map, name, offset, attribute_class, 0, NULL);
+}
+
+void
+lsm_dom_attribute_map_add_attribute (LsmDomAttributeMap *map,
+				  const char *name,
+				  ptrdiff_t offset)
+{
+	lsm_dom_attribute_map_add_bag_attribute (map, name, offset, NULL, 0, NULL);
+}
+
+static LsmDomAttribute *
+_get_attribute (LsmDomAttributeMap *map,
+		void *instance,
+		const char *name)
+{
+	LsmDomAttributeInfos *attribute_infos;
+	LsmDomAttribute *attribute;
+
+	attribute_infos = g_hash_table_lookup (map->attribute_hash, name);
+	if (attribute_infos == NULL) {
+		lsm_debug ("[LsmDomAttribute] Attribute not found (%s)", name);
+		return NULL;
+	}
+
+	if (attribute_infos->bag_infos == NULL)
+		attribute = (void *)(instance + attribute_infos->attribute_offset);
+	else {
+		LsmDomAttributeBag **bag_ptr;
+
+		bag_ptr = (void *)(instance + attribute_infos->bag_infos->bag_offset);
+		g_return_val_if_fail (bag_ptr != NULL, FALSE);
+
+		if (*bag_ptr == NULL)
+			*bag_ptr = attribute_infos->bag_infos->bag_class->init ();
+		g_return_val_if_fail (*bag_ptr != NULL, FALSE);
+
+		attribute = (((void *) *bag_ptr) + attribute_infos->attribute_offset);
+	}
+
+	g_return_val_if_fail (attribute != NULL, NULL);
+
+	return attribute;
+}
+
+gboolean
+lsm_dom_attribute_map_set_attribute (LsmDomAttributeMap *map,
+				     void *instance,
+				     const char *name,
+				     const char *value)
+{
+	LsmDomAttribute *attribute;
+
+	g_return_val_if_fail (map != NULL, FALSE);
+
+	attribute = _get_attribute (map, instance, name);
+	if (attribute == NULL)
+		return FALSE;
+
+	g_free (attribute->value);
+	attribute->value = value != NULL ? g_strdup (value) : NULL;
+
+	return TRUE;
+}
+
+gboolean
+lsm_dom_attribute_map_set_css_attribute (LsmDomAttributeMap *map,
+				      void *instance,
+				      const char *name,
+				      const char *value,
+				      LsmDomCssType type)
+{
+	LsmDomAttribute *attribute;
+
+	g_return_val_if_fail (map != NULL, FALSE);
+
+	attribute = _get_attribute (map, instance, name);
+	if (attribute == NULL)
+		return FALSE;
+
+	g_free (attribute->css_value);
+	attribute->css_value = value != NULL ? g_strdup (value) : NULL;
+	attribute->css_type = type;
+
+	return TRUE;
+}
+
+char const *
+lsm_dom_attribute_map_get_attribute (LsmDomAttributeMap *map,
+				     void *instance,
+				     const char *name)
+{
+	LsmDomAttributeInfos *attribute_infos;
+	LsmDomAttribute *attribute;
+
+	g_return_val_if_fail (map != NULL, NULL);
+
+	attribute_infos = g_hash_table_lookup (map->attribute_hash, name);
+	if (attribute_infos == NULL)
+		return NULL;
+
+	if (attribute_infos->bag_infos == NULL)
+		attribute = (void *)(instance + attribute_infos->attribute_offset);
+	else {
+		LsmDomAttributeBag **bag_ptr;
+
+		bag_ptr = (void *)(instance + attribute_infos->bag_infos->bag_offset);
+		g_return_val_if_fail (bag_ptr != NULL, NULL);
+		if (*bag_ptr == NULL)
+			return NULL;
+		attribute = (void *)(*bag_ptr + attribute_infos->attribute_offset);
+	}
+	g_return_val_if_fail (attribute != NULL, NULL);
+
+	return attribute->value;
+}
+
+gboolean
+lsm_dom_attribute_map_is_attribute_defined (LsmDomAttributeMap *map,
+					 void *instance,
+					 const char *name)
+{
+	LsmDomAttributeInfos *attribute_infos;
+	LsmDomAttribute *attribute;
+
+	g_return_val_if_fail (map != NULL, FALSE);
+
+	attribute_infos = g_hash_table_lookup (map->attribute_hash, name);
+	if (attribute_infos == NULL)
+		return FALSE;
+
+	attribute = (void *)(instance + attribute_infos->attribute_offset);
+	g_return_val_if_fail (attribute != NULL, FALSE);
+
+	return attribute->value != NULL;
+}
+
+gboolean
+lsm_dom_attribute_is_defined (const LsmDomAttribute *attribute)
+{
+	return (attribute != NULL && attribute->value != NULL);
+}
+
+static void
+lsm_dom_attribute_finalize_cb (gpointer key,
+			    gpointer value,
+			    gpointer instance)
+{
+	LsmDomAttributeInfos *attribute_infos = value;
+	LsmDomAttribute *attribute;
+
+	attribute = (void *)(instance + attribute_infos->attribute_offset);
+	if (attribute != NULL) {
+		g_free (attribute->value);
+		g_free (attribute->css_value);
+
+		attribute->value = NULL;
+		attribute->css_value = NULL;
+
+		if (attribute_infos->attribute_class != NULL &&
+		    attribute_infos->attribute_class->finalize != NULL)
+			attribute_infos->attribute_class->finalize (attribute);
+	}
+}
+
+static void
+lsm_dom_attribute_bag_finalize_cb (gpointer key,
+				gpointer value,
+				gpointer instance)
+{
+	LsmDomAttributeBagInfos *bag_infos = value;
+	LsmDomAttributeBag **bag_ptr;
+
+	bag_ptr = (void *)(instance + bag_infos->bag_offset);
+	if (*bag_ptr != NULL) {
+		if (bag_infos->bag_class->finalize != NULL)
+			bag_infos->bag_class->finalize (*bag_ptr);
+
+		*bag_ptr = NULL;
+	}
+}
+
+void
+lsm_dom_attribute_map_free_attributes (LsmDomAttributeMap *map, void *instance)
+{
+	g_return_if_fail (map != NULL);
+
+	g_hash_table_foreach (map->attribute_hash, lsm_dom_attribute_finalize_cb, instance);
+	g_hash_table_foreach (map->bag_hash, lsm_dom_attribute_bag_finalize_cb, instance);
+}
+
+char const *
+lsm_dom_attribute_get_value (const LsmDomAttribute *attribute)
+{
+	g_return_val_if_fail (attribute != NULL, NULL);
+
+	if (attribute->css_value != NULL &&
+	    attribute->css_type >= LSM_DOM_CSS_TYPE_AUTHOR)
+		return attribute->css_value;
+
+	return attribute->value;
+}
+
+void
+lsm_dom_boolean_attribute_parse (LsmDomBooleanAttribute *attribute,
+			      gboolean *style_value)
+{
+	const char *string;
+
+	g_return_if_fail (attribute != NULL);
+	g_return_if_fail (style_value != NULL);
+
+	string = lsm_dom_attribute_get_value ((LsmDomAttribute *) attribute);
+	if (string == NULL) {
+		attribute->value = *style_value;
+		return;
+	}
+
+	attribute->value = (strcmp (string, "true") == 0);
+	*style_value = attribute->value;
+}
+
+void
+lsm_dom_unsigned_attribute_parse (LsmDomUnsignedAttribute *attribute,
+			       unsigned int *style_value)
+{
+	const char *string;
+
+	g_return_if_fail (attribute != NULL);
+	g_return_if_fail (style_value != NULL);
+
+	string = lsm_dom_attribute_get_value ((LsmDomAttribute *) attribute);
+	if (string == NULL) {
+		attribute->value = *style_value;
+		return;
+	}
+
+	attribute->value = atoi (string);
+	*style_value = attribute->value;
+}
+
+void
+lsm_dom_double_attribute_parse (LsmDomDoubleAttribute *attribute,
+			     double *style_value)
+{
+	const char *string;
+
+	g_return_if_fail (attribute != NULL);
+	g_return_if_fail (style_value != NULL);
+
+	string = lsm_dom_attribute_get_value ((LsmDomAttribute *) attribute);
+	if (string == NULL) {
+		attribute->value = *style_value;
+		return;
+	}
+
+	attribute->value = atof (string);
+	*style_value = attribute->value;
+}
+
+void
+lsm_dom_string_attribute_parse (LsmDomStringAttribute *attribute,
+				char **style_value)
+{
+	const char *string;
+
+	g_return_if_fail (attribute != NULL);
+	g_return_if_fail (style_value != NULL);
+	g_return_if_fail (*style_value != NULL);
+
+	string = lsm_dom_attribute_get_value ((LsmDomAttribute *) attribute);
+	if (string == NULL) {
+		g_free (attribute->value);
+		attribute->value = g_strdup (*style_value);
+	} else {
+		g_free (*style_value);
+		*style_value = g_strdup (string);
+		g_free (attribute->value);
+		attribute->value = g_strdup (string);
+	}
+}
+
+void
+lsm_dom_enum_attribute_parse (LsmDomEnumAttribute *attribute,
+			    unsigned int *style_value,
+			    LsmDomNamedConvert convert)
+{
+	const char *string;
+
+	g_return_if_fail (attribute != NULL);
+	g_return_if_fail (style_value != NULL);
+
+	string = lsm_dom_attribute_get_value ((LsmDomAttribute *) attribute);
+	if (string == NULL) {
+		attribute->value = *style_value;
+		return;
+	}
+
+	attribute->value = convert (string);
+	*style_value = attribute->value;
+}
+
+void
+lsm_dom_enum_list_attribute_parse (LsmDomEnumListAttribute *attribute,
+				 LsmDomEnumList *style_value,
+				 LsmDomNamedConvert convert)
+{
+	const char *string;
+	char **items;
+	unsigned int i;
+
+	g_return_if_fail (attribute != NULL);
+	g_return_if_fail (style_value != NULL);
+
+	g_free (attribute->values);
+	attribute->n_values = 0;
+
+	string = lsm_dom_attribute_get_value ((LsmDomAttribute *) attribute);
+	if (string == NULL) {
+		if (style_value->n_values > 0) {
+			attribute->values = g_new (unsigned int, style_value->n_values);
+			memcpy (attribute->values, style_value->values,
+				sizeof (unsigned int) * style_value->n_values);
+		} else
+			attribute->values = NULL;
+		attribute->n_values = style_value->n_values;
+
+		return;
+	}
+
+	items = g_strsplit_set (string, " ", -1);
+	attribute->n_values = g_strv_length (items);
+
+	attribute->values = g_new (unsigned int, attribute->n_values);
+	for (i = 0; i < attribute->n_values; i++)
+		attribute->values[i] = convert (items[i]);
+
+	g_strfreev (items);
+}
+
+void
+lsm_dom_string_attribute_finalize (void *abstract)
+{
+	LsmDomStringAttribute *attribute = abstract;
+
+	g_return_if_fail (attribute != NULL);
+
+	g_free (attribute->value);
+	attribute->value = NULL;
+}
+
+void
+lsm_dom_enum_list_attribute_finalize (void *abstract)
+{
+	LsmDomEnumListAttribute *attribute = abstract;
+
+	g_return_if_fail (attribute != NULL);
+
+	g_free (attribute->values);
+	attribute->n_values = 0;
+	attribute->values = NULL;
+}
+
+static const LsmDomAttributeClass string_attribute_class = {
+	.finalize = lsm_dom_string_attribute_finalize
+};
+
+void
+lsm_dom_attribute_map_add_string (LsmDomAttributeMap *map,
+			       char const *name,
+			       ptrdiff_t offset)
+{
+	lsm_dom_attribute_map_add_attribute_full (map, name, offset, &string_attribute_class);
+}
+
+static const LsmDomAttributeClass enum_list_attribute_class = {
+	.finalize = lsm_dom_enum_list_attribute_finalize
+};
+
+void
+lsm_dom_attribute_map_add_enum_list (LsmDomAttributeMap *map,
+				   char const *name,
+				   ptrdiff_t offset)
+{
+	lsm_dom_attribute_map_add_attribute_full (map, name, offset, &enum_list_attribute_class);
+}
+
 static LsmMathmlColor *
 lsm_mathml_color_copy (LsmMathmlColor *color)
 {
diff --git a/src/lsmmathmlattributes.h b/src/lsmmathmlattributes.h
index 9940f83..83f0ef9 100644
--- a/src/lsmmathmlattributes.h
+++ b/src/lsmmathmlattributes.h
@@ -22,13 +22,149 @@
 #ifndef LSM_MATHML_ATTRIBUTES_H
 #define LSM_MATHML_ATTRIBUTES_H
 
-#include <lsmdomattributes.h>
+#include <lsmdom.h>
 #include <lsmmathml.h>
 #include <lsmmathmlenums.h>
 #include <pango/pango-attributes.h>
 
 G_BEGIN_DECLS
 
+typedef enum {
+	LSM_DOM_CSS_TYPE_USER,
+	LSM_DOM_CSS_TYPE_AUTHOR,
+	LSM_DOM_CSS_TYPE_AUTHOR_IMPORTANT,
+	LSM_DOM_CSS_TYPE_USER_IMPORTANT
+} LsmDomCssType;
+
+typedef struct {
+	char *value;
+	char *css_value;
+	LsmDomCssType css_type;
+} LsmDomAttribute;
+
+typedef struct {
+	GHashTable *attribute_hash;
+	GHashTable *bag_hash;
+} LsmDomAttributeMap;
+
+typedef struct {
+} LsmDomAttributeBag;
+
+typedef struct {
+	void * 			(*init) 	(void);
+	void 			(*finalize) 	(void *bag);
+} LsmDomAttributeBagClass;
+
+typedef struct {
+	void (*finalize) (void *attribute);
+} LsmDomAttributeClass;
+
+typedef void (*LsmDomAttributeFinalizeFunc) (void *);
+
+LsmDomAttributeMap *	lsm_dom_attribute_map_new 		(void);
+LsmDomAttributeMap *	lsm_dom_attribute_map_duplicate		(const LsmDomAttributeMap *from);
+void			lsm_dom_attribute_map_free		(LsmDomAttributeMap *map);
+
+void 		lsm_dom_attribute_map_add_bag_attribute  	(LsmDomAttributeMap *map,
+								 const char *name,
+								 ptrdiff_t attribute_offset,
+								 const LsmDomAttributeClass *attribute_class,
+								 ptrdiff_t bag_offset,
+								 const LsmDomAttributeBagClass *bag_class);
+void		lsm_dom_attribute_map_add_attribute_full	(LsmDomAttributeMap *map,
+								 char const *name,
+								 ptrdiff_t offset,
+								 const LsmDomAttributeClass *attribute_class);
+void		lsm_dom_attribute_map_add_attribute 		(LsmDomAttributeMap *map,
+								 char const *name,
+								 ptrdiff_t offset);
+
+void		lsm_dom_attribute_map_free_attributes 		(LsmDomAttributeMap *map,
+								 void *instance);
+
+gboolean	lsm_dom_attribute_map_set_attribute		(LsmDomAttributeMap *map,
+								 void *instance,
+								 char const *name,
+								 char const *value);
+char const *	lsm_dom_attribute_map_get_attribute		(LsmDomAttributeMap *map,
+								 void *instance,
+								 char const *name);
+gboolean	lsm_dom_attribute_map_set_css_attribute		(LsmDomAttributeMap *map,
+								 void *instance,
+								 char const *name,
+								 char const *value,
+								 LsmDomCssType css_type);
+gboolean	lsm_dom_attribute_map_is_attribute_defined	(LsmDomAttributeMap *map,
+								 void *instance,
+								 char const *name);
+
+gboolean 	lsm_dom_attribute_is_defined 			(const LsmDomAttribute *attribute);
+char const * 	lsm_dom_attribute_get_value 			(const LsmDomAttribute *attribute);
+
+typedef struct {
+	LsmDomAttribute attr;
+	gboolean value;
+} LsmDomBooleanAttribute;
+
+typedef struct {
+	LsmDomAttribute attr;
+	unsigned int value;
+} LsmDomUnsignedAttribute;
+
+typedef struct {
+	LsmDomAttribute attr;
+	double value;
+} LsmDomDoubleAttribute;
+
+typedef struct {
+	LsmDomAttribute attr;
+	char *value;
+} LsmDomStringAttribute;
+
+typedef struct {
+	LsmDomAttribute attr;
+	unsigned int value;
+} LsmDomEnumAttribute;
+
+typedef struct {
+	unsigned int n_values;
+	unsigned int *values;
+} LsmDomEnumList;
+
+typedef struct {
+	LsmDomAttribute attr;
+	unsigned int n_values;
+	unsigned int *values;
+} LsmDomEnumListAttribute;
+
+typedef unsigned int (*LsmDomNamedConvert) (const char *string);
+
+void 	lsm_dom_boolean_attribute_parse		(LsmDomBooleanAttribute *attribute,
+						 gboolean *default_value);
+void 	lsm_dom_unsigned_attribute_parse	(LsmDomUnsignedAttribute *attribute,
+						 unsigned *default_value);
+void 	lsm_dom_double_attribute_parse		(LsmDomDoubleAttribute *attribute,
+						 double *default_value);
+void	lsm_dom_string_attribute_parse		(LsmDomStringAttribute *attribute,
+						 char **default_value);
+void 	lsm_dom_enum_attribute_parse 		(LsmDomEnumAttribute *attribute,
+						 unsigned int *default_value,
+						 LsmDomNamedConvert convert);
+void 	lsm_dom_enum_list_attribute_parse 	(LsmDomEnumListAttribute *attribute,
+						 LsmDomEnumList *style_value,
+						 LsmDomNamedConvert convert);
+
+void	lsm_dom_string_attribute_finalize	(void *abstract);
+void 	lsm_dom_enum_list_attribute_finalize 	(void *abstract);
+
+
+void 	lsm_dom_attribute_map_add_string 	(LsmDomAttributeMap *map,
+						 char const *name,
+						 ptrdiff_t offset);
+void 	lsm_dom_attribute_map_add_enum_list 	(LsmDomAttributeMap *map,
+						 char const *name,
+						 ptrdiff_t offset);
+
 #define LSM_MATHML_SPACE_EM_VERY_VERY_THIN	0.055556
 #define LSM_MATHML_SPACE_EM_VERY_THIN		0.111111
 #define LSM_MATHML_SPACE_EM_THIN		0.166667



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]