home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / fj / lang / perl / 147 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  25.1 KB

  1. Xref: sparky fj.lang.perl:147 fj.lang.postscript:345
  2. Newsgroups: fj.lang.perl,fj.lang.postscript
  3. Path: sparky!uunet!ccut!wnoc-tyo-news!sranha!sranhd!sran230!utashiro
  4. From: utashiro@sran230.sra.co.jp (Kazumasa Utashiro)
  5. Subject: a2ps v1.24
  6. Organization: Software Research Associates, Inc., Japan
  7. Date: Wed, 18 Nov 1992 06:03:29 GMT
  8. Message-ID: <BxwE5w.2xn@sran230.sra.co.jp>
  9. Distribution: fj
  10. Lines: 842
  11.  
  12.  
  13. $B:#G/$N(J1$B7n$K%]%9%H$7$?(J a2ps v1.18 $B$N:G?7HG$G$9!#%P%0%U%#%C%/(J
  14. $B%90J30$NJQ99E@$O<!$N$h$&$J$b$N$G$9!#4pK\E*$K$O$"$^$jJQ$o$C$F(J
  15. $B$$$^$;$s!#(J
  16.  
  17.     o $B%"%s%@!<%i%$%sJ8;z$K(J Courier-Oblique $B$G$O$J$/(J 
  18.       Courier-BoldOblique $B$r;H$&!#(J
  19.  
  20.     o -toc $B%*%W%7%g%s$NDI2C!#$3$l$OL\<!$r:n$k$?$a!#CfES(J
  21.       $BH>C<$K:n$C$F$"$k$N$G!";H$$$?$$?M$OE,Ev$K9)IW$7$F$/(J
  22.       $B$@$5$$!#85!9$O(J locore.s $B$r%W%j%s%H$9$k$?$a$KIU$1$?!#(J
  23.  
  24.     o nkf $B$,$J$/$F$b<+J,$GJQ49$7$FF0$/$h$&$K$7$?!#(J
  25.  
  26. $B%*%j%8%J%k$N(J a2ps $B$H$N0c$$$O<!$N$H$*$j!#(J
  27.  
  28. >>     - $BF|K\8l$,=PNO$G$-$k!#%U%)%s%H$O(J /Ryumin-Light-H $B$,(J
  29. >>       $B;H$o$l$k!#$?$@$7!"%3!<%IJQ49$r%3%^%s%I$KMj$C$F$$$k(J
  30. >>       $B$N$G(J nkf $BEy$,I,MW!#(J
  31. >> 
  32. >>     - nroff $B=PNO$N=E$M$&$AItJ,$K(J Courier-Bold $B$H(J
  33. >>       GothicBBB-Medium-H $B$r;H$&!#(J
  34. >> 
  35. >>     - nroff $B=PNO$N%"%s%@!<%i%$%sItJ,$K(J Courier-Oblique 
  36. >>       $B$H(J /Ryumin-Light-H $B$N<PBN$r;H$&!#(J
  37. >> 
  38. >>     - $BF|IU$1ItJ,$NJQ99$,2DG=!#(J
  39. >> 
  40. >>     - $B:82<$N6y$K%i%Y%k$r=PNO$9$k!#B?$/$N%Z!<%8$NCf$+$iI,(J
  41. >>       $BMW$JItJ,$r$_$D$1$k$N$rMF0W$K$9$k$?$a!#(J
  42.  
  43. $B$^$@$"$^$jG<F@$G$-$k=PMh$G$O$J$$$N$G!"&C%j%j!<%9$H$$$&$H$3$m(J
  44. $B$G$7$g$&$+!#%P%0%l%]!<%H!"%3%a%s%HEy$"$j$^$7$?$i!"$*CN$i$;$/(J
  45. $B$@$5$$!#(J
  46.  
  47. $BK\Ev$O:G=i$+$i:n$jD>$7$?$$$1$I!"%]%9%H%9%/%j%W%H$O$h$/$o$+$i(J
  48. $B$J$$!D(J
  49.  
  50. --utashiro
  51.  
  52. #!/usr/local/bin/perl
  53. ;#
  54. ;# a2ps: ascii to ps
  55. ;#
  56. ;# Copyright (c) 1990,1991,1992 Kazumasa Utashiro
  57. ;# Software Research Associates, Inc., Japan <utashiro@sra.co.jp>
  58. ;# InterTech Data Systems, Inc., Cupertino CA <utashiro@InterTech.COM>
  59. ;#
  60. ;; $rcsid = q$Id: a2ps,v 1.24 1992/10/13 13:06:19 utashiro Exp $;
  61. ;#
  62. ;# This program is perl version of Miguel Santana's a2ps.  Postscript
  63. ;# kanji enhancement was done by N. Kanazawa <kanazawa@sra.co.jp>.
  64. ;# Converted to perl and enhanced by K. Utashiro <utashiro@sra.co.jp>.
  65. ;#
  66. ;# Please change next line for default paper.
  67. ;# ('us' for US letter size, 'a4' for A4 size)
  68. ;#
  69. ;;    $default_paper = 'a4';
  70. ;#
  71. ;# Change next line for default action of converting to jis code. If
  72. ;# the variable $jisconvert is true, a2ps tries to convert the input
  73. ;# text to jis code.  It tries to use some converting program like nkf
  74. ;# first.  If failed to exec these programs, a2ps does converting work
  75. ;# by itself.
  76. ;#
  77. ;;    $jisconvert = 1;
  78. ;#
  79. ;# WISH LIST
  80. ;#    - change algorithm to determine frame size (buggy on big font)
  81. ;#    - print toc matched pattern at the bottom of pages
  82. ;#
  83. @mon = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
  84. @day = (Sun, Mon, Tue, Wed, Thu, Fri, Sat);
  85. @mon{@mon} = ($[ .. $#mon);
  86. @day{@day} = ($[ .. $#day);
  87.  
  88. sub paper {
  89.     local($p) = shift; $paper =~ s/.*/\L$&/;
  90.     ($width, $height, $lmargin, $smargin, $fontsize_l, $fontsize_p,
  91.      $portrait_header, $landscape_header, $paper_adjust)
  92. #    = ($p eq 'us') ? (8.50, 11.06, 0.95, 1.2, 6.85, 9.5, 0.29, 0.22, 0.12)
  93.     = ($p eq 'us') ? (8.50, 11.06, 0.65, 1.2, 6.85, 9.5, 0.29, 0.22, 0.12)
  94.                : (8.27, 11.64, 1.20, 1.2, 6.60, 9.8, 0.29, 0.22, 0);
  95. }
  96. &paper($default_paper);
  97. $pixels_inch = 72;    $selfconvert = 0;
  98. $numbering = 0;        $folding = 1;        $restart = 1;
  99. $only_printable = 0;    $copies_number = 1;
  100. $landscape = 1;        $wide_pages = 0;    $twinpage = 1;
  101. $no_border = 0;        $no_header = 0;        $tab_w = 8;
  102. $skip_column = 1;    $numformat = '%-5d ';
  103. $oblique = 1;        $bold = 1;
  104. $kanji_ascii_ratio = 1.0;
  105. $default_sublabel = q#%month %mday 19%year %hour:%min#;
  106.  
  107. $re_sjis_s = '([\201-\237\340-\374][\100-\176\200-\374])+';
  108. $re_euc_s  = '([\241-\376]{2})+';
  109. $re_jin    = '\033\$[\@\B]';
  110. $re_jout   = '\033\([BJ]';
  111.  
  112. while ($_ = $ARGV[0], s/^-(.+)$/$1/ && shift) {
  113.     next if $_ eq '';
  114.     if (s/^help$//)        {&usage;                next;}
  115.     if (s/^(us|a4)$//)        {&paper($1);                next;}
  116.     if (s/^l(.*)$//)        {defined($label=$1||shift)||&usage;    next;}
  117.     if (s/^L(.*)$//)        {defined($sublabel=$1||shift)||&usage;    next;}
  118.     if (s/^toc$//)        {defined($toc=shift)||&usage;        next;}
  119.     if (s/^k([\d\.]+)$//)    {$kanji_ascii_ratio=$1;            next;}
  120.     if (s/^f([\d\.]+)$//)    {$font_size=$1;                next;}
  121.     if (s/^fx([\d\.]+)$//)    {$font_mag=$1;                next;}
  122.     if (s/^j([\d\.]*)$//)    {$ascii_mag=$1||1.2;            next;}
  123.     if (s/^d(\d*)$//)        {$debug=$1||1;                next;}
  124.  
  125.     if (s/^(n?v)//)        {$only_printable=($1 ne 'v');        redo;}
  126.     if (s/^(n?w)//)        {$wide_pages=($1 eq 'w');        redo;}
  127.     if (s/^(n?c)//)        {$jisconvert=($1 eq 'c');        redo;}
  128.     if (s/^(n?p)//)        {$landscape=($1 ne 'p');        redo;}
  129.     if (s/^(n?h)//)        {$no_header=($1 ne 'h');        redo;}
  130.     if (s/^(n?s)//)        {$no_border=($1 ne 's');        redo;}
  131.     if (s/^(n?t)//)        {$no_footer=($1 ne 't');        redo;}
  132.     if (s/^(n?f)//)        {$folding=($1 eq 'f');            redo;}
  133.     if (s/^(n?r)//)        {$restart=($1 eq 'r');            redo;}
  134.     if (s/^(n?b)//)        {$bold=($1 eq 'b');            redo;}
  135.     if (s/^(n?o)//)        {$oblique=($1 eq 'o');            redo;}
  136.     if (s/^(n?C)//)        {$selfconvert=($1 eq 'C');        redo;}
  137.     if (s/^(n?n)//)        {$numbering=($1 eq 'n');        redo;}
  138.     &usage;
  139. }
  140.  
  141. sub usage {
  142.     ($command = $0) =~ s#.*/##;
  143.     select(STDERR); $|=0;
  144.     print "syntax: $command [switches] [files]\n";
  145.     print <<"    >>";
  146.     switches are:
  147.     -l \@    label string
  148.     -L \@    sub-label string (\%default="$default_sublabel")
  149.     -[n]t    tail label (t)
  150.     -[n]n    numbering (n)
  151.     -[n]h    header (h)
  152.     -[n]s    scale (s)
  153.     -[n]w    wide page (nw)
  154.     -[n]p    portrait (np)
  155.     -[n]f    folding (f)
  156.     -[n]c    convert to jis code (c)
  157.     -[n]r    reset sheet number on each file (r)
  158.     -[n]b    use bold/gothic font for overstruck characters (b)
  159.     -[n]o    use oblique font for underlined characters (o)
  160.     -f[x]#    font size or maginificent (6.6 or 9.8)
  161.     -k#    kanji:ascii font size ratio (1.0)
  162.     -j[#]    adjust ascii font height to Japanese (1.0)
  163.     -us/a4    US letter / A4
  164.     -toc pattern
  165.         specify table of contents pattern
  166.     -help    print this message
  167.     >>
  168.     print "($rcsid)\n";
  169.     exit 1;
  170. }
  171.  
  172. $twinpage = ($landscape && !$wide_pages);
  173. $font_size = $landscape ? $fontsize_l : $fontsize_p unless ($font_size);
  174. $font_size *= $font_mag if ($font_mag);
  175. $sheet_height = ($height - $lmargin) * $pixels_inch;
  176. $sheet_width = ($width - $smargin) * $pixels_inch;
  177. $char_width = 0.6 * $font_size;
  178. $skip_column = 0 if ($numbering);
  179. $esc = $only_printable ? ' ' : '^[';
  180.  
  181. ($header, $page_width, $page_height) =
  182.     $landscape ? ($landscape_header, $sheet_height, $sheet_width)
  183.            : ($portrait_header, $sheet_width, $sheet_height);
  184. $header_size = $no_header ? 0 : $header * $pixels_inch;
  185. $linesperpage = (int(($page_height-$header_size)/($font_size * 1.1))) - 1;
  186. if (!$landscape || $wide_pages) {
  187.     $columnsperline = (int($page_width / $char_width)) - 1;
  188. } else {
  189.     $page_height = ($height - ($lmargin * 5 / 3)) * $pixels_inch;
  190.     $columnsperline = (int((int($page_height / 2)) / $char_width)) - 1;
  191. }
  192.  
  193. if ($linesperpage <= 0 || $columnsperline <= 0) {
  194.     printf STDERR "Font %g too big !!\n", $font_size;
  195.     exit(1);
  196. }
  197.  
  198. if ($debug == 2) {
  199.     require('dumpvar.pl');
  200.     &dumpvar('main',
  201.          'width', 'height', 'lmargin', 'smargin', 'font_size',
  202.          'sheet_height', 'sheet_width', 'char_width', 'skip_column',
  203.          'header', 'page_width', 'page_height', 'header_size',
  204.          'linesperpage', 'columnsperline');
  205.     exit(0);
  206. }
  207.  
  208. push(@ARGV, '') unless (@ARGV);
  209. while (@ARGV) {
  210.     $file = shift;
  211.     if ($file && !-r $file) {warn "$file: $!\n"; next;}
  212.     if ($jisconvert) {
  213.     open(F, "-|") || &jis($file);
  214.     } else {
  215.     $file = '-' if $file eq '';
  216.     open(F, $file) || (print STDERR "$file: $!\n", next);
  217.     }
  218.     $file = 'stdin' if $file =~ /^-?$/;
  219.     if ($toc) {
  220.     $TOC = $file . '.toc';
  221.     die "$TOC exists.\n" if -e $TOC;
  222.     open(TOC, ">$TOC") || die "$TOC: $!\n";
  223.     }
  224.     &print_file($file, $label);
  225.     close(F);
  226.     close(TOC) if $toc;
  227. }
  228. print "\n%%Trailer\ncleanup\ndocsave restore end\n" if $header_is_printed;
  229. exit;
  230.  
  231. ############################################################
  232.  
  233. sub print_file {
  234.     local($name, $label) = @_;
  235.     defined($label) || ($label = $name || 'stdin');
  236.     $label =~ s/[\(\)\\]/\\$&/g;
  237.     defined($sublabel) && do { $sublabel =~ s/[\(\)\\]/\\$&/g; };
  238.     $line_number=0;
  239.  
  240.     &print_header;
  241.  
  242.     print "($label) newfile\n";
  243.     if ($restart) {
  244.     print "/sheet 1 def\n";
  245.     $sheets = 0;
  246.     }
  247.     $page = 0;
  248.     $maxrest = $columnsperline - $skip_column;
  249.     $numberwidth = length(sprintf($numformat,0));
  250.     $maxrest -= $numberwidth if $numbering;
  251.     $lastnumber = -1;
  252.     $show = 's';
  253.     $line = 1; $bl = 1;
  254.  
  255.     while (<F>) {
  256.     $line_number++;
  257.     if ($toc && /$toc/o) {
  258.         print TOC "$sheets:$page:$line_number:$+:$_";
  259.     }
  260.     1 while s/\t/' 'x($tab_w-(&pwidth($`)%$tab_w))/e;
  261.     if ($only_printable) {
  262.         s/[\000-\032\034-\037\177-\377]/ /g;
  263.     } else {
  264.         s/[\200-\377]/'M-'.pack('c',ord($&)&0177)/ge;
  265.         s/[\000-\007\013\016-\032\034-\037]/'^'.pack('c',ord($&)|0100)/ge;
  266.         s/\0177/^?/g;
  267.     }
  268.     s/\033\$[B\@]/\005/g; s/\033\([BJ]/\006/g;
  269.     if (/\010/) {
  270.         if ($oblique) {
  271.         s/__\010\010(\005)?(..)/\003$1$2\004/g;
  272.         s/_\010(.)/\003$1\004/g;
  273.         s/\004\003//g;
  274.         }
  275.         if ($bold) {
  276.         s/(..)\006?\010\010\005?\1/\001$1\002/g;
  277.         s/(.)(\010\1)+/\001$1\002/g;
  278.         s/\002\001//g;
  279.         }
  280.     }
  281.     $rest = $maxrest;
  282.     @l = split(/([\001-\006\010\f\n\r])/);
  283.     while (defined($w = shift(@l))) {
  284.         if ($w eq '') { next; }
  285.         if ($w eq "\f") {$bl || &nl; &rp; next;}
  286.         $bl && &bl;
  287.         if ($w eq "\b") {$rest++, print ' bs' if ($rest<$maxrest); next;}
  288.         if ($w eq "\n") {&nl; next;}
  289.         if ($w eq "\r") {&cr; &bl; next;}
  290.         if ($w eq "\001") {print ' B'; next;}    # bold start
  291.         if ($w eq "\002") {print ' R'; next;}    # bold end
  292.         if ($w eq "\003") {print ' I'; next;}    # italic start
  293.         if ($w eq "\004") {print ' R'; next;}    # italic end
  294.         if ($w eq "\005") {                # kanji start
  295.         $kanji = 1; $show = 'ks';
  296.         print ' initkanji' if !$initkanji++;
  297.         next;
  298.         }
  299.         if ($w eq "\006") {                # kanji end
  300.         $kanji = 0; $show = 's'; next;
  301.         }
  302.         $show_width = $rest & ~$kanji;
  303.         if ($show_width < length($w)) {
  304.         ($w, $folded) = unpack("a$show_width a*", $w);
  305.         }
  306.         $rest -= length($w);
  307.         $w =~ s/[\(\)\\]/\\$&/g;
  308.         print ' (', $w, ') ', $show;
  309.         if (defined($folded)) {
  310.         unshift(@l, $folded); $rest=$maxrest; &nl;
  311.         undef($folded);
  312.         }
  313.     }
  314.     }
  315.     &ep;
  316. }
  317.  
  318. sub rp {
  319.     if ($line%$linesperpage != 1) {
  320.     $line=$linesperpage*(int($line/$linesperpage)+1)+1;
  321.     }
  322. }
  323.  
  324. sub np {
  325.     &ep if ($page++ > 0);
  326.     if (!$twinpage || ($page%2)==1) {
  327.     $sheets++;
  328.     print "%%Page: $sheets $sheets\n";
  329.     $initkanji = 0;
  330.     print "initkanji\n" if $kanji;
  331.     }
  332.     print "startpage\n";
  333.     &rp;
  334. }
  335.  
  336. sub bl {
  337.     &np if ($bl && ($line % $linesperpage) == 1);
  338.     $bl = 0;
  339.     $rest = $maxrest;
  340.     print 'bl (', ' ' x $skip_column;
  341.     if ($numbering) {
  342.     if ($line_number != $lastnumber) {
  343.         printf ($numformat, $line_number);
  344.         $lastnumber = $line_number;
  345.     } else {
  346.         print ' ' x $numberwidth;
  347.     }
  348.     }
  349.     print ') s';
  350. }
  351.  
  352. sub nl {$line++; print " nl\n"; $bl = 1;}
  353. sub cr {print ' cr ';}
  354. sub ep {print "\nendpage\n";}
  355.  
  356. sub max { $_[ ($_[$[] < $_[$[+1]) + $[]; }
  357.  
  358. sub pwidth {
  359.     return(length($_[0])) unless($_[0]=~/[\033\010\f\r]/);
  360.     local($_)=shift;
  361.     s/^.*[\f\r]//;
  362.     s/\033\$[\@B]|\033\([JB]//g;
  363.     1 while s/[^\010]\010//;
  364.     s/^\010*//;
  365.     length($_);
  366. }
  367.  
  368. sub jis {
  369.     unless ($selfconvert) {
  370.     exec "nkf -b -j @_";
  371.     exec "jconv -j @_";
  372.     exec "jcat -IJ @_";
  373.     }
  374.     open(STDIN, $file) || die "$file: $!\n" if $file = shift;
  375.     while (<>) {
  376.     print, next if (!@readahead && !/[\033\200-\377]/);
  377.     push(@readahead, $_);
  378.     next unless ($kcode = &kcode(*_));
  379.     $convf = ($kcode || 'jis') . '2jis';
  380.     eval "do \$convf(*_), print while (\$_ = shift(\@readahead));" .
  381.          "do \$convf(*_), print while (<>);";
  382.     exit(0);
  383.     }
  384.     print @readahead;
  385.     exit(0);
  386. }
  387.  
  388. sub kcode {
  389.     local(*_, $sjis, $euc) = @_;
  390.     return undef unless /[\033\200-\377]/;
  391.     return 'jis' if /$re_jin|$re_jout/o;
  392.     $sjis += length($&) while /$re_sjis_s/go;
  393.     $euc += length($&) while /$re_euc_s/go;
  394.     return ('euc', undef, 'sjis')[($sjis <=> $euc) + $[ + 1];
  395. }
  396.  
  397. sub jis2jis { 1; }
  398.  
  399. sub sjis2jis {
  400.     local(*_) = @_;
  401.     s/$re_sjis_s/&_sjis2jis($&)/geo;
  402. }
  403. sub _sjis2jis {
  404.     local($_) = @_;
  405.     s/../$s2j{$&}||&s2j($&)/ge;
  406.     "\033\$B" . $_ . "\033\(B";
  407. }
  408. sub s2j {
  409.     local($c1, $c2) = unpack('CC', shift);
  410.     if ($c2 >= 0x9f) {
  411.     $c1 = ($c1 * 2 - ($c1 >= 0xe0 ? 0xe0 : 0x60)) & 0x7f;
  412.     $c2 -= 0x7e;
  413.     } else {
  414.     $c1 = ($c1 * 2 - ($c1 >= 0xe0 ? 0xe1 : 0x61)) & 0x7f;
  415.     $c2 = ($c2 + 0x60 + ($c2 < 0x7f)) & 0x7f;
  416.     }
  417.     $s2j{$&} = pack('cc', $c1, $c2);
  418. }
  419.  
  420. sub euc2jis {
  421.     local(*_) = @_;
  422.     s/$re_euc_s/&_euc2jis($&)/geo;
  423. }
  424. sub _euc2jis {
  425.     local($_) = @_;
  426.     tr/\200-\377/\000-\177/;
  427.     "\033\$B" . $_ . "\033\(B";
  428. }
  429.  
  430. sub print_header {
  431.     return if $debug;
  432.     return if ($header_is_printed++);
  433.     print <<'---';
  434. %! a2ps 3.0
  435.  
  436. /$a2psdict 100 dict def
  437. $a2psdict begin
  438. % Initialize page description variables.
  439. /inch {72 mul} bind def
  440. ---
  441.     printf("/landscape %s def\n", $landscape ? "true" : "false");
  442.     printf("/twinpage %s def\n", $twinpage ? "true" : "false");
  443.     printf("/sheetheight %g inch def\n", $height);
  444.     printf("/sheetwidth %g inch def\n", $width);
  445.     printf("/lmargin %g inch def\n", $lmargin);
  446.     printf("/smargin %g inch def\n", $smargin);
  447.     printf("/paper_adjust %g inch def\n", $paper_adjust);
  448.     printf("/noborder %s def\n", $no_border ? "true" : "false");
  449.     if ($no_header) {
  450.     print "/noheader true def\n";
  451.     print "/headersize 0.0 def\n";
  452.     }
  453.     else {
  454.     printf("/noheader false def\n");
  455.     printf("/headersize %g inch def\n",
  456.            $landscape ? $landscape_header : $portrait_header);
  457.     }
  458.     if ($no_footer) {
  459.     print "/nofooter true def\n";
  460.     } else {
  461.     print "/nofooter false def\n";
  462.     }
  463.     printf("/bodyfontsize %g def\n", $font_size);
  464.     printf("/kanjiAsciiRatio %g def\n", $kanji_ascii_ratio);
  465.     printf("/lines %d def\n", $linesperpage);
  466.     printf("/columns %d def\n", $columnsperline);
  467.     $sublabel = $default_sublabel unless defined($sublabel);
  468.     print "/date (", &date($sublabel, time), ") def\n";
  469.     if ($ascii_mag) {
  470.     printf("/doasciimag true def /asciimagsize %f def\n", $ascii_mag);
  471.     } else {
  472.     printf("/doasciimag false def\n");
  473.     }
  474.     &print_template;
  475.     print "%%EndProlog\n\n";
  476.  
  477.     if ($copies_number > 1) {
  478.     printf("/#copies %d def\n", $copies_number);
  479.     }
  480.  
  481.     printf("/docsave save def\n");
  482.     printf("startdoc\n");
  483. }
  484.  
  485. sub date {
  486.     local($_, $time) = @_;
  487.     local($sec, $min, $hour, $mday, $mon, $year, $wday)    = localtime($time);
  488.  
  489.     s/[\\%]%/\377/g;                # save escaped %
  490.     s/%default/$default_sublabel/g;        # %default
  491.  
  492.     s/%user/$ENV{'USER'}||(getpwuid($<))[0]/ge;    # %user
  493.  
  494.     # compatible with mh_format(5)
  495.     s/%month\b/$mon[$mon]/g;            # %month
  496.     s/%sec\b/sprintf("%02d",$sec)/ge;        # %sec
  497.     s/%min\b/sprintf("%02d",$min)/ge;        # %min
  498.     s/%hour\b/$hour/g;                # %hour
  499.     s/%mday\b/$mday/g;                # %mday
  500.     s/%mon\b/$mon+1/ge;                # %mon
  501.     s/%wday\b/$wday/g;                # %wday
  502.     s/%year\b/$year/g;                # %year
  503.     s/%day\b/$day[$wday]/g;            # %day
  504.  
  505.     s/\377/%/g;                    # restore %
  506.     $_;
  507. }
  508.  
  509. sub print_template {
  510.     while(<DATA>) {
  511.     last if /^__END__$/;
  512.     print;
  513.     }
  514. }
  515. __END__
  516. %!  PostScript Source Code
  517. %
  518. %  File: imag:/users/local/a2ps/header.ps
  519. %  Created: Tue Nov 29 12:14:02 1988 by miguel@imag (Miguel Santana)
  520. %  Version: 2.0
  521. %  Description: PostScript prolog for a2ps ascii to PostScript program.
  522. %  Edit History:
  523. %  - Original version by evan@csli (Evan Kirshenbaum).
  524. %  - Modified by miguel@imag to:
  525. %    1) Correct an overflow bug when printing page number 10 (operator
  526. %    cvs).
  527. %    2) Define two other variables (sheetwidth, sheetheight) describing
  528. %    the physical page (by default A4 format).
  529. %    3) Minor changes (reorganization, comments, etc).
  530. %  - Modified by tullemans@apolloway.prl.philips.nl
  531. %    1) Correct stack overflows with regard to operators cvs and copy.
  532. %       The resulting substrings where in some cases not popped off 
  533. %       the stack, what can result in a stack overflow.
  534. %    2) Replaced copypage and erasepage by showpage. Page througput
  535. %       degrades severely (see red book page 140) on our ps-printer
  536. %       after printing sheet 16 (i.e. page 8) of a file which was 
  537. %       actually bigger. For this purpose the definitions of startdoc
  538. %       and startpage are changed.
  539. %  - Modified by Tim Clark <T.Clark@uk.ac.warwick> to:
  540. %    1) Print one page per sheet (portrait) as an option.
  541. %    2) Reduce size of file name heading, if it's too big.
  542. %    3) Save and restore PostScript state at begining/end. It now uses
  543. %    conventional %%Page %%Trailer markers.
  544. %    4) Print one wide page per sheet in landscape mode as an option.
  545. %  - Modified by miguel@imag.fr to
  546. %    1) Add new option to print n copies of a file.
  547. %    2) Add new option to suppress heading printing.
  548. %    3) Add new option to suppress page surrounding border printing.
  549. %    4) Add new option to change font size. Number of lines and columns
  550. %    are now automatically adjusted, depending on font size and
  551. %    printing mode used.
  552. %    5) Minor changes (best layout, usage message, etc).
  553. %  - Modified by kanazawa@sra.co.jp to:
  554. %    1) Handle Japanese code
  555. %  - Modified by utashiro@sra.co.jp to:
  556. %    1) Fix bug in printing long label
  557. %    2) Handle caridge-return
  558. %    3) Specify kanji-ascii character retio
  559. %    4) Add footer label
  560. %    5) Change filename->fname becuase ghostscript has operator filename
  561. %    6) Support three different font style
  562. %
  563.  
  564. % Copyright (c) 1988, Miguel Santana, miguel@imag.imag.fr
  565. %
  566. % Permission is granted to copy and distribute this file in modified
  567. % or unmodified form, for noncommercial use, provided (a) this copyright
  568. % notice is preserved, (b) no attempt is made to restrict redistribution
  569. % of this file, and (c) this file is not distributed as part of any
  570. % collection whose redistribution is restricted by a compilation copyright.
  571. %
  572.  
  573.  
  574. % General macros.
  575. /xdef {exch def} bind def
  576. /getfont {exch findfont exch scalefont} bind def
  577.  
  578. % Page description variables and inch function are defined by a2ps program.
  579.  
  580. % Character size for differents fonts.
  581.    landscape
  582.    { /filenamefontsize 12 def }
  583.    { /filenamefontsize 16 def }
  584. ifelse
  585. /datefontsize filenamefontsize 0.8 mul def
  586. /headermargin filenamefontsize 0.25 mul def
  587. /bodymargin bodyfontsize 0.7 mul def
  588.  
  589. % Font assignment to differents kinds of "objects"
  590. /filenamefontname /Helvetica-Bold def
  591. /stdfilenamefont filenamefontname filenamefontsize getfont def
  592. /datefont /Helvetica datefontsize getfont def
  593. /footerfont /Helvetica-Bold datefontsize getfont def
  594. /mag { doasciimag { [ 1 0 0 asciimagsize 0 0 ] makefont } if } def
  595. /bodynfont /Courier bodyfontsize getfont mag def
  596. /bodybfont /Courier-Bold bodyfontsize getfont mag def
  597. %/bodyofont /Courier-Oblique bodyfontsize getfont mag def
  598. /bodyofont /Courier-BoldOblique bodyfontsize getfont mag def
  599. /fontarray [ bodynfont bodybfont bodyofont ] def
  600. /bodyfont bodynfont def
  601. /R { /fonttype 0 def } bind def R
  602. /B { /fonttype 1 def } bind def
  603. /I { /fonttype 2 def } bind def
  604.  
  605. % Initializing kanji fonts
  606. /kanji_initialized false def
  607. /initkanji {
  608.    kanji_initialized not
  609.    {
  610.       /bodykfontsize bodyfontsize kanjiAsciiRatio mul def
  611.       /bodyknfont /Ryumin-Light-H bodykfontsize getfont def
  612.       /bodykbfont /GothicBBB-Medium-H bodykfontsize getfont def
  613.       /bodykofont bodyknfont [ 1 0 .2 1 0 0 ] makefont def
  614.       /KanjiRomanDiff 1.2 bodyfontsize mul 1.0 bodykfontsize mul sub def
  615.       /KanjiRomanDiffHalf KanjiRomanDiff 2 div def
  616.       /kfontarray [ bodyknfont bodykbfont bodykofont ] def
  617.       /kanji_initialized true def
  618.    } if
  619. } def
  620.  
  621. % Backspace width
  622. /backspacewidth
  623.    bodyfont setfont (0) stringwidth pop
  624.    def
  625.  
  626. % Logical page attributs (a half of a real page or sheet).
  627. /pagewidth
  628.    bodyfont setfont (0) stringwidth pop columns mul bodymargin dup add add
  629.    def
  630. /pageheight
  631.    bodyfontsize 1.1 mul lines mul bodymargin dup add add headersize add
  632.    def
  633.  
  634. % Coordinates for upper corner of a logical page and for sheet number.
  635. % Coordinates depend on format mode used.
  636. % In twinpage mode, coordinate x of upper corner is not the same for left
  637. % and right pages: upperx is an array of two elements, indexed by sheetside.
  638. /rightmargin smargin 3 div def
  639. /leftmargin smargin 2 mul 3 div def
  640. /topmargin lmargin twinpage {3} {2} ifelse div def
  641. landscape
  642. {  % Landscape format
  643.    /uppery rightmargin pageheight add bodymargin add def
  644.    /sheetnumbery sheetwidth leftmargin pageheight add datefontsize add sub def
  645.    twinpage
  646.    {  % Two logical pages
  647.       /upperx [ topmargin 2 mul            % upperx for left page
  648.         dup topmargin add pagewidth add    % upperx for right page
  649.           ] def
  650.       /sheetnumberx sheetheight topmargin 2 mul sub def
  651.    }
  652.    {  /upperx [ topmargin dup ] def
  653.       /sheetnumberx sheetheight topmargin sub datefontsize sub def
  654.    }
  655.    ifelse
  656. }
  657. {  % Portrait format
  658.    /uppery topmargin pageheight add def
  659.    /upperx [ leftmargin dup ] def
  660.    /sheetnumberx sheetwidth rightmargin sub datefontsize sub def
  661.    /sheetnumbery
  662.      sheetheight 
  663.      topmargin pageheight add datefontsize add headermargin add
  664.       sub
  665.       def
  666.  
  667. }
  668. ifelse
  669.  
  670. % Strings used to make easy printing numbers
  671. /pnum 12 string def
  672. /empty 12 string def
  673.  
  674. % Other initializations.
  675. datefont setfont
  676. /datewidth date stringwidth pop def
  677. /pagenumwidth (Page 999) stringwidth pop def
  678. /filenameroom
  679.          pagewidth
  680.      filenamefontsize 4 mul datewidth add pagenumwidth add
  681.       sub
  682.    def
  683.  
  684.  
  685. % Function startdoc: initializes printer and global variables.
  686. /startdoc
  687.     { /sheetside 0 def            % sheet side that contains current page
  688.       /sheet 1 def            % sheet number
  689.    } bind def
  690.  
  691. % Function newfile: init file name and reset page number for each new file.
  692. /newfile
  693.     { cleanup
  694.       /fname xdef
  695.       stdfilenamefont setfont
  696.       /filenamewidth fname stringwidth pop def
  697.       /filenamefont
  698.      filenamewidth filenameroom gt
  699.      {
  700.            filenamefontname
  701.            filenamefontsize filenameroom mul filenamewidth div
  702.         getfont
  703.      }
  704.      {  stdfilenamefont }
  705.      ifelse
  706.      def
  707.       /pagenum 1 def
  708.     } bind def
  709.  
  710. % Function printpage: Print a physical page.
  711. /printpage
  712.     { /sheetside 0 def
  713.       twinpage
  714.       {  noborder not
  715.         { sheetnumber }
  716.      if
  717.       }
  718.       {  noheader noborder not and
  719.         { sheetnumber }
  720.      if
  721.       }
  722.       ifelse
  723.       showpage 
  724. %      pagesave restore
  725.       /sheet sheet 1 add def
  726.     } bind def
  727.  
  728. % Function cleanup: terminates printing, flushing last page if necessary.
  729. /cleanup
  730.     { twinpage sheetside 1 eq and
  731.          { printpage }
  732.       if
  733.     } bind def
  734.  
  735. %
  736. % Function startpage: prints page header and page border and initializes
  737. % printing of the file lines.
  738. /startpage
  739.     { sheetside 0 eq
  740.     { % /pagesave save def
  741.       landscape
  742.         { sheetwidth 0 inch translate    % new coordinates system origin
  743.           90 rotate                % landscape format
  744.           paper_adjust neg 0 translate
  745.         } if
  746.     } if
  747.       noborder not { printborder } if
  748.       noheader not { printheader } if
  749.       nofooter not { printfooter } if
  750.      upperx sheetside get  bodymargin  add
  751.         uppery
  752.         bodymargin bodyfontsize add  noheader {0} {headersize} ifelse  add
  753.      sub
  754.       moveto
  755.     } bind def
  756.  
  757. % Function printheader: prints page header.
  758. /printheader
  759.     { upperx sheetside get  uppery headersize sub 1 add  moveto
  760.       datefont setfont
  761.       gsave
  762.         datefontsize headermargin rmoveto
  763.     date show                    % date/hour
  764.       grestore
  765.       gsave
  766.     pagenum pnum cvs pop
  767.        pagewidth pagenumwidth sub
  768.        headermargin
  769.     rmoveto
  770.         (Page ) show pnum show                % page number
  771.       grestore
  772.       empty pnum copy pop
  773.       gsave
  774.         filenamefont setfont
  775.           filenameroom fname stringwidth pop sub 2 div datewidth add
  776.           filenamefontsize 2 mul 
  777.        add 
  778.        headermargin
  779.     rmoveto
  780.         fname show                        % file name
  781.       grestore
  782.     } bind def
  783.  
  784. % Function printfooter: prints page footer.
  785. /printfooter
  786.     { upperx 0 get sheetnumbery moveto
  787.       footerfont setfont
  788.       fname show
  789.     } bind def
  790.  
  791. % Function printborder: prints border page.
  792. /printborder 
  793.     { upperx sheetside get uppery moveto
  794.       gsave                    % print the four sides
  795.         pagewidth 0 rlineto            % of the square
  796.         0 pageheight neg rlineto
  797.         pagewidth neg 0 rlineto
  798.         closepath stroke
  799.       grestore
  800.       noheader not
  801.          { 0 headersize neg rmoveto pagewidth 0 rlineto stroke }
  802.       if
  803.     } bind def
  804.  
  805. %
  806. % Function endpage: adds a sheet number to the page (footnote) and prints
  807. % the formatted page (physical impression). Activated at the end of each
  808. % source page (lines reached or FF character).
  809. /endpage
  810.    { /pagenum pagenum 1 add def
  811.      twinpage  sheetside 0 eq  and
  812.         { /sheetside 1 def }
  813.         { printpage }
  814.      ifelse
  815.    } bind def
  816.  
  817. % Function sheetnumber: prints the sheet number.
  818. /sheetnumber
  819.     { sheetnumberx sheetnumbery moveto
  820.       datefont setfont
  821.       sheet pnum cvs
  822.      dup stringwidth pop (0) stringwidth pop sub neg 0 rmoveto show
  823.       empty pnum copy pop
  824.     } bind def
  825.  
  826. % Function bs: go back one character width to emulate BS
  827. /bs { backspacewidth neg 0 rmoveto } bind def
  828.  
  829. % Function s: print a source string
  830. /s  { fontarray fonttype get setfont
  831.       show
  832.     } bind def
  833.  
  834. % Function ks: print a kanji source string
  835. /ks { kfontarray fonttype get setfont
  836.       KanjiRomanDiffHalf 0 rmoveto
  837.       KanjiRomanDiff 0 3 -1 roll ashow
  838.       KanjiRomanDiffHalf neg 0 rmoveto
  839.     } def
  840.  
  841. % Function bl: beginning of line
  842. /bl { gsave } bind def
  843.  
  844. % Function nl: newline
  845. /nl {
  846.       grestore
  847.       0 bodyfontsize 1.1 mul neg rmoveto
  848.     } bind def
  849.  
  850. % Function cr: caridge return
  851. /cr { grestore } bind def
  852. __END__
  853.