[tex-live] New, Patched Version of 'epstopdf' for GS >= 7

Volker Simonis simonis at informatik.uni-tuebingen.de
Thu Feb 20 16:42:29 CET 2003


	
Hello together,

I use pdfLaTeX and include a lot of pdf-graphics which I generate in the 
following way: GIF -> (XFIG + XV) -> EPS -> (epstopdf) -> PDF

Until now no problem. Recently however I upgraded ghostscript from 5.x to 7.x 
and realized that pdf-files generated in the above way got look quite ugly 
now. The problem is that apparently the default settings for the pdfwriter 
device in ghostscript have changed such that images are by default 
JPG-encoded (AutoFilterColorImages, ColorImageFilter, EncodeColorImages, ..). 
Of course this looks ugly for screenshots.

I could solve the problem by executing ghostscript with the right parameters, 
however there is no chance to hand over these parameters through epstopdf. So 
I hacked a new version of epstopdf (2.8) which gives the user the opportunity 
to specify these paramters if he wants. 

Unfortunately, I don't exactly now where to post the new version since I 
found no offical epstopdf home page (the version on CTAN is QUITE old, still 
version 2.5, TeXLive comes with the newest version I'm aware of: 2.7). 

I hope the new version can be usfull to others as well an that you guys know 
how to put it to the right place.

Any comments are always welcome.

Sincerely,

Volker Simonis
-------------- next part --------------
#!/usr/bin/env perl

# Change by Thomas Esser, Sept. 1998: The above lines allows us to find
# perl along $PATH rather than guessing a fixed location. The above
# construction should work with most shells.

use strict;

# A script to transform an EPS file so that:
#   a) it is guarenteed to start at the 0,0 coordinate
#   b) it sets a page size exactly corresponding to the BoundingBox
# This means that when Ghostscript renders it, the result needs no
# cropping, and the PDF MediaBox is correct.
#   c) the result is piped to Ghostscript and a PDF version written
#
# It needs a Level 2 PS interpreter.
# If the bounding box is not right, of course, you have problems...
#
# The only thing I have not allowed for is the case of
# "%%BoundingBox: (atend)", which is more complicated.
#
# Sebastian Rahtz, for Elsevier Science
#
# now with extra tricks from Hans Hagen's texutil.
#
# History
#  1999/05/06 v2.5 (Heiko Oberdiek)
#    * New options: --hires, --exact, --filter, --help.
#    * Many cosmetics: title, usage, ...
#    * New code for debug, warning, error
#    * Detecting of cygwin perl
#    * Scanning for %%{Hires,Exact,}BoundingBox.
#    * Scanning only the header in order not to get a wrong
#      BoundingBox of an included file.
#    * (atend) supported.
#    * uses strict; (earlier error detecting).
#    * changed first comment from '%!PS' to '%!';
#    * corrected (atend) pattern: '\s*\(atend\)'
#    * using of $bbxpat in all BoundingBox cases,
#      correct the first white space to '...Box:\s*$bb...'
#    * corrected first line (one line instead of two before 'if 0;';
#  2000/11/05 v2.6 (Heiko Oberdiek)
#    * %%HiresBoundingBox corrected to %%HiResBoundingBox
#  2001/03/05 v2.7 (Heiko Oberdiek)
#    * Newline before grestore for the case that there is no
#      whitespace at the end of the eps file.
#  2003/02/20 v2.8 (Volker Simonis)
#    * Added options for handing over the Adobe Destiller Paramters to GS 
#

### program identification
my $program = "epstopdf";
my $filedate="2003/02/20";
my $fileversion="2.8";
my $copyright = "Copyright 1998-2001 by Sebastian Rahtz et al.";
my $title = "\U$program\E $fileversion, $filedate - $copyright\n";

### ghostscript command name
my $GS = "gs";
$GS = "gswin32c" if $^O eq 'MSWin32';
$GS = "gswin32c" if $^O =~ /cygwin/;

### options
$::opt_help=0;
$::opt_debug=0;
$::opt_compress=1;
$::opt_gs=1;
$::opt_hires=0;
$::opt_exact=0;
$::opt_filter=0;
$::opt_outfile="";
$::opt_AutoFilterColorImages=0;
$::opt_AutoFilterGrayImages=0;
$::opt_ColorImageFilter="/FlateEncode";
$::opt_GrayImageFilter="/FlateEncode";
$::opt_MonoImageFilter="/FlateEncode";
$::opt_EncodeColorImages=1;
$::opt_EncodeGrayImages=1;
$::opt_EncodeMonoImages=1;
$::opt_DownsampleColorImages=0;
$::opt_DownsampleGrayImages=0;
$::opt_DownsampleMonoImages=0;

### usage
my @bool = ("false", "true");
my $usage = <<"END_OF_USAGE";
${title}Syntax:  $program [options] <eps file>
Options:
  --help:           print usage
  --outfile=<file>: write result to <file>
  --(no)filter:     read standard input   (default: $bool[$::opt_filter])
  --(no)gs:         run ghostscript       (default: $bool[$::opt_gs])
  --(no)compress:   use compression       (default: $bool[$::opt_compress])
  --(no)hires:      scan HiResBoundingBox (default: $bool[$::opt_hires])
  --(no)exact:      scan ExactBoundingBox (default: $bool[$::opt_exact])
  --(no)debug:      debug informations    (default: $bool[$::opt_debug])

  --(no)AutoFilterColorImages:            (default: $bool[$::opt_AutoFilterColorImages])
  --(no)AutoFilterGrayImages:             (default: $bool[$::opt_AutoFilterGrayImages])
  --ColorImageFilter=<filter>:            (default: $::opt_ColorImageFilter)
  --GrayImageFilter=<filter>:             (default: $::opt_GrayImageFilter)
  --MonoImageFilter=<filter>:             (default: $::opt_MonoImageFilter)
  --(no)EncodeColorImages:                (default: $bool[$::opt_EncodeColorImages])
  --(no)EncodeGrayImages:                 (default: $bool[$::opt_EncodeGrayImages])
  --(no)EncodeMonoImages:                 (default: $bool[$::opt_EncodeMonoImages])
  --(no)DownsampleColorImages:            (default: $bool[$::opt_DownsampleColorImages])
  --(no)DownsampleGrayImages:             (default: $bool[$::opt_DownsampleGrayImages])
  --(no)DownsampleMonoImages:             (default: $bool[$::opt_DownsampleMonoImages])

Examples for producing 'test.pdf':
  * $program test.eps
  * produce postscript | $program --filter >test.pdf
  * produce postscript | $program -f -d -o=test.pdf
Example: look for HiResBoundingBox and produce corrected PostScript:
  * $program -d --nogs -hires test.ps>testcorr.ps
END_OF_USAGE

### process options
use Getopt::Long;
GetOptions (
  "help!",
  "debug!",
  "filter!",
  "compress!",
  "gs!",
  "hires!",
  "exact!",
  "outfile=s",
  "AutoFilterColorImages!",
  "AutoFilterGrayImages!",
  "ColorImageFilter=s",
  "GrayImageFilter=s",
  "MonoImageFilter=s",
  "EncodeColorImages!",
  "EncodeGrayImages!",
  "EncodeMonoImages!",
  "DownsampleColorImages!",
  "DownsampleGrayImages!",
  "DownsampleMonoImages!",
) or die $usage;

### help functions
sub debug {
  print STDERR "* @_\n" if $::opt_debug;
}
sub warning {
  print STDERR "==> Warning: @_!\n";
}
sub error {
  die "$title!!! Error: @_!\n";
}
sub errorUsage {
  die "$usage\n!!! Error: @_!\n";
}

### option help
die $usage if $::opt_help;

### get input filename
my $InputFilename = "";
if ($::opt_filter) {
  @ARGV == 0 or
    die errorUsage "Input file cannot be used with filter option";
  $InputFilename = "-";
  debug "Input file: standard input";
}
else {
  @ARGV > 0 or die errorUsage "Input filename missing";
  @ARGV < 2 or die errorUsage "Unknown option or too many input files";
  $InputFilename = $ARGV[0];
  -f $InputFilename or error "'$InputFilename' does not exist";
  debug "Input filename:", $InputFilename;
}

### option compress
my $GSOPTS = "";
$GSOPTS = "-dUseFlateCompression=".$bool[$::opt_compress]." ";
$GSOPTS .= "-dAutoFilterColorImages=".$bool[$::opt_AutoFilterColorImages]." ";
$GSOPTS .= "-dAutoFilterGrayImages=".$bool[$::opt_AutoFilterGrayImages]." ";
$GSOPTS .= "-dColorImageFilter=".$::opt_ColorImageFilter." ";
$GSOPTS .= "-dGrayImageFilter=".$::opt_GrayImageFilter." ";
$GSOPTS .= "-dMonoImageFilter=".$::opt_MonoImageFilter." ";
$GSOPTS .= "-dEncodeColorImages=".$bool[$::opt_EncodeColorImages]." ";
$GSOPTS .= "-dEncodeGrayImages=".$bool[$::opt_EncodeGrayImages]." ";
$GSOPTS .= "-dEncodeMonoImages=".$bool[$::opt_EncodeMonoImages]." ";
$GSOPTS .= "-dDownsampleColorImages=".$bool[$::opt_DownsampleColorImages]." ";
$GSOPTS .= "-dDownsampleGrayImages=".$bool[$::opt_DownsampleGrayImages]." ";
$GSOPTS .= "-dDownsampleMonoImages=".$bool[$::opt_DownsampleMonoImages]." ";

### option BoundingBox types
my $BBName = "%%BoundingBox:";
!($::opt_hires and $::opt_exact) or
  error "Options --hires and --exact cannot be used together";
$BBName = "%%HiResBoundingBox:" if $::opt_hires;
$BBName = "%%ExactBoundingBox:" if $::opt_exact;
debug "BoundingBox comment:", $BBName;

### option outfile
my $OutputFilename = $::opt_outfile;
if ($OutputFilename eq "") {
  if ($::opt_gs) {
    $OutputFilename = $InputFilename;
    if (!$::opt_filter) {
      $OutputFilename =~ s/\.[^\.]*$//;
      $OutputFilename .= ".pdf";
    }
  }
  else {
    $OutputFilename = "-"; # standard output
  }
}
if ($::opt_filter) {
  debug "Output file: standard output";
}
else {
  debug "Output filename:", $OutputFilename;
}

### option gs
if ($::opt_gs) {
  debug "Ghostscript command:", $GS;
  debug "Compression:", ($::opt_compress) ? "on" : "off";
}

### open input file
open(IN,"<$InputFilename") or error "Cannot open",
  ($::opt_filter) ? "standard input" : "'$InputFilename'";
binmode IN;

### open output file
if ($::opt_gs) {
  my $pipe = "$GS -q -sDEVICE=pdfwrite $GSOPTS " .
          "-sOutputFile=$OutputFilename - -c quit";
  debug "Ghostscript pipe:", $pipe;
  open(OUT,"|$pipe") or error "Cannot open Ghostscript for piped input";
}
else {
  open(OUT,">$OutputFilename") or error "Cannot write '$OutputFilename";
}

### scan first line
my $header = 0;
$_ = <IN>;
if (/%!/) {
  # throw away binary junk before %!
  s/(.*)%!/%!/o;
}
$header = 1 if /^%/;
debug "Scanning header for BoundingBox";
print OUT;

### variables and pattern for BoundingBox search
my $bbxpatt = '[0-9eE\.\-]';
               # protect backslashes: "\\" gets '\'
my $BBValues = "\\s*($bbxpatt+)\\s+($bbxpatt+)\\s+($bbxpatt+)\\s+($bbxpatt+)";
my $BBCorrected = 0;

sub CorrectBoundingBox {
  my ($llx, $lly, $urx, $ury) = @_;
  debug "Old BoundingBox:", $llx, $lly, $urx, $ury;
  my ($width, $height) = ($urx - $llx, $ury - $lly);
  my ($xoffset, $yoffset) = (-$llx, -$lly);
  debug "New BoundingBox: 0 0", $width, $height;
  debug "Offset:", $xoffset, $yoffset;

  print OUT "%%BoundingBox: 0 0 $width $height\n";
  print OUT "<< /PageSize [$width $height] >> setpagedevice\n";
  print OUT "gsave $xoffset $yoffset translate\n";
}

### scan header
if ($header) {
  while (<IN>) {

    ### end of header
    if (!/^%/ or /^%%EndComments/) {
      print OUT;
      last;
    }

    ### BoundingBox with values
    if (/^$BBName$BBValues/) {
      CorrectBoundingBox $1, $2, $3, $4;
      $BBCorrected = 1;
      last;
    }

    ### BoundingBox with (atend)
    if (/^$BBName\s*\(atend\)/) {
      debug $BBName, "(atend)";
      if ($::opt_filter) {
        warning "Cannot look for BoundingBox in the trailer",
                "with option --filter";
        last;
      }
      my $pos = tell(IN);
      debug "Current file position:", $pos;

      # looking for %%BoundingBox
      while (<IN>) {
        # skip over included documents
        if (/^%%BeginDocument/) {
          while (<IN>) {
            last if /^%%EndDocument/;
          }
        }
        if (/^$BBName$BBValues/) {
          CorrectBoundingBox $1, $2, $3, $4;
          $BBCorrected = 1;
          last;
        }
      }

      # go back
      seek(IN, $pos, 0) or error "Cannot go back to line '$BBName (atend)'";
      last;
    }

    # print header line
    print OUT;
  }
}

### print rest of file
while (<IN>) {
  print OUT;
}

### close files
close(IN);
print OUT "\ngrestore\n" if $BBCorrected;
close(OUT);
warning "BoundingBox not found" unless $BBCorrected;
debug "Ready.";
;


More information about the tex-live mailing list