[lasem] [Mathml] Implement <lasem:itex> element



commit 0f13bff18d202c153550910dec235fe7b29de11a
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Wed Jan 13 17:31:47 2010 +0100

    [Mathml] Implement <lasem:itex> element
    
    This element helps to add an itex expression anywhere in the mathml
    data.

 src/Makefile.am            |    2 +
 src/lsmmathml.h            |    2 +
 src/lsmmathmldocument.c    |    5 ++
 src/lsmmathmlitexelement.c |  171 ++++++++++++++++++++++++++++++++++++++++++++
 src/lsmmathmlitexelement.h |   58 +++++++++++++++
 test/mathml/misc/itex.mml  |   12 +++
 6 files changed, 250 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8b8bcf7..52f3cf3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -66,6 +66,7 @@ liblasem_la_SOURCES =				\
 	lsmmathmlglyphtableams.c		\
 	lsmmathmlalignmarkelement.c		\
 	lsmmathmlaligngroupelement.c		\
+	lsmmathmlitexelement.c			\
 	lsmmathmlutils.c			\
 	lsmmathmllayoututils.c			\
 	lsmsvgenums.c				\
@@ -150,6 +151,7 @@ LASEM_HDRS = \
 	lsmmathmlglyphtableams.h		\
 	lsmmathmlalignmarkelement.h		\
 	lsmmathmlaligngroupelement.h		\
+	lsmmathmlitexelement.h			\
 	lsmmathmlutils.h			\
 	lsmmathmllayoututils.h			\
 	lsmsvg.h				\
diff --git a/src/lsmmathml.h b/src/lsmmathml.h
index d474914..8b94683 100644
--- a/src/lsmmathml.h
+++ b/src/lsmmathml.h
@@ -53,6 +53,8 @@ typedef struct _LsmMathmlActionElement LsmMathmlActionElement;
 typedef struct _LsmMathmlAlignMarkElement LsmMathmlAlignMarkElement;
 typedef struct _LsmMathmlAlignGroupElement LsmMathmlAlignGroupElement;
 
+typedef struct _LsmMathmlItexElement LsmMathmlItexElement;
+
 typedef struct _LsmMathmlView LsmMathmlView;
 
 typedef struct _LsmMathmlStyle LsmMathmlStyle;
diff --git a/src/lsmmathmldocument.c b/src/lsmmathmldocument.c
index 51f5f47..c8d8c52 100644
--- a/src/lsmmathmldocument.c
+++ b/src/lsmmathmldocument.c
@@ -41,6 +41,7 @@
 #include <lsmmathmlstyleelement.h>
 #include <lsmmathmlalignmarkelement.h>
 #include <lsmmathmlaligngroupelement.h>
+#include <lsmmathmlitexelement.h>
 #include <lsmmathmlview.h>
 #include <lsmdebug.h>
 #include <gio/gio.h>
@@ -123,6 +124,10 @@ lsm_mathml_document_create_element (LsmDomDocument *document, const char *tag_na
 		node = lsm_mathml_align_mark_element_new ();
 	else if (strcmp (tag_name, "maligngroup") == 0)
 		node = lsm_mathml_align_group_element_new ();
+	else if (strcmp (tag_name, "lasem:itex") == 0)
+		node = lsm_mathml_itex_element_new ();
+	else
+		lsm_debug ("[MathmlDocument::create_element] Unknown tag (%s)", tag_name);
 
 	return LSM_DOM_ELEMENT (node);
 }
