home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / gl / text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-12  |  4.8 KB  |  249 lines

  1. /* Framebuffer Graphics Libary for Linux, Copyright 1993 Harm Hanemaayer */
  2. /* text.c    Text writing and fonts */
  3.  
  4.  
  5. #include <stdlib.h>
  6. #include <vga.h>
  7. #include "inlstring.h"        /* include inline string operations */
  8.  
  9. #include "vgagl.h"
  10. #include "def.h"
  11.  
  12.  
  13.  
  14. /* Text/font functions */
  15.  
  16. static int initialized = 0;
  17. static int font_width = 8;
  18. static int font_height = 8;
  19. static char *font_address;
  20. static int font_charactersize = 64;
  21. static int font_writemode = WRITEMODE_OVERWRITE;
  22. static int compressed_font_bg = 0;
  23. static int compressed_font_fg = 15;
  24.  
  25. static void writecompressed( int x, int y, char *s );
  26.  
  27.  
  28. void gl_colorfont( int fw, int fh, int fg, void *_dp ) {
  29.     uchar *dp = _dp;
  30.     int i;
  31.     i = fw * fh * 256;
  32.     switch (BYTESPERPIXEL) {
  33.     case 1 :
  34.         while (i > 0) {
  35.             if (*dp)
  36.                 *dp = fg;
  37.             dp++;
  38.             i--;
  39.         }
  40.         break;
  41.     case 2 :
  42.         while (i > 0) {
  43.             if (*(ushort *)dp)
  44.                 *(ushort *)dp = fg;
  45.             dp += 2;
  46.             i--;
  47.         }
  48.         break;
  49.     case 3 :
  50.         while (i > 0) {
  51.             if (*(ushort *)dp || *(dp + 2)) {
  52.                 *(ushort *)dp = fg;
  53.                 *(dp + 2) = fg >> 16;
  54.             }
  55.             dp += 3;
  56.             i--;
  57.         }
  58.         break;
  59.     case 4 :
  60.         while (i > 0) {
  61.             if (*(int *)dp)
  62.                 *(int *)dp = fg;
  63.             dp += 4;
  64.             i--;
  65.         }
  66.         break;
  67.         
  68.     }
  69. }
  70.  
  71. void gl_setfont( int fw, int fh, void *font ) {
  72.     int i;
  73.     font_width = fw;
  74.     font_height = fh;
  75.     font_charactersize = font_width * font_height * BYTESPERPIXEL;
  76.     font_address = font;
  77. }
  78.  
  79. void gl_setwritemode( int m ) {
  80.     font_writemode = m;
  81.  
  82. void gl_write( int x, int y, char *s ) {
  83. /* clipping in putbox */
  84.     int i;
  85.     if (font_writemode & FONT_COMPRESSED) {
  86.         writecompressed(x, y, s);
  87.         return;
  88.     }
  89.     i = 0;
  90.     if (font_writemode == WRITEMODE_OVERWRITE) {
  91.         while (s[i] != 0) {
  92.             gl_putbox(x + i * font_width, y, font_width,
  93.                 font_height, font_address +
  94.                 (unsigned char)s[i] * font_charactersize);
  95.             i++;
  96.         }
  97.     }
  98.     else {    /* masked write */
  99.         while (s[i] != 0) {
  100.             gl_putboxmask(x + i * font_width, y, font_width, 
  101.                 font_height, font_address + 
  102.                 (unsigned char)s[i] * font_charactersize);
  103.             i++;
  104.         }
  105.     }
  106. }
  107.  
  108. void gl_writen( int x, int y, int n, char *s ) {
  109.     char *str = alloca(n + 1);
  110.     memcpy(str, s, n);
  111.     str[n] = 0;
  112.     gl_write(x, y, str);
  113. }
  114.  
  115. void gl_expandfont( int fw, int fh, int fg, void *_f1, void *_f2 ) {
  116. /* Convert bit-per-pixel font to byte(s)-per-pixel font */
  117.     uchar *f1 = _f1;
  118.     uchar *f2 = _f2;
  119.     int i, x, y, b;
  120.     for (i = 0; i < 256; i++) {
  121.         for (y = 0; y < fh; y++)
  122.             for (x = 0; x < fw; x++) {
  123.                 if (x % 8 == 0)
  124.                     b = *f1++;
  125.                 if (b & (128 >> (x % 8)))    /* pixel */
  126.                     switch (BYTESPERPIXEL) {
  127.                     case 1 :
  128.                         *f2 = fg;
  129.                         f2++;
  130.                         break;
  131.                     case 2 :
  132.                         *(ushort *)f2 = fg;
  133.                         f2 += 2;
  134.                         break;
  135.                     case 3 :
  136.                         *(ushort *)f2 = fg;
  137.                         *(f2 + 2) = fg >> 16;
  138.                         f2 += 3;
  139.                         break;
  140.                     case 4 :
  141.                         *(uint *)f2 = fg;
  142.                         f2 += 4;
  143.                     }
  144.                 else    /* no pixel */
  145.                     switch (BYTESPERPIXEL) {
  146.                     case 1 :
  147.                         *f2 = 0;
  148.                         f2++;
  149.                         break;
  150.                     case 2 :
  151.                         *(ushort *)f2 = 0;
  152.                         f2 += 2;
  153.                         break;
  154.                     case 3 :
  155.                         *(ushort *)f2 = 0;
  156.                         *(f2 + 2) = 0;
  157.                         f2 += 3;
  158.                         break;
  159.                     case 4 :
  160.                         *(uint *)f2 = 0;
  161.                         f2 += 4;
  162.                     }
  163.             }
  164.     }
  165.  
  166. static void expandcharacter( int bg, int fg, int c, unsigned char *bitmap ) {
  167.     int x, y;
  168.     unsigned char *font;
  169.     font = font_address + c * (font_height * (font_width + 7) / 8);
  170.  
  171.     for (y = 0; y < font_height; y++)
  172.         for (x = 0; x < font_width; x++) {
  173.             int b;
  174.             if (x % 8 == 0)
  175.                 b = *font++;
  176.             if (b & (128 >> (x % 8)))    /* pixel */
  177.                 switch (BYTESPERPIXEL) {
  178.                 case 1 :
  179.                     *bitmap = fg;
  180.                     bitmap++;
  181.                 break;
  182.                 case 2 :
  183.                     *(ushort *)bitmap = fg;
  184.                     bitmap += 2;
  185.                     break;
  186.                 case 3 :
  187.                     *(ushort *)bitmap = fg;
  188.                     *(bitmap + 2) = fg >> 16;
  189.                     bitmap += 3;
  190.                     break;
  191.                 case 4 :
  192.                     *(uint *)bitmap = fg;
  193.                     bitmap += 4;
  194.                 }
  195.             else    /* background pixel */
  196.                 switch (BYTESPERPIXEL) {
  197.                 case 1 :
  198.                     *bitmap = bg;
  199.                     bitmap++;
  200.                     break;
  201.                 case 2 :
  202.                     *(ushort *)bitmap = bg;
  203.                     bitmap += 2;
  204.                 break;
  205.                 case 3 :
  206.                     *(ushort *)bitmap = bg;
  207.                     *(bitmap + 2) = bg;
  208.                     bitmap += 3;
  209.                     break;
  210.                 case 4 :
  211.                     *(uint *)bitmap = bg;
  212.                     bitmap += 4;
  213.                 }
  214.         }
  215. }
  216.  
  217. /* Write using compressed font. */
  218.  
  219. static void writecompressed( int x, int y, char *s ) {
  220.     unsigned char *bitmap;
  221.     int i;
  222.     i = 0;
  223.     bitmap = alloca(font_width * font_height * BYTESPERPIXEL);
  224.     if (font_writemode == WRITEMODE_OVERWRITE) {
  225.         while (s[i] != 0) {
  226.             expandcharacter(compressed_font_bg,
  227.                 compressed_font_fg, s[i], bitmap);
  228.             gl_putbox(x + i * font_width, y, font_width,
  229.                 font_height, bitmap);
  230.             i++;
  231.         }
  232.     }
  233.     else {    /* masked write */
  234.         while (s[i] != 0) {
  235.             expandcharacter(0, compressed_font_fg, s[i], bitmap);
  236.             gl_putboxmask(x + i * font_width, y, font_width, 
  237.                 font_height, bitmap);
  238.             i++;
  239.         }
  240.     }
  241. }
  242.  
  243. void gl_setfontcolors( int bg, int fg ) {
  244.     compressed_font_bg = bg;
  245.     compressed_font_fg = fg;
  246. }
  247.