home *** CD-ROM | disk | FTP | other *** search
/ The CIA World Factbook 1992 / k3bimage.iso / sel / 11 / 0002 / cursive.c < prev    next >
Encoding:
Text File  |  1991-12-02  |  9.0 KB  |  297 lines

  1. Scan the string for spaces or signs */
  2.         for( ; ;p++ ){
  3.                 if( *p == SP || *p == TAB || *p == PLUS )
  4.                         continue;
  5.                 if( *p == MINUS ){
  6.                         f = -f;
  7.                         continue;
  8.                 }
  9.                 break;
  10.         }
  11.  
  12.         /* Now assemble the number */
  13.         while( *p >= LEASTN && *p <= MOSTN )
  14.                 n = n*10 + *p++ - LEASTN;
  15.         if( f < 0 )
  16.                 n = -n;
  17.  
  18.         return( n uments are recognized:
  19.  *
  20.  *        -in   Sets the amount of space to insert between letters.
  21.  *          The default is "-i1".  "-i0" gives interesting
  22.  *          results.
  23.  *        -tn   Sets the length of the trailing line off the end
  24.  *          of each word.  The default is "-t1".
  25.  *
  26.  *      One character in the text is treated in a special way:
  27.  *
  28.  *        '_'   Can be inserted in text to slightly lengthen the
  29.  *          the spacing between two letters, or add a longer
  30.  *          tail to the end of a word.
  31.  *
  32.  *  Internals:  Unfortunately, the program is a kludge and the font is
  33.  *      somewhat difficult to edit.  It should be easy to port
  34.  *      though.  Systems with short integers or unsigned characters
  35.  *      should be no problem.  You should probably run "xstr" on the
  36.  *      font.c file, but if you haven't got "xstr", just compiling it
  37.  *      the usual way works fine.
  38.  *
  39.  *  Copyright:  Both the cursive program and the font it generates are
  40.  *      copyrighted by yours truly.  However, both may be used
  41.  *      and distributed freely.  You even have my permission to
  42.  *      sell it, or include in it a system that you sell.  I only
  43.  *      ask that my name be retained on this program, or any direct
  44.  *      decendents of this program with approximately the same
  45.  *      visibility as in this posting.
  46.  *
  47.  *  Mail:   I'm interested in any comments you have on this program.
  48.  *      I can be mailed at "janc@crim.eecs.umich.edu".  Better yet,
  49.  *      if you are into teleconferencing, call M-Net at (313) 994-6333
  50.  *      and type "newuser" at the "login:" prompt to give yourself an
  51.  *      account.  Send mail to janc, or join the "ugames" conference.
  52.  *
  53.  *      Have fun,
  54.  *                ___                _     __  _
  55.  *               (   >              ' )   /   // _/_
  56.  *                __/___.  ____      / / / __|/  /  _  __
  57.  *               / / (_/|_/ / <__   (_(_/ (_) \_<__</_/ (__
  58.  *              <_/
  59.  */
  60.  
  61. #include "a:stdio.h"
  62. #include "cursive.h"
  63.  
  64. char *buffer[6];    /* memory buffers to build up line in */
  65. int c[6];       /* current index in each of the buffer lines */
  66. int tail[6];        /* which buffer lines have tails in them */
  67. int lasttail;       /* which line was the last letter's tail */
  68. int space;      /* how much white space before the next letter */
  69. int interspace = 1; /* how much to spread adjacent letters out */
  70. int taillen = 1;    /* how long the tails on ends of words should be */
  71. char firstletter;   /* is this the first letter on the line? */
  72. char message[256] = ""; /* message to print */
  73.  
  74. char *malloc();
  75.  
  76. /* These are used by atoi */
  77.  
  78. #define TAB             '\t'
  79. #define SP              ' '
  80. #define PLUS            '+'
  81. #define MINUS           '-'     /* Redundant, but clearer */
  82. #define LEASTN          '0'
  83. #define MOSTN           '9'
  84.  
  85.  
  86. main(argc,argv)
  87. int argc;
  88. char **argv;
  89. {
  90. int i;
  91. char *s,*m;
  92. char ch;
  93.  
  94.     m = message;
  95.     for (i = 1; i < argc; i++)
  96.     {
  97.         if (*argv[i] == '-')
  98.             switch(ch = argv[i][1])
  99.             {
  100.             case 'i':
  101.             case 't':
  102.                 s = argv[i]+2;
  103.                 if (*s == '\000')
  104.                     if (++i < argc)
  105.                         s = argv[i];
  106.                 if (*s < '0' || *s > '9')
  107.                 {
  108.                     printf("%s: Illegal value %s\n",
  109.                         argv[0],s);
  110.                     exit(1);
  111.                 }
  112.                 if (ch == 'i')
  113.                     interspace = atoi(s);
  114.                 else
  115.                     taillen = atoi(s);
  116.                 break;
  117.             default:
  118.                 printf("usage: %s [-tn] [-in] message\n",
  119.                     argv[0]);
  120.                 exit(1);
  121.             }
  122.         else
  123.         {
  124.             if (m != message)
  125.                 *(m++) = ' ';
  126.             for (s=argv[i]; *s != '\000'; s++)
  127.                 *(m++) = *s;
  128.         }
  129.     }
  130.     /* Do the deed */
  131.     if (m != message)
  132.     {
  133.         /* Message from the arg list */
  134.         *(m++) = 0;
  135.         prline(message);
  136.     }
  137.     else
  138.     {
  139.         /* Message from standard input */
  140.         while (gets(message) != NULL)
  141.             prline(message);
  142.     }
  143. }
  144.  
  145.  
  146. /* Add the given letter to the end of the current line */
  147.  
  148. place(let)
  149. struct letter *let;
  150. {
  151. int i,j,n;
  152. int col;
  153. int maxx= -10000;
  154. char pad;
  155. char *l;
  156.  
  157.     if (firstletter)
  158.     {
  159.         col = space;        /* Leading spaces */
  160.         firstletter = 0;
  161.     }
  162.     else
  163.     {
  164.         /* Find the furthest left position we can place this letter */
  165.         for(i=0; i<6; i++)
  166.         {
  167.             if (let->line[i][0] != '\000' &&
  168.                 (col = c[i] - let->spcs[i]) > maxx)
  169.                 maxx = col;
  170.         }
  171.  
  172.         /* Insert some spaces between letters */
  173.         col = maxx + space + interspace;
  174.     }
  175.  
  176.     for(i=0; i<6; i++)
  177.  
  178.         /* If Nothing on this Line, Skip It */
  179.         if (let->line[i][0] != '\000')
  180.         {
  181.             /* Number of Spaces to Put on this Line? */
  182.             n = col - c[i] + let->spcs[i];
  183.  
  184.             /* Flyers off Ends of Letters */
  185.             if (tail[i])
  186.                 for (j = 0;
  187.                      j < 5 && n > space + 2; j++,n--)
  188.                     buffer[i][c[i]++] = '_';
  189.  
  190.             /* Connecting Lines Between Letters */
  191.             pad = (lasttail == i && let->tailin == i) ? '_' : ' ';
  192.  
  193.             /* In the middle of the intersection of South and */
  194.             /* East University, someone is playing the piano. */
  195.             for ( ; n > 0; n--)
  196.                 buffer[i][c[i]++] = pad;
  197.  
  198.             /* Copy in the letter text */
  199.             for (l = let->line[i]; *l != '\000'; l++)
  200.                 buffer[i][c[i]++] = *l;
  201.  
  202.             tail[i] = (i == let->tailout);
  203.         }
  204.  
  205.     lasttail = let->tailout;
  206.     space = 0;
  207. }
  208.  
  209. /* Lengthen the last trailer by n */
  210. tailer(n)
  211. int n;
  212. {
  213. int j;
  214.     if (lasttail >= 0)
  215.         for (j = 0; j < n; j++)
  216.             buffer[lasttail][c[lasttail]++] = '_';
  217. }
  218.  
  219. /* Handle a line */
  220. prline(s)
  221. char *s;
  222. {
  223. int l,i;
  224. char *ch;
  225. short lcode;
  226.  
  227.     lasttail = -1;
  228.     firstletter = 1;
  229.     space = 0;
  230.     /* Get some memory to put the output into */
  231.     l = strlen(s) * (CHARWIDTH + interspace);
  232.     for (i=0;i<6;i++)
  233.     {
  234.         buffer[i] = malloc((unsigned)l);
  235.         tail[i] = c[i] = 0;
  236.     }
  237.  
  238.     /* do each letter */
  239.     for (ch=s; *ch != '\000'; ch++)
  240.     {
  241.  
  242.         *ch &= '\177';
  243.         /* Find the letter */
  244.         lcode = (lasttail != 2) ? code1[*ch] : code2[*ch];
  245.         if (lcode >= 0)
  246.             place(&list[lcode]);
  247.         else
  248.             /* Various Special characters */
  249.             switch(lcode)
  250.             {
  251.             case SP:
  252.                 /* Insert white space before next letter */
  253.                 tailer(taillen);
  254.                 space += 3;
  255.                 lasttail = -1;
  256.                 break;
  257.             case ST:
  258.                 /* Lengthen the last tail */
  259.                 if (lasttail >= 0)
  260.                     buffer[lasttail][c[lasttail]++] = '_';
  261.                 break;
  262.             }
  263.     }
  264.     tailer(taillen);
  265.  
  266.     /* print the line and release the memory */
  267.     for (i=0;i<6;i++)
  268.     {
  269.         buffer[i][c[i]++] = '\n';
  270.         write(1,buffer[i],c[i]);
  271.         free(buffer[i]);
  272.     }
  273. }
  274.  
  275. /*
  276. **      atoi
  277. **
  278. **      Convert an ASCII string to an integer
  279. **
  280. */
  281.  
  282.  
  283. int atoi( ap )
  284. char *ap;
  285. {
  286.         register int n, c;
  287.         register char *p;
  288.         int f;
  289.  
  290.         p = ap;
  291.         n = 0;
  292.         f = 1;
  293.  
  294.         /* 
  295. =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            );
  296. }
  297.  
  298.