summaryrefslogtreecommitdiff
path: root/info/maketexwork/ex-06-08
blob: 9cbd15ae66dd6fc17f81960695891b734bf5600d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! perl
#
# Usage: gs-eps epsfile <outputfile> <resolution> <device>
#
# Where: epsfile is the encapsulated postscript file
#        outputfile is the output file (the default name
#          is <basename epsfile>.<device>)
#        resolution is the output resolution (default=300)
#        device is the GS driver to use (default=pbm)

($epsfile,$outputfile,$res,$device) = @ARGV;

if (! $epsfile) {
  printf "Usage: gs-eps epsfile <outputfile> <resolution>";
  printf " <gsdriver>\n";
  printf "Note: parameters are positional.  To specify a";
  printf " driver, you\n";
  printf "must also specify an outputfile and resolution.\n";
  exit 1;
  }
    
$epsfile =~ tr/\\/\//; # translate \foo\bar -> /foo/bar

if (! -r $epsfile) {
  printf "Cannot read file: $epsfile\n";
  exit 1;
  }

if (! $res)         { $res = 300 }
if (! $device)      { $device = "pbm" }

if (! $outputfile ) {
  @pathname = split(/\//,$epsfile);
  $outputfile = $pathname[$#pathname];
  $outputfile =~ s/.eps$//;
  $outputfile = join(".", $outputfile, $device);
  }

printf "Converting $epsfile to $outputfile at ${res}dpi...\n";

open (EPSFILE,$epsfile);

undef $bbox;
undef $showpg;
while (<EPSFILE>) {
  $bbox = $_ if /\%\%\s*BoundingBox:\s*\d+\s+\d+\s+\d+\s+\d+/;
  $showpage = $_ if /showpage/;
  last if ($bbox && $showpage);
  }

if (! $bbox) {
  printf "Cannot find a bounding box in $epsfile";
  exit 1;
  }

$bbox =~ s/\D*//;   # remove everything preceding the digits

($llx,$lly,$urx,$ury) = split(/\s/,$bbox);

$xsize = sprintf("%d", (($urx - $llx) * $res / 72) + 0.5);
$ysize = sprintf("%d", (($ury - $lly) * $res / 72) + 0.5);

printf "$llx neg $lly neg translate .gt. gs-eps-a.$$\n";
printf "quit .gt. gs-eps-b.$$\n";

if (! $showpg) {
  printf "showpage .gt. gs-eps-b.$$\n";
  printf "quit .gtgt. gs-eps-b.$$\n";
  }

# join sillyness to keep the length of lines in the
# script small enough to print in the book.
$gscmd = join(" ", "gs -sDEVICE=$device",
                   "-q -sOutputFile=$outputfile",
                   "-g${xsize}x${ysize} -r$res",
	 	   "gs-eps-a.$$ $epsfile -",
		   ".lt. gs-eps-b.$$");

printf "$gscmd\n";

printf "rm -f gs-eps-a.$$ gs-eps-b.$$\n";