[glabels/vala] Added parsing of text objects.



commit 267629532518e72caafb5282c41258f744642bce
Author: Jim Evins <evins snaught com>
Date:   Sun Jul 29 18:42:42 2012 -0400

    Added parsing of text objects.

 glabels/color_node.vala        |   12 +++++
 glabels/enum_util.vala         |   40 +++++++++++++++
 glabels/label_object_text.vala |   10 +++-
 glabels/text_line.vala         |   21 ++++++++-
 glabels/text_lines.vala        |   17 ++++++-
 glabels/xml_label.vala         |  103 +++++++++++++++++++++++++++++++++++++++-
 6 files changed, 197 insertions(+), 6 deletions(-)
---
diff --git a/glabels/color_node.vala b/glabels/color_node.vala
index f92bb21..ebf689f 100644
--- a/glabels/color_node.vala
+++ b/glabels/color_node.vala
@@ -45,6 +45,18 @@ namespace glabels
 		}
 
 
+		public ColorNode.from_legacy_color( uint32 color )
+		{
+			this( false, Color.from_legacy_color( color ), null );
+		}
+
+
+		public ColorNode.from_key( string key )
+		{
+			this( true, Color.none(), key );
+		}
+
+
 		public bool equal( ColorNode cn )
 		{
 			return ( (this.field_flag == cn.field_flag) && this.color.equal( cn.color ) && (this.key == cn.key) );
diff --git a/glabels/enum_util.vala b/glabels/enum_util.vala
index 4e738c8..a1581d3 100644
--- a/glabels/enum_util.vala
+++ b/glabels/enum_util.vala
@@ -68,6 +68,46 @@ namespace glabels
 
 
 		/****************************************************************************/
+		/* Utilities to deal with ValignType.                                       */
+		/****************************************************************************/
+
+		public string valign_to_string( ValignType valign )
+		{
+			switch (valign) {
+			case ValignType.TOP:
+				return "top";
+			case ValignType.CENTER:
+				return "center";
+			case ValignType.BOTTOM:
+				return "bottom";
+			default:
+				return "?";
+			}
+		}
+
+		public ValignType string_to_valign( string align_string )
+		{
+			switch (align_string)
+			{
+			case "top":
+			case "Top":
+			case "TOP":
+				return ValignType.TOP;
+			case "center":
+			case "Center":
+			case "CENTER":
+				return ValignType.CENTER;
+			case "bottom":
+			case "Bottom":
+			case "BOTTOM":
+				return ValignType.BOTTOM;
+			default:
+				return ValignType.TOP;
+			}
+		}
+
+
+		/****************************************************************************/
 		/* Utilities to deal with PangoWeight types                                 */
 		/****************************************************************************/
 
diff --git a/glabels/label_object_text.vala b/glabels/label_object_text.vala
index a746021..b7d5e6b 100644
--- a/glabels/label_object_text.vala
+++ b/glabels/label_object_text.vala
@@ -329,17 +329,23 @@ namespace glabels
 		}
 
 
-		private TextLines get_lines()
+		public TextLines get_lines()
 		{
 			Gtk.TextIter start, end;
 
 			buffer.get_bounds( out start, out end );
 			string text = buffer.get_text( start, end, false );
-			TextLines lines = new TextLines( text );
+			TextLines lines = new TextLines.parse( text );
 
 			return lines;
 		}
 
+
+		public void set_lines( TextLines lines )
+		{
+			buffer.set_text( lines.expand( null ) );
+		}
+
 	}
 
 }
diff --git a/glabels/text_line.vala b/glabels/text_line.vala
index 8eb057d..607a268 100644
--- a/glabels/text_line.vala
+++ b/glabels/text_line.vala
@@ -29,8 +29,15 @@ namespace glabels
 		public unowned List<TextNode> nodes { get; private set; }
 
 
-		public TextLine( string text, int i_start, out int i_next )
+		public TextLine()
 		{
+		}
+
+
+		public TextLine.parse( string text, int i_start, out int i_next )
+		{
+			this();
+
 			i_next = i_start;
 
 			for ( int i = i_start; text[i] != 0; i = i_next )
@@ -49,6 +56,18 @@ namespace glabels
 		}
 
 
