| 1 |
#!/usr/bin/env perl |
| 2 |
# extractres: extract resources from PostScript file |
| 3 |
# includeres: add resources to a PostScript file |
| 4 |
# |
| 5 |
# (c) Reuben Thomas 2012 |
| 6 |
# (c) Angus J. C. Duggan 1991-1997 |
| 7 |
# See file LICENSE for details. |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
|
| 12 |
use File::Basename; |
| 13 |
use Getopt::Long; |
| 14 |
|
| 15 |
my $prog = basename($0); |
| 16 |
|
| 17 |
# Resource extensions |
| 18 |
my %extn = ("font" => ".pfa", "file" => ".ps", "procset" => ".ps", |
| 19 |
"pattern" => ".pat", "form" => ".frm", "encoding" => ".enc"); |
| 20 |
# Resource types |
| 21 |
my %type = ("%%BeginFile:" => "file", "%%BeginProcSet:" => "procset", |
| 22 |
"%%BeginFont:" => "font"); |
| 23 |
|
| 24 |
sub filename { # make filename for resource in @_ |
| 25 |
my $name; |
| 26 |
foreach (@_) { # sanitise name |
| 27 |
s/[!()\$\#*&\\\|\`\'\"\~\{\}\[\]\<\>\?]//g; |
| 28 |
$name .= $_; |
| 29 |
} |
| 30 |
$name =~ s@.*/@@; # drop directories |
| 31 |
die "$prog: filename not found for resource ", join(" ", @_), "\n" |
| 32 |
if $name =~ /^$/; |
| 33 |
return $name; |
| 34 |
} |
| 35 |
|
| 36 |
sub extract { |
| 37 |
my %resources = (); # list of resources included |
| 38 |
my %merge = (); # list of resources extracted this time |
| 39 |
|
| 40 |
my $merge; |
| 41 |
unless (GetOptions('merge' => \$merge)) { |
| 42 |
print STDERR "Usage: $prog [-merge] [FILE]\n"; |
| 43 |
exit 1; |
| 44 |
} |
| 45 |
|
| 46 |
my $prolog = ""; |
| 47 |
my $body = ""; |
| 48 |
my $resource = ""; |
| 49 |
my $output = \$prolog; |
| 50 |
my $saveout; |
| 51 |
while (<>) { |
| 52 |
if (/^%%BeginResource:/ || /^%%BeginFont:/ || /^%%BeginProcSet:/) { |
| 53 |
my ($comment, @res) = split(/\s+/); # look at resource type |
| 54 |
my $type = defined($type{$comment}) ? $type{$comment} : shift(@res); |
| 55 |
my $name = filename(@res, $extn{$type}); # make file name |
| 56 |
$saveout = $output; |
| 57 |
if (!defined($resources{$name})) { |
| 58 |
$prolog .= "%%IncludeResource: $type " . join(" ", @res) . "\n"; |
| 59 |
if (!-e $name) { |
| 60 |
open RES, ">$name" || die "$prog: can't write file `$name'\n"; |
| 61 |
$resources{$name} = ""; |
| 62 |
$merge{$name} = $merge; |
| 63 |
$output = \$resources{$name}; |
| 64 |
} else { # resource already exists |
| 65 |
close(RES); |
| 66 |
undef $output; |
| 67 |
} |
| 68 |
} elsif ($merge{$name}) { |
| 69 |
open RES, ">>$name" || die "$prog: can't append to file `$name'\n"; |
| 70 |
$resources{$name} = ""; |
| 71 |
$output = \$resources{$name}; |
| 72 |
} else { # resource already included |
| 73 |
undef $output; |
| 74 |
} |
| 75 |
} elsif (/^%%EndResource/ || /^%%EndFont/ || /^%%EndProcSet/) { |
| 76 |
if (defined $output) { |
| 77 |
$$output .= $_; |
| 78 |
print RES $$output; |
| 79 |
} |
| 80 |
$output = $saveout; |
| 81 |
next; |
| 82 |
} elsif ((/^%%EndProlog/ || /^%%BeginSetup/ || /^%%Page:/)) { |
| 83 |
$output = \$body; |
| 84 |
} |
| 85 |
$$output .= $_ if defined $output; |
| 86 |
} |
| 87 |
|
| 88 |
print $prolog . $body; |
| 89 |
} |
| 90 |
|
| 91 |
sub include { |
| 92 |
while (<>) { |
| 93 |
if (/^%%IncludeResource:/ || /^%%IncludeFont:/ || /^%%IncludeProcSet:/) { |
| 94 |
my ($comment, @res) = split(/\s+/); |
| 95 |
my $type = defined($type{$comment}) ? $type{$comment} : shift(@res); |
| 96 |
my $name = filename(@res); |
| 97 |
if (open(RES, $name) || open(RES, "$name$extn{$type}")) { |
| 98 |
print do { local $/; <RES> }; |
| 99 |
close(RES); |
| 100 |
} else { |
| 101 |
print "%%IncludeResource: ", join(" ", $type, @res), "\n"; |
| 102 |
print STDERR "$prog: resource `$name' not found\n"; |
| 103 |
} |
| 104 |
} else { |
| 105 |
print $_; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
# Perform function according to name |
| 111 |
if ($prog eq "extractres") { |
| 112 |
extract(); |
| 113 |
} elsif ($prog eq "includeres") { |
| 114 |
include(); |
| 115 |
} else { |
| 116 |
die "$prog: I need to be called `extractres' or `includeres'\n"; |
| 117 |
} |