summaryrefslogtreecommitdiffstats
path: root/tools/writerbf.c
blob: b3ba8649ac8e0a8cc665f22e9473df8f10a57f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * writerbf - write an incore font in .rbf format.
 * Must be compiled with -DFONT=font_name and linked
 * with compiled in font.
 *
 * Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
 */
#include <stdio.h>
#include "../firmware/font.h"

extern MWCFONT FONT;
PMWCFONT pf = &FONT;

static int
WRITEBYTE(FILE *fp, unsigned char c)
{
	return putc(c, fp) != EOF;
}

static int
WRITESHORT(FILE *fp, unsigned short s)
{
	putc(s, fp);
	return putc(s>>8, fp) != EOF;
}

static int
WRITELONG(FILE *fp, unsigned long l)
{
	putc(l, fp);
	putc(l>>8, fp);
	putc(l>>16, fp);
	return putc(l>>24, fp) != EOF;
}

static int
WRITESTR(FILE *fp, char *str, int count)
{
	return fwrite(str, 1, count, fp) == count;
}

static int
WRITESTRPAD(FILE *fp, char *str, int totlen)
{
	int ret;
	
	while (*str && totlen > 0)
		if (*str) {
			ret = putc(*str++, fp);
			--totlen;
		}
	while (--totlen >= 0)
		ret = putc(' ', fp);
	return ret;
}

/* write font, < 0 return is error*/
int
rbf_write_font(PMWCFONT pf)
{
	FILE *ofp;
	int i;
	char name[256];

	sprintf(name, "%s.fnt", pf->name);
	ofp = fopen(name, "wb");
	if (!ofp)
		return -1;
	
	/* write magic and version #*/
	WRITESTR(ofp, VERSION, 4);

	/* internal font name*/
	WRITESTRPAD(ofp, pf->name, 64);

	/* copyright - FIXME not converted with bdf2c*/
	WRITESTRPAD(ofp, " ", 256);

	/* font info*/
	WRITESHORT(ofp, pf->maxwidth);
	WRITESHORT(ofp, pf->height);
	WRITESHORT(ofp, pf->ascent);
	WRITELONG(ofp, pf->firstchar);
	WRITELONG(ofp, pf->defaultchar);
	WRITELONG(ofp, pf->size);

	/* variable font data sizes*/
	WRITELONG(ofp, pf->bits_size);		  /* # words of MWIMAGEBITS*/
	WRITELONG(ofp, pf->offset? pf->size: 0);  /* # longs of offset*/
	WRITELONG(ofp, pf->width? pf->size: 0);	  /* # bytes of width*/

	/* variable font data*/
	for (i=0; i<pf->bits_size; ++i)
		WRITESHORT(ofp, pf->bits[i]);
	if (pf->offset)
		for (i=0; i<pf->size; ++i)
			WRITELONG(ofp, pf->offset[i]);
	if (pf->width)
		for (i=0; i<pf->size; ++i)
			WRITEBYTE(ofp, pf->width[i]);
}

int
main(int ac, char **av)
{
	rbf_write_font(pf);
}