diff --git a/src/lsmmathmlitexelement.c b/src/lsmmathmlitexelement.c
new file mode 100644
index 0000000..2c74715
--- /dev/null
+++ b/src/lsmmathmlitexelement.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright © 2010 Emmanuel Pacaud
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ * 	Emmanuel Pacaud <emmanuel gnome org>
+ */
+
+#include <lsmmathmlitexelement.h>
+#include <lsmmathmlview.h>
+#include <lsmdomtext.h>
+#include <lsmdebug.h>
+
+static GObjectClass *parent_class;
+
+/* GdomNode implementation */
+
+static const char *
+lsm_mathml_itex_element_get_node_name (LsmDomNode *node)
+{
+	return "lasem:itex";
+}
+
+static gboolean
+lsm_mathml_itex_can_append_child (LsmDomNode *self, LsmDomNode *child)
+{
+	return (LSM_IS_DOM_TEXT (child));
+}
+
+/* LsmMathmlElement implementation */
+
+static void
+_update (LsmMathmlElement *self, LsmMathmlStyle *style)
+{
+	LsmMathmlItexElement *itex_element = LSM_MATHML_ITEX_ELEMENT (self);
+	LsmDomNode *node;
+	GString *string = g_string_new ("");
+	gboolean need_conversion;
+
+	for (node = LSM_DOM_NODE (self)->first_child; node != NULL; node = node->next_sibling) {
+		if (LSM_IS_DOM_TEXT (node)) {
+			g_string_append (string, lsm_dom_node_get_node_value (node));
+		}
+	}
+
+	need_conversion = g_strcmp0 (itex_element->itex, string->str) != 0;
+
+	lsm_debug ("[MathmlItex::update] itex = '%s'", itex_element->itex);
+
+	if (need_conversion) {
+		LsmMathmlDocument *document;
+
+		g_free (itex_element->itex);
+		itex_element->itex = string->str;
+
+		lsm_debug ("[MathmlItex::update] need conversion");
+
+		document = lsm_mathml_document_new_from_itex (itex_element->itex,
+							      string->len, NULL);
+		if (document != NULL) {
+			if (itex_element->math != NULL)
+				g_object_unref (lsm_dom_node_get_owner_document (LSM_DOM_NODE (itex_element->math)));
+
+			itex_element->math = LSM_MATHML_ELEMENT (lsm_mathml_document_get_root_element (document));
+			lsm_mathml_element_update (itex_element->math, style);
+		}
+	}
+
+	g_string_free (string, FALSE);
+}
+
+static const LsmMathmlBbox *
+_measure (LsmMathmlElement *self, LsmMathmlView *view, const LsmMathmlBbox *bbox)
+{
+	LsmMathmlItexElement *itex_element = LSM_MATHML_ITEX_ELEMENT (self);
+
+	if (itex_element->math != NULL)
+		return lsm_mathml_element_measure (itex_element->math, view, bbox);
+
+	self->bbox.width = 0.0;
+	self->bbox.height = 0.0;
+	self->bbox.depth = 0.0;
+
+	return &self->bbox;
+}
+
+static void
+_layout (LsmMathmlElement *self, LsmMathmlView *view,
+	 double x, double y, const LsmMathmlBbox *bbox)
+{
+	LsmMathmlItexElement *itex_element = LSM_MATHML_ITEX_ELEMENT (self);
+
+	if (itex_element->math != NULL)
+		lsm_mathml_element_layout (itex_element->math, view, x, y, bbox);
+}
+
+static void
+_render (LsmMathmlElement *self, LsmMathmlView *view)
+{
+	LsmMathmlItexElement *itex_element = LSM_MATHML_ITEX_ELEMENT (self);
+
+	if (itex_element->math != NULL)
+		lsm_mathml_element_render (itex_element->math, view);
+}
+
+/* LsmMathmlItexElement implementation */
+
+LsmDomNode *
+lsm_mathml_itex_element_new (void)
+{
+	return g_object_new (LSM_TYPE_MATHML_ITEX_ELEMENT, NULL);
+}
+
+static void
+lsm_mathml_itex_element_init (LsmMathmlItexElement *self)
+{
+	self->itex = NULL;
+	self->math = NULL;
+}
+
+static void
+lsm_mathml_itex_element_finalize (GObject *object)
+{
+	LsmMathmlItexElement *itex_element = LSM_MATHML_ITEX_ELEMENT (object);
+
+	g_free (itex_element->itex);
+	itex_element->itex = NULL;
+
+	if (itex_element->math != NULL)
+		g_object_unref (lsm_dom_node_get_owner_document (LSM_DOM_NODE (itex_element->math)));
+	itex_element->math = NULL;
+
+	parent_class->finalize (object);
+}
+
+/* LsmMathmlItexElement class */
+
+static void
+lsm_mathml_itex_element_class_init (LsmMathmlItexElementClass *itex_class)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (itex_class);
+	LsmDomNodeClass *d_node_class = LSM_DOM_NODE_CLASS (itex_class);
+	LsmMathmlElementClass *m_element_class = LSM_MATHML_ELEMENT_CLASS (itex_class);
+
+	parent_class = g_type_class_peek_parent (itex_class);
+
+	object_class->finalize = lsm_mathml_itex_element_finalize;
+
+	d_node_class->get_node_name = lsm_mathml_itex_element_get_node_name;
+	d_node_class->can_append_child = lsm_mathml_itex_can_append_child;
+
+	m_element_class->update = _update;
+	m_element_class->measure = _measure;
+	m_element_class->layout = _layout;
+	m_element_class->render = _render;
+}
+
+G_DEFINE_TYPE (LsmMathmlItexElement, lsm_mathml_itex_element, LSM_TYPE_MATHML_ELEMENT)
diff --git a/src/lsmmathmlitexelement.h b/src/lsmmathmlitexelement.h
new file mode 100644
index 0000000..d00b3f4
--- /dev/null
+++ b/src/lsmmathmlitexelement.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2010  Emmanuel Pacaud
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ * 	Emmanuel Pacaud <emmanuel gnome org>
+ */
+
+#ifndef LSM_MATHML_ITEX_ELEMENT_H
+#define LSM_MATHML_ITEX_ELEMENT_H
+
+#include <lsmmathml.h>
+#include <lsmmathmlelement.h>
+#include <lsmmathmldocument.h>
+
+G_BEGIN_DECLS
+
+#define LSM_TYPE_MATHML_ITEX_ELEMENT             (lsm_mathml_itex_element_get_type ())
+#define LSM_MATHML_ITEX_ELEMENT(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), LSM_TYPE_MATHML_ITEX_ELEMENT, LsmMathmlItexElement))
+#define LSM_MATHML_ITEX_ELEMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), LSM_TYPE_MATHML_ITEX_ELEMENT, LsmMathmlItexElementClass))
+#define LSM_IS_MATHML_ITEX_ELEMENT(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LSM_TYPE_MATHML_ITEX_ELEMENT))
+#define LSM_IS_MATHML_ITEX_ELEMENT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), LSM_TYPE_MATHML_ITEX_ELEMENT))
+#define LSM_MATHML_ITEX_ELEMENT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), LSM_TYPE_MATHML_ITEX_ELEMENT, LsmMathmlItexElementClass))
+
+typedef struct _LsmMathmlItexElementClass LsmMathmlItexElementClass;
+
+struct _LsmMathmlItexElement {
+	LsmMathmlElement	element;
+
+	char *itex;
+
+	LsmMathmlElement *math;
+};
+
+struct _LsmMathmlItexElementClass {
+	LsmMathmlElementClass  parent_class;
+};
+
+GType lsm_mathml_itex_element_get_type (void);
+
+LsmDomNode *		lsm_mathml_itex_element_new 		(void);
+
+G_END_DECLS
+
+#endif
diff --git a/test/mathml/misc/itex.mml b/test/mathml/misc/itex.mml
new file mode 100644
index 0000000..784a047
--- /dev/null
+++ b/test/mathml/misc/itex.mml
@@ -0,0 +1,12 @@
+<math xmlns="http://www.w3.org/1998/Math/MathML"; xmlns:lasem="lasem.dtd">
+<mstyle mathsize="20pt">
+	<mfrac>
+	<lasem:itex> $f(x)=\sqrt{\frac{x-1}{x+1}}$ </lasem:itex>
+	<mrow>
+	<mi>a</mi>
+	<mo>+</mo>
+	<lasem:itex>$$\sqrt{2}$$</lasem:itex>
+	</mrow>
+	</mfrac>
+	</mstyle>
+</math>



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