#!/usr/bin/perl # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # mp2eps version 1.0 : produce postscript files from metapost sources # # Copyright (C) 2004 Glad Deschrijver, Wed Jun 16 2004, Wed Oct 06 2004 # # Glad.Deschrijver@UGent.be # # # # This program is free software; you can redistribute it and/or # # modify it under the terms of the GNU General Public License # # as published by the Free Software Foundation; either version 2 # # of the License, or (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program; if not, write to the Free Software # # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # # USA. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # use File::Spec::Functions; use File::Basename; use File::Find; use File::Copy; $delete = "yes"; $preview = "no"; $previewcmd = "gv"; $rename = "yes"; $standalone = "yes"; $typeset = "latex"; $progname = "$0"; $version = "1.0"; $copyright = "Copyright (C) 2004 Glad Deschrijver"; $file = ""; $extension = 1; $mp2epstmp = "_mp2eps_"; sub usage { print < --preview=yes/no preview using program defined by --previewcommand [$preview] -p short for --preview=yes --previewcommand=CMD use CMD to preview the output file [$previewcmd] -h, --help display this help and exit -v, --version output version information and exit EOF } sub parsefilename { if ($file eq "") { print "$progname: At least one file name is required as an argument.\n"; exit 1; } else { $F = $file; $F =~ s/\.[^\.]*$//; $ext = $file; $ext =~ s/.*\.//g; if ($ext eq "mp") { $file = $F; } if (! -r "$file.mp") { print "$progname: $file.mp not found or not readable."; exit 1; } } $path = dirname("$file.mp"); $file = basename("$file.mp"); $file =~ s/\.mp$//; chdir($path); } sub createtexfile { $tmpfile2 = "${tmpfile}.tex"; open(F, ">$tmpfile2"); print F "\\documentclass\{report\}\n"; print F "\\usepackage\{amssymb\}\n"; print F "\\usepackage\{amsmath\}\n"; print F "\\usepackage\{times,mathptmx\}\n"; print F "\\usepackage\{graphicx\}\n"; print F "\\usepackage\{color\}\n"; print F "\\begin\{document\}\n"; print F "\\pagestyle\{empty\}\n"; print F "\\includegraphics\{$file.$extension\}\n"; print F "\\end\{document\}"; close(F); } sub removewanted { /^$tmpfile/ && unlink($File::Find::name); } sub typesetfile { $tmpfile = "${file}${mp2epstmp}"; if ($^O eq "MSWin32") { system("mptotex \"$file.mp\" > \"${tmpfile}.tex\""); } else { system("mpto -tex \"$file.mp\" > \"${tmpfile}.tex\""); } system("latex \"${tmpfile}.tex\""); system("dvitomp \"${tmpfile}.dvi\" \"$file.mpx\""); find(\&removewanted, curdir()); system("mpost \"$file.mp\""); open(F, "<$file.mp"); @filecontents = ; close(F); $i = 0; foreach $fileline (@filecontents) { if ($fileline =~ /^beginfig/) { $fileline =~ s/.*\(//; $fileline =~ s/\).*//; chop($fileline); $fignums[$i++] = $fileline; } } undef @filecontents; for ($i = 0; $i < @fignums; $i++) { $extension = $fignums[$i]; $file2 = "${file}_${extension}"; if ($standalone eq "yes") { createtexfile(); system("latex $tmpfile"); system("dvips -E ${tmpfile}.dvi -o ${tmpfile}.eps"); open(F, "<${tmpfile}.eps"); @tmpfilecontents = ; close(F); open(F, ">${file2}.eps"); foreach $tmpfileline (@tmpfilecontents) { $tmpfileline =~ s/$tmpfile\.eps/\"${file2}.eps\"/g; $tmpfileline =~ s/$tmpfile\.dvi/\"${file2}.dvi\"/g; print F $tmpfileline; } close(F); undef @tmpfilecontents; # move("${tmpfile}.eps", "${file2}.eps"); find(\&removewanted, curdir()); } else { move("${file}.${extension}", "${file2}.eps"); } if ($preview eq "yes") { system("$previewcmd $file2.eps"); } } } sub deleteauxfiles { unlink("${file}.mpx"); unlink("${file}.log"); for ($i = 0; $i < @fignums; $i++) { unlink("${file}.$fignums[$i]"); } if ($rename eq "yes") { for ($i = 0; $i < @fignums2; $i++) { unlink("${file}_$fignums2[$i].eps"); } } unlink("mptextmp.mp"); unlink("mptextmp.mpx"); } sub renameoutputfiles { open(F, "<${file}.mp"); @filecontents = ; close(F); $i = 0; foreach $fileline (@filecontents) { if ($fileline =~ /\%.* /) { $filenum = $fileline; $filename = $fileline; $filenum =~ s/.*number=\"([^\"]*)\".*/\1/; $filename =~ s/.*name=\"([^\"]*)\".*/\1/; chop($filenum); chop($filename); $fignums2[$i++] = $filenum; $filename =~ s/\.(eps|ps)$//; $tmpfile = "${file}_$i"; open(F, "<${tmpfile}.eps"); @tmpfilecontents = ; close(F); open(F, ">${filename}.eps"); foreach $tmpfileline (@tmpfilecontents) { $tmpfileline =~ s/$tmpfile\.eps/${filename}.eps/g; $tmpfileline =~ s/$tmpfile\.dvi/${filename}.dvi/g; print F $tmpfileline; } close(F); undef @tmpfilecontents; } } undef @filecontents; } foreach $arg (@ARGV) { if ($arg eq "-h" || $arg eq "--help") { usage(); exit 0; } elsif ($arg =~ /^--preview=/) { $preview = $arg; $preview =~ s/^--preview=//; } elsif ($arg eq "-p") { $preview = "yes"; } elsif ($arg =~ /^--standalone=/) { $standalone = $arg; $standalone =~ s/^--standalone=//; } elsif ($arg eq "-s") { $standalone = "yes"; } elsif ($arg =~ /^--rename=/) { $rename = $arg; $rename =~ s/^--rename=//; } elsif ($arg =~ /^--typeset=/) { $typeset = $arg; $typeset =~ s/^--typeset=//; } elsif ($arg eq "-v" || $arg eq "--version") { print "$progname $version\n"; print "$copyright\n"; print "This program comes with ABSOLUTELY NO WARRANTY. This is free\n"; print "software, and you are welcome to redistribute it under\n"; print "the terms of the GNU General Public License.\n"; exit 0; } elsif ($arg =~ /^-/) { usage(); exit 1; } else { $file = $arg; } } parsefilename; typesetfile; if ($rename eq "yes") { renameoutputfiles; } if ($delete eq "yes") { deleteauxfiles; }