+		public void append( TextNode node )
+		{
+			nodes.append( node );
+		}
+
+
+		public bool empty()
+		{
+			return nodes.first() == null;
+		}
+
+
 		public void expand( MergeRecord? record, ref StringBuilder builder )
 		{
 			/* special case: something like ${ADDRESS2} = "" on line by itself. */
diff --git a/glabels/text_lines.vala b/glabels/text_lines.vala
index 18149ed..98eaada 100644
--- a/glabels/text_lines.vala
+++ b/glabels/text_lines.vala
@@ -29,19 +29,32 @@ namespace glabels
 		public unowned List<TextLine> lines { get; private set; }
 
 
-		public TextLines( string text )
+		public TextLines()
 		{
+		}
+
+
+		public TextLines.parse( string text )
+		{
+			this();
+
 			int i_next  = 0;
 
 			for ( int i = 0; text[i] != 0; i = i_next )
 			{
 				stderr.printf( "Text[%d] = %c, ", i, text[i] ); 
-				lines.append( new TextLine( text, i, out i_next ) );
+				lines.append( new TextLine.parse( text, i, out i_next ) );
 				stderr.printf( "i_next =%d\n", i_next ); 
 			}
 		}
 
 
+		public void append( TextLine line )
+		{
+			lines.append( line );
+		}
+
+
 		public string expand( MergeRecord? record )
 		{
 			StringBuilder builder = new StringBuilder();
diff --git a/glabels/xml_label.vala b/glabels/xml_label.vala
index fee45ab..37bc8bf 100644
--- a/glabels/xml_label.vala
+++ b/glabels/xml_label.vala
@@ -277,18 +277,119 @@ namespace glabels
 			object.w = XmlUtil.get_prop_length( node, "w", 0 );
 			object.h = XmlUtil.get_prop_length( node, "h", 0 );
 
+			/* align attr */
+			object.text_alignment = EnumUtil.string_to_align( XmlUtil.get_prop_string( node, "align", "left" ) );
+
+			/* valign attr */
+			object.text_valignment = EnumUtil.string_to_valign( XmlUtil.get_prop_string( node, "valign", "top" ) );
+
+			/* auto_shrink attr */
+			object.auto_shrink = XmlUtil.get_prop_bool( node, "auto_shrink", false );
+
 			/* affine attrs */
 			parse_affine_attrs( node, object );
 
 			/* shadow attrs */
 			parse_shadow_attrs( node, object );
 
-			// TODO: parse contents.
+			for ( unowned Xml.Node* child = node.children; child != null; child = child->next )
+			{
+				switch (child->name)
+				{
+				case "Span":
+					parse_toplevel_span_node( child, object );
+					break;
+
+				default:
+					if ( child->is_text() == 0 )
+					{
+						message( "Unexpected %s child: \"%s\"", node.name, child->name );
+					}
+					break;
+				}
+			}
 
 			label.add_object( object );
 		}
 
 
+		private void parse_toplevel_span_node( Xml.Node        node,
+		                                       LabelObjectText object )
+		{
+			stdout.printf( "Span\n" );
+			/* font_family attr */
+			object.font_family = XmlUtil.get_prop_string( node, "font_family", "Sans" );
+
+			/* font_size attr */
+			object.font_size = XmlUtil.get_prop_double( node, "font_size", 12.0 );
+
+			/* font_weight attr */
+			object.font_weight = EnumUtil.string_to_weight( XmlUtil.get_prop_string( node, "font_weight", "normal" ) );
+
+			/* font_italic attr */
+			object.font_italic_flag = XmlUtil.get_prop_bool( node, "font_italic", false );
+
+			/* color attrs */
+			string color_field_string = XmlUtil.get_prop_string( node, "color_field", null );
+			if ( color_field_string != null )
+			{
+				object.text_color_node = ColorNode.from_key( color_field_string );
+			}
+			else
+			{
+				object.text_color_node = ColorNode.from_legacy_color( XmlUtil.get_prop_uint( node, "color", 0 ) );
+			}
+
+			/* line_spacing attr */
+			object.text_line_spacing = XmlUtil.get_prop_double( node, "line_spacing", 1 );
+
+			/* Now descend children and build lines of text nodes */
+			TextLines lines = new TextLines();
+			TextLine  line  = new TextLine();
+			Regex strip_regex = new Regex( "\\A\\n\\s*|\\n\\s*\\Z" );
+			for ( unowned Xml.Node* child = node.children; child != null; child = child->next )
+			{
+				switch (child->name)
+				{
+				case "Span":
+					message( "Unexpected rich text (not supported, yet!)" );
+					break;
+
+				case "Field":
+					stdout.printf( "Field\n" );
+					line.append( new TextNode( true, XmlUtil.get_prop_string( child, "name", null ) ) );
+					break;
+
+				case "NL":
+					stdout.printf( "NL\n" );
+					lines.append( line );
+					line = new TextLine();
+					break;
+
+				default:
+					if ( child->is_text() != 0 )
+					{
+						/* Literal text. */
+						string raw_data = child->get_content();
+						string data = strip_regex.replace( raw_data, -1, 0, "" );
+						stdout.printf( "Literal = \"%s\"\n", data );
+						line.append( new TextNode( false, data ) );
+					}
+					else
+					{
+						message( "Unexpected %s child: \"%s\"", node.name, child->name );
+					}
+					break;
+				}
+			}
+			if ( !line.empty() )
+			{
+				lines.append( line );
+			}
+			object.set_lines( lines );
+		}
+
+
 		private void parse_affine_attrs( Xml.Node    node,
 		                                 LabelObject object )
 		{



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