[LON-CAPA-cvs] cvs: loncom /interface lonprintout.pm

foxr lon-capa-cvs@mail.lon-capa.org
Mon, 16 Oct 2006 10:32:46 -0000


foxr		Mon Oct 16 06:32:46 2006 EDT

  Modified files:              
    /loncom/interface	lonprintout.pm 
  Log:
  Bug 3586 - Allowed specification of not just where to substitute
  pieces of the title into e.g. surrounding text, but a limit on 
  each field size like sprintf but truncating overfull fields.
  
  
  
Index: loncom/interface/lonprintout.pm
diff -u loncom/interface/lonprintout.pm:1.485 loncom/interface/lonprintout.pm:1.486
--- loncom/interface/lonprintout.pm:1.485	Mon Oct  9 22:18:50 2006
+++ loncom/interface/lonprintout.pm	Mon Oct 16 06:32:46 2006
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # Printout
 #
-# $Id: lonprintout.pm,v 1.485 2006/10/10 02:18:50 albertel Exp $
+# $Id: lonprintout.pm,v 1.486 2006/10/16 10:32:46 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -23,7 +23,6 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 # /home/httpd/html/adm/gpl.txt
-#
 # http://www.lon-capa.org/
 #
 #
@@ -50,6 +49,40 @@
 my %parmhash;
 my $resources_printed;
 
+#
+#   printf_style_subst  item format_string repl
+#  
+# Does printf style substitution for a format string that
+# can have %[n]item in it.. wherever, %[n]item occurs,
+# rep is substituted in format_string.  Note that
+# [n] is an optional integer length.  If provided,
+# repl is truncated to at most [n] characters prior to 
+# substitution.
+#
+sub printf_style_subst {
+    my ($item, $format_string, $repl) = @_;
+
+    while ($format_string =~ /%\d*$item/) {
+	my $start = $-[0];
+	my $end   = $+[0];
+	my $len   = $end - $start;
+
+	# see if we need to truncate:
+
+	my $subst = $repl;
+	my $fmt   = substr($format_string, $start, $len);
+	my $size  = $fmt;
+	$size     =~ s/%(\d*)$item/$1/;
+	if ($size ne "") {
+	    $subst = substr($subst, 0, $size);
+	}
+	$format_string =~ s/%(\d*)$item/$subst/;
+
+    }
+
+    return $format_string;
+}
+
 
 # Format a header according to a format.  
 # 
@@ -60,17 +93,48 @@
 #     %n    - Student name.
 #
 sub format_page_header {
-    my ($format, $assignment, $course, $student) = @_;
+    my ($width, $format, $assignment, $course, $student) = @_;
     
+    $width = &recalcto_mm($width); # Get width in mm.
     #  Default format?
 
     if ($format eq '') {
+	# For the default format, we may need to truncate
+	# elements..  To do this we need to get the page width.
+	# we assume that each character is about 2mm in width.
+	# (correct for the header text size??).  We ignore
+	# any formatting (e.g. boldfacing in this).
+	# 
+	# - Allow the student/course to be one line.
+	#   but only truncate the course.
+	# - Allow the assignment to be 2 lines (wrapped).
+	#
+	my $chars_per_line = $width/2; # Character/textline.
+
+
+	my $firstline = "$student $course";
+	if (length($firstline) > $chars_per_line) {
+	    my $lastchar = $chars_per_line - length($student) - 1;
+	    if ($lastchar > 0) {
+		$course = substr($course, 0, $lastchar);
+	    } else {		# Nothing left of course:
+		$course = '';
+	    }
+	}
+	if (length($assignment) > $chars_per_line) {
+	    $assignment = substr($assignment, 0, $chars_per_line);
+	}
+	
 	$format =  "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}";
 	
     } else {
-	$format =~ s/%a/$assignment/g;
-	$format =~ s/%c/$course/g;
-	$format =~ s/%n/$student/g;
+	# An open question is how to handle long user formatted page headers...
+	# A possible future is to support e.g. %na so that the user can control
+	# the truncation of the elements that can appear in the header.
+	#
+	$format =  &printf_style_subst("a", $format, $assignment);
+	$format =  &printf_style_subst("c", $format, $course);
+	$format =  &printf_style_subst("n", $format, $student);
     }
     
 
@@ -761,7 +825,7 @@
     my $courseidinfo = &get_course();
     if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
     my $header_text  = $parmhash{'print_header_format'};
-    $header_text     = &format_page_header($header_text, $assignment,
+    $header_text     = &format_page_header($textwidth, $header_text, $assignment,
 					   $courseidinfo, $name);
     my $topmargintoinsert = '';
     if ($topmargin ne '0') {$topmargintoinsert='\setlength{\topmargin}{'.$topmargin.'}';}
@@ -1467,7 +1531,7 @@
 		    if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
 		    $prevassignment=$assignment;
 		    my $header_text = $parmhash{'print_header_format'};
-		    $header_text    = &format_page_header($header_text,
+		    $header_text    = &format_page_header($textwidth, $header_text,
 							  $assignment, 
 							  $courseidinfo, 
 							  $name);
@@ -2005,7 +2069,7 @@
     if ($usersection ne '') {$courseidinfo.=' - Sec. '.$usersection}
     my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header');
     my $header_line =
-	&format_page_header($parmhash{'print_header_format'},
+	&format_page_header($LaTeXwidth, $parmhash{'print_header_format'},
 			    $currentassignment, $courseidinfo, $fullname);
     my $header_start = ($columns_in_format == 1) ? '\lhead'
 	                                         : '\fancyhead[LO]';