Commit 5e8fc30b authored by zak@balfor.local's avatar zak@balfor.local
Browse files

Various improvements to the colspec fixup script

parent 1f14c6a0
Loading
Loading
Loading
Loading
+33 −9
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@
use strict;

my $table_width  = 12.75;   # cm
my $gutter_width =  0.09;   # cm
my $gutter_width =  0.55;    # 2 mm

my $str = join '', <>;

@@ -35,6 +35,11 @@ sub rel2abs {
    my $total         = 0;
    my $output        = '';
    
    my $gutters;
    my $content_width;
    my $total_width;
    my @num_cache;
    
    $str =~ /^(\s+)/;
    my $ws = $1;
    
@@ -43,11 +48,30 @@ sub rel2abs {
        push @widths, $1;
    }

    my $unit = ($table_width - ($#widths * $gutter_width)) / ($total);
    msg("!!! WARNING: Total Percent > 100%: $total%") if $total > 100;

    if (! $total) {
        die 'Something bad has happened - the script believes that there are no columns';
    }

    $gutters = $#widths * $gutter_width;
    $content_width = $table_width - $gutters;
    # Don't forget that $#... is the last offset not the count

    foreach (@widths) {
        $output .= $ws . '<colspec colnum="'. $colnum .'" colwidth="'. sprintf ("%0.2f", $_ * $unit) .'cm" />' . "\n";
        my $temp = sprintf ("%0.2f", $_/100 * $content_width);
        $total_width += $temp;

        if ($total_width > $content_width) {
            $temp -= $total_width - $content_width;
            msg("!!! WARNING: Column width reduced from " .
                ($temp + ($total_width - $content_width)) . " to $temp !!!");
            $total_width -= $total_width - $content_width;
        }
        
        $output .= $ws . '<colspec colnum="'. $colnum .'" colwidth="'. $temp .'cm" />' . "\n";
        ++$colnum;
        push @num_cache, $temp;
    }
   
    return $output . "\n$ws";