]> git.lyx.org Git - lyx.git/blobdiff - lib/reLyX/BasicLyX.pm
The Box patch
[lyx.git] / lib / reLyX / BasicLyX.pm
index e2d730b7e506cf6bc44b9a80b1261aff2ab41d05..d5ddc5c1fa6f91805a5a9f932bce6531affd88d8 100644 (file)
@@ -218,14 +218,6 @@ my $MathEnvironments = "(math|displaymath|xxalignat|(equation|eqnarray|align|ali
 # ListLayouts may have standard paragraphs nested inside them.
 my $ListLayouts = "Itemize|Enumerate|Description";
 
-# Striaght translation of LaTeX lengths to LyX ones.
-my %lengthAsLyXString = ('\textwidth' => 'text%',
-                        '\columnwidth' => 'col%',
-                        '\paperwidth' => 'page%',
-                        '\linewidth' => 'line%',
-                        '\paperheight' => 'pheight%',
-                        '\textheight' => 'theight%');
-
 # passed a string and an array
 # returns true if the string is an element of the array.
 sub foundIn {
@@ -360,6 +352,60 @@ sub ending_math {
     return 0;
 }
 
+
+sub regularizeLatexLength {
+    my $LatexLength = shift;
+
+    # Remove any whitespace
+    $LatexLength =~ s/\s//g;
+    # Remove a leading '+' as unnecessary
+    $LatexLength =~ s/^\+?(\d)/$1/;
+    # Split into value and unit parts
+    my $val;
+    my $unit;
+    if ($LatexLength =~ /(-?\d+[.,]?\d*)(\\?[a-zA-Z]*)$/) {
+       $val  = $1;
+       $unit = $2;
+    }
+    # If the input is invalid, return what we have.
+    return $LatexLength if ($val eq '' || $unit eq '');
+
+    # '4,5' is a valid LaTeX number. Change it to '4.5'
+    $val =~ s/,/./;
+    # If the unit is not a LaTeX macro, then ensure it is lower case
+    if (!($unit =~ /^\\/)) {
+       $unit =~ s/([a-z]*)/\L$1/i;
+    }
+
+    $LatexLength = $val . $unit;
+    return $LatexLength;
+}
+    
+
+sub getAsLyXLength {
+    # Straight translation of LaTeX lengths to LyX ones.
+    my %lengthAsLyXString = ('\textwidth'   => 'text%',
+                            '\columnwidth' => 'col%',
+                            '\paperwidth'  => 'page%',
+                            '\linewidth'   => 'line%',
+                            '\paperheight' => 'pheight%',
+                            '\textheight'  => 'theight%');
+
+    my $LatexLength = shift;
+    $LatexLength = regularizeLatexLength($LatexLength);
+
+    my $LyXLength = $LatexLength;
+    # If $LatexLength is something like '4.5\columnwidth', translate into
+    # LyXese.
+    if ($LatexLength =~ /([+-]?\d+\.?\d*)(\\[a-z]*)/) {
+       if (defined($lengthAsLyXString{$2})) {
+           $LyXLength = ($1 * 100) . $lengthAsLyXString{$2};
+       }
+    }
+
+    return $LyXLength;
+}
+
 ##########################   MAIN TRANSLATOR SUBROUTINE   #####################
 sub basic_lyx {
 # This subroutine is called by Text::TeX::process each time subroutine
@@ -1046,14 +1092,7 @@ sub basic_lyx {
                my $height = '0pt';
                $tok = $fileobject->eatOptionalArgument;
                if (defined($tok->print)) {
-                   $height = $tok->print;
-                   # if something like '4.5\columnwidth', translate into
-                   # LyXese.
-                   if ($height =~ /([0-9.]*)\s*(\\[a-z]*)/) {
-                       if (defined($lengthAsLyXString{$2})) {
-                           $height = ($1 * 100) . $lengthAsLyXString{$2};
-                       }
-                   }
+                   $height = getAsLyXLength($tok->print);
                }
 
                # Read the inner-pos optional argument, if it exists
@@ -1067,21 +1106,12 @@ sub basic_lyx {
 
                # Read the width as (a reference to) an array of tokens.
                $tok = $fileobject->eatBalanced;
-               # $width is Something like either '4.5cm' or '\columnwidth'.
-               my $width = pop(@{$tok})->print;
-               # If $width is something like '\columnwidth', then manipulate
-               # it into LyX format and also extract the length itself.
-               if (defined($lengthAsLyXString{$width})) {
-                   $width = $lengthAsLyXString{$width};
-                   my $val = pop(@{$tok});
-                   $val = (defined($val)) ? $val->print : '0';
-                   $width = ($val * 100) . $width;
-               }
+               my $width = getAsLyXLength($tok->exact_print);
 
                print OUTFILE "position $pos\n";
                print OUTFILE "inner_position $innerpos\n";
-               print OUTFILE "height $height\n";
-               print OUTFILE "width $width\n";
+               print OUTFILE "height \"$height\"\n";
+               print OUTFILE "width \"$width\"\n";
                print OUTFILE "collapsed false\n";
 
            # \begin document