1 % Copyright (C) 1991, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
3 % This software is provided AS-IS with no warranty, either express or
6 % This software is distributed under license and may not be copied,
7 % modified or distributed except as expressly authorized under the terms
8 % of the license contained in the file LICENSE in this distribution.
10 % For more information about licensing, please refer to
11 % http://www.ghostscript.com/licensing/. For information on
12 % commercial licensing, go to http://www.artifex.com/licensing/ or
13 % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
16 % $Id: gslp.ps,v 1.7 2005/05/07 22:56:56 ray Exp $
17 % gslp.ps - format and print text
19 % This utility provides functionality approximately equivalent to the Unix
20 % `enscript' program. It prints plain text files using a single font.
21 % It currently handles tabs and formfeeds, but not backspaces.
22 % It will line-wrap when using fixed-pitch fonts.
23 % It will also do kerning and width adjustment.
24 % Standard switches implemented:
25 % -12BclqRr -b<header> -f<font> -F<hfont> -L<lines> -p<outfile>
26 % Sun switches implemented:
29 % -GghKkmow -# -C -d -J -n -P -S -s -t -v
31 % --add-to-space <units>
32 % add the given number of 1/72" units to the width of each
33 % space (may be negative)
34 % --add-to-width <units>
35 % add the given number of 1/72" units to the width of each
36 % character (may be negative)
38 % print in <n> columns
40 % treat the file as PostScript if it starts with %!
42 % start printing at page <n>
44 % kern using information from the given .AFM file
46 % stop printing after page <n>
47 % --(heading|footing)-(left|center|right) <string>
48 % set the heading/footing fields; use -B first to clear
49 % --margin-(top|bottom|left|right) <inches>
51 % --no-eject-(file|formfeed)
52 % end-of-file/FF only starts a new column, not a new sheet
54 % use double (n=2), triple (n=3), etc. spacing
55 % Also, the string %# in a heading or footing is replaced with the page #.
56 /PageNumberString (%#) def
61 % build iso-latin-1 version of a font
62 /font-to-iso-latin-1 { % <font> font-to-iso-latin-1 <font>
63 %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
64 dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
65 /Encoding ISOLatin1Encoding def currentdict end
66 dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn
70 /find-latin-font { % <name> find-latin-font <font>
71 findfont font-to-iso-latin-1
74 % Define the initial values of the printing parameters.
78 /BodyFont null def % use default
79 /defaultBodyFontPortrait
80 /Courier find-latin-font 10 scalefont def
81 /defaultBodyFontLandscape
82 /Courier find-latin-font 7 scalefont def
84 { Landscape { defaultBodyFontLandscape } { defaultBodyFontPortrait } ifelse } def
86 /DetectFileType false def
96 /HeadingRight (page ) PageNumberString concatstrings def
97 /HeadingFont null def % use default
99 /Courier-Bold find-latin-font 10 scalefont def
100 /Kern 0 dict def % no kerning
102 /MarginBottom 36 def % 1/2"
103 /MarginLeft 36 def % 1/2"
104 /MarginRight 36 def % 1/2"
105 /MarginTop 36 def % 1/2"
106 /MaxLines 9999 def % max lines per page
107 /Noisy true def % i.e., not quiet
108 /OutFile null def % null = write directly to device
113 /Truncate false def % wrap long lines, don't truncate
115 % When writing to a file, we want to write out PostScript;
116 % when writing to the printer, we want to execute it;
117 % some commands should be executed regardless.
118 % lpexec provides for all this.
120 /lpdef { % <name> <value> lpdef -
124 /lpexec { % <arg1> ... <argn> </op> <n> <do_always> lpexec -
128 /t exch def 1 add /n exch def cvx
130 n 1 sub { n -1 roll dup wosp } repeat
138 /lpmoveto { % <x> <y> lpmoveto -
139 % Round the coordinates for smaller output.
141 exch 100 mul round 100 div
142 dup dup cvi eq { cvi } if
144 1 index X eq { neg exch pop /V 1 } { neg /M 2 } ifelse true lpexec
146 /lpshow { % <string> lpshow -
151 addwidth 0 6 -1 roll /awidthshow 6 true lpexec
153 4 -1 roll /widthshow 4 true lpexec
157 addwidth 0 3 -1 roll /ashow 3 true lpexec
160 dup dup length =string length gt {
174 OutFile null ne { cvx /setfont 1 false lpexec } { pop } ifelse
177 % Define some utility procedures.
179 /banner % ypos left center right
181 /addspace 0 def /addwidth 0 def
183 3 -1 roll bannerstring pop 0 4 index pwidth showline2 pop
184 exch bannerstring pwidth exch sub 2 div 3 index pwidth showline2 pop
186 % Prevent the last character of the heading from grazing
188 % ****** WHY DOES IT REQUIRE SO MUCH PADDING? ******
189 ( ) stringwidth pop 2 mul add
191 3 -1 roll pwidth showline2 pop
194 /bannerstring % string -> string width
195 { PageNumberString search
196 { exch pop pindex 4 string cvs concatstrings exch concatstrings
198 if dup stringwidth pop
203 /skipping pindex PageFirst ge pindex PageLast le and not def
204 pagex pagey Landscape {/BL} {/B} ifelse 2 true lpexec
206 skipping { nulldevice /OutFile null def } if
208 { lheight hdescent add
209 HeadingLeft HeadingCenter HeadingRight banner
213 /addspace AddToSpace def /addwidth AddToWidth def
214 pairkern length 0 ne {
215 /addspace AddToSpace lpdef /addwidth AddToWidth lpdef
222 topskip plength sub hdescent add
223 FootingLeft FootingCenter FootingRight banner
229 pagesave exch 0 true lpexec
230 /pindex pindex 1 add def
234 { lindex colines 1 sub add colines idiv colines mul
235 dup llength ge { pop endpage beginpage } { /lindex exch def } ifelse
238 /fontheight % <font> fontheight <ascent> <height>
241 (|^_j) false charpath
242 pathbbox exch pop dup 2 index sub 4 -2 roll pop pop
243 grestore exch 1.25 mul exch 1.25 mul
247 dup length wosp ( dict\n) ws
248 { (dup) ws exch wosp wosp ( put\n) ws } forall
250 /wosp { ( ) ws wo } def
252 dup type /dicttype eq { wdict } { OutFile exch write==only } ifelse
255 OutFile exch writestring
258 /outfont { % <name> <font> outfont -
262 dup wosp (-ISOLatin1) ws wosp ( RE) ws
263 /FontMatrix get 0 get 1000 mul round cvi wosp
264 ( scalefont def\n) ws
271 /CharFF StringFF 0 get def
273 /CharTAB StringTAB 0 get def
275 /showline % line -> leftover_line (handles \f)
276 { { showline1 dup length 0 eq { exit } if
277 dup 0 get CharFF ne { exit } if
278 EjectFF { endpage beginpage } { endcolumn } ifelse
284 /showline1 % line -> leftover_line (handles page break)
285 { lindex llength eq { endpage beginpage } if
286 lindex colines idiv cowidth mul % x
287 lindex colines mod 1 add lheight mul neg fascent sub % y
290 /lindex lindex 1 add def
294 /ty exch def /tx exch def
297 /showline2 { % string x y xlimit -> leftover_string (handles tabs)
298 2 index exch 5 2 roll setxy {
299 % Stack: xinit xlimit string
300 showline3 dup length 0 eq { exit } if
301 dup 0 get CharTAB ne { exit } if
302 tx 3 index sub tabwx div
303 0.05 add ceiling tabwx mul 3 index add ty setxy
305 tx 2 index ge { exit } if
306 } loop exch pop exch pop
309 /showline3 { % xlimit string -> xlimit leftover_string
310 % (finds line break / tab / formfeed)
312 cwx div 0.1 add cvi 0 .max 1 index length .min
313 1 index 0 3 -1 roll getinterval
315 StringFF search { exch pop exch pop } if
316 StringTAB search { exch pop exch pop } if
319 dup pairkern length 0 eq {
322 { kproc } exch /kshow 2 true lpexec
326 length dup 2 index length exch sub getinterval
329 /kproc { % <char1> <char2> kproc -
330 pairkern currentfont /Encoding get 3 index get
332 get currentfont /Encoding get 2 index get
334 get currentfont /FontMatrix get 0 get mul
341 addwidth add 2 index 32 eq { addspace add } if
342 dup 0 eq { pop } { 0 rmoveto } ifelse
347 { dup length 1 sub 1 exch getinterval
350 /e== { % <object> e== - -- print an object to stderr
351 (%stderr) (w) file dup 3 -1 roll write==only flushfile
354 /eprint { % <string> eprint - -- print a string to stderr
355 (%stderr) (w) file dup 3 -1 roll writestring flushfile
358 % Read kerning information from a .AFM file.
360 /readkern { % <afmfile> readkern <pairkerndict>
361 /mfilename 1 index def
362 (r) file /mfile exch def
363 mfile =string readline pop
364 (StartFontMetrics ) anchorsearch {
367 { mfile =string readline pop
368 (EndFontMetrics) anchorsearch { pop pop exit } if
369 (KPX ) anchorsearch {
370 pop token pop cvlit /char1 exch def
371 token pop cvlit /char2 exch def
372 token pop /kern exch def pop
373 kdict char1 .knownget not {
374 5 dict kdict char1 2 index .growput
383 mfilename eprint ( does not begin with StartFontMetrics.\n) eprint
389 % The main printing procedure
395 /lpfirst { % - lpfirst -
396 % Define some abbreviating procedures.
397 /B {save 3 1 roll translate /X 0 def} lpdef
398 /BL {save 3 1 roll 90 rotate translate /X 0 def} lpdef
399 /E {showpage restore} lpdef
400 /V {neg X exch moveto} lpdef
401 /M {/X 2 index def neg moveto} lpdef
402 /S {currentfile =string readline pop show} lpdef
403 /RE { % <isoname> <fontname> RE <font>
405 %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
406 dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
407 /Encoding ISOLatin1Encoding def currentdict end
412 /lp { % file initial_chars ->
416 doFirst { lpfirst /doFirst false def } if
418 % Initialize the device and fonts.
420 BodyFont null eq { defaultBodyFont } { BodyFont } ifelse def
427 HeadingFont null eq { defaultHeadingFont } { HeadingFont } ifelse def
435 % Get the layout parameters.
437 gsave % for possible rotation
438 Landscape { 90 rotate } if
439 BFont setfont ( ) stringwidth pop /cwx exch def
440 cwx Tab mul /tabwx exch def
441 BFont fontheight /fheight exch def /fascent exch def
442 Headers Footers or { HFont fontheight } { 0 0 } ifelse
443 /hheight exch def /hascent exch def
444 /hdescent hheight hascent sub def
445 fheight Spacing mul /lheight exch def
446 Headers { hheight lheight add } { 0 } ifelse
448 Footers { hheight lheight add } { 0 } ifelse
450 /pskip topskip botskip add def
451 % Translate the page so that (0,0) corresponds to
452 % the top of the topmost body line.
454 2 index sub MarginBottom MarginTop add sub /plength exch def
455 2 index sub MarginLeft MarginRight add sub /pwidth exch def
456 pwidth Columns div /cowidth exch def
458 exch MarginBottom add plength add topskip sub
459 /pagey exch def /pagex exch def
460 plength pskip sub lheight div cvi MaxLines .min
461 dup /colines exch def
462 Columns mul /llength exch def
464 OutFile null ne { nulldevice } if
468 { (Page height = ) eprint llength e==
472 % Write the kerning table, if relevant.
473 OutFile null ne Kern length 0 ne and {
474 (/kproc) ws /kproc load wosp ( def\n) ws
475 (/pairkern) ws Kern wosp ( def\n) ws
478 % Disable stack recording so we can use stopped with readline.
479 $error /recordstacks false put
481 % Initialize for the first page.
482 /lbuf 64000 string def
486 % Iterate through the file.
488 { dup length /pos exch def
489 lbuf exch 0 exch putinterval
490 { lpfile lbuf pos lbuf length pos sub getinterval readline } stopped
491 { % Filled the line before a CR or EOF.
494 { % Reached CR and/or EOF first.
495 exch length pos add lbuf exch 0 exch getinterval
496 1 index { showline } if % omit final empty line
497 { dup length 0 eq Truncate or { pop () exit } if
504 pindex PageLast gt { exit } if
509 %**************** WHY IS THIS COMMENTED OUT? ****************
510 % EjectEOF { endpage } { endcolumn } ifelse
519 % prints <file> using the current parameter settings.
520 % Usage: [ <arg1> ... <argn> ] lpcommand
521 % interprets args like a command line.
523 /lp { save lpdict begin () lp end restore } def
527 /splitfn % (FontNN.NN) -> <font>
528 { dup /arg exch def length
529 { dup 0 le { exit } if
530 dup 1 sub arg exch get dup 48 ge 1 index 59 le and exch 46 eq or not { exit } if
533 arg exch 0 exch getinterval dup cvn find-latin-font
534 exch arg exch anchorsearch pop pop cvr scalefont
537 % Parse the command line switches.
539 /doswitch % argn ... arg1 (-?) restofswitch ->
540 { exch dup cvn lpdict exch known
542 { exch pop (Unknown switch: ) eprint eprint (\n) eprint }
546 /more % argn ... arg1 restofswitch ->
548 { (- ) dup 1 3 index 0 get put
549 exch dup length 1 sub 1 exch getinterval
557 /-- { (--) exch concatstrings
558 dup cvn lpdict exch known
560 { (Unknown switch: ) eprint eprint (\n) eprint }
563 /--add-to-space { cvr /AddToSpace exch def } def
564 /--add-to-width { cvr /AddToWidth exch def } def
565 /--columns { cvi 1 .max /Columns exch def } def
566 /--detect { /DetectFileType true def } def
567 /--first-page { cvi /PageFirst exch def } def
568 /--footing-center { /FootingCenter exch def /Footers true def } def
569 /--footing-left { /FootingLeft exch def /Footers true def } def
570 /--footing-right { /FootingRight exch def /Footers true def} def
571 /--heading-center { /HeadingCenter exch def /Headers true def } def
572 /--heading-left { /HeadingLeft exch def /Headers true def } def
573 /--heading-right { /HeadingRight exch def /Headers true def } def
574 /--kern { readkern /Kern exch def } def
575 /--last-page { cvi /PageLast exch def } def
576 /--margin-bottom { cvr 72.0 mul /MarginBottom exch def } def
577 /--margin-left { cvr 72.0 mul /MarginLeft exch def } def
578 /--margin-right { cvr 72.0 mul /MarginRight exch def } def
579 /--margin-top { cvr 72.0 mul /MarginTop exch def } def
580 /--no-eject-file { /EjectEOF false def } def
581 /--no-eject-formfeed { /EjectFF false def } def
582 /--spacing { cvr /Spacing exch def } def
584 /-# { pop } def % ignore
586 (-1)cvn { /Columns 1 def more } def
587 (-2)cvn { /Columns 2 def more } def
588 /-b { /HeadingLeft exch def /HeadingCenter () def /HeadingRight PageNumberString def
592 /-B { /HeadingLeft () def /HeadingCenter () def /HeadingRight () def
594 /FootingLeft () def /FootingCenter () def /FootingRight () def
599 /-C { pop } def % ignore
600 /-c { /Truncate true def more } def
601 /-d { pop } def % ignore
602 /-f { splitfn /BodyFont exch def } def
603 /-F { splitfn /HeadingFont exch def } def
604 /-G { more } def % ignore
605 /-g { more } def % ignore
606 /-h { more } def % ignore
607 /-J { pop } def % ignore
608 /-K { more } def % ignore
609 /-k { more } def % ignore
611 /-L { cvi /MaxLines exch def } def
612 /-m { more } def % ignore
613 /-n { pop } def % ignore
614 /-o { more } def % ignore
615 /-p { (w) file /OutFile exch def OutFile (%!\n) writestring } def
616 /-P { pop } def % ignore
617 /-q { /Noisy false def more } def
618 /-r { /Landscape true def more } def
619 /-R { /Landscape false def more } def
620 /-S { pop } def % ignore
621 /-s { pop } def % ignore
622 /-T { cvi /Tab exch def } def
623 /-v { pop } def % ignore
624 /-w { more } def % ignore
627 { break not { dup /HeadingLeft exch def } if
629 { (Printing ) eprint dup eprint (\n) eprint
632 % If requested, check for a PostScript file.
634 { dup 2 string readstring pop dup (%!) eq
635 { % Yes, it's a PostScript file.
636 pop dup 80 string readline pop pop cvx exec
647 /lpcstring 8192 string def
651 /lpcommand % <[arg1 ... argn]> lpcommand <any_printed>
652 { % Push the commands on the stack in reverse order
654 dup length 1 sub -1 0 { 1 index exch get exch } for pop
658 { dup mark eq { pop exit } if
659 dup length 2 ge { dup 0 get (-) 0 get eq } { false } ifelse
660 { dup 0 2 getinterval
661 exch dup length 2 sub 2 exch getinterval
664 { dup /matched false def
665 { /matched true def /any true def lp1 } lpcstring filenameforall
666 matched { pop } { lp1 } ifelse % let the error happen
671 { OutFile (%stdout) (w) file ne { OutFile closefile } if
679 { ] dup length 0 ne { lpcommand } { false } ifelse not
682 /PROGNAME where { pop PROGNAME } { (gslp) } ifelse
683 ( [-12BclqRr] [-b<header>] [-f<font>] [-F<hfont>]\n)
684 ( [-L<lines>] [-p<outfile>] [-T<tabwidth>]\n)
685 ( [--add-to-(space|width) <units>] [--columns <n>]\n)
686 ( [--detect] [--first-page <page#>] [--last-page <page#>]\n)
687 ( [--(heading|footing)-(left|right|center) <string>]\n)
688 ( [--kern <afmfile>] [--margin-(top|bottom|left|right) <inches>]\n)
689 ( [--no-eject-(file|formfeed)] [--spacing <n>] file1 ... filen\n)
690 ] { 2 copy writestring pop } forall dup flushfile closefile