| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
## File: backcite.dtx Copyright (C) 1995--1999 Michael Mehlich |
| 4 |
## This program can be redistributed and/or modified under the terms |
| 5 |
## of the LaTeX Project Public License Distributed from CTAN |
| 6 |
## archives in directory macros/latex/base/lppl.txt; either |
| 7 |
## version 1 of the License, or any later version. |
| 8 |
|
| 9 |
### DESCRIPTION: |
| 10 |
### This script transforms dvi-files to pdf-files using dvihps and ghostscripts pdfwriter |
| 11 |
### It has been tested with emtex's dvips and ghostscript 4.01 on WindowsNT |
| 12 |
### Usage: dvi2pdf <options> |
| 13 |
### where <options> are the usual dvips options and/or one of the following ones |
| 14 |
### -nopdf produce only the ps-file not the pdf-file |
| 15 |
### (this option is automatically used if output file has suffix .ps) |
| 16 |
### -noborder produces an outputfile where the hyper-links don't have borders |
| 17 |
### -s<x>,<y> shifts the hyper-links by (<x>,<y>) |
| 18 |
|
| 19 |
#Note: This script overwrites eventually existing files named |
| 20 |
# $$.ps, $pdffile.ps, $pdffile.pdf |
| 21 |
# where $$ is the process id (as provided by perl) |
| 22 |
# and $pdffile is the output file name given with the option -o or |
| 23 |
# derived from the input file name, i.e. the input file name without suffix |
| 24 |
|
| 25 |
### CONFIGURATION: |
| 26 |
#Access paths for the programs dvihps and ghostscript |
| 27 |
$GHOSTSCRIPT = "gswin32c"; #how to call ghostscript |
| 28 |
$DVIHPS = "dvips"; #how to call hyper postscript capable dvips |
| 29 |
$REMOVEFILE = "del"; #command to delete file |
| 30 |
|
| 31 |
### Probably no changes necessary below this line |
| 32 |
|
| 33 |
#some local initializations |
| 34 |
$argno = 0; |
| 35 |
$arglist = ""; |
| 36 |
$dvifile = ""; |
| 37 |
$psfile = ""; |
| 38 |
$pdffile = ""; |
| 39 |
$xshift = 0; $yshift = 0; #shift for hyper references |
| 40 |
$referenceborder = 1; #do we want to have a border surrounding hyper references? |
| 41 |
$nopdf = 0; #we just patch the ps-file if ($nopdf == 1) |
| 42 |
|
| 43 |
#scan arguments for input and output file and preserver rest for forwarding to dvips |
| 44 |
while ($argno <= $#ARGV) |
| 45 |
{ if ($ARGV[$argno] eq "") |
| 46 |
{ #empty argument, ignore |
| 47 |
} |
| 48 |
elsif ($ARGV[$argno] =~ m/^-s([\+\-0-9]+)\,([\+\-0-9]+)$/) |
| 49 |
{ $xshift = $1; |
| 50 |
$yshift = $2; |
| 51 |
} |
| 52 |
elsif ($ARGV[$argno] =~ m/^-nopdf$/) |
| 53 |
{ $nopdf = 1; |
| 54 |
} |
| 55 |
elsif ($ARGV[$argno] =~ m/^-noborder$/) |
| 56 |
{ $referenceborder = 0; |
| 57 |
} |
| 58 |
elsif ($ARGV[$argno] =~ m/^-(.)(.+)$/) |
| 59 |
{ #argument follows flag directly |
| 60 |
if ($1 eq "o") |
| 61 |
{ #argument is output file |
| 62 |
($pdffile ne "") && die("Sorry, multiple output files cannot be handled"); |
| 63 |
$pdffile = $2; |
| 64 |
} |
| 65 |
else |
| 66 |
{ #argument is dvi-argument |
| 67 |
if ($ARGV[$argno] =~ m/ /) |
| 68 |
{ $arglist .= ' '.'"'.$ARGV[$argno].'"'; |
| 69 |
} |
| 70 |
else |
| 71 |
{ $arglist .= ' '.$ARGV[$argno]; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
elsif ($ARGV[$argno] =~ m/^-(.)$/) |
| 76 |
{ #flag without directly following argument |
| 77 |
if ( ($1 eq "a") || ($1 eq "k") || ($1 eq "m") || ($1 eq "q") || ($1 eq "r") |
| 78 |
|| ($1 eq "s") || ($1 eq "z") || ($1 eq "A") || ($1 eq "B") || ($1 eq "E") |
| 79 |
|| ($1 eq "F") || ($1 eq "K") || ($1 eq "M") || ($1 eq "N") || ($1 eq "R") |
| 80 |
|| ($1 eq "U") || ($1 eq "V") || ($1 eq "Z") |
| 81 |
) |
| 82 |
{ #flag without argument |
| 83 |
if (!($1 eq "z")) |
| 84 |
{ $arglist .= ' '.$ARGV[$argno]; |
| 85 |
} |
| 86 |
} |
| 87 |
elsif ($1 eq "i") |
| 88 |
{ die("Sorry, separate file per section cannot be handled"); |
| 89 |
} |
| 90 |
elsif ($1 eq "f") |
| 91 |
{ die("Sorry, run in filter mode not possible"); |
| 92 |
} |
| 93 |
else |
| 94 |
{ #flag with argument |
| 95 |
if ($1 eq "o") |
| 96 |
{ #argument is output file |
| 97 |
($pdffile ne "") && die("Sorry, multiple output files cannot be handled"); |
| 98 |
$pdffile = $ARGV[$argno+1]; |
| 99 |
} |
| 100 |
else |
| 101 |
{ #argument is dvi-argument |
| 102 |
if ($ARGV[$argno+1] =~ m/ /) |
| 103 |
{ $arglist .= ' '.$ARGV[$argno].' '.'"'.$ARGV[$argno+1].'"'; |
| 104 |
} |
| 105 |
else |
| 106 |
{ $arglist .= ' '.$ARGV[$argno].' '.$ARGV[$argno+1]; |
| 107 |
} |
| 108 |
} |
| 109 |
$argno++; |
| 110 |
} |
| 111 |
} |
| 112 |
else |
| 113 |
{ #nonflag argument |
| 114 |
($dvifile ne "") && die("Sorry, multiple input files cannot be handled"); |
| 115 |
$dvifile = $ARGV[$argno]; |
| 116 |
} |
| 117 |
$argno++; |
| 118 |
} |
| 119 |
$arglist = substr($arglist,1,length($arglist)-1); |
| 120 |
|
| 121 |
($dvifile eq "") && die("Sorry, no input file"); |
| 122 |
if (!($dvifile =~ m/\.[^\.\:\\\/]*$/)) |
| 123 |
{ $dvifile .= '.dvi'; |
| 124 |
} |
| 125 |
(!($dvifile =~ m/\.dvi$/)) && die("Sorry, input file has illegal suffix different from .dvi"); |
| 126 |
|
| 127 |
if ($pdffile eq "") |
| 128 |
{ ($pdffile = $dvifile) =~ s/\.[^\:\\\/\.]*$/\.pdf/; |
| 129 |
if ($pdffile eq $dvifile) |
| 130 |
{ $pdffile = $dvifile.'.pdf'; |
| 131 |
} |
| 132 |
} |
| 133 |
else |
| 134 |
{ if ($pdffile =~ m/\.ps$/) |
| 135 |
{ $nopdf = 1; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
($psfile = $pdffile) =~ s/\.[^\:\\\/\.]*$/\.ps/; |
| 140 |
|
| 141 |
$tmpfile = "$$.ps"; |
| 142 |
|
| 143 |
#output arguments |
| 144 |
print STDERR "DVI file: $dvifile\n"; |
| 145 |
print STDERR "PS file: $psfile\n"; |
| 146 |
if ($nopdf == 0) |
| 147 |
{ print STDERR "PDF file: $pdffile\n"; |
| 148 |
} |
| 149 |
print STDERR "TMP file: $tmpfile\n"; |
| 150 |
print STDERR "Arg list: $arglist\n"; |
| 151 |
print STDERR "Shifting: ($xshift,$yshift)\n"; |
| 152 |
print STDERR "HyBorder: "; if ($referenceborder) { print STDERR "preserve"; } else { print STDERR "none"; }; print STDERR "\n"; |
| 153 |
|
| 154 |
#call dvips |
| 155 |
print STDERR "*** $DVIHPS -z $arglist -o $tmpfile $dvifile\n"; |
| 156 |
system("$DVIHPS -z $arglist -o $tmpfile $dvifile"); |
| 157 |
(!(-f "$tmpfile")) && die("Error, $DVIHPS could not generate '$tmpfile'"); |
| 158 |
|
| 159 |
#path postscript file because we want to use a different fitheight for references to pages |
| 160 |
print STDERR "*** PATCH $tmpfile $psfile\n"; |
| 161 |
|
| 162 |
open(STDIN,"<$tmpfile") || die("Input file '$tmpfile' not found.\n"); |
| 163 |
open(STDOUT,">$psfile") || do { close(STDIN); die("Cannot open file '$psfile' for writing.\n") }; |
| 164 |
|
| 165 |
while (<STDIN>) |
| 166 |
{ if (/^\s*\((.+)\)\s+\[([0-9]+)\s+\[([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\]\s+([0-9]+)\]\s+def\s*$/) |
| 167 |
{ $name = $1; # name of an anchor |
| 168 |
$page = $2; # page the anchor is on |
| 169 |
$xa = $3 + $xshift; # rectangular box of anchor |
| 170 |
$ya = $4 + $yshift; |
| 171 |
$xb = $5 + $xshift; |
| 172 |
$yb = $6 + $yshift; |
| 173 |
$fitheight = (($ya > $yb) ? $ya : $yb) + 10; # screen top to show when reference to this anchor is choosen |
| 174 |
print STDOUT "($name) [$page [$xa $ya $xb $yb] $fitheight] def\n"; |
| 175 |
} |
| 176 |
elsif (/^(.*)\s+\((.+)\)\s+\[\[([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\]\s+\[(.+)\]\s+\[(.+)\]\] pdfm\s+(.*)$/) |
| 177 |
{ $before = $1; |
| 178 |
$name = $2; # name of the reference |
| 179 |
$xa = $3 + $xshift; # rectangular box of reference |
| 180 |
$ya = $4 + $yshift; |
| 181 |
$xb = $5 + $xshift; |
| 182 |
$yb = $6 + $yshift; |
| 183 |
$borderinfo = $7; #horizontal_corner_radius vertical_corner_radius box_line_width [dash_on_size] [dash_off_size] |
| 184 |
$bordercolor = $8; #red green blue |
| 185 |
$after = $9; |
| 186 |
if ($borderinfo =~ m/^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*\[\s*([0-9]+)\s+([0-9]+)\s*\]\s*$/) |
| 187 |
{ $horizontalcornerradius = $1; |
| 188 |
$verticalcornerradius = $2; |
| 189 |
$boxlinewidth = $3; |
| 190 |
$dashonsize = $4; |
| 191 |
$dashoffsize = $5; |
| 192 |
if ($referenceborder == 0) |
| 193 |
{ $boxlinewidth = 0; |
| 194 |
} |
| 195 |
$borderinfo = "$horizontalcornerradius $verticalcornerradius $boxlinewidth [$dashonsize $dashoffsize]"; |
| 196 |
} |
| 197 |
print STDOUT "$before ($name) [[$xa $ya $xb $yb] [$borderinfo] [$bordercolor]] pdfm $after\n"; |
| 198 |
} |
| 199 |
elsif (/^(.*)\s+\[\[([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\]\s+\[(.+)\]\s+\[(.+)\]\]\s+\((.+)\)\s+pdfm\s+(.*)$/) |
| 200 |
{ $before = $1; |
| 201 |
$xa = $2 + $xshift; # rectangular box of reference |
| 202 |
$ya = $3 + $yshift; |
| 203 |
$xb = $4 + $xshift; |
| 204 |
$yb = $5 + $yshift; |
| 205 |
$borderinfo = $6; #horizontal_corner_radius vertical_corner_radius box_line_width [dash_on_size] [dash_off_size] |
| 206 |
$bordercolor = $7; #red green blue |
| 207 |
$name = $8; |
| 208 |
$after = $9; |
| 209 |
if ($borderinfo =~ m/^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*\[\s*([0-9]+)\s+([0-9]+)\s*\]\s*$/) |
| 210 |
{ $horizontalcornerradius = $1; |
| 211 |
$verticalcornerradius = $2; |
| 212 |
$boxlinewidth = $3; |
| 213 |
$dashonsize = $4; |
| 214 |
$dashoffsize = $5; |
| 215 |
if ($referenceborder == 0) |
| 216 |
{ $boxlinewidth = 0; |
| 217 |
} |
| 218 |
$borderinfo = "$horizontalcornerradius $verticalcornerradius $boxlinewidth [$dashonsize $dashoffsize]"; |
| 219 |
} |
| 220 |
print STDOUT "$before [[$xa $ya $xb $yb] [$borderinfo] [$bordercolor]] ($name) pdfm $after\n"; |
| 221 |
} |
| 222 |
else |
| 223 |
{ print STDOUT $_; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
close(STDIN); |
| 228 |
close(STDOUT); |
| 229 |
|
| 230 |
# delete auxiliary file |
| 231 |
print STDERR "*** $REMOVEFILE $tmpfile\n"; |
| 232 |
system("$REMOVEFILE $tmpfile"); |
| 233 |
|
| 234 |
if ($nopdf == 0) |
| 235 |
{ # create pdf-file by calling ghostscript |
| 236 |
print STDERR "*** $GHOSTSCRIPT -q -dNOPAUSE -sDEVICE#pdfwrite -sOutputFile#$pdffile $psfile -c quit\n"; |
| 237 |
system("$GHOSTSCRIPT -q -dNOPAUSE -sDEVICE#pdfwrite -sOutputFile#$pdffile $psfile -c quit"); |
| 238 |
(!(-f "$pdffile")) && die("Error, $DVIHPS could not generate '$pdffile'"); |
| 239 |
} |