-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
epsffit.in.in
176 lines (151 loc) · 5.1 KB
/
epsffit.in.in
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!@PERL@
# -*- perl -*-
my $version_banner = <<END;
epsffit @VERSION@
Copyright (c) Reuben Thomas 2016.
Released under the GPL version 3, or (at your option) any later version.
END
use v5.10;
use warnings;
use strict;
BEGIN
{
# Relocatable header
@relocatable_pl@
if ("@RELOCATABLE@" eq "yes") {
my $exec_prefix = "@exec_prefix@";
my $orig_installdir = "@bindir@"; # see Makefile.am's *_SCRIPTS variables
my ($orig_installprefix, $curr_installprefix) = find_prefixes($orig_installdir, find_curr_installdir());
sub relocate { # the subroutine is defined whether or not the enclosing block is executed
my ($dir) = @_;
if ("@RELOCATABLE@" eq "yes") {
$dir =~ s%^$orig_installprefix/%$curr_installprefix/%;
$dir =~ s,/$,,;
}
return $dir;
}
}
# Relocate the directory variables that we use.
my $pkgdatadir = &relocate("@pkgdatadir@");
# End of relocatable header; "real" Perl starts here.
unshift (@INC, '@pkgdatadir@') unless $ENV{PSUTILS_UNINSTALLED};
}
use File::Basename;
use Getopt::Long;
use List::Util qw(min);
use PSUtils;
my $prog = basename($0);
my ($help_flag, $version_flag, $center, $rotate, $aspect, $maximize, $showpage);
sub usage {
my ($exit_code) = @_;
print STDOUT <<END;
Usage: $prog [OPTION...] LLX LLY URX URY [INFILE [OUTFILE]]
Fit an Encapsulated PostScript file to a given bounding box.
-c, --center center the image in the given bounding box
-r, --rotate rotate the image by 90 degrees counter-clockwise
-a, --aspect adjust the aspect ratio to fit the bounding box
-m, --maximize rotate the image to fill more of the page if possible
-s, --showpage append a /showpage to the file to force printing
--help display this help and exit
--version display version information and exit
(LLX, LLY) are the coordinates of the lower left corner of the box, and
(URX, URY) the upper right.
If OUTFILE is not specified, writes to standard output.
If INFILE is not specified, reads from standard input.
END
exit $exit_code;
}
# Get arguments
Getopt::Long::Configure("bundling");
# Having configured bundling, must give short option names explicitly
my $opts = GetOptions(
"center|centre|c" => \$center,
"rotate|r" => \$rotate,
"aspect|a" => \$aspect,
"maximize|maximise|m" => \$maximize,
"showpage|s" => \$showpage,
"help" => \$help_flag,
"version" => \$version_flag,
) or usage(1);
if ($version_flag) {
print STDOUT $version_banner;
exit 0;
}
usage(0) if $help_flag;
usage(1) if $#ARGV < 3 || $#ARGV > 5;
my $bbfound = 0; # %%BoundingBox: found
my ($urx, $ury, $llx, $lly) = (0, 0, 0, 0);
my $fllx = singledimen(shift);
my $flly = singledimen(shift);
my $furx = singledimen(shift);
my $fury = singledimen(shift);
my ($infile, $outfile) = setup_input_and_output();
while (<$infile>) {
if (/^%[%!]/) {
# still in comment section
if (/^%%BoundingBox:/) {
if (/^%%BoundingBox: +([\d.]+) +([\d.]+) +([\d.]+) +([\d.]+)$/) {
$bbfound = 1;
$llx = int($1); # accept doubles, but convert to int
$lly = int($2);
$urx = int($3 + 0.5);
$ury = int($4 + 0.5);
}
} elsif (/^%%EndComments/) {
# don't repeat %%EndComments
last;
} else {
print $outfile $_;
}
} else {
last;
}
}
die("no %%BoundingBox:\n") unless $bbfound;
# put BB, followed by scale & translate
my ($xoffset, $yoffset) = ($fllx, $flly);
my ($width, $height) = ($urx - $llx, $ury - $lly);
$rotate = 1 if $maximize &&
(($width > $height && $fury - $flly > $furx - $fllx) ||
($width < $height && $fury - $flly < $furx - $fllx));
my ($fwidth, $fheight) = ($furx - $fllx, $fury - $flly);
($fwidth, $fheight) = ($fheight, $fwidth) if $rotate;
my ($xscale, $yscale) = ($fwidth / $width, $fheight / $height);
$xscale = $yscale = min($xscale, $yscale) if !$aspect; # preserve aspect ratio?
$width *= $xscale; # actual width and height after scaling
$height *= $yscale;
if ($center) {
if ($rotate) {
$xoffset += ($fheight - $height) / 2;
$yoffset += ($fwidth - $width) / 2;
} else {
$xoffset += ($fwidth - $width) / 2;
$yoffset += ($fheight - $height) / 2;
}
}
say $outfile "%%BoundingBox: " . int($xoffset) . " " . int($yoffset) . " " .
int($xoffset + ($rotate ? $height : $width)) . " " .
int($yoffset + ($rotate ? $width : $height));
if ($rotate) { # compensate for original image shift
$xoffset += $height + $lly * $yscale; # displacement for rotation
$yoffset -= $llx * $xscale;
} else {
$xoffset -= $llx * $xscale;
$yoffset -= $lly * $yscale;
}
say $outfile "%%EndComments";
if ($showpage) {
say $outfile "save /showpage{}def /copypage{}def /erasepage{}def";
} else {
say $outfile "%%BeginProcSet: epsffit 1 0";
}
say $outfile "gsave " . (sprintf "%.3f %.3f translate", $xoffset, $yoffset);
say $outfile "90 rotate" if $rotate;
say $outfile (sprintf "%.3f %.3f scale", $xscale, $yscale);
say $outfile "%%EndProcSet" unless $showpage;
while (<$infile>) {
print $outfile $_;
}
say $outfile "grestore";
say $outfile "restore showpage" if $showpage; # just in case
1; # exit with a true value (value of previous statement may be false)