Main Page   File List   Globals  

FBfont.c

Go to the documentation of this file.
00001 
00010 /* This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; version 2 of the License. */
00013 
00014 #include <stdio.h>
00015 #include <stdarg.h>
00016 #include "FBlib.h"
00017 
00018 struct font
00019 {
00020         char **glyphs;
00021         char filemode;
00022         char fontheight;
00023         int len;
00024 };
00025 
00026 void insert_glyph(int num, char* glyph);
00027 void delete_all_glyphs();
00028 char *get_glyph(int num);
00029 
00030 FILE *gunzip(FILE* fp, char* path);
00031 
00032 struct font current_font;
00033 
00034 int FB_change_font(char *psf_file)
00035 {
00036         FILE *font;
00037         unsigned char byte;
00038         int j,k;
00039         char *glyph;
00040         
00041         font = fopen(psf_file, "r");
00042         if(font == NULL)
00043         {
00044                 fprintf(stderr, "%s was not found", psf_file);
00045                 return PARAM_ERR;
00046         }
00047                 byte = fgetc(font);
00048         if(byte == 31) /* gzipped psf */
00049         {
00050                 byte = fgetc(font);
00051                 if(byte == 139)
00052                 {
00053                         font = gunzip(font, psf_file);
00054                         byte = fgetc(font);
00055                 }
00056         }
00057         if(byte != 0x36)
00058         {
00059                 fprintf(stderr, "%s is not a psf font file", psf_file);
00060                 return PARAM_ERR;
00061         }
00062         byte = fgetc(font);
00063         if(byte != 0x04)
00064         {
00065                 fprintf(stderr, "%s is not a psf font file", psf_file);
00066                 return PARAM_ERR;
00067         }
00068         delete_all_glyphs();
00069         current_font.filemode = fgetc(font);
00070         if(current_font.filemode == 1 || current_font.filemode == 3)
00071                 current_font.len = 512;
00072         if(current_font.filemode == 0 || current_font.filemode == 2)
00073                 current_font.len = 256;
00074         current_font.fontheight = fgetc(font);
00075         glyph = malloc(current_font.fontheight);
00076         for(k=0; k < current_font.len; k++)
00077         {
00078                 for(j=0; j < current_font.fontheight; j++)
00079                         glyph[j] = fgetc(font);
00080                 insert_glyph(k, glyph);
00081         }
00082         free(glyph);
00083         return OK;
00084 }
00085 
00086 int FB_putc(int c, int x, int y, FB_pixel col)
00087 {
00088         char *glyph;
00089         int i,j,k=0;
00090         
00091         if(c > 256 && current_font.len == 256)
00092                 return PARAM_ERR;
00093         if((glyph = get_glyph(c)) != NULL)
00094                 for(i = 0; i < current_font.fontheight; i++)
00095                 {
00096                         for(j = 128; j > 0; j/=2)
00097                         {
00098                                 if(glyph[i] & j)
00099                                         FB_putpixel(x+k, y+i, col);
00100                                 k++;
00101                         }
00102                         k=0;
00103                 }
00104         return OK;
00105 }
00106 
00107 int FB_printf(int x, int y, FB_pixel col, char *format, ...)
00108 {
00109         va_list ap;
00110         char string[20];
00111         int len,i,row=0, column=0, j;
00112 
00113         len = strlen(format);
00114         va_start(ap, format);
00115         for(i=0; i < len; i++)
00116         {
00117                 if(format[i] != '%' && format[i] != '\\')
00118                 {
00119                         FB_putc(format[i], x+(column*8), y+row, col);
00120                         column++;
00121                 }
00122                 if(format[i] == '\\')
00123                 {
00124                         if(format[i+1] == 'n')
00125                         {
00126                                 row++;
00127                                 column=0;
00128                         }
00129                         else if(format[i+1] == 'r')
00130                                 column=0;
00131                         i++;
00132                         continue;
00133                 }
00134                 if(format[i] == '%')
00135                 {
00136                         switch(format[i+1])
00137                         {
00138                                 case '%': sprintf(string, "%%"); break;
00139                                 case 'd': sprintf(string, "%d", va_arg(ap, int)); break;
00140                                 case 'x': sprintf(string, "%x", va_arg(ap, int)); break;
00141 //                              case 'c': sprintf(string, "%c", va_arg(ap, char)); break;
00142                                 case 'c': sprintf(string, "%c", (char)va_arg(ap, int)); break;
00143                                 case 'g': sprintf(string, "%g", va_arg(ap, double)); break;
00144                         }         
00145                         for(j=0; j < strlen(string); j++)
00146                         {
00147                                 FB_putc(string[j], x+(column*8), y+row, col);
00148                                 column++;
00149                         }
00150                         i++;
00151                         continue;
00152                 }
00153         }
00154         va_end(ap);
00155         return 0;
00156 }
00157 
00158 void insert_glyph(int num, char* glyph)
00159 {
00160         char* aux;
00161         
00162         if(current_font.glyphs == NULL)
00163                 current_font.glyphs = malloc(sizeof(char*) * current_font.len);
00164         aux = malloc(current_font.fontheight);
00165         memcpy(aux, glyph, current_font.fontheight);
00166         current_font.glyphs[num] = aux;
00167 
00168         return;
00169 }
00170 
00171 void delete_all_glyphs()
00172 {
00173         int i;
00174         
00175         for(i=0; i<current_font.len; i++)
00176                 free(current_font.glyphs[i]);
00177         free(current_font.glyphs);
00178 
00179         return;
00180 }
00181 
00182 char *get_glyph(int num)
00183 {
00184         if(current_font.glyphs != NULL)
00185                 return current_font.glyphs[num];
00186         else
00187                 return NULL;
00188 }
00189 
00190 FILE *gunzip(FILE* fp, char* path)
00191 {
00192         char command[200];
00193         
00194         fclose(fp);
00195         sprintf(command, "gunzip -c %s > /tmp/FBfont.psf", path);
00196         system(command);
00197         fp = fopen("/tmp/FBfont.psf", "r");
00198         system("rm /tmp/FBfont.psf");
00199         return fp;
00200 }
00201 

Generated on Sat Jul 5 09:04:51 2003 for LibFB by doxygen1.2.18