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

foxr lon-capa-cvs@mail.lon-capa.org
Mon, 27 Dec 2004 14:06:33 -0000


foxr		Mon Dec 27 09:06:33 2004 EDT

  Modified files:              
    /loncom/xml	londefdef.pm 
  Log:
  - Bug 3602 - attempt to address image alignment.  I don't think there are
  any perfect solutions for horizontal alignment, however I've offered the
  user a choice between two evils by adding the TeXwrap element to the 
  <IMG> tag :   
     TeXwrap='parbox' attempts to use a parbox and newlines. This 
  results in very little wrapping but tends to ensure there's nothing on top
  of the picture.
     TeXwrap = 'parpic' attempts to use a parpic to get the text to wrap around 
  the picture better.. however if the paragraph is too short, the next para will
  fill into the picture.. and even worse, the parpic seems not to honor the end
  of page very well.
  
  parbox is the default because it appears to be the safest at this point.
  
  
Index: loncom/xml/londefdef.pm
diff -u loncom/xml/londefdef.pm:1.247 loncom/xml/londefdef.pm:1.248
--- loncom/xml/londefdef.pm:1.247	Fri Nov 19 17:16:33 2004
+++ loncom/xml/londefdef.pm	Mon Dec 27 09:06:32 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.247 2004/11/19 22:16:33 albertel Exp $
+# $Id: londefdef.pm,v 1.248 2004/12/27 14:06:32 foxr Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -163,6 +163,7 @@
                           '\usepackage{textcomp}'.
                           '\usepackage{makeidx}'.
                           '\usepackage[dvips]{graphicx}'.
+			  '\usepackage{picins}'.
                           '\usepackage{epsfig}'.
                           '\usepackage{calc}'.
                           '\usepackage{amsmath}'.
@@ -2443,13 +2444,46 @@
 	    $currentstring.='[IMAGE: '.$alttag.']';
 	}
     } elsif ($target eq 'tex') {
+	#
+	#  The alignment will require some superstructure to be put around
+	#  the \includegraphics stuff.  At present we can only partially
+	#  simulate the alignments offered by html.
+	#
+	#
+	my $align = lc(&Apache::lonxml::get_param('align', 
+						  $parstack,
+						  $safeeval,
+						  undef,1));
+	if(!$align) {
+	    $align = "bottom";	# This is html's default so it's ours too.
+	}
+	#
+	&Apache::lonxml::debug("Alignemnt = $align");
+	#  LaTeX's image/text wrapping is really bad since it wants to
+	#  make figures float.  
+        #   The user has the optional parameter (applicable only to l/r
+	# alignment to use the picins/parpic directive to get wrapped text
+	# this is also imperfect.. that's why we give them a choice...
+	# so they can't yell at us for our choice.
+	#
+	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
+							    $parstack,
+							    $safeeval,
+							    undef,0);
+	&Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
+	if(!$latex_rendering) {
+	    $latex_rendering = "parbox";
+	}
+	&Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
+
 	my $oldSRC=$src;
         $oldSRC=~s/\.(gif|jpg|png)$/\.eps/;
 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 	#if original gif/jpg/png file exist do following:
 	if (-e $src) {
 	    #what is the image size?
-	    my $width_param=&image_size($src,$scaling,$parstack,$safeeval);
+	    my $width_param=&image_width($src,$scaling,$parstack,$safeeval);
+	    my $height_param=&image_height($src,$scaling,$parstack,$safeeval);
             my ($file,$path)=&file_path($src); 
 	    my $newsrc = $src;
 	    $newsrc =~ s/\.(gif|jpg|png)$/.eps/i;
@@ -2459,7 +2493,8 @@
 	    if (-e $newsrc) {
 		#eps counterpart for image exist 
 		if ($path) {
-		    $currentstring .= '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
+		    $currentstring .= '\graphicspath{{'.$path.'}}'
+			             .'\includegraphics[width='.$width_param.' mm,height='.$height_param.'mm]{'.$file.'} ';
 		}
 	    } else {
 		#there is no eps counterpart for image - check for ps one
@@ -2475,6 +2510,35 @@
 		    $currentstring.=&eps_generation($src,$file,$width_param);
 		}
 	    }
+	    #    If there's an alignment specification we need to honor it here.
+	    #    For the horizontal alignments, we will also honor the
+	    #    value of the latex specfication.  The default is parbox,
+	    #    and that's used for illegal values too.  
+	    #    
+	    #    Even though we set a default alignment value, the user
+	    #    could have given us an illegal value.  In that case we
+	    #    just use the default alignment of bottom..
+	    if      ($align eq "top")    {
+		$currentstring = '\raisebox{-'.$height_param.'mm}{'.$currentstring.'}';
+	    } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
+		my $offset = $height_param/2;
+		$currentstring = '\raisebox{-'.$offset.'mm}{'.$currentstring.'}';
+	    } elsif ($align eq "left")   { 
+		if ($latex_rendering eq "parpic") { 
+		    $currentstring = '\parpic[l]{'.$currentstring.'}';
+		} else {    	                                 # parbox rendering
+		    $currentstring = '\newline'."\n".'\parbox{'.$width_param.'mm}{'.$currentstring.'}';
+		}
+	    } elsif ($align eq "right")  {   
+		if ($latex_rendering eq "parpic") {
+		    $currentstring = '\parpic[r]{'.$currentstring.'}';
+		} else {	                                 # parbox rendering. 
+		    $currentstring = '\parbox{'.$width_param.'mm}{\begin{flushright}'
+			             .$currentstring.'\end{flushright}} \newline'."\n";
+		}
+	    } else {		# Bottom is also default.
+		# $currentstring = '\raisebox{'.$height_param.'mm}{'.$currentstring.'}';
+	    }
 	} else {
 	    #original image file doesn't exist so check the alt attribute
 	    my $alt = 
@@ -3699,12 +3763,25 @@
 	    $width_param = $TeXwidth;
 	}
     } elsif ($TeXheight ne '') {
-	$width_param = $TeXheight/$height_param*$width_param;
+	$height_param = $TeXheight;
+	$width_param  = $TeXheight/$height_param*$width_param;
     } elsif ($width ne '') {
 	$width_param = $width*$scaling;      
     }
     if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
-    return $width_param;
+    return ($height_param, $width_param);
+}
+
+sub image_width {
+    my ($height, $width) = &image_size(@_);
+    return $width;
+}
+#  Not yet 100% sure this is correct in all circumstances..
+#  due to my uncertainty about mods to image_size.
+#
+sub image_height {
+    my ($height, $width) = &image_size(@_);
+    return $height;
 }
 
 sub eps_generation {