blob: 3bd55a7c80cfe323f08a3538790c66309d728a35 [file] [log] [blame]
Daniel Stenberg1b5afda2002-09-12 13:59:59 +00001/*
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 Stenberg1b5afda2002-09-12 13:59:59 +00009#include "../firmware/font.h"
10
11extern MWCFONT FONT;
12PMWCFONT pf = &FONT;
13
14static int
15WRITEBYTE(FILE *fp, unsigned char c)
16{
17 return putc(c, fp) != EOF;
18}
19
20static int
21WRITESHORT(FILE *fp, unsigned short s)
22{
23 putc(s, fp);
24 return putc(s>>8, fp) != EOF;
25}
26
27static int
28WRITELONG(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
36static int
37WRITESTR(FILE *fp, char *str, int count)
38{
39 return fwrite(str, 1, count, fp) == count;
40}
41
42static int
43WRITESTRPAD(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*/
58int
59rbf_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 Linenberg038df5c2002-09-16 03:18:49 +000083 WRITESHORT(ofp, 0);
Daniel Stenberg1b5afda2002-09-12 13:59:59 +000084 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 Linenberg038df5c2002-09-16 03:18:49 +000096 if (ftell(ofp) & 2)
97 WRITESHORT(ofp, 0); /* pad to 32-bit boundary*/
98
Daniel Stenberg1b5afda2002-09-12 13:59:59 +000099 if (pf->offset)
100 for (i=0; i<pf->size; ++i)
101 WRITELONG(ofp, pf->offset[i]);
Eric Linenberg038df5c2002-09-16 03:18:49 +0000102
Daniel Stenberg1b5afda2002-09-12 13:59:59 +0000103 if (pf->width)
104 for (i=0; i<pf->size; ++i)
105 WRITEBYTE(ofp, pf->width[i]);
106}
107
108int
109main(int ac, char **av)
110{
111 rbf_write_font(pf);
112}