--- Render.pm.orig 2007-12-02 12:45:55.000000000 +0000 +++ Render.pm 2007-12-03 19:01:13.000000000 +0000 @@ -1046,132 +1046,105 @@ # ========================= sub trimInlineImage_v2 { - my ($in,$sandbox,$bgcolor,$LATEXWDIR,$ptsz) = @_; - - (my $out = $in ) =~ s/\.(png|gif)/.xpm/; + (my $out = $in) =~ s/\.(png|gif)/.xpm/; + my $trim_out = "mod_$out"; $sandbox->sysCommand("$PATHTOCONVERT %IN|F% %OUT|F%", IN => $in, - OUT => $out ); - - my ($pre, $xpm); - my ($canvaschar,$cpp) = (' ',1); - my ($col, $flag, $cnt) = (0,0,0); - my ($midu, $midl, $sum) = (0,0,''); - - open(F,"<$out") || print STDERR $!; - - ## run through the XPM image line-by-line - ## counting the number of lines above and below the 'cdot' - while(){ - - if ($flag) { - # print length($_); - if ($col == 0) { - $col = (length($_)-4)/$cpp; - $sum = '0' x $col; - # print $sum."\n"; - } - # print ' '.$col.' '.($col == 0); - $xpm .= $_; # store the image as we go - - # count of line above 'cdot' - $midu = $cnt if ( !(substr($_,1,$cpp) =~ m/\Q$canvaschar\E|\;/) - and ($midu eq 0 ) ); - # count ending line of 'cdot' - $midl = $cnt if ( !(substr($_,1,$cpp) =~ m/\Q$canvaschar\E|\;/) ); - - $cnt += 1 if ( substr($_,0,1) eq "\""); # count the lines - - # print substr($_,1,$cpp)." $midu $cnt\n"; - - ### form a bit-map of the pixels used to - ### calculate the horizontal trim later. - $_ =~ s/^\"//; $_ =~ s/\"\,?\n$//; - # print $_."\n"; - foreach my $c ( 0 .. (length($_)/$cpp-1) ) { - my $t = substr($_,$c*$cpp,$cpp); - if ( ($t ne $canvaschar) or substr($sum,$c,1) == 1 ) { - substr($sum,$c,1,'1'); - } - } - # print $sum."\n"; - - } else { - $pre .= $_; - if ($_ =~ m/\"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\"/) { - $cpp = $4; - &TWiki::Func::writeDebug( "cpp:'".$cpp."'" ) if ($debug); - } - if ($_ =~ m/None|gray100/) { - $canvaschar = substr($_,1,$cpp); - } - # &TWiki::Func::writeDebug( "canvaschar:'".$canvaschar."'" ) if ($debug); + OUT => $out); + trim_file_v2($out, $trim_out); + $sandbox->sysCommand("$PATHTOCONVERT %OUT|F% -transparent %BGC|S% %IN|F%", + OUT => $trim_out, + BGC => $bgcolor, + IN => $in); + unlink("mod_$out") unless ($debug); + unlink("$out") unless ($debug); +} - } - $flag = 1 if m!/\*\spixels\s\*/!; # switch between 'pre' and the image +sub trim_file_v2 { + my ($infile, $outfile) = @_; + my ($pre, $img) = read_xpm($infile); + my $cpp = get_xpm_cpp($pre); + my $canvaschar = get_xpm_char($pre, 'None', $cpp) + || get_xpm_char($pre, 'gray100', $cpp); + trim_img_v2($img, $canvaschar, $cpp); + my ($nr, $nc) = (scalar(@$img), length($img->[0])/$cpp); + for (@$pre) { + /^"/ or next; + s/^"[0-9]+\s+[0-9]+/"$nc $nr/; + last; } + open(F,">$outfile") or print STDERR $!; + print F $_ + for @$pre; + print F qq("$_",\n) + for @$img[0 .. @$img - 2]; + print F qq("$img->[-1]"};\n); close(F); - # print $xpm; - +} - # print "$cnt $midu $midl $col\n"; - # print "blank uptop: ".($midu)."\n"; - # print "blank below: ".($cnt-$midl-1)."\n"; - - my $adt = ($cnt - $midl - 1 - $midu); - $adt = 0 if ($adt < 0); - # print "add to top: ".$adt."\n"; - - my $adb = $midu - ($cnt - $midl - 1) ; - $adb = 0 if ($adb < 3); - # print "add to bottom: ".$adb."\n"; - - ## calculate the amount of horizontal trim - # print $sum ."\n"; - my ($frnt,$back) = (0,0); - $frnt = length($1)*$cpp if ($sum =~ m/^(1+0+)/); # from the left - $back = length($1)*$cpp if ($sum =~ m/(0+1+)$/); # and from the right - # print "frnt: $frnt back: $back\n"; - - $pre =~ m/\"$col\s+$cnt/; - my $nr = $cnt + $adt + $adb; # calculate the new number of rows - my $nc = $col - ($frnt + $back)/$cpp; # calculate the new number of columns - $pre =~ s/\"$col\s+$cnt/\"$nc $nr/; - - ## output the image - open(F,">mod_$out") || print STDERR $!; - print F $pre; - for (1..$adt) { - print F '"'; - print F $canvaschar x $nc; - print F "\",\n"; - } - # print F $xpm; - foreach my $l (split(/\n/,$xpm)) { - $l =~ s/^\".{$frnt}/\"/; - $l =~ s/.{$back}\"(\,?)$/\"$1/; - $l .= ',' if ( !($l =~ m/\,$/) and ($adb > 0) ); - - print F $l."\n" unless ( ($l =~ m/^\};/) and ($adb > 0) ); +sub trim_img_v2 { + my ($img, $canvaschar, $cpp) = @_; + my $sum = ""; + my ($midu, $midl); + my $edge = "0{0,2}"; + for (my $cnt = 0; $cnt < @$img; ++$cnt) { + (my $bitmap = $img->[$cnt]) + =~ s/(.{$cpp})/$1 eq $canvaschar ? 0 : 1/ge; # Get bitmap + $sum = "$sum" | "$bitmap"; + if ($bitmap =~ /^${edge}1/o) { # consider this to mean \cdot start + $midu = $cnt if !defined($midu); + $midl = $cnt; + } } - for (1..$adb) { - print F '"'; - print F $canvaschar x $nc; - print F "\",\n"; + my ($lskip, $rskip) = (0, 0); # if in doubt, leave as is + $lskip = length($1) * $cpp + if $sum =~ /^(${edge}1+0+)/o; + $rskip = length($1) * $cpp + if $sum =~ /(0+1+${edge})$/o; + s/^.{$lskip}(.*).{$rskip}$/$1/ + for @$img; + my $l = length($img->[0])/$cpp; + my $adt = (@$img - $midl - 1 - $midu); + unshift(@$img, ($canvaschar x $l) x $adt) + if $adt > 0; + my $adb = $midu - (@$img - $midl - 1) ; + push(@$img, ($canvaschar x $l) x $adb) + if $adb >= 3; +} + +sub read_xpm { + my ($infile) = @_; + local *F; + open(F, "<$infile") or print STDERR $!; + my (@pre, @img); + my $curr = \@pre; + while () { + push(@$curr, $_); + $curr = \@img if m!/\*\spixels\s\*/!; } - print F "};\n" if ($adb > 0); - close(F); + @img = grep { /^"/ } @img; + s/"(.*)".*/$1/s for @img; + return (\@pre, \@img); +} +sub get_xpm_cpp { + my ($pre) = @_; + for (@$pre) { + /\"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\"/ + or next; + &TWiki::Func::writeDebug( "cpp:'$4'" ) if ($debug); + return $4; + } +} - $sandbox->sysCommand("$PATHTOCONVERT mod_%OUT|F% -transparent %BGC|S% %IN|F%", - OUT => $out, - BGC => $bgcolor, - IN => $in); - - unlink("mod_$out") unless ($debug); - unlink("$out") unless ($debug); +sub get_xpm_char { + my ($pre, $color, $cpp) = @_; + for (@$pre) { + /\Q$color\E/ + and return substr($_, 1, $cpp); + } } # =========================