| 1 |
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q' |
| 2 |
if 0; |
| 3 |
use strict; |
| 4 |
|
| 5 |
# $Id: epstopdf.pl 18319 2010-05-17 16:34:21Z karl $ |
| 6 |
# (Copyright lines below.) |
| 7 |
# |
| 8 |
# Redistribution and use in source and binary forms, with or without |
| 9 |
# modification, are permitted provided that the following conditions are met: |
| 10 |
# |
| 11 |
# 1. Redistributions of source code must retain the above copyright |
| 12 |
# notice, this list of conditions and the following disclaimer. |
| 13 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer in the |
| 15 |
# documentation and/or other materials provided with the distribution. |
| 16 |
# 3. The name of the author may not be used to endorse or promote |
| 17 |
# products derived from this software without specific prior written |
| 18 |
# permission. |
| 19 |
# |
| 20 |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 21 |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 |
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 24 |
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 28 |
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
# POSSIBILITY OF SUCH DAMAGE. |
| 31 |
# |
| 32 |
# ---------------------------------------------------------------- |
| 33 |
# |
| 34 |
# This is a script to transform an EPS file such that: |
| 35 |
# a) it is guaranteed to start at the 0,0 coordinate. |
| 36 |
# b) it sets a page size exactly corresponding to the BoundingBox |
| 37 |
# This means that when Ghostscript renders it, the result needs no |
| 38 |
# cropping, and the PDF MediaBox is correct. |
| 39 |
# c) the result is piped to Ghostscript and a PDF version written. |
| 40 |
# |
| 41 |
# It needs a Level 2 PS interpreter. |
| 42 |
# If the input bounding box is not right, of course there will be problems. |
| 43 |
# |
| 44 |
# One thing not allowed for: the case of |
| 45 |
# "%%BoundingBox: (atend)" when input is not seekable (e.g., from a pipe), |
| 46 |
# |
| 47 |
# emacs-page |
| 48 |
# History |
| 49 |
# 2010/05/09 v2.16 (Karl Berry) |
| 50 |
# * make --nogs dump edited PostScript to stdout by default |
| 51 |
# (report from Reinhard Kotucha). |
| 52 |
# 2010/03/19 v2.15 (Karl Berry) |
| 53 |
# * let --outfile override --filter again. |
| 54 |
# * recognize MSWin64 as well as MSWin32, just in case. |
| 55 |
# 2010/03/08 v2.14 (Manuel P\'egouri\'e-Gonnard) |
| 56 |
# * In restricted mode, forbid --gscmd (all platforms) and call GS with full |
| 57 |
# path relative to self location (Windows). |
| 58 |
# 2010/02/26 v2.13 (Karl Berry) |
| 59 |
# * New release. |
| 60 |
# 2010/02/23 (Manuel P\'egouri\'e-Gonnard) |
| 61 |
# * Use kpsewhich for filename validation in restricted mode, both input and |
| 62 |
# output. Requires kpathsea 5.1.0 (TL2010), rejects the name with earlier |
| 63 |
# versions of kpsewhich. |
| 64 |
# * Call external programs with full path on win32 in order to avoid obvious |
| 65 |
# attacks with rogue versions of these programs in the current directory. |
| 66 |
# 2009/11/27 v2.12 (Karl Berry) |
| 67 |
# * Make --filter work again |
| 68 |
# 2009/11/25 (Manuel P\'egouri\'e-Gonnard) |
| 69 |
# * Better extension detection, suggested by A. Cherepanov. |
| 70 |
# 2009/10/18 (Manuel P\'egouri\'e-Gonnard) |
| 71 |
# * Better argument validation (Alexander Cherepanov). |
| 72 |
# * Use list form of pipe open() (resp. system()) to prevent injection. |
| 73 |
# Since Perl's fork() emulation doesn't work on Windows with Perl 5.8.8 from |
| 74 |
# TeX Live 2009, use a temporary file instead of a pipe on Windows. |
| 75 |
# 2009/10/14 (Manuel P\'egouri\'e-Gonnard) |
| 76 |
# * Added restricted mode. |
| 77 |
# 2009/09/27 v2.11 (Karl Berry) |
| 78 |
# * Fixed two bugs in the (atend) handling code (Martin von Gagern) |
| 79 |
# * Improved handling of CR line ending (Martin von Gagern) |
| 80 |
# * More error checking |
| 81 |
# * --version option |
| 82 |
# * Create source repository in TeX Live |
| 83 |
# 2009/07/17 v2.9.11gw |
| 84 |
# * Added -dSAFER to default gs options |
| 85 |
# TL2009 wants to use a restricted variant of -shell-escape, |
| 86 |
# allowing epstopdf to run. However without -dSAFER Ghostscript |
| 87 |
# allows writing to files (other than given in -sOutputFile) |
| 88 |
# and running commands (through Ghostscript pipe's language feature). |
| 89 |
# 2009/05/09 v2.9.10gw |
| 90 |
# * Changed cygwin name for ghostscript to gs |
| 91 |
# 2008/08/26 v2.9.9gw |
| 92 |
# * Switch to embed fonts (default=yes) (J.P. Chretien) |
| 93 |
# * turned no AutoRotatePages into an option (D. Kreil) (default = None) |
| 94 |
# * Added resolution switch (D. Kreil) |
| 95 |
# * Added BSD-style license |
| 96 |
# 2007/07/18 v2.9.8gw |
| 97 |
# 2007/05/18 v.2.9.7gw (Gerben Wierda) |
| 98 |
# * Merged both supplied 2.9.6 versions |
| 99 |
# 2007/05/15 v2.9.6tp (Theo Papadopoulo) |
| 100 |
# * Simplified the (atend) support |
| 101 |
# 2007/01/24 v2.9.6sw (Staszek Wawrykiewicz) |
| 102 |
# * patched to work also on Windows |
| 103 |
# 2005/10/06 v2.9.5gw (Gerben Wierda) |
| 104 |
# * Fixed a horrendous bug in the (atend) handling code |
| 105 |
# 2005/10/06 v2.9.4gw (Gerben Wierda) |
| 106 |
# * This has become the official version for now |
| 107 |
# 2005/10/01 v2.9.3draft (Gerben Wierda) |
| 108 |
# * Quote OutFilename |
| 109 |
# 2005/09/29 v2.9.2draft (Gerben Wierda) |
| 110 |
# * Quote OutFilename |
| 111 |
# 2004/03/17 v2.9.1draft (Gerben Wierda) |
| 112 |
# * No autorotate page |
| 113 |
# 2003/04/22 v2.9draft (Gerben Wierda) |
| 114 |
# * Fixed bug where with cr-eol files everything up to the first %! |
| 115 |
# * in the first 2048 bytes was gobbled (double ugh!) |
| 116 |
# 2002/02/21 v2.8draft (Gerben Wierda) |
| 117 |
# * Fixed bug where last line of buffer was not copied out (ugh!) |
| 118 |
# 2002/02/18 v2.8draft (Gerben Wierda) |
| 119 |
# * Handle different eol styles transparantly |
| 120 |
# * Applied fix from Peder Axensten for Freehand bug |
| 121 |
# 2001/03/05 v2.7 (Heiko Oberdiek) |
| 122 |
# * Newline before grestore for the case that there is no |
| 123 |
# whitespace at the end of the eps file. |
| 124 |
# 2000/11/05 v2.6 (Heiko Oberdiek) |
| 125 |
# * %%HiresBoundingBox corrected to %%HiResBoundingBox |
| 126 |
# 1999/05/06 v2.5 (Heiko Oberdiek) |
| 127 |
# * New options: --hires, --exact, --filter, --help. |
| 128 |
# * Many cosmetics: title, usage, ... |
| 129 |
# * New code for debug, warning, error |
| 130 |
# * Detecting of cygwin perl |
| 131 |
# * Scanning for %%{Hires,Exact,}BoundingBox. |
| 132 |
# * Scanning only the header in order not to get a wrong |
| 133 |
# BoundingBox of an included file. |
| 134 |
# * (atend) supported. |
| 135 |
# * uses strict; (earlier error detecting). |
| 136 |
# * changed first comment from '%!PS' to '%!'; |
| 137 |
# * corrected (atend) pattern: '\s*\(atend\)' |
| 138 |
# * using of $bbxpat in all BoundingBox cases, |
| 139 |
# correct the first white space to '...Box:\s*$bb...' |
| 140 |
# * corrected first line (one line instead of two before 'if 0;'; |
| 141 |
# |
| 142 |
# Thomas Esser, Sept. 1998: change initial lines to find |
| 143 |
# perl along $PATH rather than guessing a fixed location. The above |
| 144 |
# construction should work with most shells. |
| 145 |
# |
| 146 |
# Originally by Sebastian Rahtz, for Elsevier Science |
| 147 |
# with extra tricks from Hans Hagen's texutil and many more. |
| 148 |
# emacs-page |
| 149 |
|
| 150 |
### program identification |
| 151 |
my $program = "epstopdf"; |
| 152 |
my $ident = '($Id: epstopdf.pl 18319 2010-05-17 16:34:21Z karl $) 2.16'; |
| 153 |
my $copyright = <<END_COPYRIGHT ; |
| 154 |
Copyright 2009-2010 Karl Berry et al. |
| 155 |
Copyright 2002-2009 Gerben Wierda et al. |
| 156 |
Copyright 1998-2001 Sebastian Rahtz et al. |
| 157 |
License RBSD: Revised BSD <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5> |
| 158 |
This is free software: you are free to change and redistribute it. |
| 159 |
There is NO WARRANTY, to the extent permitted by law. |
| 160 |
END_COPYRIGHT |
| 161 |
my $title = "$program $ident\n"; |
| 162 |
|
| 163 |
my $on_windows = $^O =~ /^MSWin/; |
| 164 |
my $on_windows_or_cygwin = $on_windows || $^O eq "cygwin"; |
| 165 |
|
| 166 |
### ghostscript command name |
| 167 |
my $GS = $on_windows ? "gswin32c" : "gs"; |
| 168 |
|
| 169 |
### restricted mode |
| 170 |
my $restricted = 0; |
| 171 |
$restricted = 1 if $0 =~ /repstopdf/; |
| 172 |
|
| 173 |
### options |
| 174 |
$::opt_autorotate = "None"; |
| 175 |
$::opt_compress = 1; |
| 176 |
$::opt_debug = 0; |
| 177 |
$::opt_embed = 1; |
| 178 |
$::opt_exact = 0; |
| 179 |
$::opt_filter = 0; |
| 180 |
$::opt_gs = 1; |
| 181 |
$::opt_gscmd = ""; |
| 182 |
$::opt_hires = 0; |
| 183 |
$::opt_outfile = ""; |
| 184 |
$::opt_res = 0; |
| 185 |
|
| 186 |
### usage |
| 187 |
my @bool = ("false", "true"); |
| 188 |
my $resmsg = $::opt_res ? $::opt_res : "[use gs default]"; |
| 189 |
my $rotmsg = $::opt_autorotate ? $::opt_autorotate : "[use gs default]"; |
| 190 |
my $usage = <<"END_OF_USAGE"; |
| 191 |
${title}Usage: $program [OPTION]... [EPSFILE] |
| 192 |
|
| 193 |
Convert EPS to PDF, by default using Ghostscript. |
| 194 |
|
| 195 |
Options: |
| 196 |
--help display this help and exit |
| 197 |
--version display version information and exit |
| 198 |
|
| 199 |
--outfile=FILE write result to FILE |
| 200 |
--(no)compress use compression (default: $bool[$::opt_compress]) |
| 201 |
--(no)debug write debugging info (default: $bool[$::opt_debug]) |
| 202 |
--(no)embed embed fonts (default: $bool[$::opt_embed]) |
| 203 |
--(no)exact scan ExactBoundingBox (default: $bool[$::opt_exact]) |
| 204 |
--(no)filter read standard input (default: $bool[$::opt_filter]) |
| 205 |
--(no)gs run ghostscript (default: $bool[$::opt_gs]) |
| 206 |
--(no)hires scan HiResBoundingBox (default: $bool[$::opt_hires]) |
| 207 |
--gscmd=VAL pipe output to VAL (default: $GS) |
| 208 |
--res=DPI set image resolution (default: $resmsg) |
| 209 |
--autorotate=VAL set AutoRotatePages (default: $rotmsg) |
| 210 |
Recognized VAL choices: None, All, PageByPage |
| 211 |
For EPS files, PageByPage is equivalent to All |
| 212 |
--restricted use restricted mode (default: $bool[$restricted]) |
| 213 |
|
| 214 |
Examples producing test.pdf: |
| 215 |
* $program test.eps |
| 216 |
* produce postscript | $program --filter >test.pdf |
| 217 |
* produce postscript | $program -f -d -o=test.pdf |
| 218 |
|
| 219 |
Example: look for HiResBoundingBox and produce corrected PostScript: |
| 220 |
* $program -d --nogs --hires test.ps >testcorr.ps |
| 221 |
|
| 222 |
When reporting bugs, please include an input file and command line |
| 223 |
options so the problem can be reproduced. |
| 224 |
|
| 225 |
Report bugs to: tex-k\@tug.org |
| 226 |
epstopdf home page: <http://tug.org/epstopdf/> |
| 227 |
END_OF_USAGE |
| 228 |
|
| 229 |
### process options |
| 230 |
use Getopt::Long; |
| 231 |
GetOptions ( |
| 232 |
"autorotate=s", # \ref{val_autorotate} |
| 233 |
"compress!", |
| 234 |
"debug!", |
| 235 |
"embed!", |
| 236 |
"exact!", |
| 237 |
"filter!", |
| 238 |
"gs!", |
| 239 |
"gscmd=s", # \ref{val_gscmd} |
| 240 |
"help", |
| 241 |
"hires!", |
| 242 |
"outfile=s", # \ref{openout_any} |
| 243 |
"res=i", # validated by getopt ('i' specifier) |
| 244 |
"restricted", |
| 245 |
"version", |
| 246 |
) or die $usage; |
| 247 |
|
| 248 |
### help functions |
| 249 |
sub debug { print STDERR "* @_\n" if $::opt_debug; } |
| 250 |
sub warning { print STDERR "==> Warning: @_\n"; } |
| 251 |
sub error { die "$title!!! Error: @_\n"; } |
| 252 |
sub errorUsage { die "Error: @_ (try --help for more information)\n"; } |
| 253 |
|
| 254 |
### restricted option |
| 255 |
$restricted = 1 if $::opt_restricted; |
| 256 |
debug "Restricted mode activated" if $restricted; |
| 257 |
|
| 258 |
### safer external commands for Windows in restricted mode |
| 259 |
my $kpsewhich = 'kpsewhich'; |
| 260 |
if ($restricted && $on_windows) { |
| 261 |
use File::Basename; |
| 262 |
my $mydirname = dirname $0; |
| 263 |
# $mydirname is the location of the Perl script |
| 264 |
$kpsewhich = "$mydirname/../../../bin/win32/$kpsewhich"; |
| 265 |
$GS = "$mydirname/../../../tlpkg/tlgs/bin/$GS"; |
| 266 |
} |
| 267 |
debug "kpsewhich command: $kpsewhich"; |
| 268 |
|
| 269 |
### check if a name is "safe" according to kpse's open(in|out)_any |
| 270 |
# return true if name is ok, false otherwise |
| 271 |
sub safe_name |
| 272 |
{ |
| 273 |
my ($mode, $name) = @_; |
| 274 |
my $option = ""; |
| 275 |
$option = '-safe-in-name' if $mode eq 'in'; |
| 276 |
$option = '-safe-out-name' if $mode eq 'out'; |
| 277 |
error "Unknown check mode in safe_name(): $mode" unless $option; |
| 278 |
my @args = ($kpsewhich, '-progname', 'repstopdf', $option, $name); |
| 279 |
my $bad = system {$args[0]} @args; |
| 280 |
return ! $bad; |
| 281 |
} |
| 282 |
|
| 283 |
### help, version options. |
| 284 |
if ($::opt_help) { |
| 285 |
print $usage; |
| 286 |
exit (0); |
| 287 |
} |
| 288 |
|
| 289 |
if ($::opt_version) { |
| 290 |
print $title; |
| 291 |
print $copyright; |
| 292 |
exit (0); |
| 293 |
} |
| 294 |
|
| 295 |
### get input filename (\ref{openin_any} for validation) |
| 296 |
my $InputFilename = ""; |
| 297 |
if ($::opt_filter) { |
| 298 |
@ARGV == 0 or |
| 299 |
errorUsage "Input file cannot be used with filter option"; |
| 300 |
debug "Filtering: will read standard input"; |
| 301 |
} else { |
| 302 |
@ARGV > 0 or errorUsage "Input filename missing"; |
| 303 |
@ARGV < 2 or errorUsage "Unknown option or too many input files"; |
| 304 |
$InputFilename = $ARGV[0]; |
| 305 |
#-r $InputFilename or error "\"$InputFilename\" not readable"; |
| 306 |
debug "Input filename:", $InputFilename; |
| 307 |
} |
| 308 |
|
| 309 |
### emacs-page |
| 310 |
### option gscmd |
| 311 |
if ($::opt_gscmd) { |
| 312 |
if ($restricted) { # \label{val_gscmd} |
| 313 |
error "Option forbidden in restricted mode: --gscmd"; |
| 314 |
} else { |
| 315 |
debug "Switching from $GS to $::opt_gscmd"; |
| 316 |
$GS = $::opt_gscmd; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
### start building GS command line for the pipe |
| 321 |
my @GS = ($GS); |
| 322 |
push @GS, qw(-q -dNOPAUSE -dSAFER -sDEVICE=pdfwrite); |
| 323 |
|
| 324 |
### option outfile |
| 325 |
my $OutputFilename = $::opt_outfile; |
| 326 |
if (! $OutputFilename) { |
| 327 |
if ($::opt_gs) { |
| 328 |
if ($::opt_filter) { |
| 329 |
debug "Filtering: will write standard output"; |
| 330 |
$OutputFilename = "-"; |
| 331 |
} else { |
| 332 |
# Ghostscript, no filter: replace input extension with .pdf. |
| 333 |
$OutputFilename = $InputFilename; |
| 334 |
my $ds = $on_windows_or_cygwin ? '\\/' : '/'; |
| 335 |
$OutputFilename =~ s/\.[^\.$ds]*$//; |
| 336 |
$OutputFilename .= ".pdf"; |
| 337 |
} |
| 338 |
} else { |
| 339 |
debug "No Ghostscript: will write standard output"; |
| 340 |
$OutputFilename = "-"; |
| 341 |
} |
| 342 |
} |
| 343 |
debug "Output filename:", $OutputFilename; |
| 344 |
push @GS, "-sOutputFile=$OutputFilename"; |
| 345 |
|
| 346 |
### options compress, embed, res, autorotate |
| 347 |
push @GS, ('-dPDFSETTINGS=/prepress', '-dMaxSubsetPct=100', |
| 348 |
'-dSubsetFonts=true', '-dEmbedAllFonts=true') if $::opt_embed; |
| 349 |
push @GS, '-dUseFlateCompression=false' unless $::opt_compress; |
| 350 |
push @GS, "-r$::opt_res" if $::opt_res; |
| 351 |
$resmsg= $::opt_res ? $::opt_res : "[use gs default]"; |
| 352 |
push @GS, "-dAutoRotatePages=/$::opt_autorotate" if $::opt_autorotate; |
| 353 |
$rotmsg = $::opt_autorotate ? $::opt_autorotate : "[use gs default]"; |
| 354 |
# \label{val_autorotate} |
| 355 |
error "Invalid value for autorotate: '$::opt_autorotate' " |
| 356 |
. "(use 'All', 'None' or 'PageByPage')." |
| 357 |
if ($::opt_autorotate and |
| 358 |
not $::opt_autorotate =~ /^(None|All|PageByPage)$/); |
| 359 |
|
| 360 |
### option BoundingBox types |
| 361 |
my $BBName = "%%BoundingBox:"; |
| 362 |
!($::opt_hires and $::opt_exact) or |
| 363 |
error "Options --hires and --exact cannot be used together"; |
| 364 |
$BBName = "%%HiResBoundingBox:" if $::opt_hires; |
| 365 |
$BBName = "%%ExactBoundingBox:" if $::opt_exact; |
| 366 |
debug "BoundingBox comment:", $BBName; |
| 367 |
|
| 368 |
### validate input file name in restricted mode \label{openin_any} |
| 369 |
if ($restricted and not $::opt_filter |
| 370 |
and not safe_name('in', $InputFilename)) { |
| 371 |
error "Input filename '$InputFilename' not allowed in restricted mode."; |
| 372 |
} |
| 373 |
|
| 374 |
### validate output file name in restricted mode \label{openout_any} |
| 375 |
if ($restricted and not safe_name('out', $OutputFilename)) { |
| 376 |
error "Output filename '$OutputFilename' not allowed in restricted mode."; |
| 377 |
} |
| 378 |
|
| 379 |
### option gs |
| 380 |
if ($::opt_gs) { |
| 381 |
debug "Ghostscript command:", $GS; |
| 382 |
debug "Compression:", ($::opt_compress) ? "on" : "off"; |
| 383 |
debug "Embedding:", ($::opt_embed) ? "on" : "off"; |
| 384 |
debug "Rotation:", $rotmsg; |
| 385 |
debug "Resolution:", $resmsg; |
| 386 |
} |
| 387 |
|
| 388 |
### emacs-page |
| 389 |
### open input file |
| 390 |
if ($::opt_filter) { |
| 391 |
open(IN, '<-') || error("Cannot open stdin: $!"); |
| 392 |
} else { |
| 393 |
open(IN, '<', $InputFilename) || error("Cannot open $InputFilename: $!"); |
| 394 |
} |
| 395 |
binmode IN; |
| 396 |
|
| 397 |
### open output file |
| 398 |
my $outname; # used in error message at end |
| 399 |
my $tmp_filename; # temporary file for windows |
| 400 |
my $OUT; # filehandle for output (GS pipe or temporary file) |
| 401 |
use File::Temp 'tempfile'; |
| 402 |
if ($::opt_gs) { |
| 403 |
if (! $on_windows_or_cygwin) { # list piped open works |
| 404 |
push @GS, qw(- -c quit); |
| 405 |
debug "Ghostscript pipe:", join(' ', @GS); |
| 406 |
open($OUT, '|-', @GS) or error "Cannot open Ghostscript for piped input"; |
| 407 |
} else { # use a temporary file on Windows/Cygwin. |
| 408 |
($OUT, $tmp_filename) = tempfile(UNLINK => 1); |
| 409 |
debug "Using temporary file '$tmp_filename'"; |
| 410 |
} |
| 411 |
$outname = $GS; |
| 412 |
} |
| 413 |
else { |
| 414 |
debug "No Ghostscript: opening $OutputFilename"; |
| 415 |
if ($OutputFilename eq "-") { |
| 416 |
$OUT = *STDOUT; |
| 417 |
} else { |
| 418 |
open($OUT, '>', $OutputFilename) |
| 419 |
|| error ("Cannot write \"$OutputFilename\": $!"); |
| 420 |
$outname = $OutputFilename; |
| 421 |
} |
| 422 |
} |
| 423 |
binmode $OUT; |
| 424 |
|
| 425 |
# reading a cr-eol file on a lf-eol system makes it impossible to parse |
| 426 |
# the header and besides it will read the intire file into yor line by line |
| 427 |
# scalar. this is also true the other way around. |
| 428 |
|
| 429 |
### emacs-page |
| 430 |
### scan a block, try to determine eol style |
| 431 |
|
| 432 |
my $buf; |
| 433 |
my $buflen; |
| 434 |
my @bufarray; |
| 435 |
my $inputpos; |
| 436 |
|
| 437 |
# We assume 2048 is big enough. |
| 438 |
my $EOLSCANBUFSIZE = 2048; |
| 439 |
|
| 440 |
$buflen = read(IN, $buf, $EOLSCANBUFSIZE); |
| 441 |
if ($buflen > 0) { |
| 442 |
my $crlfpos; |
| 443 |
my $lfpos; |
| 444 |
my $crpos; |
| 445 |
|
| 446 |
$inputpos = 0; |
| 447 |
|
| 448 |
# remove binary junk before header |
| 449 |
# if there is no header, we assume the file starts with ascii style and |
| 450 |
# we look for a eol style anyway, to prevent possible loading of the |
| 451 |
# entire file |
| 452 |
if ($buf =~ /%!/) { |
| 453 |
# throw away binary junk before %! |
| 454 |
$buf =~ s/(.*?)%!/%!/o; |
| 455 |
$inputpos = length($1); |
| 456 |
} |
| 457 |
$lfpos = index($buf, "\n"); |
| 458 |
$crpos = index($buf, "\r"); |
| 459 |
$crlfpos = index($buf, "\r\n"); |
| 460 |
|
| 461 |
if ($crpos > 0 and ($lfpos == -1 or $lfpos > $crpos+1)) { |
| 462 |
# The first eol was a cr and it was not immediately followed by a lf |
| 463 |
$/ = "\r"; |
| 464 |
debug "The first eol character was a CR ($crpos) and not immediately followed by a LF ($lfpos)"; |
| 465 |
} |
| 466 |
|
| 467 |
# Now we have set the correct eol-character. Get one more line and add |
| 468 |
# it to our buffer. This will make the buffer contain an entire line |
| 469 |
# at the end. Then split the buffer in an array. We will draw lines from |
| 470 |
# that array until it is empty, then move again back to <IN> |
| 471 |
$buf .= <IN> unless eof(IN); |
| 472 |
$buflen = length($buf); |
| 473 |
|
| 474 |
# Some extra magic is needed here: if we set $/ to \r, Perl's re engine |
| 475 |
# still thinks eol is \n in regular expressions (not very nice) so we |
| 476 |
# cannot split on ^, but have to split on a look-behind for \r. |
| 477 |
if ($/ eq "\r") { |
| 478 |
@bufarray = split(/(?<=\r)/ms, $buf); # split after \r |
| 479 |
} |
| 480 |
else { |
| 481 |
@bufarray = split(/^/ms, $buf); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
### getline |
| 486 |
sub getline |
| 487 |
{ |
| 488 |
if ($#bufarray >= 0) { |
| 489 |
$_ = shift(@bufarray); |
| 490 |
} |
| 491 |
else { |
| 492 |
$_ = <IN>; |
| 493 |
} |
| 494 |
$inputpos += length($_) if defined $_; |
| 495 |
return defined($_); |
| 496 |
} |
| 497 |
|
| 498 |
### scan first line |
| 499 |
my $header = 0; |
| 500 |
getline(); |
| 501 |
if (/%!/) { |
| 502 |
# throw away binary junk before %! |
| 503 |
s/(.*)%!/%!/o; |
| 504 |
} |
| 505 |
$header = 1 if /^%/; |
| 506 |
debug "Scanning header for BoundingBox"; |
| 507 |
print $OUT $_; |
| 508 |
|
| 509 |
### variables and pattern for BoundingBox search |
| 510 |
my $bbxpatt = '[0-9eE\.\-]'; |
| 511 |
# protect backslashes: "\\" gets '\' |
| 512 |
my $BBValues = "\\s*($bbxpatt+)\\s+($bbxpatt+)\\s+($bbxpatt+)\\s+($bbxpatt+)"; |
| 513 |
my $BBCorrected = 0; |
| 514 |
|
| 515 |
sub CorrectBoundingBox |
| 516 |
{ |
| 517 |
my ($llx, $lly, $urx, $ury) = @_; |
| 518 |
debug "Old BoundingBox:", $llx, $lly, $urx, $ury; |
| 519 |
my ($width, $height) = ($urx - $llx, $ury - $lly); |
| 520 |
my ($xoffset, $yoffset) = (-$llx, -$lly); |
| 521 |
debug "New BoundingBox: 0 0", $width, $height; |
| 522 |
debug "Offset:", $xoffset, $yoffset; |
| 523 |
|
| 524 |
print $OUT "%%BoundingBox: 0 0 $width $height$/"; |
| 525 |
print $OUT "<< /PageSize [$width $height] >> setpagedevice$/"; |
| 526 |
print $OUT "gsave $xoffset $yoffset translate$/"; |
| 527 |
} |
| 528 |
|
| 529 |
### emacs-page |
| 530 |
### scan header |
| 531 |
if ($header) { |
| 532 |
HEADER: while (getline()) { |
| 533 |
### Fix for freehand bug ### by Peder Axensten |
| 534 |
next HEADER if(!/\S/); |
| 535 |
|
| 536 |
### end of header |
| 537 |
if (!/^%/ or /^%%EndComments/) { |
| 538 |
print $OUT $_; |
| 539 |
last; |
| 540 |
} |
| 541 |
|
| 542 |
### BoundingBox with values |
| 543 |
if (/^$BBName$BBValues/o) { |
| 544 |
CorrectBoundingBox $1, $2, $3, $4; |
| 545 |
$BBCorrected = 1; |
| 546 |
last; |
| 547 |
} |
| 548 |
|
| 549 |
### BoundingBox with (atend) |
| 550 |
if (/^$BBName\s*\(atend\)/) { |
| 551 |
debug $BBName, "(atend)"; |
| 552 |
if ($::opt_filter) { |
| 553 |
warning "Cannot look for BoundingBox in the trailer", |
| 554 |
"with option --filter"; |
| 555 |
last; |
| 556 |
} |
| 557 |
my $pos = $inputpos; |
| 558 |
debug "Current file position:", $pos; |
| 559 |
|
| 560 |
# looking for %%BoundingBox |
| 561 |
while (getline()) { |
| 562 |
# skip over included documents |
| 563 |
my $nestDepth = 0; |
| 564 |
$nestDepth++ if /^%%BeginDocument/; |
| 565 |
$nestDepth-- if /^%%EndDocument/; |
| 566 |
if ($nestDepth == 0 && /^$BBName$BBValues/o) { |
| 567 |
CorrectBoundingBox $1, $2, $3, $4; |
| 568 |
$BBCorrected = 1; |
| 569 |
last; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
# go back |
| 574 |
seek(IN, $pos, 0) or error "Cannot go back to line \"$BBName (atend)\""; |
| 575 |
last; |
| 576 |
} |
| 577 |
|
| 578 |
# print header line |
| 579 |
print $OUT $_; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
### print rest of file |
| 584 |
while (getline()) { |
| 585 |
print $OUT $_; |
| 586 |
} |
| 587 |
|
| 588 |
### emacs-page |
| 589 |
### close files |
| 590 |
close(IN); |
| 591 |
print $OUT "$/grestore$/" if $BBCorrected; |
| 592 |
close($OUT); |
| 593 |
|
| 594 |
### actually run GS if we were writing to a temporary file |
| 595 |
if (defined $tmp_filename) { |
| 596 |
push @GS, $tmp_filename; |
| 597 |
push @GS, qw(-c quit); |
| 598 |
debug "Ghostscript command:", join(' ', @GS); |
| 599 |
system @GS; |
| 600 |
} |
| 601 |
|
| 602 |
# if ghostscript exited badly, we should too. |
| 603 |
if ($? & 127) { |
| 604 |
error(sprintf "Writing to $outname failed, signal %d\n", $? & 127); |
| 605 |
} elsif ($? != 0) { |
| 606 |
error(sprintf "Writing to $outname failed, error code %d\n", $? >> 8); |
| 607 |
} |
| 608 |
|
| 609 |
warning "BoundingBox not found" unless $BBCorrected; |
| 610 |
debug "Done."; |
| 611 |
|
| 612 |
# vim: ts=8 sw=2 expandtab: |