home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / sun / apps / 2848 < prev    next >
Encoding:
Text File  |  1992-12-22  |  14.3 KB  |  372 lines

  1. Newsgroups: comp.sys.sun.apps
  2. Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!darwin.sura.net!mlb.semi.harris.com!SU19AZ!kswatek
  3. From: kswatek@su19az.ess.harris.com (Ken Swatek)
  4. Subject: Re: PostScript filter
  5. Followup-To: comp.sys.sun.apps
  6. References: <BILLD.92Dec21161647@gdstech.GRUMMAN.COM>
  7. Date: Tue, 22 Dec 1992 17:11:34 GMT
  8. Nntp-Posting-Host: su19az.ess.harris.com
  9. Reply-To: kswatek@su19az.ess.harris.com
  10. Organization: Harris Corporation, Government Aerospace Systems Division
  11. Sender: news@mlb.semi.harris.com
  12. Message-ID: <1992Dec22.171134.3986@mlb.semi.harris.com>
  13. Lines: 357
  14.  
  15. In article 92Dec21161647@gdstech.GRUMMAN.COM, billd@gdstech.GRUMMAN.COM (Bill D'Camp) writes:
  16. >I want to hang an Apple LaserWriter off of a SPARC 1.  Can someone
  17. >tell me where to find a PostScript filter which will convert output
  18. >for lpr, either public domain (ftpable) or commercial?
  19. >--
  20. >    _   /|
  21. >    \`o_O'
  22. >      ( )    Aachk! Phft!
  23. >       U     billd@gdstech.grumman.com
  24. >"All opinions expressed herein are my own, they do not represent the
  25. >opinions of my employer."
  26. >
  27.  
  28. Try this one.  I also have a version which will change paper trays, if there
  29. is more than one.
  30.  
  31. Ken
  32. ______________________________________________________________________________
  33.  
  34. Ken Swatek                             INTERNET: kswatek@su19az.ess.harris.com
  35. Harris Corporation                     UUCP:     uunet!su19az!kswatek
  36. Government Aerospace Systems Division  CCMail:   harris.kswatek
  37. Mail Stop: 102-4827                    FAX:      407-729-3116
  38. P.O. Box 94000                         Pager:    407-690-8548 (Digital)
  39. Melbourne, Florida 32902               Voice:    407-727-6081
  40. ______________________________________________________________________________
  41.  
  42. /*****************************************************************************/
  43. /*                                                                           */
  44. /*   text2ps.c : this program is an ASCII text-to-postscript filter.         */
  45. /*               Input is any Ascii text file.                               */
  46. /*               Output is a postscript text file, printable on a postscript */
  47. /*                  printer, such as the Apple Laser Writer II.              */
  48. /*                                                                           */
  49. /*               Invoke this program with:                                   */
  50. /*                     "text2ps < [text-file] > [postscript-file]"           */
  51. /*                                                                           */
  52. /*               History:                                                    */
  53. /*               6/3/88   written by: Sharon Mitchell                        */
  54. /*               6/16/88  modified to allow 80 character wrap (necessary     */
  55. /*                        for DOS files) -- skm                              */
  56. /*               7/15/88  modified to recognize all blank lines as valid     */
  57. /*               10/11/91 modified to pass through files that are already PS */
  58. /*               11/19/92 Corrected TAB spacing                              */
  59. /*                                                                           */
  60. /*****************************************************************************/
  61.  
  62. #include "stdio.h"
  63.  
  64. #define bool char
  65. #define TRUE    1
  66. #define FALSE   0
  67. #define MAX_LINES 65  /* This may be changed to adjust text on page */
  68. #define CPL 80        /* Characters per line */
  69.  
  70. int  locate_next_tab();
  71. int  output_line();
  72. int  output_blank_lines();
  73. char line_buff[256];  /* holds up to one line of postscript output */
  74. char *buff_ptr;       /* pointer to the postscript output buffer */
  75. bool blank_line, non_blank_char;
  76. int  blank_line_ctr=0;
  77. int  line_ctr = 0;
  78.  
  79. /*****************************************************************************/
  80. /*                                                                           */
  81. /*    main() : this procedure reads the stream input one character at a      */
  82. /*             time, determining the output to the postcript file (stdout)   */
  83. /*             based upon the value of the input character.                  */
  84. /*                                                                           */
  85. /*****************************************************************************/
  86.  
  87. void main()
  88. {
  89.    char    a,b;
  90.  
  91. /* if the file is already PostScript just pass it straight through           */
  92.    a = getchar();
  93.    b = getchar();
  94.    if (a == '%') 
  95.    {
  96.      if (b =='!') 
  97.      {
  98.        putchar (a);
  99.        putchar (b);
  100.        while ((a = getchar()) != EOF) 
  101.        {
  102.          putchar(a);
  103.        }
  104.        exit(0);
  105.      }
  106.    }
  107. /* generate prologue (postscript file header) */
  108.  
  109.    printf ("%%!PS-Adobe-1.0\n");
  110.    printf ("/StartPage{/sv save def 48 760 moveto}def\n");
  111.    printf ("/ld -11.4 def\n");
  112.    printf ("/S{count{gsave show grestore}repeat 0 ld rmoveto}def\n");
  113.    printf ("/L{ld mul 0 exch rmoveto}def\n");
  114.    printf ("/B{0 ld rmoveto}def\n");
  115.    printf ("/EndPage{showpage sv restore}def\n");
  116.    printf ("/Courier findfont 11 scalefont setfont\n");
  117.    printf ("StartPage\n");
  118.  
  119. /* initialize counters and buffers */
  120.  
  121.    blank_line = FALSE;
  122.    non_blank_char = FALSE;
  123.    buff_ptr = &line_buff[0];
  124.  
  125. /* read the input file, one character at a time, generating appropriate
  126.    control characters in the postscript file */
  127.    
  128.    convert2ps(a);
  129.    convert2ps(b);
  130.    while ((a=getchar()) != EOF) convert2ps(a);
  131.  
  132.    if ((buff_ptr-&line_buff[0]) > 0)
  133.    {  output_line (line_buff,buff_ptr,line_ctr);
  134.       printf (")S\n");
  135.    }
  136.    printf ("EndPage\n");
  137.    exit(0);
  138. }
  139. /*****************************************************************************/
  140. /*  convert2ps :  Test char and generate appropriate control characters      */
  141. /*****************************************************************************/
  142. convert2ps(c)
  143. char c;
  144. {
  145.    int     start_pos,stop_pos;
  146.    int     num_blanks;
  147.  
  148.       switch (c)
  149.       {
  150.  
  151. /* Carriage-control/line-feed character encountered */
  152.  
  153.          case '\n' :  if (!blank_line && non_blank_char)
  154.                       {  line_ctr = output_line (line_buff,buff_ptr,line_ctr);
  155.                          printf (")S\n");
  156.                          blank_line_ctr=0;
  157.                          non_blank_char=FALSE;
  158.                          if (++line_ctr >= MAX_LINES)
  159.                          {  printf ("EndPage\nStartPage\n");
  160.                             line_ctr=0;
  161.                          }
  162.                       }
  163.                       else  {
  164.                          blank_line = TRUE;
  165.                          blank_line_ctr++;
  166.                          }
  167.                       buff_ptr = &line_buff[0];
  168.                       break;
  169.  
  170. /* Form-feed character encountered */
  171.  
  172.          case '\f' :  if (!blank_line && non_blank_char)
  173.                       {  line_ctr = output_line (line_buff,buff_ptr,line_ctr);
  174.                          printf (")S\n");
  175.                          buff_ptr = &line_buff[0];
  176.                          blank_line_ctr=0;
  177.                          non_blank_char=FALSE;
  178.                       }
  179.                       else
  180.                          if (blank_line)
  181.                          {  line_ctr = output_blank_lines(blank_line_ctr,
  182.                                                           line_ctr);
  183.                             blank_line=FALSE;
  184.                          }
  185.                       printf ("EndPage\nStartPage\n");
  186.                       line_ctr=0;
  187.                       break;
  188.  
  189. /* Carriage-return character encountered */
  190.  
  191.          case '\r' :  if (!blank_line && non_blank_char)
  192.                       {  line_ctr = output_line (line_buff,buff_ptr,line_ctr);
  193.                          printf (")\n");
  194.                          buff_ptr = &line_buff[0];
  195.                          blank_line_ctr=0;
  196.                       }
  197.                       else
  198.                          line_ctr=output_blank_lines(blank_line_ctr,line_ctr);
  199.                       break;
  200.  
  201. /*  Blank character */
  202.  
  203.          case ' '  :  *buff_ptr++=c;
  204.                       break;
  205.  
  206. /*  Tab character -- expand tab according to tab-table.  The tab table
  207.     may be changed and this program recompiled if different tab expansion
  208.     desired                                                               */
  209.  
  210.          case '\t' :  start_pos = buff_ptr - &line_buff[0] -1;
  211.                       stop_pos = locate_next_tab(start_pos);
  212.                       for (num_blanks=1; num_blanks<=stop_pos; num_blanks++) {
  213.                           *buff_ptr++=' ';
  214.                           }
  215.                       break;
  216.  
  217. /* Special characters requiring a "\" to be placed in front of them in the
  218.    postscript file                                                          */
  219.  
  220.          case '\\' :
  221.          case '('  :
  222.          case ')'  :  if (blank_line) {
  223.                          line_ctr=output_blank_lines(blank_line_ctr,line_ctr);
  224.                          blank_line=FALSE;
  225.                          }
  226.                       non_blank_char=TRUE;
  227.                       *buff_ptr++='\\';
  228.                       *buff_ptr++=c;
  229.                       break;
  230.  
  231. /*  Any other character */
  232.  
  233.          default   :  if (blank_line) {
  234.                          line_ctr=output_blank_lines(blank_line_ctr,line_ctr);
  235.                          blank_line=FALSE;
  236.                          }
  237.                       non_blank_char=TRUE;
  238.                       *buff_ptr++=c;
  239.  
  240.       }  /*  end case  */
  241. } /* end convert2ps */
  242.  
  243. /*****************************************************************************/
  244. /*                                                                           */
  245. /*   locate_next_tab: this routine uses the tab table "tab[10]" to           */
  246. /*                    determine the number of blanks which need to           */
  247. /*                    be placed into the postscript output buffer.           */
  248. /*                                                                           */
  249. /*          Input:  pos - position of current pointer into postscript        */
  250. /*                        output buffer                                      */
  251. /*                                                                           */
  252. /*          Returns: integer value representing the number of blanks         */
  253. /*                   which need to be placed into the postscript             */
  254. /*                   output file in order to expand the tab to the           */
  255. /*                   correct position.                                       */
  256. /*                                                                           */
  257. /*****************************************************************************/
  258.  
  259. int locate_next_tab(pos)
  260.  
  261. int   pos;
  262. {
  263.    int n=0;
  264.    static int tab[10] = {7,15,23,31,39,47,55,63,71,79};
  265.  
  266.    while (pos >= tab[n++]);
  267.    return (tab[n-1]-pos);
  268. }
  269.  
  270.  
  271. /*****************************************************************************/
  272. /*                                                                           */
  273. /*   output_line: this routine outputs the postscript line buffer to         */
  274. /*                stdout (which is directed to the postscript output         */
  275. /*                file), placing a "(" in the initial line position.         */
  276. /*                                                                           */
  277. /*          Input:  line_buff[] - postscript line buffer                     */
  278. /*                  buff_ptr - pointer into line_buff                        */
  279. /*                                                                           */
  280. /*          Returns: line_ctr - number of lines output on page so far        */
  281. /*                                                                           */
  282. /*****************************************************************************/
  283.  
  284. int output_line(line_buff,buff_ptr,line_ctr)
  285.  
  286. char *buff_ptr;
  287. char line_buff[];
  288. int  line_ctr;
  289.  
  290. {
  291.    bool eol = FALSE;
  292.    char *i;
  293.    int j = 0;  /* character counter for the output line */
  294.  
  295.    i = &line_buff[0];
  296.    while (!eol)  {
  297.  
  298.       putchar('(');
  299.       while ( j++ < CPL && i < buff_ptr )  {
  300.          if (*i == '\\') {
  301.             putchar(*i++);
  302.             putchar(*i++);
  303.             } 
  304.          else
  305.             putchar(*i++);
  306.          }
  307.       if (i >= buff_ptr)  /* end of line has been reached */
  308.          eol = TRUE;
  309.       else  {  /* line has exceeded maximum line length */
  310.          printf (")S\n");
  311.          line_ctr++;
  312.          j=0;
  313.          if (line_ctr > MAX_LINES) {
  314.             printf("EndPage\nStartPage\n");
  315.             line_ctr = 0;
  316.             }  /* end if */
  317.          } /* end else */
  318.  
  319.       } /* end while */
  320.  
  321.    return (line_ctr);
  322. }
  323.  
  324.  
  325. /*****************************************************************************/
  326. /*                                                                           */
  327. /*   output_blank_lines: this routine outputs appropriate postscript         */
  328. /*                notation for blank lines ("B" or "nL", where n             */
  329. /*                indicates 2 or more blank lines.                           */
  330. /*                                                                           */
  331. /*          Input:  blank_line_ctr - number of blank lines encountered       */
  332. /*                                   since the last non-blank line was       */
  333. /*                                   output                                  */
  334. /*                  line_ctr - number of lines output to postscript file     */
  335. /*                             so far                                        */
  336. /*                                                                           */
  337. /*          Returns: new value of line_ctr                                   */
  338. /*                                                                           */
  339. /*****************************************************************************/
  340.  
  341. int output_blank_lines(blank_line_ctr,line_ctr)
  342.  
  343. int blank_line_ctr;
  344. int line_ctr;
  345.  
  346. {  int count;
  347.    int lines_left;
  348.    int lines_on_next_page;
  349.  
  350.    count = line_ctr;
  351.    if (blank_line_ctr + line_ctr >= MAX_LINES) {
  352.       lines_left = MAX_LINES - line_ctr;
  353.       lines_on_next_page = blank_line_ctr - lines_left;
  354.       printf("%d L\n",lines_left);
  355.       printf("EndPage\nStartPage\n");
  356.       printf("%d L\n",lines_on_next_page);
  357.       count = lines_on_next_page;
  358.       }
  359.    else
  360.       if (blank_line_ctr==1) {
  361.          printf ("B\n");
  362.          count++;
  363.          }
  364.       else
  365.          if (blank_line_ctr>1) {
  366.             printf ("%d L\n",blank_line_ctr);
  367.             count += blank_line_ctr;
  368.             }
  369.    return(count);
  370. }
  371.  
  372.