#!/usr/bin/perl # convert Fig file to HTML imagemap # Andrew Daviel TRIUMF Dec 2003 advax@triumf.ca #rect two 94 273 160 347 #circle circle 228 66 245 83 #poly L 275 188 329 190 328 227 399 229 399 265 283 264 # # # # # # # also # alt="xx" mandatory ... # .. now can use shape in A; see http://www.w3.org/TR/html401/struct/objects.html#adef-shape unless ($ARGV[1]) { &usage ; exit ; } open (IN,$ARGV[0]) or die ; open (OUT,">$ARGV[1]") or die ; if ($ARGV[3]) { open (OUT2,">>$ARGV[3]") or open (OUT2,">$ARGV[3]") or die ; $mkarea = 1 ; $out2 = '' ; print OUT2 "\n" ; } unless ($apn) { $apn = 9 ; } $indepth = 0 ; if ($ARGV[2]) { $indepth = $ARGV[2] ; } $_ = () ; unless (/^#FIG 3.2/) { die "Not a FIG version 3.2 file\n" ; } while () { chomp ; tr/ / /s ; if (/^2 5 /) { # picture ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$u6,$u7,$npts) = split(/ /) ; $_ = () ; chomp ; tr/ \t/ /s ; s/^ // ; ($flip,$name) = split(/ /) ; print "Picture $name flip $flip\n" ; $_ = () ; chomp ; tr/ \t/ /s ; s/^ // ; @xy = split(/ /) ; $x1 = $x2 = $y1 = $y2 = '' ; for ($i=0;$i<4;$i++) { $x = int(shift(@xy)/15+0.5) ; $y = int(shift(@xy)/15+0.5) ; if ($x1 eq '' || $x < $x1) { $x1 = $x ; } if ($x2 eq '' || $x > $x2) { $x2 = $x ; } if ($y1 eq '' || $y < $y1) { $y1 = $y ; } if ($y2 eq '' || $y > $y2) { $y2 = $y ; } } if ($depth == $indepth || !$indepth) { $w = ($x2 - $x1) ; $h = $y2 - $y1 ; if ($w<0) { $w = -$w ; } if ($h<0) { $h = -$h ; } print "add picture rect $w x $h\n" ; if ($mkarea) { print OUT2< EOT } } } elsif (/^1 3 /) { # circle radius # 1 x sty wid fg bg dep x fil angle x x X Y R R X Y ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$x,$y,$r,$r2,$x2,$y2,$z,$z2) = split(/ /) ; if ($depth == $indepth || !$indepth) { $r = int($r/15+0.5) ; $y = int($y/15+0.5) ; $x = int($x/15+0.5) ; print "circle $x,$y,$r\n" ; $x2 = $x + $r ; if ($url) { if ($comment) { print OUT "#$comment\n" ; } print OUT "circle $url $x $y $x2 $y\n" ; if ($mkarea) { $out2 .= "\n" ; } } $comment = $url = '' ; } } elsif (/^5 1 /) { # arc # 5 1 0 1 0 7 50 0 -1 0.000 0 0 0 0 8713.919 7162.297 7860 7290 8670 6300 9570 7050 } elsif (/^1 4 /) { # circle diameter # 1 4 0 1 0 7 50 0 -1 0.000 1 0.0000 10830 1020 417 417 10413 1020 11 } elsif (/^1 1 /) { # ellipse radius # 1 1 0 1 0 7 50 0 -1 0.000 1 0.0000 8520 3270 690 330 8520 3270 9210 3270 } elsif (/^1 2 /) { # ellipse diameter # 1 2 0 1 0 7 50 0 -1 0.000 1 0.0000 11040 3315 450 285 10590 3315 11 # } elsif (/^2 1 /) { # open poly # #2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 5 } elsif (/^2 4 /) { # rect rounded # 2 4 0 1 0 7 50 0 -1 0.000 0 0 13 0 0 5 } elsif (/^3 2 /) { # inter spline open #3 2 0 1 0 7 50 0 -1 0.000 0 0 0 4 } elsif (/^3 3 /) { # close inter spline #3 3 0 1 0 7 50 0 -1 0.000 0 0 0 4 } elsif (/^3 0 /) { # open approx spline # 3 0 0 1 0 7 50 0 -1 0.000 0 0 0 4 } elsif (/^3 1 /) { # closed approx spline #3 1 0 1 0 7 50 0 -1 0.000 0 0 0 5 } elsif (/^#(.*)/) { if ($url) { $comment = $1 ; print "Comment: $comment\n" ; } else { $url = $1 ; $url =~ s/^ // ; if ($url =~ /^[\s]*href[\s]*=[\s]*"(.*)"/i) { $url = $1 ; # for fig2dev compatability } print "URL $url\n" ; if ($url =~ /^js:(.*)/) { $url = "javascript:alert(\'$1\')" ; } } } elsif (/^2 2 /) { # box ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$u6,$u7,$npts) = split(/ /) ; $_ = () ; chomp ; tr/ \t/ /s ; s/^ // ; @xy = split(/ /) ; $x1 = $x2 = $y1 = $y2 = '' ; for ($i=0;$i<4;$i++) { $x = int(shift(@xy)/15+0.5) ; $y = int(shift(@xy)/15+0.5) ; if ($x1 eq '' || $x < $x1) { $x1 = $x ; } if ($x2 eq '' || $x > $x2) { $x2 = $x ; } if ($y1 eq '' || $y < $y1) { $y1 = $y ; } if ($y2 eq '' || $y > $y2) { $y2 = $y ; } } if ($depth == $indepth || !$indepth) { $w = ($x2 - $x1) ; $h = $y2 - $y1 ; if ($w<0) { $w = -$w ; } if ($h<0) { $h = -$h ; } print "add rect $w x $h\n" ; if ($url) { if ($comment) { print OUT "#$comment\n" ; } print OUT "rect $url $x1,$y1 $x2,$y2\n" ; if ($mkarea) { $out2 .= "\n" ; } } $comment = $url = '' ; } } elsif (/^2 3 /) { # closed line ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$u6,$u7,$npts) = split(/ /) ; if ($depth == $indepth || !$indepth) { print "line $width $npts points\n" ; $wid = $width*0.012 ; $start = 1 ; undef @x ; undef @y ; while (1) { unless ($npts) { last ; } $_ = () ; chomp ; unless (/^\t[\s]*[\d]+/) { print "expected TAB + digits\n" ; } tr/ \t/ /s ; s/^ // ; @pts = split(/ /) ; $npts2 = @pts ; unless ($npts2/2 == $npts || $npts2 == 12) { print "expected $npts points got $npts2 pairs\n" ; } $npts -= $npts2/2 ; if ($start) { $x = int(shift(@pts)/15+0.5) ; $y = int(shift(@pts)/15+0.5) ; push(@x,$x) ; push(@y,$y) ; print "x $x y $y\n" ; } for ($i=$start;$i<$npts2/2;$i++) { $x = int(shift(@pts)/15+0.5) ; $y = int(shift(@pts)/15+0.5) ; push(@x,$x) ; push(@y,$y) ; print "x $x y $y\n" ; } $start = 0 ; } if ($url) { if ($comment) { print OUT "#$comment\n" ; } print OUT "poly $url" ; for ($i=0;$i<(@x-1);$i++) { print OUT " $x[$i] $y[$i]" ; } print OUT "\n" ; if ($mkarea) { $out2 .= "\n" ; } } $comment = $url = '' ; } } elsif(/^4 [\d] /) { # text # 4 u fg depth u font pt angle flags # 0 times add 1 bold add 2 oblique 0 # 4 av ss 1 # 8 bookman 2 # 12 courier 3 # 16 helvetica ss 4 # 20 helveitic narrow ss 5 # 24 new sc 6 # 28 palatino 7 # 33 chancery $ptz{0} = 1.2 ; $ptz{1} = 1.4 ; $ptz{2} = 1.4 ; $ptz{3} = 1.7 ; $ptz{4} = 1.4 ; $ptz{5} = 1.1 ; $ptz{6} = 1.4 ; $ptz{7} = 1.4 ; $ptz{8} = 1 ; @f = split(/ /) ; $fg = $f[2]; $depth = $f[3] ; $font = $f[5] ; $pt = $f[6] ; $angle = $f[7] ; $x = int($f[11]/15+0.5) ; $y = int($f[12]/15+0.5) ; if ($depth == $indepth || !$indepth) { $fntype = int($font/4) ; $fnsty = $font - 4*$fntype ; for ($i=0;$i<13;$i++) { shift(@f) ; } $text = join(' ',@f) ; $text =~ s/\\001$// ; $text =~ s/\\([\d][\d][\d])/pack("C",oct($1))/ge; print "text at $x $y font $font $pt pt fntype $fntype fnsty $fnsty\n$text\n" ; $xmag = $ptz{$fntype}*$pt ; $ymag = 1.6*$pt ; $xoff = $x ; $yoff = $y ; $fwidth = sprintf("%f.1",$pt/18) ; if ($font & 2) { $fwidth *= 1.5 ; } $wid = $fwidth*0.012 ; $ap = $draw_r{$wid} ; @word = unpack("C*",$text) ; # foreach $ch (@word) { # &cdraw(chr($ch)) ; # } } } elsif(/^[\d] [\d] /) { print "Unknown type $_\n" ; } } close(OUT) ; if ($mkarea) { print OUT2 "$out2\n" ; close(OUT2) ; } sub usage { print STDERR<