Debian Codegraph @

src/gri_merge

raw
1 #!/usr/bin/perl
2 $usage = "
3 PURPOSE: Strings Gri PostScript files together.
4
5 BUGS: With old versions, of gri, make sure to match each `set clip
6 postscript on' with a `set clip postscript off'.
7
8 USAGE (style 1):
9 gri_merge [OPTIONS] CxR a.ps b.ps ... > merged_file.ps
10 Merges the files onto one page, in 'C' columns and 'R' rows. The C*R
11 files are given in the order of words on a page. The page is
12 presumed to be 8.5x11 in size, as are all the input files, and the
13 input files are sized to fit, and kept in natural scale.
14
15 USAGE (style 2):
16 gri_merge [OPTIONS] xcm ycm enlarge a.ps [b.ps ...] > merged_file.ps
17 Where `enlarge' is a scale factor applied after offsetting `xcm' to
18 the right and `ycm' upward.
19
20 EXAMPLE (style 2):
21 The following
22 gri_merge 2 12 .5 a.ps \\
23 12 12 .5 b.ps \\
24 2 2 .5 c.ps \\
25 12 2 .5 d.ps > all.ps
26 produces 4 panels from gri plots done using margins and sizes
27 as specified in the following lines in a gri commandfile
28 set x margin 2
29 set x size 15
30 set y margin 2
31 set y size 15
32 The OPTIONS, available if your 'perl' has 'getopts' library, are:
33 -u graylevel -- set graylevel for underlay beneath panels, by default 0.75.
34 Values range from 0 (black) to 1 (white), although a value
35 of precisely 1 means do NOT draw underlay.
36 -b graylevel -- Set value for background under individual panels, again 0
37 for black to 1 for white, with 1 meaning no drawing.
38 -h -- Print this help message and quit.
39 ";
40
41 eval { use Getopt::Std; ; };
42 my $use_getopts = $@ ? 0 : 1;
43
44 $underlay_gray = 0.9;
45 $opt_u = -1; # illegal value
46 $background_gray = 1;
47 $opt_b = -1; # illegal value
48 if ($use_getopts) {
49 &getopts('hu:b:');
50 if ($opt_h) {
51 print "$usage";
52 exit 0;
53 }
54 }
55
56 $underlay_gray = $opt_u if $opt_u != -1;
57 $underlay_gray = 0 if ($underlay_gray < 0);
58 $underlay_gray = 1 if ($underlay_gray > 1);
59
60 $background_gray = $opt_b if $opt_b != -1;
61 $background_gray = 0 if ($background_gray < 0);
62 $background_gray = 1 if ($background_gray > 1);
63
64 $width_in = 8.5;
65 $height_in = 11.0;
66 $width = $width_in * 2.54; # Page width
67 $height = $height_in * 2.54; # Page height
68
69 # See if RowXColumn format
70 ($columns, $rows) = split("[xX]", $ARGV[0]);
71 if ($rows) {
72 #
73 # Style 1
74 $xfac = 1 / $columns;
75 $yfac = 1 / $rows;
76 $mag = ($xfac < $yfac) ? $xfac : $yfac;
77 $i = 1;
78 $xmarg = 72 / 2.54 * ($width - $width * $columns * $mag) / 2;
79 $ymarg = 72 / 2.54 * ($height - $height * $rows * $mag) / 2;
80 for ($r = $rows - 1; $r > -1; $r--) {
81 for ($c = 0; $c < $columns && $i <= $#ARGV; $c ++) {
82 last if ($i > $#ARGV);
83 $done_prolog = 0;
84 $xcm = $width * $mag * $c;
85 $ycm = $height * $mag * $r;
86 $xoff = $xcm * 72 / 2.54 + $xmarg;
87 $yoff = $ycm * 72 / 2.54 + $ymarg;
88 print STDERR "Panel '$file' origin = ($xcm, $ycm) cm, magnified $mag\n";
89 open (FILE, "$ARGV[$i]") || die "Can't open $i-th file $ARGV[$i]\n";
90 while(<FILE>) {
91 next if /^\/origstate save def/; # This data-explorer line messes up
92 next if /^origstate restore/; # This data-explorer line messes up
93 next if /^%%Page:/; # These mess things up
94 if (/showpage/) {
95 print "grestore % gri_merge\n";
96 last if ($i != $#ARGV);
97 }
98 if ($i == 1 || $done_prolog) {
99 if (/%%BoundingBox: (atend)/) {
100 print "%%BoundingBox: 0 0 612 792\n";
101 } elsif (/%%BoundingBox:/) {
102 ;
103 } else {
104 print;
105 }
106 }
107 if (/%%EndProlog/) {
108 $done_prolog = 1;
109 &grey_page if ($i == 1);
110 &white_panel;
111 }
112 }
113 $i++;
114 }
115 }
116 } else {
117 #
118 # Style 2
119 $nargs = $#ARGV + 1;
120 die "$usage. ERROR: need 4, 8, 12, ..., arguments, not $nargs as given.\n"
121 if ($nargs < 4 || $nargs%4);
122 for($i = 0; $i < $nargs; $i += 4) {
123 $done_prolog = 0;
124 $xcm = $ARGV[$i + 0];
125 $ycm = $ARGV[$i + 1];
126 $xoff = 28.45 * $xcm;
127 $yoff = 28.45 * $ycm;
128 $mag = $ARGV[$i + 2];
129 $file = $ARGV[$i + 3];
130 print STDERR "Panel $i: '$file' origin = ($xcm, $ycm) cm, magnified $mag\n";
131 open (FILE, "$file") || die "Cannot open PostScript file $file\n";
132 while(<FILE>) {
133 next if /^\/origstate save def/; # See above
134 next if /^origstate restore/; # See above
135 next if /^%%Page:/;
136 if (/showpage/) {
137 print "grestore % gri_merge\n";
138 last if ($i != ($nargs - 4));
139 }
140 if ($i == 0 || $done_prolog) {
141 if (/^%%BoundingBox: (atend)/) {
142 print "%%BoundingBox: 0 0 612 792\n";
143 } elsif (/^%%BoundingBox:/) {
144 ;
145 } else {
146 print;
147 }
148 }
149 if (/%%EndProlog/) {
150 $done_prolog = 1;
151 &grey_page if ($i == 0);
152 &white_panel;
153 }
154 }
155 }
156 }
157
158 sub grey_page {
159 return if $underlay_gray == 1;
160 print "% gri_merge: `$ARGV[$i]' offset by ($xcm, $ycm) and scaled by $mag\n";
161 print "newpath % gri_merge\n";
162 if ($background_gray != 1) {
163 print "$underlay_gray setgray % gri_merge\n";
164 print "0 0 moveto % gri_merge\n";
165 print "$width_in 72 mul 0 lineto % gri_merge\n";
166 print "$width_in 72 mul $height_in 72 mul lineto % gri_merge\n";
167 print "0 $height_in 72 mul lineto % gri_merge\n";
168 print "closepath % gri_merge\n";
169 print "fill % gri_merge\n";
170 }
171 }
172 sub white_panel {
173 print "% gri_merge: `$ARGV[$i]' offset by ($xcm, $ycm) and scaled by $mag\n";
174 print "gsave % gri_merge\n";
175 print "$xoff $yoff translate % gri_merge\n";
176 print "$mag $mag scale % gri_merge\n";
177 if ($background_gray != 1) {
178 print "newpath % gri_merge\n";
179 print "$background_gray setgray % gri_merge\n";
180 print "0 0 moveto % gri_merge\n";
181 print "$width_in 72 mul 0 lineto % gri_merge\n";
182 print "$width_in 72 mul $height_in 72 mul lineto % gri_merge\n";
183 print "0 $height_in 72 mul lineto % gri_merge\n";
184 print "closepath % gri_merge\n";
185 print "fill % gri_merge\n";
186 }
187 }