Debian Codegraph @

texk/dvipsk/contrib/configs/mkdvipspapers

raw
1 #!/usr/local/bin/perl
2
3 #
4 # mkdvipspapers: a simple Perl script that generates a bunch of paper sizes
5 # definitions for dvips.
6 #
7 # Copyright (C) 1995, Yves Arrouye <[email protected]> [06/15/95]
8 #
9 # THIS CODE IS PROVIDED AS IS, WITHOUT ANY WARRANTIES, EITHER EXPRESSED OR
10 # IMPLIED, OF ITS FITNESS TO ANY PURPOSE. IT'S AUTHOR CANNOT BE HELD LIABLE
11 # FOR ANY DAMAGES OCCURING DUE TO THE USE OF THIS CODE. USE AT YOUR OWN RISKS!
12
13 # Known bugs: this script is written in Perl 5. (If you ever rewrite it in
14 # Perl 4, I'm interested to get your version; I can provide a Perl-4 options
15 # parser that is Getopt modified for it).
16
17 # Last change: added the -p (--paper) option for specifying default size
18 # (the first that will be generated). Arranged for the letter and a4 defs
19 # be the first ones if nothing is specified (i.e. in the dvips default).
20 # Note: the default paper size need not be in the chose ones, it is taken from
21 # all the known papers sizes.
22
23 # Read what follows for simple documentation and explanations.
24
25 #
26 # Options accepted by the script:
27 #
28 # --help print a small help message
29 # --version print version
30 #
31 # -s, --standard generate standard definitions (Adobe ones)
32 # -n, --non-standard generate dvips non-standard definitions
33 # -d, --dvips generate dvips (i.e. standard + some) definitions
34 # -g, --gs generate non-standard gs definitions
35 # -e, --extra generate non-standard extra definitions
36 # -l, --local generate local definitions
37 # -a, --all generate all definitions
38 #
39 # -p, --paper default paper size
40 #
41 # default behaviour is the same as --dvips.
42 #
43
44 use Getopt::Long;
45
46 #
47 # The paper sizes definitions are stored in arrays with the following entries
48 # for each paper size:
49 #
50 # - the name of the paper for dvips, e.g. letter;
51 # - the name of the paper for the DSC PaperSize comment, e.g. Letter;
52 # - the name of an accessible PostScript operator that may select the
53 # correct paper size, e.g. letter;
54 # - the name of a PostScript operator in statusdict that may select the
55 # correct paper tray. e.g. lettertray (this is preferred);
56 # - the physical width and height of the paper, either in points (no units
57 # or bp, as in 4000bp), in inches (e.g 8.5in), centimeters (e.g. 21cm)
58 # or in millimeters (e.g. 297mm);
59 # - a flag indicating the orientation of the sheet, 0 meaning portrait and
60 # 1 meaning landscape (this actually used only for gs' setpage operator).
61 #
62 # The PostScript code generated in the Setup section does the following.
63 # First, we look in statusdict in order to look for a tray selection operator.
64 # If there is none, but the specified PostScript operator is accessible with
65 # the current dictionaries stacking order, we use it. If setpagedevice is
66 # implemented and the other methods did fail, we use it. Otherwise, if the
67 # setpage operator is present in statusdict, use this one. The setpage op. is
68 # assumed to be
69 #
70 # <width> <height> <orient> setpage --
71 #
72 # where <orient> is 0 for portrait and 1 for landscape (in which case <width>
73 # and <height> are swapped!).
74 #
75
76 #
77 # This code has been tested with an Apple LaserWriter II, an Hewlett Packard
78 # 4L laser printer, Ghostscript 3.33 and Ghostview 1.5 with success.
79 #
80
81 #
82 # Enter your local definitions here in the same format as the examples
83 # below.
84 #
85 # Papers defined here: (none).
86 #
87
88 @localpapersdefs = (
89 );
90
91 #
92 # Standard (Adobe) names. Missing names are a4small and lettersmall, because
93 # I don't have their definition handy.
94 #
95 # Papers defined here: 11x17, a3, a4, b5, ledger, legal, letter, note.
96 #
97
98 @stdpapersdefs = (
99 [
100 'letter',
101 'Letter', 'letter', 'lettertray',
102 '8.5in', '11in', 0
103 ],
104 [
105 'a4',
106 'A4', 'a4', 'a4tray',
107 '210mm', '297mm', 0
108 ],
109 [
110 '11x17',
111 '11x17', '11x17', '11x17tray',
112 '11in', '17in', 0
113 ],
114 [
115 'a3',
116 'A3', 'a3', 'a3tray',
117 '297mm', '420mm', 0
118 ],
119 [
120 'b5',
121 'B5', 'ab5', 'b5tray',
122 '177mm', '250mm', 0
123 ],
124 [
125 'ledger',
126 'Ledger', 'ledger', 'ledgertray',
127 '11in', '17in', 1
128 ],
129 [
130 'legal',
131 'Legal', 'legal', 'legaltray',
132 '8.5in', '14in', 0
133 ],
134 [
135 'note',
136 'Note', 'note', 'notetray',
137 '7.5in', '10in', 0
138 ]
139 );
140
141 #
142 # Names that are present in the standard dvips distribution.
143 #
144 # Papers defined here: tabloid (aka 11x17).
145 #
146
147 @dvipspapersdefs = (
148 [
149 'tabloid',
150 'Tabloid', '11x17', '11x17tray',
151 '11in', '17in', 0
152 ],
153 );
154
155 #
156 # Names that are present in the standard gs distribution. (Some names are
157 # currently missing...)
158 #
159 # Papers defined here: a0, a1, a2, a5, a6, a7, a8, a9, a10,
160 # b0, b1, b2, b3, b4.
161 #
162
163 @gspapersdefs = (
164 [
165 'a0',
166 'A0', 'a0', 'a0tray',
167 '840mm', '1188mm', 0
168 ],
169 [
170 'a1',
171 'A1', 'a1', 'a1tray',
172 '594mm', '840mm', 0
173 ],
174 [
175 'a2',
176 'A2', 'a2', 'a2tray',
177 '420mm', '594mm', 0
178 ],
179 [
180 'a5',
181 'A5', 'a5', 'a5tray',
182 '148mm', '210mm', 0
183 ],
184 [
185 'a6',
186 'A6', 'a6', 'a6tray',
187 '105mm', '148mm', 0
188 ],
189 [
190 'a7',
191 'A7', 'a7', 'a7tray',
192 '74mm', '105mm', 0
193 ],
194 [
195 'a8',
196 'A8', 'a8', 'a8tray',
197 '52.5mm', '74mm', 0
198 ],
199 [
200 'a9',
201 'A9', 'a9', 'a9tray',
202 '37mm', '52.5mm', 0
203 ],
204 [
205 'a10',
206 'A10', 'a10', 'a10tray',
207 '26.25mm', '37mm', 0
208 ],
209 [
210 'b0',
211 'B0', 'b0', 'b0tray',
212 '2836bp', '4008bp', 0
213 ],
214 [
215 'b1',
216 'B1', 'b1', 'b1tray',
217 '2004bp', '2836bp', 0
218 ],
219 [
220 'b2',
221 'B2', 'b2', 'b2tray',
222 '1418bp', '2004bp', 0
223 ],
224 [
225 'b3',
226 'B3', 'b3', 'b3tray',
227 '1002bp', '1418bp', 0
228 ],
229 [
230 'b4',
231 'B4', 'b4', 'b4tray',
232 '709bp', '1002bp', 0
233 ],
234 );
235
236 @extrapapersdefs = (
237 [
238 'executive',
239 'Executive', 'executivepage', 'executivepagetray',
240 '7.25in', '10.5in', 0
241 ],
242 [
243 'halfexecutive',
244 'HalfExecutive', 'halfexecutivepage', 'halfexecutivepagetray',
245 '5.25in', '7.25in', 0
246 ]
247 );
248
249 sub basename {
250 local($fullname, $ext) = @_;
251 local($basename) = $fullname;
252
253 $basename =~ s,(.*/)?([^/]+),$2,;
254 $ext && $basename =~ s,$ext$,,;
255
256 $basename;
257 }
258
259 sub dumpsizes {
260 local($legend, @sizes) = @_;
261
262 print "$legend";
263
264 for $spec (@sizes) {
265 local(@array) = @$spec;
266 local($name) = @array;
267
268 print $name, " ";
269 }
270 print "\n";
271 }
272
273 sub usage {
274 local($exitcode) = @_;
275 local($myname) = &basename($0);
276
277 if ($exitcode) {
278 select STDERR;
279 }
280
281 print "usage: $myname [ --version ] [ -h, --help ] [ -s, --standard ] [ -n, --non-standard ] [ -d, --dvips ] [ -g, --gs ] [ -e, --extra ] [ -l, --local ] [ -a, --all ] [ -p, --paper papername ]\n";
282
283 if ($exitcode == 0) {
284 print "\n";
285 print "options: --version\t\tprint version information\n";
286 print " -h, --help\t\tprint this help message\n";
287 print " -s, --standard\t\tdefine standard paper sizes\n";
288 print " -n, --non-standard\tdefine dvips' non-standard paper sizes\n";
289 print " -d, --dvips\t\tdefine all dvips paper sizes\n";
290 print " -g, --gs\t\tdefine gs' non-standard paper sizes\n";
291 print " -e, --extra\t\tdefine extra paper sizes\n";
292 print " -l, --local\t\tdefine local paper sizes\n";
293 print " -a, --all\t\tdefine all previous paper sizes\n";
294 print " -p, --paper papername\tspecify default paper size\n";
295 print "\npapers: "; &dumpsizes("--standard\t\t", @stdpapersdefs);
296 print " "; &dumpsizes("--non-standard\t\t", @dvipspapersdefs);
297 print " "; &dumpsizes("--gs\t\t\t", @gspapersdefs);
298 print " "; &dumpsizes("--extra\t\t", @extrapapersdefs);
299 print " "; &dumpsizes("--local\t\t", @localpapersdefs);
300 }
301
302 exit $exitcode;
303 }
304
305 sub pssize {
306 local ($size) = @_;
307 local $number = $size;
308
309 $number =~ s/\D+$//;
310
311 if ($size =~ /in$/) {
312 $number *= 72;
313 } elsif ($size =~ /cm$/) {
314 $number *= 72 / 2.54;
315 } elsif ($size =~ /mm$/) {
316 $number *= 72 / 25.4;
317 }
318
319 return int($number + .4);
320 }
321
322 sub genpaperdefs {
323 local ($papername, $dscname, $psname, $trayname,
324 $width, $height, $orient) = @$_;
325 local ($pswidth, $psheight) = (&pssize($width), &pssize($height));
326
327 local ($exch) = ($orient == 1 ? " exch" : "");
328
329 if ($width =~ /^\d$/) { $width = "${width}bp"; }
330 if ($height =~ /^\d$/) { $height = "${height}bp"; }
331
332 print <<EPD
333 @ $papername $width $height
334 @+ ! %%DocumentPaperSizes: $dscname
335 @+ %%PaperSize: $dscname
336 @+ %%BeginPaperSize: $dscname
337 @+ /setpagedevice where {
338 @+ pop 1 dict dup /PageSize [ $pswidth $psheight$exch ] put setpagedevice
339 @+ } {
340 @+ statusdict /$trayname known {
341 @+ statusdict begin $trayname end
342 @+ /$psname where { pop $psname } if
343 @+ } {
344 @+ /$psname where {
345 @+ pop $psname
346 @+ } {
347 @+ statusdict /setpage known {
348 @+ statusdict begin
349 @+ $pswidth $psheight $orient setpage
350 @+ end
351 @+ } if
352 @+ } ifelse
353 @+ } ifelse
354 @+ } ifelse
355 @+ %%EndPaperSize
356
357 EPD
358 }
359
360 if (!&GetOptions("standard|s", "non-standard|n", "dvips|d", "gs|g", "extra|e",
361 "local|l", "all|a", "paper|p=s", "help|h", "version")) {
362 &usage(1);
363 } elsif ($#ARGV != -1) {
364 &usage(1);
365 } elsif ($opt_help) {
366 &usage(0);
367 } elsif ($opt_version) {
368 print &basename($0),
369 " version 1.2, by Yves Arrouye <Yves.Arrouye\@imag.fr>\n";
370 exit(0);
371 } else {
372 @allpapersdefs = (@stdpapersdefs, @dvipspapersdefs, @gspapersdefs,
373 @extrapapersdefs, @localpapersdefs);
374 if ($opt_all) {
375 @paperdefs = @allpapersdefs;
376 } else {
377 @papersdefs = ();
378
379 if ($opt_dvips) {
380 $opt_standard = 1;
381 $opt_non_standard = 1;
382 }
383
384 if ($opt_standard && !$opt_dvips) {
385 @papersdefs = (@papersdefs, @stdpapersdefs);
386 }
387 if ($opt_non_standard && !$opt_dvips) {
388 @papersdefs = (@papersdefs, @dvipspapersdefs);
389 }
390 if ($opt_gs) {
391 @papersdefs = (@papersdefs, @gsspapersdefs);
392 }
393 if ($opt_extra) {
394 @papersdefs = (@papersdefs, @extrapapersdefs);
395 }
396 if ($opt_local) {
397 @papersdefs = (@papersdefs, @localpapersdefs);
398 }
399
400 if ($#papersdefs == -1) {
401 @papersdefs = (@stdpapersdefs, @dvipspapersdefs);
402 }
403 }
404 }
405
406 # Find default paper def in *all* definitions and generate config
407
408 if ($opt_paper) {
409 for (@allpapersdefs) {
410 local(@paper) = @$_;
411 local($papername) = @paper;
412
413 if ($papername eq $opt_paper) {
414 genpaperdefs @paper;
415 last;
416 }
417 }
418 }
419
420 # Generate config for all selected paper sizes but the default one
421
422 for (@defaultpaper, @papersdefs) {
423 local($papername) = @$_;
424
425 if ($papername eq $opt_paper) { next; }
426
427 genpaperdefs $_;
428 }
429