home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / pstext.zoo / pstext / pstext.c < prev    next >
C/C++ Source or Header  |  1990-01-01  |  11KB  |  362 lines

  1. /* 
  2.   pstext
  3.   by Dan Judd
  4.      St. Olaf College
  5.      11/29/89
  6.   
  7.   This program takes plain ascii text and converts it to Postscript.
  8.   It takes a few options.
  9.   [-p ] prints portrait mode 1 page (default)
  10.   [-l ] prints landscape mode 1 page
  11.   [-ld ] dual landscape mode 2 pages per page
  12.   [-d ] dual portrait mode, prints 2 pages per page
  13.   [-n number] number of lines per page
  14.   [-t tabstring ] use a different tabstring size (NNNNNNNN default)
  15.   [-f fontname ] use a different font (Courier default)
  16.   [-s pointsize ] use a different point size (12 default)
  17.  
  18.   It handles tabs and backspaces in an intelligent way in any font.
  19.  
  20.   Copyright Dan Judd 1989
  21.   This program is freely redistributable, but may not be sold.
  22.   send bug fixes to danjudd@thor.acc.stolaf.edu
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <strings.h>
  27. #include <ctype.h>
  28. #define TRUE 1
  29. #define FALSE 0
  30. #define DFLT_PTSIZE 12
  31. #define DFLT_FONT "Courier"
  32. #define DFLT_TABSTRING "NNNNNNNN"
  33. #define DFLT_STYLE 1
  34. #define DFLT_LINES 0
  35.  
  36. main(argc,argv)
  37. int argc;
  38. char *argv[];
  39. {
  40.  double ptsize=DFLT_PTSIZE;
  41.  char *font;
  42.  char *tabstring;
  43.  int style=DFLT_STYLE;
  44.  int numlines=DFLT_LINES;
  45.  FILE *fpout=stdout;
  46.  FILE *fpin=stdin;
  47.  short badflag=FALSE;
  48.  char ch;
  49.  char *progname;
  50.  double atof();
  51.  
  52.  font=(char *) malloc(sizeof(DFLT_FONT));
  53.  strcpy(font,DFLT_FONT);
  54.  tabstring=(char *) malloc(sizeof(DFLT_TABSTRING));
  55.  strcpy(tabstring,DFLT_TABSTRING);
  56.  /* parse args */
  57.  progname=*argv++;
  58.  
  59.  while ((--argc > 0)&&(*argv[0]=='-')) {
  60.    switch(*++argv[0]) {
  61.     
  62.     case 'l':    if(*++argv[0]=='d') {
  63.           style=3;
  64.           }
  65.          else {
  66.           style=4;
  67.           };
  68.         break;
  69.     case 'p':    style=1;
  70.         break;
  71.     case 'd':    style=2;
  72.         break;
  73.     case 'f':    if (*++argv[0] !='\0'){
  74.           font=*argv;
  75.          }
  76.         else
  77.          if (--argc) {
  78.           font=*++argv;
  79.           }
  80.          else {
  81.           badflag=TRUE;
  82.           }
  83.         break;
  84.     case 't':    if (*++argv[0] !='\0'){
  85.           tabstring=*argv;
  86.          }
  87.         else
  88.          if (--argc) {
  89.           tabstring=*++argv;
  90.           }
  91.          else {
  92.           badflag=TRUE;
  93.           }
  94.         break;
  95.     case 'n':    if (*++argv[0] !='\0'){
  96.           if((numlines=atof(*argv)) <= 0){
  97.             fprintf(stderr,"specify a line number 1 or more\n");
  98.             badflag=TRUE;
  99.             }
  100.         }
  101.         else
  102.         if (--argc) {
  103.           if((numlines=atof(*++argv)) <= 0){
  104.             fprintf(stderr,"specify a line number 1 or more\n");
  105.             badflag=TRUE;
  106.             }
  107.           }
  108.          else {
  109.           badflag=TRUE;
  110.           }
  111.         break;
  112.     case 's':    if (*++argv[0] !='\0'){
  113.           if((ptsize=atof(*argv)) <= 0){
  114.             fprintf(stderr,"specify a point size greater than 0\n");
  115.             badflag=TRUE;
  116.             }
  117.         }
  118.         else
  119.         if (--argc) {
  120.           if((ptsize=atof(*++argv)) <= 0){
  121.             fprintf(stderr,"specify a point size greater than 0\n");
  122.             badflag=TRUE;
  123.             }
  124.           }
  125.          else {
  126.           badflag=TRUE;
  127.           }
  128.         break;
  129.     default:    badflag=TRUE;
  130.         break;
  131.  
  132.     }
  133. if(badflag) {
  134.  fprintf(stderr,"Usage: %s [-p] [-l] [-ld] [-d] [-n lines ] [-t tabstring]");
  135.  fprintf(stderr,"[-f fontname] [-s pointsize]\n",progname);
  136.  exit(1);
  137.   }
  138.     *argv++;
  139.     }
  140.  
  141.  /* set up postscript header */
  142.  
  143.  printhead(fpout,font,ptsize,style,numlines,tabstring);
  144.  /* read files */
  145.  if (argc ==0) {
  146.    getfile(stdin,fpout);
  147.    }
  148.   else {
  149.    while(argc--!=0) {
  150.     if((fpin=fopen(*argv,"r"))!=NULL) {
  151.       getfile(fpin,fpout);
  152.       fclose(fpin);
  153.       *argv++;
  154.       }
  155.      else{
  156.       fprintf(stderr,"Unable to open file %s\n",*argv);
  157.       fprintf(fpout,"\nlp\n");
  158.       exit(1);
  159.       }
  160.     }
  161.   }
  162.  /* makesure showpage is done so last page prints */
  163.  fprintf(fpout,"\nlp\n");
  164.  exit(0);
  165.  }
  166.  
  167.  /*
  168.    getfile(fpin,fpout)
  169.     FILE *fpin,fpout;
  170.    Read in text from a file and output appropriate postscript
  171.   */
  172.  getfile(fpin,fpout)
  173.   FILE *fpin,*fpout;
  174.   {
  175.    char ch;
  176.  
  177.    fprintf(fpout,"(");
  178.    while ((ch=getc(fpin))!=EOF) {
  179.    if(isprint(ch)) {
  180.     switch (ch ) {
  181.      case '(':
  182.      case ')':
  183.      case '\\':
  184.         fprintf(fpout,"\\%c",ch);
  185.         break;
  186.      default:    fprintf(fpout,"%c",ch);
  187.         break;
  188.      }
  189.    }
  190.   else {
  191.     switch (ch ) {
  192.      case '\n':    fprintf(fpout,")s\n(");
  193.         break;
  194.      case '\t':    fprintf(fpout,")S\nht (");
  195.         break;
  196.      case '\b':    fprintf(fpout,")S\nbs(");
  197.         break;
  198.      case '\r':    fprintf(fpout,")S\ncr(");
  199.         break;
  200.      case '\f':    fprintf(fpout,")S\nnp(");
  201.         break;
  202.      default:
  203.         break;
  204.         }
  205.       }
  206.    }
  207.  /* send trailer */
  208.  fprintf(fpout,")s\nnp\n");
  209.  }
  210. printhead(fp,font,size,style,numlines,tabstring)
  211.  FILE *fp;
  212.  char *font;
  213.  double size;
  214.  int style;
  215.  int numlines;
  216.  char *tabstring;
  217.  {
  218.   fprintf(fp,"%%! PS - Adobe\n");
  219.   fprintf(fp,"%% Created by pstext by Dan Judd\n");
  220.   fprintf(fp,"/PAGE_STYLE %d def\n",style);
  221.   fprintf(fp,"/NUM_LINES %d def\n",numlines);
  222.   fprintf(fp,"/FONT (%s) cvn def\n",font);
  223.   fprintf(fp,"/FONT_SIZE %lf def\n",size);
  224.   fprintf(fp,"%%check if font exists\n");
  225.  
  226. /* this is vaguely device depenent, FontDirectory is standard, but */
  227. /* the Next uses SharedFontDirectory instead. The program won't crash,*/
  228. /* but the only font you can use is Courier. Blech. */
  229. /* if this is a problem just comment out the next line */
  230.  
  231.   fprintf(fp,"FontDirectory FONT known not { /FONT (Courier) cvn def} if\n");
  232.   fprintf(fp,"%%misc hardware defs\n");
  233.   fprintf(fp,"/XLMARGIN 18 def\n");
  234.   fprintf(fp,"/XRMARGIN 14 def\n");
  235.   fprintf(fp,"/XPAGE 612 def\n");
  236.   fprintf(fp,"/YPAGE 792 def\n");
  237.   fprintf(fp,"/YTMARGIN 8 def\n");
  238.   fprintf(fp,"/YBMARGIN 13 def\n");
  239.   fprintf(fp,"/PAGENUM 1 def\n");
  240.   fprintf(fp,"%%\n");
  241.   fprintf(fp,"%%short defs to save space\n");
  242.   fprintf(fp,"%%\n");
  243.   fprintf(fp,"/bd {bind def} bind def\n");
  244.   fprintf(fp,"/m {moveto} bd\n");
  245.   fprintf(fp,"/l {lineto}bd\n");
  246.   fprintf(fp,"/gs {gsave}bd\n");
  247.   fprintf(fp,"/gr {grestore}bd\n");
  248.   fprintf(fp,"/tr {translate} bd\n");
  249.   fprintf(fp,"/rt {rotate} bd\n");
  250.   fprintf(fp,"%%\n");
  251.   fprintf(fp,"%%Set up vars and np depending on PAGE_STYLE\n");
  252.   fprintf(fp,"%%\n");
  253.   fprintf(fp,"%% Style 1\n");
  254.   fprintf(fp,"1 PAGE_STYLE eq {\n");
  255.   fprintf(fp,"    /PAGES_PER_SHEET 1 def\n");
  256.   fprintf(fp,"    /sp {} def\n");
  257.   fprintf(fp,"    /np { showpage bp    } bd\n");
  258.   fprintf(fp,"    } if\n");
  259.   fprintf(fp,"%% Style 2\n");
  260.   fprintf(fp,"2 PAGE_STYLE eq {\n");
  261.   fprintf(fp,"    /PAGES_PER_SHEET 2 def\n");
  262.   fprintf(fp,"    /sp {gs XPAGE 2 div dup 0 m YPAGE l stroke \n");
  263.   fprintf(fp,"        newpath 0 0 m 0 YPAGE l XPAGE 2 div XRMARGIN sub dup\n");
  264.   fprintf(fp,"        YPAGE l 0 l 0 0 l clip\n");
  265.   fprintf(fp,"        } bd\n");
  266.   fprintf(fp,"    /np { /PAGENUM 1 PAGENUM add def\n");
  267.   fprintf(fp,"        PAGENUM PAGES_PER_SHEET\n");
  268.   fprintf(fp,"        gt {gr showpage sp /PAGENUM 1 def } \n");
  269.   fprintf(fp,"        {gr gs XPAGE 2 div 0 tr} ifelse\n");
  270.   fprintf(fp,"        bp\n");
  271.   fprintf(fp,"        } bd\n");
  272.   fprintf(fp,"    } if\n");
  273.   fprintf(fp,"%% Style 3\n");
  274.   fprintf(fp,"3 PAGE_STYLE eq {\n");
  275.   /* define if you want double page landscape mode to have fonts */
  276.   /* proportional to single page portrait */
  277. #ifdef LANDPROP
  278.   fprintf(fp,"    /FONT_SIZE FONT_SIZE .642 mul def\n");
  279. #endif
  280.   fprintf(fp,"    /TMP XPAGE def\n");
  281.   fprintf(fp,"    /XPAGE YPAGE def\n");
  282.   fprintf(fp,"    /YPAGE TMP def\n");
  283.   fprintf(fp,"    /TMP XRMARGIN def\n");
  284.   fprintf(fp,"    /XRMARGIN YTMARGIN def\n");
  285.   fprintf(fp,"    /YTMARGIN TMP def\n");
  286.   fprintf(fp,"    /TMP XLMARGIN def\n");
  287.   fprintf(fp,"    /XLMARGIN YBMARGIN def\n");
  288.   fprintf(fp,"    /YBMARGIN TMP def\n");
  289.   fprintf(fp,"    /PAGES_PER_SHEET 2 def\n");
  290.   fprintf(fp,"    /sp {gs 0 XPAGE tr -90 rt\n");
  291.   fprintf(fp,"        XPAGE 2 div dup 0 moveto YPAGE l stroke \n");
  292.   fprintf(fp,"        newpath 0 0 m 0 YPAGE l XPAGE 2 div XRMARGIN sub dup\n");
  293.   fprintf(fp,"        YPAGE l 0 l 0 0 l clip\n");
  294.   fprintf(fp,"        } bd\n");
  295.   fprintf(fp,"    /np { /PAGENUM 1 PAGENUM add def\n");
  296.   fprintf(fp,"        PAGENUM PAGES_PER_SHEET\n");
  297.   fprintf(fp,"        gt {gr showpage sp /PAGENUM 1 def } \n");
  298.   fprintf(fp,"        {gr gs 0 XPAGE 2 div tr -90 rt } ifelse\n");
  299.   fprintf(fp,"        bp\n");
  300.   fprintf(fp,"        } bd\n");
  301.   fprintf(fp,"    } if\n");
  302.   fprintf(fp,"%% Style 4\n");
  303.   fprintf(fp,"4 PAGE_STYLE eq {\n");
  304.   fprintf(fp,"    /TMP XPAGE def\n");
  305.   fprintf(fp,"    /XPAGE YPAGE def\n");
  306.   fprintf(fp,"    /YPAGE TMP def\n");
  307.   fprintf(fp,"    /TMP XRMARGIN def\n");
  308.   fprintf(fp,"    /XRMARGIN YTMARGIN def\n");
  309.   fprintf(fp,"    /YTMARGIN TMP def\n");
  310.   fprintf(fp,"    /TMP XLMARGIN def\n");
  311.   fprintf(fp,"    /XLMARGIN YBMARGIN def\n");
  312.   fprintf(fp,"    /YBMARGIN TMP def\n");
  313.   fprintf(fp,"    /PAGES_PER_SHEET 1 def\n");
  314.   fprintf(fp,"    /sp {gs 0 XPAGE tr -90 rt\n");
  315.   fprintf(fp,"        } bd\n");
  316.   fprintf(fp,"    /np { gr showpage sp bp} bd\n");
  317.   fprintf(fp,"    } if\n");
  318.   fprintf(fp,"%%\n");
  319.   fprintf(fp,"%%set up fonts\n");
  320.   fprintf(fp,"%%\n");
  321.   fprintf(fp,"NUM_LINES 1 ge {FONT findfont 1 scalefont setfont\n");
  322.   fprintf(fp,"    /FONT_HT currentfont /FontBBox get 3 get 0 exch\n");
  323.   fprintf(fp,"        currentfont /FontMatrix get transform exch pop\n");
  324.   fprintf(fp,"        currentfont /FontBBox get 0 get 0 exch\n");
  325.   fprintf(fp,"        currentfont /FontMatrix get transform exch pop\n");
  326.   fprintf(fp,"        sub def\n");
  327.   fprintf(fp,"    /TMP FONT_HT 1 sub def\n");
  328.   fprintf(fp,"    /FONT_SIZE YPAGE YBMARGIN sub YTMARGIN sub 1 sub dup\n");
  329.   fprintf(fp,"    NUM_LINES div TMP mul sub NUM_LINES div def\n");
  330.   fprintf(fp,"    } if\n");
  331.   fprintf(fp,"FONT findfont FONT_SIZE scalefont setfont\n");
  332.   fprintf(fp,"%%get height of font set tolerances\n");
  333.   fprintf(fp,"/FONT_HT currentfont  /FontBBox get 3 get 0 exch\n");
  334.   fprintf(fp,"    currentfont  /FontMatrix get transform exch pop \n");
  335.   fprintf(fp,"    currentfont  /FontBBox  get 0 get 0 exch\n");
  336.   fprintf(fp,"    currentfont  /FontMatrix get transform exch pop \n");
  337.   fprintf(fp,"    sub  def \n");
  338.   fprintf(fp,"/FONT_TOL FONT_HT YBMARGIN add def\n");
  339.   fprintf(fp,"%%\n");
  340.   fprintf(fp,"%%Routines common to all page styles\n");
  341.   fprintf(fp,"%%\n");
  342.   fprintf(fp,"/bp { XLMARGIN YPAGE YTMARGIN sub FONT_SIZE sub m\n");
  343.   fprintf(fp,"    } bd\n");
  344.   fprintf(fp,"/s { show  currentpoint exch pop dup FONT_TOL \n");
  345.   fprintf(fp,"    gt {FONT_SIZE sub XLMARGIN  exch m} {np} ifelse}  bd\n");
  346.   fprintf(fp,"/S {show} bd\n");
  347.   fprintf(fp,"/OFFSET (_) stringwidth pop neg def\n");
  348.   fprintf(fp,"/bs { OFFSET 0 rmoveto } bd\n");
  349.   fprintf(fp,"/bs {XLMARGIN currentpoint pop OFFSET add lt {OFFSET 0 rmoveto}");
  350.   fprintf(fp,"{XLMARGIN currentpoint exch pop m}ifelse} bd");
  351.   fprintf(fp,"/TABLEN (%s) stringwidth pop def\n",tabstring);
  352.   fprintf(fp,"/ht {currentpoint exch XLMARGIN sub TABLEN div cvi 1 add TABLEN \n");
  353.   fprintf(fp,"    mul XLMARGIN add exch m}bd\n");
  354.   fprintf(fp,"/cr {currentpoint XLMARGIN exch m pop} bd\n");
  355.   fprintf(fp,"/lp {gr 1 PAGENUM ne {showpage}if} bd\n");
  356.   fprintf(fp,"%%\n");
  357.   fprintf(fp,"%%begin data et al\n");
  358.   fprintf(fp,"%%\n");
  359.   fprintf(fp,"sp\n");
  360.   fprintf(fp,"bp\n");
  361.  }
  362.