[LON-CAPA-cvs] cvs: loncom /xml lontable.pm lontable.test

foxr foxr@source.lon-capa.org
Mon, 29 Dec 2008 11:57:37 -0000


foxr		Mon Dec 29 11:57:37 2008 EDT

  Modified files:              
    /loncom/xml	lontable.pm lontable.test 
  Log:
  Add code to support horizontal alignment, correct bordering, and 
  table width.
  
  
Index: loncom/xml/lontable.pm
diff -u loncom/xml/lontable.pm:1.6 loncom/xml/lontable.pm:1.7
--- loncom/xml/lontable.pm:1.6	Tue Dec 23 11:49:32 2008
+++ loncom/xml/lontable.pm	Mon Dec 29 11:57:37 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 #  Generating TeX tables.
 #
-# $Id: lontable.pm,v 1.6 2008/12/23 11:49:32 foxr Exp $
+# $Id: lontable.pm,v 1.7 2008/12/29 11:57:37 foxr Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -108,6 +108,7 @@
 
 =over3
 
+
 =item alignment
 
 Table alignment.  Some table styles support this but not all.
@@ -132,6 +133,12 @@
 not error checked.  Any use of a non-existent theme is reported by the LaTeX::Table package
 when the table text is generated.
 
+=item width
+
+The width of the table.  This can be expressed as fractions of full width, or in any
+TeX unit measure e.g. 0.75 for 75% of the width, or 10.8cm  This forces the table to the
+tabularx environment.
+
 =back
 
 =head3 Member data
@@ -399,6 +406,26 @@
 
 =pod
 
+=head 2 width
+
+Gets and optionally sets the width of the table.
+
+=head 3 Examples:
+
+ $table->width("0.8");    # 80% of the column width.
+ my $newwidth = $table->width("10cm");   # 10cm width returns "10cm".
+
+=cut
+sub width {
+    my ($self, $new_value) = @_;
+    if (defined($new_value)) {
+	$self->{'width'} = $new_value;
+    }
+    return $self->{'width'}; 	# Could be undef.
+}
+
+=pod
+
 =head2 start_row
 
 Begins a new row in the table.  If a row is already open, that row is
@@ -657,6 +684,20 @@
 
     my $table = LaTeX::Table->new();
 
+    # Add the caption if supplied.
+
+    if ($this->{'caption'} ne "") {
+	$table->set_caption($this->caption);
+    }
+
+    
+    # Set the width if defined:
+
+    if (defined ($this->{'width'})) {
+	$table->set_width($this->{'width'});
+	$table->set_width_environment('tabularx');
+    }
+
     # Build up the data:
 
     my @data;
@@ -669,17 +710,46 @@
     for (my $row = 0; $row < $row_count; $row++) {
 	my @row;
 	my $cells      = $rows->[$row]->{'cells'};
+	my $def_halign = $rows->[$row]->{'default_halign'};
 	my $cell_count = scalar(@$cells);
 	my $startcol   = 1;
 	my @underlines;		# Array of \cline cells if cellborder on.
 
 	for (my $cell  = 0; $cell < $cell_count; $cell++) {
 	    my $contents = $cells->[$cell]->{'contents'};
+
+	    #
+	    #  Cell alignment is the default alignment unless
+	    #  explicitly specified in the cell.
+	    #  NOTE: at this point I don't know how to do vert alignment.
+	    #
+
+	    my $halign   = $def_halign;
+	    if (defined ($cells->[$cell]->{'halign'})) {
+		$halign = $cells->[$cell]->{'halign'};
+	    }
+
+	    # Create the horizontal alignment character:
+
+	    my $col_align = 'l';
+	    if ($halign eq 'right') {
+		$col_align = 'r';
+	    }
+	    if ($halign eq 'center') {
+		$col_align = 'c';
+	    }
+	    if ($inner_border || ($outer_border && ($cell == 0))) {
+		$col_align = '|'.$col_align;
+	    }
+	    if ($inner_border || ($outer_border && ($cell == ($cell_count -1)))) {
+		$col_align = $col_align.'|';
+	    }
+
+	    #factor in spans:
+
 	    my $cspan    = $cells->[$cell]->{'colspan'};
 	    my $nextcol  = $startcol + $cspan;
-	    if ($cspan > 1) {
-		$contents = '\multicolumn{'.$cspan.'}{|l|}{'.$contents.'}';
-	    }
+	    $contents = '\multicolumn{'.$cspan.'}{'.$col_align.'}{'.$contents.'}';
 	    if ($inner_border && ($cells->[$cell]->{'rowspan'} == 1)) {
 		my $lastcol = $nextcol -1;
 		push(@underlines, "\\cline{$startcol-$lastcol}");
Index: loncom/xml/lontable.test
diff -u loncom/xml/lontable.test:1.3 loncom/xml/lontable.test:1.4
--- loncom/xml/lontable.test:1.3	Tue Dec 23 11:49:32 2008
+++ loncom/xml/lontable.test	Mon Dec 29 11:57:37 2008
@@ -3,7 +3,7 @@
 # The LearningOnline Network with CAPA
 #  Generating TeX tables.
 #
-# $Id: lontable.test,v 1.3 2008/12/23 11:49:32 foxr Exp $
+# $Id: lontable.test,v 1.4 2008/12/29 11:57:37 foxr Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -219,30 +219,35 @@
 #  +----------+---------+----+-----------+---------+
 
 
-$testobject = new Apache::lontable({theme => "Dresden"});
+$testobject = new Apache::lontable({theme => "Dresden",
+				    caption => "This is the table caption",
+				    outer_border => 1,
+				    inner_border => 1,
+				    width        => '1.0\textwidth', 
+				    alignment    => 'left'});
 
 $testobject->start_row();
 $testobject->add_cell('2 cols 3 rows', {rowspan => 3, colspan => 2});
 $testobject->add_cell('2 cols 1 row', {colspan => 2});
 $testobject->end_row();
 
-$testobject->start_row();
-$testobject->add_cell('ordinary cell');
+$testobject->start_row({default_halign => 'left'});
 $testobject->add_cell('ordinary cell');
+$testobject->add_cell('ordinary cell', {halign => 'center'});
 $testobject->end_row();
 
-$testobject->start_row();
-$testobject->add_cell('2 rows 1 col', {rowspan => 2});
+$testobject->start_row({default_halign => 'right'});
+$testobject->add_cell('2 rows 1 col', {rowspan => 2, halign => 'right'});
 $testobject->add_cell('ordinary cell');
 $testobject->end_row();
 
-$testobject->start_row();
-$testobject->add_cell('ordinary cell');
+$testobject->start_row({default_halign => 'center'});
 $testobject->add_cell('ordinary cell');
 $testobject->add_cell('ordinary cell');
+$testobject->add_cell('ordinary cell', {halign => 'left'});
 $testobject->end_row();
 
-#  First of all the table should have figured out tere are 4 cols and 4 rows:
+#  First of all the table should have figured out there are 4 cols and 4 rows:
 
 ok($testobject->get_object_attribute('column_count') == 4, 'col count with spans');