home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / lwf / part01 / kfont.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  3KB  |  130 lines

  1. /* 
  2.  * $Locker:  $ 
  3.  */ 
  4. static char    *rcsid = "$Header: showsnf.c,v 1.4 87/12/17 13:12:46 rws Exp $";
  5. #include <stdio.h>
  6. #include <sys/types.h> 
  7. #include <sys/file.h> 
  8. #ifdef SYSV
  9. #include <fcntl.h>
  10. #endif /* SYSV */
  11. #include <sys/stat.h> 
  12. #include <errno.h> 
  13.  
  14. #include "misc.h"
  15. #include "Xmd.h"
  16. #include "X.h"
  17. #include "Xproto.h"
  18. #include "fontstruct.h"
  19. #include "font.h"
  20.  
  21. #include "fc.h"
  22.  
  23. #ifndef u_char
  24. #define u_char    unsigned char
  25. #endif
  26.  
  27. char *    malloc();
  28.  
  29. int glyphPad = DEFAULTGLPAD;
  30. int bitorder = DEFAULTBITORDER;
  31.  
  32. TempFont    tf;
  33. char        *kbuf;
  34.  
  35. int openkfont(file)
  36.     char    *file;
  37. {
  38.     struct stat    st;
  39.     FontInfoRec f;
  40.     int    fd, i, strings;
  41.  
  42.     if (stat(file, &st) < 0) {
  43.         fprintf(stderr, "can't stat %s\n", file);
  44.         return(0);
  45.     }
  46.  
  47.     fd = open(file, O_RDONLY);
  48.     if (fd < 0) {
  49.         fprintf(stderr, "can't open %s\n", file);
  50.         return(0);
  51.     }
  52.     kbuf = malloc(st.st_size);
  53.     read(fd, kbuf, st.st_size);
  54.     close(fd);
  55.     tf.pFI = (FontInfoPtr)kbuf;
  56.     tf.pCI = (CharInfoPtr)(kbuf + BYTESOFFONTINFO(tf.pFI));
  57.     tf.pGlyphs = ((unsigned char *)tf.pCI) + BYTESOFCHARINFO(tf.pFI);
  58.     tf.pFP = (FontPropPtr)(tf.pGlyphs + BYTESOFGLYPHINFO(tf.pFI));
  59.     strings = (int)tf.pFP + BYTESOFPROPINFO(tf.pFI);
  60.  
  61.     for (i=0; i<tf.pFI->nProps; i++) {
  62.         tf.pFP[i].name += strings;
  63.         if (tf.pFP[i].indirect)
  64.             tf.pFP[i].value += strings;
  65.     }
  66.     return(1);
  67. }
  68.  
  69. closekfont()
  70. {
  71.     free(kbuf);
  72. }
  73.  
  74. char hexstring[80];
  75.  
  76. char *kimage(crow, ccol)
  77.     int     crow, ccol;
  78. {
  79.     hexstring[0] = 0;
  80.     DumpBitmaps(&tf, crow, ccol);
  81.     return(hexstring);
  82. }
  83.  
  84. DumpBitmaps(pFont, crow, ccol)
  85.     TempFont *pFont;
  86.     int        crow, ccol;
  87. {
  88.     int            ch;    /* current character */
  89.     int            r;    /* current row */
  90.     int            b;    /* current bit in the row */
  91.     FontInfoPtr        pFI = pFont->pFI;
  92.     CharInfoPtr        pCI = pFont->pCI;
  93.     u_char *        bitmap = (u_char *)pFont->pGlyphs;
  94.     int            bpr;
  95.  
  96.     ch = (crow - pFont->pFI->firstRow)
  97.      * (pFont->pFI->lastCol - pFont->pFI->firstCol + 1)
  98.      + ccol - pFont->pFI->firstCol;
  99.  
  100.     bpr = GLWIDTHBYTESPADDED(pCI[ch].metrics.rightSideBearing
  101.         - pCI[ch].metrics.leftSideBearing, glyphPad);
  102.  
  103.         if ( !pCI[ch].exists || pCI[ch].metrics.characterWidth == 0)
  104.             return;
  105.  
  106.         for (r=0; r <  pCI[ch].metrics.descent + pCI[ch].metrics.ascent; r++)
  107.         {
  108.         unsigned char *row = bitmap + pCI[ch].byteOffset+(r*bpr);
  109.             for ( b=0;
  110.         b < pCI[ch].metrics.rightSideBearing - pCI[ch].metrics.leftSideBearing;
  111.         b += 4) {
  112.         if (bitorder == LSBFirst) {
  113.             addxchar( (b&4)? (row[b>>3] & 0xf0)>>4
  114.                      :   (row[b>>3] & 0x0f) );
  115.         } else {
  116.             addxchar( (b&4)? (row[b>>3] & 0x0f)
  117.                      :   (row[b>>3] & 0xf0)>>4 );
  118.         }
  119.         }
  120.         }
  121. }
  122.  
  123. addxchar(nib)
  124. int nib;
  125. {    char xs[3];
  126.  
  127.     sprintf(xs, "%1x", nib);
  128.     strcat(hexstring, xs);
  129. }
  130.