Daniel Stenberg | 1b5afda | 2002-09-12 13:59:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * writerbf - write an incore font in .rbf format. |
| 3 | * Must be compiled with -DFONT=font_name and linked |
| 4 | * with compiled in font. |
| 5 | * |
| 6 | * Copyright (c) 2002 by Greg Haerr <greg@censoft.com> |
| 7 | */ |
| 8 | #include <stdio.h> |
Daniel Stenberg | 1b5afda | 2002-09-12 13:59:59 +0000 | [diff] [blame] | 9 | #include "../firmware/font.h" |
| 10 | |
| 11 | extern MWCFONT FONT; |
| 12 | PMWCFONT pf = &FONT; |
| 13 | |
| 14 | static int |
| 15 | WRITEBYTE(FILE *fp, unsigned char c) |
| 16 | { |
| 17 | return putc(c, fp) != EOF; |
| 18 | } |
| 19 | |
| 20 | static int |
| 21 | WRITESHORT(FILE *fp, unsigned short s) |
| 22 | { |
| 23 | putc(s, fp); |
| 24 | return putc(s>>8, fp) != EOF; |
| 25 | } |
| 26 | |
| 27 | static int |
| 28 | WRITELONG(FILE *fp, unsigned long l) |
| 29 | { |
| 30 | putc(l, fp); |
| 31 | putc(l>>8, fp); |
| 32 | putc(l>>16, fp); |
| 33 | return putc(l>>24, fp) != EOF; |
| 34 | } |
| 35 | |
| 36 | static int |
| 37 | WRITESTR(FILE *fp, char *str, int count) |
| 38 | { |
| 39 | return fwrite(str, 1, count, fp) == count; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | WRITESTRPAD(FILE *fp, char *str, int totlen) |
| 44 | { |
| 45 | int ret; |
| 46 | |
| 47 | while (*str && totlen > 0) |
| 48 | if (*str) { |
| 49 | ret = putc(*str++, fp); |
| 50 | --totlen; |
| 51 | } |
| 52 | while (--totlen >= 0) |
| 53 | ret = putc(' ', fp); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | /* write font, < 0 return is error*/ |
| 58 | int |
| 59 | rbf_write_font(PMWCFONT pf) |
| 60 | { |
| 61 | FILE *ofp; |
| 62 | int i; |
| 63 | char name[256]; |
| 64 | |
| 65 | sprintf(name, "%s.fnt", pf->name); |
| 66 | ofp = fopen(name, "wb"); |
| 67 | if (!ofp) |
| 68 | return -1; |
| 69 | |
| 70 | /* write magic and version #*/ |
| 71 | WRITESTR(ofp, VERSION, 4); |
| 72 | |
| 73 | /* internal font name*/ |
| 74 | WRITESTRPAD(ofp, pf->name, 64); |
| 75 | |
| 76 | /* copyright - FIXME not converted with bdf2c*/ |
| 77 | WRITESTRPAD(ofp, " ", 256); |
| 78 | |
| 79 | /* font info*/ |
| 80 | WRITESHORT(ofp, pf->maxwidth); |
| 81 | WRITESHORT(ofp, pf->height); |
| 82 | WRITESHORT(ofp, pf->ascent); |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 83 | WRITESHORT(ofp, 0); |
Daniel Stenberg | 1b5afda | 2002-09-12 13:59:59 +0000 | [diff] [blame] | 84 | WRITELONG(ofp, pf->firstchar); |
| 85 | WRITELONG(ofp, pf->defaultchar); |
| 86 | WRITELONG(ofp, pf->size); |
| 87 | |
| 88 | /* variable font data sizes*/ |
| 89 | WRITELONG(ofp, pf->bits_size); /* # words of MWIMAGEBITS*/ |
| 90 | WRITELONG(ofp, pf->offset? pf->size: 0); /* # longs of offset*/ |
| 91 | WRITELONG(ofp, pf->width? pf->size: 0); /* # bytes of width*/ |
| 92 | |
| 93 | /* variable font data*/ |
| 94 | for (i=0; i<pf->bits_size; ++i) |
| 95 | WRITESHORT(ofp, pf->bits[i]); |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 96 | if (ftell(ofp) & 2) |
| 97 | WRITESHORT(ofp, 0); /* pad to 32-bit boundary*/ |
| 98 | |
Daniel Stenberg | 1b5afda | 2002-09-12 13:59:59 +0000 | [diff] [blame] | 99 | if (pf->offset) |
| 100 | for (i=0; i<pf->size; ++i) |
| 101 | WRITELONG(ofp, pf->offset[i]); |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 102 | |
Daniel Stenberg | 1b5afda | 2002-09-12 13:59:59 +0000 | [diff] [blame] | 103 | if (pf->width) |
| 104 | for (i=0; i<pf->size; ++i) |
| 105 | WRITEBYTE(ofp, pf->width[i]); |
| 106 | } |
| 107 | |
| 108 | int |
| 109 | main(int ac, char **av) |
| 110 | { |
| 111 | rbf_write_font(pf); |
| 112 | } |