home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / tex / files / !tex / TeXsource / beebe / h / chargf < prev    next >
Encoding:
Text File  |  1990-05-18  |  10.2 KB  |  351 lines

  1. /* -*-C-*- chargf.h */
  2. /*-->chargf*/
  3. /**********************************************************************/
  4. /******************************* chargf *******************************/
  5. /**********************************************************************/
  6.  
  7. #ifdef __STDC__
  8. int
  9. chargf(BYTE c, void (*outfcn)())
  10. {
  11. #else
  12. int
  13. chargf(c,outfcn)    /* return 0 on success, and EOF on failure */
  14. BYTE c;            /* current character value */
  15. void (*outfcn)();    /* (possibly NULL) function to output current row */
  16. {
  17. #endif
  18.     UNSIGN16 d;            /* step in m index */
  19.     BOOLEAN do_output;        /* FALSE if outfcn is NULL */
  20.     register UNSIGN16 k;    /* loop step */
  21.     register INT16 m,n;        /* column,row indices in image[n][m] */
  22.     long p;            /* pointer into font file */
  23.     BYTE paint_switch;        /* alternates between BLACK and WHITE */
  24.     struct char_entry *tcharptr;/* temporary char_entry pointer */
  25.     register BYTE the_byte;    /* current command byte */
  26.     UNSIGN32 the_word;        /* temporary result holder */
  27.  
  28. /* NB: We only test for equality with BLACK.   WHITE is any non-zero
  29. bit pattern, so we cannot test for equality with it */
  30. #define BLACK ((BYTE)0)
  31. #define WHITE ((BYTE)(!BLACK))
  32.  
  33.     /*******************************************************************
  34.     This function is called to process a single character description in
  35.     the GF font file,  and to set the  character metrics hp, wp,  xoffp,
  36.     and yoffp for it.  The character  description may start either at  a
  37.     special (XXX) command, or at a beginning-of-character (BOC or  BOC1)
  38.     command.   It  processes  the  character  description  up  to,   and
  39.     including, the end-of-character (EOC) command which terminates it.
  40.  
  41.     The GF  raster description  is encoded  in a  complex form,  but  is
  42.     guaranteed to step across raster rows  from left to right, and  down
  43.     from the  top  row  to  bottom  row,  such  that  in  references  to
  44.     image[n][m], m never decreases in a row, and n never increases in  a
  45.     character.  This means that we  only require enough memory space  to
  46.     hold one row, provided that outfcn(c,yoff) is called each time a row
  47.     is     completed.    This   is   an    important   economization    for
  48.     high-resolution output  devices --  e.g. a  10pt character  at  2400
  49.     dots/inch would require about  8Kb for the  entire image, but  fewer
  50.     than 25 bytes for a  single row.  A 72pt  character (such as in  the
  51.     aminch font) would need  about 430Kb for the  image, but only  about
  52.     200 bytes for a row.
  53.  
  54.     Access to bit m in the current row is controlled through the  macros
  55.     SETBIT(m) and  TESTBIT(m) so  we  need not  be concerned  about  the
  56.     details of bit masking.  Too bad C does not have a bit data type!
  57.  
  58.     The row image is recorded in such a way that bits min_m .. max_m are
  59.     mapped onto  bits  0  ..  (max_m -  min_m)  in  img_row[],  so  that
  60.     outfcn(c,yoff) should be relieved of any shifting operations.
  61.  
  62.     outfcn(c,yoff) is NEVER called if either of hp or wp is <= 0, or if
  63.     if it is NULL.
  64.     *******************************************************************/
  65.  
  66.     if ((c < FIRSTPXLCHAR) || (LASTPXLCHAR < c))
  67.     {
  68.     (void)warning(
  69.         "chargf():  Character value out of range for GF font file");
  70.     return(EOF);
  71.     }
  72.     tcharptr = &(fontptr->ch[c]);
  73.  
  74.     p = (long)tcharptr->fontrp;        /* font file raster pointer */
  75.     if (p < 0L)
  76.     {
  77.     (void)warning(
  78.         "chargf():  Requested character not found in GF font file");
  79.     return(EOF);
  80.     }
  81.     if (FSEEK(fontfp,p,0))
  82.     {
  83.     (void)warning(
  84.         "chargf():  FSEEK() failure for GF font file character raster");
  85.     return(EOF);
  86.     }
  87.  
  88.     do_output = (BOOLEAN)(outfcn != (void(*)())NULL);
  89.  
  90.     (void)skgfspec();            /* skip any GF special commands */
  91.  
  92.     the_byte = (BYTE)nosignex(fontfp,(BYTE)1);
  93.     if ((the_byte != GFBOC) && (the_byte != GFBOC1))
  94.     {
  95.     (void)warning(
  96.         "chargf():  GF font file not positioned at BOC or BOC1 command");
  97.     return(EOF);
  98.     }
  99.  
  100.     for (;;)    /* loop with exit at EOC, or at BOC or BOC1 if no rasters */
  101.     {
  102.     switch (the_byte)
  103.     {
  104.     case GFPAINT0:
  105.         paint_switch = (BYTE)(!paint_switch);
  106.         break;
  107.  
  108.     case GFPAINT1:
  109.         d = (UNSIGN16)nosignex(fontfp,(BYTE)1);
  110.         if (do_output && (paint_switch == BLACK))
  111.         {
  112.             for (k = 0; k < d; (++m, ++k))
  113.             SETBIT(m);
  114.         }
  115.         else
  116.         m += (INT16)d;
  117.         paint_switch = (BYTE)(!paint_switch);
  118.         break;
  119.  
  120.     case GFPAINT2:
  121.         d = (UNSIGN16)nosignex(fontfp,(BYTE)2);
  122.         if (do_output && (paint_switch == BLACK))
  123.         {
  124.             for (k = 0; k < d; (++m, ++k))
  125.             SETBIT(m);
  126.         }
  127.         else
  128.         m += (INT16)d;
  129.         paint_switch = (BYTE)(!paint_switch);
  130.         break;
  131.  
  132.     case GFPAINT3:            /* METAFONT never needs this */
  133.         d = (UNSIGN16)nosignex(fontfp,(BYTE)3);    /* NOTE: truncation */
  134.         if (do_output && (paint_switch == BLACK))
  135.         {
  136.             for (k = 0; k < d; (++m, ++k))
  137.             SETBIT(m);
  138.         }
  139.         else
  140.         m += (INT16)d;
  141.         paint_switch = (BYTE)(!paint_switch);
  142.         break;
  143.  
  144.     case GFBOC:    /* beginning-of-character -- long form */
  145.         the_word = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  146.         p = (long)signex(fontfp,(BYTE)4);
  147.         if ((UNSIGN32)c != the_word)
  148.         {
  149.         if (p < 0L)
  150.         {
  151.             (void)warning(
  152.     "chargf():    Requested character not found in back chain in GF font file");
  153.             return(EOF);
  154.         }
  155.         else
  156.              (void)FSEEK(fontfp,p,0);    /* must follow back chain */
  157.         break;
  158.         }
  159.         else
  160.         {
  161.         min_m = (INT16)signex(fontfp,(BYTE)4);
  162.         max_m = (INT16)signex(fontfp,(BYTE)4);
  163.         min_n = (INT16)signex(fontfp,(BYTE)4);
  164.         max_n = (INT16)signex(fontfp,(BYTE)4);
  165.         }
  166.         if ((min_m < MIN_M) || (MAX_M < max_m) ||
  167.         (min_n < MIN_N) || (MAX_N < max_n))
  168.         {
  169.         (void)warning(
  170.             "chargf():  GF font file character box too large for me");
  171.         return(EOF);
  172.         }
  173.         m = min_m;
  174.         n = max_n;
  175.         paint_switch = WHITE;
  176.         img_words = (UNSIGN16)((max_m - min_m + 1 + 31) >> 5);
  177.         if (do_output)
  178.         (void)clrrow();
  179.         tcharptr->hp = (COORDINATE)(max_n - min_n + 1);
  180.         tcharptr->wp = (COORDINATE)(max_m - min_m + 1);
  181.         tcharptr->xoffp = -(COORDINATE)(min_m);
  182.         tcharptr->yoffp = (COORDINATE)(max_n);
  183.         if (!VISIBLE(tcharptr))
  184.         return(0); /* empty character raster -- nothing to output */
  185.         break;
  186.  
  187.     case GFBOC1:        /* beginning-of-character -- short form */
  188.         if (c != (BYTE)nosignex(fontfp,(BYTE)1))
  189.         {
  190.         (void)warning(
  191.             "chargf():    Requested character not found in GF font file");
  192.         return(EOF);
  193.         }
  194.         else
  195.         {
  196.         min_m = (INT16)nosignex(fontfp,(BYTE)1);
  197.         max_m = (INT16)nosignex(fontfp,(BYTE)1);
  198.         min_m = max_m - min_m;
  199.         min_n = (INT16)nosignex(fontfp,(BYTE)1);
  200.         max_n = (INT16)nosignex(fontfp,(BYTE)1);
  201.         min_n = max_n - min_n;
  202.         }
  203.         if ((min_m < MIN_M) || (MAX_M < max_m) ||
  204.         (min_n < MIN_N) || (MAX_N < max_n))
  205.         {
  206.         (void)warning(
  207.             "chargf():  GF font file character box too large for me");
  208.         return(EOF);
  209.         }
  210.         m = min_m;
  211.         n = max_n;
  212.         paint_switch = WHITE;
  213.         img_words = (UNSIGN16)((max_m - min_m + 1 + 31) >> 5);
  214.         if (do_output)
  215.         (void)clrrow();
  216.         tcharptr->hp = (COORDINATE)(max_n - min_n + 1);
  217.         tcharptr->wp = (COORDINATE)(max_m - min_m + 1);
  218.         tcharptr->xoffp = -(COORDINATE)(min_m);
  219.         tcharptr->yoffp = (COORDINATE)(max_n);
  220.         if (!VISIBLE(tcharptr))
  221.         return(0);/* empty character raster -- nothing to output */
  222.         break;
  223.  
  224.     case GFEOC:            /* end-of-character */
  225.         if (do_output)
  226.         {
  227.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  228.         (void)clrrow();        /* clear next one */
  229.         --n;            /* advance to next row below */
  230.         for ( ; n >= min_n ; --n)    /* output any remaining rows */
  231.             (void)(*outfcn)(c,max_n-n);
  232.         }
  233.         return(0);            /* exit outer loop */
  234.  
  235.     case GFSKIP0:
  236.         if (do_output)
  237.         {
  238.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  239.         (void)clrrow();        /* clear next one */
  240.         }
  241.         --n;            /* advance to next row below */
  242.         m = min_m;
  243.         paint_switch = WHITE;
  244.         break;
  245.  
  246.     case GFSKIP1:
  247.         if (do_output)
  248.         {
  249.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  250.         (void)clrrow();        /* clear next one */
  251.         }
  252.         --n;            /* advance to next row below */
  253.         d = (UNSIGN16)nosignex(fontfp,(BYTE)1);
  254.         if (do_output)
  255.         {
  256.         for (k = 0; k < d; (--n,++k))    /* output d white rows */
  257.             (void)(*outfcn)(c,max_n-n);
  258.         }
  259.         else
  260.         n -= (INT16)d;
  261.         m = min_m;
  262.         paint_switch = WHITE;
  263.         break;
  264.  
  265.     case GFSKIP2:
  266.         if (do_output)
  267.         {
  268.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  269.         (void)clrrow();        /* clear next one */
  270.         }
  271.         --n;            /* advance to next row below */
  272.         d = (UNSIGN16)nosignex(fontfp,(BYTE)2);
  273.         if (do_output)
  274.         {
  275.         for (k = 0; k < d; (--n,++k))    /* output d white rows */
  276.             (void)(*outfcn)(c,max_n-n);
  277.         }
  278.         else
  279.         n -= (INT16)d;
  280.         m = min_m;
  281.         paint_switch = WHITE;
  282.         break;
  283.  
  284.     case GFSKIP3:            /* METAFONT never needs this */
  285.         if (do_output)
  286.         {
  287.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  288.         (void)clrrow();        /* clear next one */
  289.         }
  290.         --n;            /* advance to next row below */
  291.         d = (UNSIGN16)nosignex(fontfp,(BYTE)3);    /* NOTE truncation */
  292.         if (do_output)
  293.         {
  294.         for (k = 0; k < d; (++k, --n))    /* output d white rows */
  295.             (void)(*outfcn)(c,max_n-n);
  296.         }
  297.         else
  298.         n -= (INT16)d;
  299.         m = min_m;
  300.         paint_switch = WHITE;
  301.         break;
  302.  
  303.     case GFXXX1:
  304.     case GFXXX2:
  305.     case GFXXX3:
  306.     case GFXXX4:
  307.     case GFYYY:
  308.     case GFNOOP:
  309.         (void)UNGETC((char)the_byte,fontfp);
  310.         (void)skgfspec();
  311.         break;
  312.  
  313.     default:
  314.         if (((BYTE)GFPAINT0 < the_byte) && (the_byte < GFPAINT1))
  315.         {
  316.         d = (UNSIGN16)(the_byte - GFPAINT0);
  317.         if (do_output && (paint_switch == BLACK))
  318.         {
  319.             for (k = 0; k < d; (++m, ++k))
  320.                 SETBIT(m);
  321.         }
  322.         else
  323.             m += (INT16)d;
  324.         paint_switch = (BYTE)(!paint_switch);
  325.         }
  326.         else if (((BYTE)GFNROW0 <= the_byte) && (the_byte <= GFNROWMAX))
  327.         {
  328.         if (do_output)
  329.         {
  330.             (void)(*outfcn)(c,max_n-n);    /* output current row */
  331.             (void)clrrow();    /* clear next one */
  332.         }
  333.         n--;            /* advance to next row below */
  334.         paint_switch = BLACK;
  335.         m = min_m + (UNSIGN16)(the_byte - GFNROW0);
  336.         }
  337.         else
  338.         {
  339.         (void)sprintf(message,
  340.             "chargf():  Unexpected byte %d (0x%02x) in GF font file \
  341. at position %ld",
  342.             the_byte,the_byte,(long)FTELL(fontfp));
  343.         (void)warning(message);
  344.         return(EOF);
  345.         }
  346.         break;
  347.     }                /* end switch */
  348.     the_byte = (BYTE)nosignex(fontfp,(BYTE)1);
  349.     }                    /* end for() */
  350. }
  351.