summaryrefslogtreecommitdiff
path: root/nokia-generate-bdf.c
blob: 0e45f2f34a9ab35dca05ec205818e19d394f7c76 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * nokia-generate-bdf.c: BDF generator for Nokia 3310 fonts
 *
 * Copyright (c) 2013, Přemysl Eric Janouch <p@janouch.name>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include <stdlib.h>
#include <string.h>
#include "nokia-fonts.c"

#define N_ELEMENTS(a) (sizeof (a) / sizeof (a)[0])

static void
generate_big_char (struct nokia_big_char *ch, wchar_t code)
{
	printf ("STARTCHAR U+%04X\n", code);
	printf ("ENCODING %d\n", code);
	printf ("SWIDTH %d 0\n", (int) (.5 + 1000. * ch->advance / 14.));
	printf ("DWIDTH %d\n", ch->advance);
	printf ("BBX %d 14 0 -2\n", ch->width);
	printf ("BITMAP\n");
	printf (ch->width > 8 ? "0000\n" : "00\n");

	unsigned i;
	for (i = 0; i < 13; i++)
	{
		unsigned short row = ch->rows[i];

		printf ("%02X", (row >> 8) & 0xFF);
		if (ch->width > 8)
			printf ("%02X", row & 0xFF);
		putchar ('\n');
	}

	printf ("ENDCHAR\n");
}

static void
generate_big_font (void)
{
	// Line height for this font = 14 = 1 above + 13

	printf ("STARTFONT 2.1\n");
	printf ("FONT -nokia-nokia-medium-r-normal--14-140-75-75-p-65-iso10646-1\n");
	printf ("SIZE 14 75 75\n");
	printf ("FONTBOUNDINGBOX 14 14 0 -2\n");
	printf ("STARTPROPERTIES 2\n");
	printf ("FONT_ASCENT 12\n");
	printf ("FONT_DESCENT 2\n");
	printf ("ENDPROPERTIES\n");

	printf ("CHARS %zu\n", N_ELEMENTS (nokia_big_data)
		+ N_ELEMENTS (nokia_big_wide_data));

	unsigned i;
	for (i = 0; i < N_ELEMENTS (nokia_big_data); i++)
		generate_big_char (&nokia_big_data[i], ' ' + i);

	for (i = 0; i < N_ELEMENTS (nokia_big_wide_data); i++)
	{
		struct nokia_big_char_wide *chw = &nokia_big_wide_data[i];
		generate_big_char (&chw->ch, chw->code);
	}

	printf ("ENDFONT\n");
}

static void
generate_small_char (struct nokia_small_char *ch, wchar_t code)
{
	printf ("STARTCHAR U+%04X\n", code);
	printf ("ENCODING %d\n", code);
	printf ("SWIDTH %d 0\n", (int) (.5 + 1000. * ch->advance / 10.));
	printf ("DWIDTH %d\n", ch->advance);
	printf ("BBX %d 10 0 -2\n", ch->width);
	printf ("BITMAP\n");
	printf ("00\n");

	unsigned i;
	for (i = 0; i < 8; i++)
		printf ("%02X\n", ch->rows[i] & 0xFF);

	printf ("00\n");
	printf ("ENDCHAR\n");
}

static void
generate_small_font (void)
{
	// Line height for this font = 10 = 1 above + 8 + 1 below

	printf ("STARTFONT 2.1\n");
	printf ("FONT -nokia-nokia-medium-r-normal--10-100-75-75-p-40-iso10646-1\n");
	printf ("SIZE 10 75 75\n");
	printf ("FONTBOUNDINGBOX 10 10 0 -2\n");
	printf ("STARTPROPERTIES 2\n");
	printf ("FONT_ASCENT 8\n");
	printf ("FONT_DESCENT 2\n");
	printf ("ENDPROPERTIES\n");

	printf ("CHARS %zu\n", N_ELEMENTS (nokia_small_data)
		+ N_ELEMENTS (nokia_small_wide_data));

	unsigned i;
	for (i = 0; i < N_ELEMENTS (nokia_small_data); i++)
		generate_small_char (&nokia_small_data[i], ' ' + i);

	for (i = 0; i < N_ELEMENTS (nokia_small_wide_data); i++)
	{
		struct nokia_small_char_wide *chw = &nokia_small_wide_data[i];
		generate_small_char (&chw->ch, chw->code);
	}

	printf ("ENDFONT\n");
}

static void
generate_small_bold_font (void)
{
	// Line height for this font = 10 = 1 above + 8 + 1 below

	printf ("STARTFONT 2.1\n");
	printf ("FONT -nokia-nokia-bold-r-normal--10-100-75-75-p-40-iso10646-1\n");
	printf ("SIZE 10 75 75\n");
	printf ("FONTBOUNDINGBOX 10 10 0 -2\n");
	printf ("STARTPROPERTIES 2\n");
	printf ("FONT_ASCENT 8\n");
	printf ("FONT_DESCENT 2\n");
	printf ("ENDPROPERTIES\n");

	printf ("CHARS %zu\n", N_ELEMENTS (nokia_small_bold_data)
		+ N_ELEMENTS (nokia_small_bold_wide_data));

	unsigned i;
	for (i = 0; i < N_ELEMENTS (nokia_small_bold_data); i++)
		generate_small_char (&nokia_small_bold_data[i], ' ' + i);

	for (i = 0; i < N_ELEMENTS (nokia_small_bold_wide_data); i++)
	{
		struct nokia_small_char_wide *chw = &nokia_small_bold_wide_data[i];
		generate_small_char (&chw->ch, chw->code);
	}

	printf ("ENDFONT\n");
}

static void
generate_menu_font (void)
{
	printf ("STARTFONT 2.1\n");
	printf ("FONT -nokia-nokia-medium-r-normal--6-60-75-75-p-40-iso10646-1\n");
	printf ("SIZE 6 75 75\n");
	printf ("FONTBOUNDINGBOX 6 6 0 0\n");
	printf ("STARTPROPERTIES 2\n");
	printf ("FONT_ASCENT 6\n");
	printf ("FONT_DESCENT 0\n");
	printf ("ENDPROPERTIES\n");

	printf ("CHARS %zu\n", N_ELEMENTS (nokia_menu_data));

	unsigned i, k;
	for (i = 0; i < N_ELEMENTS (nokia_menu_data); i++)
	{
		struct nokia_menu_char *ch = &nokia_menu_data[i];

		printf ("STARTCHAR U+%04X\n", '-' + i);
		printf ("ENCODING %d\n", '-' + i);
		printf ("SWIDTH %d 0\n", (int) (.5 + 1000. * ch->advance / 6.));
		printf ("DWIDTH %d\n", ch->advance);
		printf ("BBX %d 6 0 0\n", ch->width);
		printf ("BITMAP\n");

		for (k = 0; k < 6; k++)
			printf ("%02X\n", ch->rows[k] & 0xFF);

		printf ("ENDCHAR\n");
	}

	printf ("ENDFONT\n");
}

static void
usage (const char *program_name)
{
	fprintf (stderr, "Usage: %s { big | menu"
		" | small | small-bold }\n", program_name);
	exit (1);
}

int
main (int argc, char *argv[])
{
	if (argc != 2)
		usage (argv[0]);
	if (!strcmp (argv[1], "big"))
		generate_big_font ();
	else if (!strcmp (argv[1], "menu"))
		generate_menu_font ();
	else if (!strcmp (argv[1], "small"))
		generate_small_font ();
	else if (!strcmp (argv[1], "small-bold"))
		generate_small_bold_font ();
	else
		usage (argv[0]);

	return 0;
}