-
Notifications
You must be signed in to change notification settings - Fork 1
/
convfont.c
658 lines (610 loc) · 28 KB
/
convfont.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#ifdef _MSC_VER
#include "getopt.h"
#pragma warning(disable : 4996)
#else
#include <getopt.h>
#endif
#include "convfont.h"
#include "parse_fnt.h"
#include "parse_text.h"
#include "serialize_font.h"
/* http://benoit.papillault.free.fr/c/disc2/exefmt.txt */
#define VERSION_MAJOR 1
#define VERSION_MINOR 2
/*******************************************************************************
* STYLES AND WEIGHTS *
*******************************************************************************/
string_value_pair_t weight_names[] = {
{ "thin", 0x20 },
{ "extra light", 0x30 },
{ "extralight", 0x30 },
{ "light", 0x40 },
{ "semilight", 0x60 },
{ "normal", 0x80 },
{ "medium", 0x90 },
{ "semibold", 0xA0 },
{ "bold", 0xC0 },
{ "extra bold", 0xE0 },
{ "extrabold", 0xE0 },
{ "black", 0xF0 }
};
string_list_t weights = {
.count = 12,.strings = weight_names
};
string_value_pair_t style_names[] = {
{ "sans-serif", 0 },
{ "sansserif", 0 },
{ "serif", 1 },
{ "upright", 0 },
{ "oblique", 2 },
{ "italic", 4 },
{ "monospaced", 8 },
{ "fixed", 8 },
{ "proportional", 0 }
};
string_list_t styles = {
9, style_names
};
/* Compares a string against a list of strings and numeric values to associate
with that string. Returns -1 if no match is found. */
int check_string_for_value(const char *string, const string_list_t *possible_values) {
for (int i = 0; i < possible_values->count; i++)
if (strcaseeq(string, possible_values->strings[i].string))
return possible_values->strings[i].value;
return -1;
}
bool strcaseeq(const char *str1, const char *str2) {
for (; *str1 != '\0' && *str2 != '\0'; str1++, str2++)
if (tolower(*str1) != tolower(*str2))
return false;
return true;
}
/*******************************************************************************
* ERRORS *
*******************************************************************************/
int verbosity = 0;
noreturn void throw_error(const int code, const char *string) {
if (string != NULL)
throw_errorf(code, "%s", string);
exit(code);
}
noreturn void throw_errorf(const int code, const char *string, ...) {
va_list argp;
va_start(argp, string);
vthrow_errorf(code, string, argp);
va_end(argp);
}
noreturn void vthrow_errorf(const int code, const char *string, va_list args) {
if (string != NULL) {
fprintf(stderr, "ERROR: ");
vfprintf(stderr, string, args);
fprintf(stderr, "\n");
}
exit(code);
}
/*******************************************************************************
* SOME OUTPUT-RELATED STUFF *
*******************************************************************************/
bool unix_newline_style =
#ifdef _WIN32
false
#else
true
#endif
;
void output_format_byte(const uint8_t byte, void *custom_data) {
fputc(byte, custom_data);
}
typedef struct {
FILE *file;
int row_counter;
bool first_line;
} format_c_array_data_t;
void print_newline(FILE *file) {
if (!unix_newline_style)
fputc('\r', file);
fputc('\n', file);
}
void output_format_c_array(const uint8_t byte, void *custom_data) {
format_c_array_data_t *state = (format_c_array_data_t *)custom_data;
if (!state->row_counter)
if (state->first_line)
state->first_line = false;
else {
fputc(',', state->file);
print_newline(state->file);
}
else
fprintf(state->file, ", ");
fprintf(state->file, "0x%02X", byte);
state->row_counter = (state->row_counter + 1) % 16;
}
void write_string(char *string, FILE *out_file) {
do
fputc(*string, out_file);
while (*string++ != '\0');
}
/*******************************************************************************
* HELP *
*******************************************************************************/
void show_help(char *name) {
printf("\nUsage:\n"
"\t%s -o <output format> -f <font FNT> [-metrics] [-f <font FNT 2> [-metrics]] <output file name>\n"
"\tSpecifying more than one input font is only valid for the font pack output format.\n"
"\nOutput formats:\n"
"\t-o fontpack: A fontpack ready to be passed to convhex\n"
"\t-o carray: A C-style array\n"
"\t-o asmarray: An assembly-style array\n"
"\t-o binary: A straight binary blob\n"
#ifdef _WIN32
"\t-Z: Use CR+LF newlines (default for this platform)\n"
"\t-z: Use LR newlines instead of CR+LF newlines\n"
#else
"\t-z: Use LF newlines (default for this platform)\n"
"\t-Z: Use CR+LF newlines instead of LF newlines\n"
#endif
"\nIndividual font properties:\n"
"\t-f: <file name> input Font\n"
"\t-t: <file name> input Text-format font\n"
"\t-a: <n> space Above\n"
"\t-b: <n> space Below\n"
"\t-i: <n> Italic space adjust\n"
"\t-w: <n> Weight\n"
"\t The following strings may also be used:\n"
"\t thin, extralight, light, semilight, normal, medium, semibold, bold, extrabold, black\n"
"\t-s: <n> Style\n"
"\t The following strings may also be used:\n"
"\t sans-serif, serif, upright, oblique, italic, monospaced, proportional\n"
"\t-c: <n> Cap height\n"
"\t-x: <n> x height\n"
"\t-l: <n> baseLine height\n"
"\tNumbers may be prefixed with 0x to specify hexadecimal instead of decimal.\n"
"\nFont pack properties:\n"
"\t-N: \"<s>\" font pack Name\n"
"\t-A: \"<s>\" Author\n"
"\t-C: \"<s>\" pseudoCopyright\n"
"\t-D: \"<s>\" Description\n"
"\t-V: \"<s>\" Version\n"
"\t-P: \"<s>\" code Page\n", name);
}
/*******************************************************************************
* MAIN *
*******************************************************************************/
#define MAX_FONTS 64
fontlib_font_t *fonts[MAX_FONTS] =
{
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
};
int fonts_loaded = 0;
int main(int argc, char *argv[]) {
FILE *in_file;
FILE *out_file;
printf("convfont v%u.%u by drdnar\n", VERSION_MAJOR, VERSION_MINOR);
if (argc <= 1) {
printf("No inputs supplied.\n\n");
show_help(argv[0]);
return 0;
}
/* Settings */
char *font_pack_name = NULL;
char *author = NULL;
char *pseudocopyright = NULL;
char *description = NULL;
char *version = NULL;
char *codepage = NULL;
char *output_file_name = NULL;
char *recent_input_file_name = NULL;
fontlib_font_t *current_font = NULL;
int temp_n;
output_formats_t output_format = output_unspecified;
size_t strl;
int option;
while ((option = getopt(argc, argv, "hvo:Zf:a:b:i:w:s:c:x:l:N:A:C:D:V:P:t:")) != -1) {
switch (option) {
case 'h':
show_help(argv[0]);
return 0;
case 'v':
verbosity++;
break;
case 'o':
if (output_format != output_unspecified)
throw_error(bad_options, "-o: Cannot specify more than one output format.");
switch (optarg[0]) {
case 'c':
output_format = output_c_array;
break;
case 'a':
output_format = output_asm_array;
break;
case 'f':
case 'p':
output_format = output_fontpack;
break;
case 'b':
output_format = output_binary_blob;
break;
default:
throw_error(bad_options, "-o: Unknown output format.");
break;
}
break;
case 'Z':
unix_newline_style = false;
break;
case 'z':
unix_newline_style = true;
break;
case 'f':
if (fonts_loaded > 0 && output_format != output_fontpack)
throw_error(bad_options, "-f: Cannot have multiple input fonts unless -o fontpack is specified first.");
if (fonts_loaded >= MAX_FONTS - 1)
throw_error(bad_options, "-f: Too many fonts. What on Earth makes you think your font pack needs so many fonts?");
recent_input_file_name = optarg;
if (verbosity >= 1)
printf("Processing input file %s . . .\n", recent_input_file_name);
in_file = fopen(recent_input_file_name, "rb");
if (!in_file)
throw_error(bad_infile, "-f: Cannot open input file.");
int ver = read_word(in_file);
if (ver != 0x200 && ver != 0x300)
throw_error(bad_infile, "-f: Input file does not appear to be an FNT at all.");
current_font = parse_fnt(in_file, 0);
fclose(in_file);
fonts[fonts_loaded++] = current_font;
break;
case 't':
if (fonts_loaded > 0 && output_format != output_fontpack)
throw_error(bad_options, "-f: Cannot have multiple input fonts unless -o fontpack is specified first.");
if (fonts_loaded >= MAX_FONTS - 1)
throw_error(bad_options, "-f: Too many fonts. What on Earth makes you think your font pack needs so many fonts?");
recent_input_file_name = optarg;
if (verbosity >= 1)
printf("Processing input file %s . . .\n", recent_input_file_name);
in_file = fopen(recent_input_file_name, "r");
if (!in_file)
throw_error(bad_infile, "-f: Cannot open input file.");
current_font = parse_text(in_file, 0);
fclose(in_file);
fonts[fonts_loaded++] = current_font;
break;
case 'a':
if (current_font == NULL)
throw_error(bad_options, "-a: Must specify a font before specifying metrics.");
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > 64 || temp_n < 0)
throw_error(bad_options, "-a: Number too large or small.");
current_font->space_above = (uint8_t)temp_n;
break;
case 'b':
if (current_font == NULL)
throw_error(bad_options, "-b: Must specify a font before specifying metrics.");
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > 64 || temp_n < 0)
throw_error(bad_options, "-b: Number too large or small.");
current_font->space_below = (uint8_t)temp_n;
break;
case 'i':
if (current_font == NULL)
throw_error(bad_options, "-i: Must specify a font before specifying metrics.");
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > 24 || temp_n < 0)
throw_error(bad_options, "-i: Number too large or small.");
current_font->italic_space_adjust = (uint8_t)temp_n;
break;
case 'w':
if (current_font == NULL)
throw_error(bad_options, "-w: Must specify a font before specifying metrics.");
temp_n = check_string_for_value(optarg, &weights);
if (temp_n == -1) {
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > 255 || temp_n < 0)
throw_error(bad_options, "-w: Number too large or small.");
}
current_font->weight = (uint8_t)temp_n;
break;
case 's':
if (current_font == NULL)
throw_error(bad_options, "-s: Must specify a font before specifying metrics.");
temp_n = check_string_for_value(optarg, &styles);
if (temp_n == -1) {
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > 255 || temp_n < 0)
throw_error(bad_options, "-s: Number too large or small.");
}
current_font->style |= (uint8_t)temp_n;
break;
case 'c':
if (current_font == NULL)
throw_error(bad_options, "-c: Must specify a font before specifying metrics.");
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > current_font->height || temp_n < 0)
throw_error(bad_options, "-c: Number too large or small.");
current_font->cap_height = (uint8_t)temp_n;
break;
case 'x':
if (current_font == NULL)
throw_error(bad_options, "-x: Must specify a font before specifying metrics.");
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > current_font->height || temp_n < 0)
throw_error(bad_options, "-x: Number too large or small.");
current_font->x_height = (uint8_t)temp_n;
break;
case 'l':
if (current_font == NULL)
throw_error(bad_options, "-l: Must specify a font before specifying metrics.");
temp_n = (int)strtol(optarg, NULL, 0);
if (temp_n > current_font->height || temp_n < 0)
throw_error(bad_options, "-l: Number too large or small.");
current_font->baseline_height = (uint8_t)temp_n;
break;
case 'N':
if (output_format != output_fontpack)
throw_error(bad_options, "-N: Must specify font pack output format.");
if (font_pack_name != NULL)
throw_error(bad_options, "-N: Duplicate.");
if ((strl = strlen(optarg)) >= 4096)
throw_error(bad_options, "-N: Way too long a string!");
else if (strl > 255)
printf("-N: Recommend against such a long string.\n");
font_pack_name = optarg;
break;
case 'A':
if (output_format != output_fontpack)
throw_error(bad_options, "-A: Must specify font pack output format.");
if (author != NULL)
throw_error(bad_options, "-A: Duplicate.");
if ((strl = strlen(optarg)) >= 4096)
throw_error(bad_options, "-A: Way too long a string!");
else if (strl > 255)
printf("-A: Recommend against such a long string. You are not an aristocrat.\n");
author = optarg;
break;
case 'C':
if (output_format != output_fontpack)
throw_error(bad_options, "-C: Must specify font pack output format.");
if (pseudocopyright != NULL)
throw_error(bad_options, "-C: Duplicate.");
if (strlen(optarg) > 255)
throw_error(bad_options, "-C: Screw the copyright lawyers. You don't need such a long copyright string.\n");
pseudocopyright = optarg;
break;
case 'D':
if (output_format != output_fontpack)
throw_error(bad_options, "-D: Must specify font pack output format.");
if (description != NULL)
throw_error(bad_options, "-D: Duplicate.");
if ((strl = strlen(optarg)) >= 4096)
throw_error(bad_options, "-D: Way too long a string!");
else if (strl > 255)
printf("-D: Recommend against such a long string. (It's called the \"description\" field, not \"dissertation\"!)\n");
description = optarg;
break;
case 'V':
if (output_format != output_fontpack)
throw_error(bad_options, "-V: Must specify font pack output format.");
if (version != NULL)
throw_error(bad_options, "-V: Duplicate.");
if ((strl = strlen(optarg)) >= 4096)
throw_error(bad_options, "-V: Way too long a string!");
else if (strl > 255)
printf("-V: Recommend against such a long string. (It's called the version field, not the changelog!)\n");
version = optarg;
break;
case 'P':
if (output_format != output_fontpack)
throw_error(bad_options, "-P: Must specify font pack output format.");
if (codepage != NULL)
throw_error(bad_options, "-P: Duplicate.");
if (strlen(optarg) > 255)
printf("-P: Strongly recommend against such a long string. (What, are you trying to embed a complete Unicode translation table?)\n");
codepage = optarg;
break;
case '?':
throw_error(bad_options, "Unknown option; check syntax.");
break;
}
}
if (optind == argc)
throw_error(bad_options, "Last parameter must be output file name; none was given.");
output_file_name = argv[optind];
if (optind < argc - 1)
throw_error(bad_options, "Too many trailing parameters.");
if (current_font == NULL)
throw_error(bad_options, "No input font(s) given. . . . Nothing to do.");
/* Now write output */
format_c_array_data_t c_array_data;
out_file = fopen(output_file_name, "wb");
if (!out_file)
throw_error(bad_outfile, "Cannot open output file.");
if (output_format == output_fontpack) {
/* Write header */
for (char *s = "FONTPACK"; *s != '\0'; s++)
fputc(*s, out_file);
/* Offset to metadata */
int location = 12 + fonts_loaded * 3;
int mdlocation = 0;
bool no_metadata = font_pack_name == NULL && author == NULL && pseudocopyright == NULL && description == NULL && version == NULL && codepage == NULL;
if (no_metadata)
output_ezword(0, output_format_byte, out_file);
else {
output_ezword(location, output_format_byte, out_file);
location += MEATADATA_STRUCT_SIZE;
mdlocation = location;
if (font_pack_name != NULL)
location += (int)strlen(font_pack_name) + 1;
if (author != NULL)
location += (int)strlen(author) + 1;
if (pseudocopyright != NULL)
location += (int)strlen(pseudocopyright) + 1;
if (description != NULL)
location += (int)strlen(description) + 1;
if (version != NULL)
location += (int)strlen(version) + 1;
if (codepage != NULL)
location += (int)strlen(codepage) + 1;
}
/* Font count */
fputc(fonts_loaded, out_file);
/* Fonts table */
for (int i = 0; i < fonts_loaded; location += compute_font_size(fonts[i++]))
output_ezword(location, output_format_byte, out_file);
if (location >= MAX_APPVAR_SIZE) {
fclose(out_file);
remove(argv[optind]);
throw_error(bad_options, "Cannot form appvar; output appvar size would exceed 64 K appvar size limit.");
}
/* Serialize font metadata */
if (!no_metadata) {
output_ezword(MEATADATA_STRUCT_SIZE, output_format_byte, out_file);
if (font_pack_name != NULL) {
output_ezword(mdlocation, output_format_byte, out_file);
mdlocation += (int)strlen(font_pack_name) + 1;
} else
output_ezword(0, output_format_byte, out_file);
if (author != NULL) {
output_ezword(mdlocation, output_format_byte, out_file);
mdlocation += (int)strlen(author) + 1;
} else
output_ezword(0, output_format_byte, out_file);
if (pseudocopyright != NULL) {
output_ezword(mdlocation, output_format_byte, out_file);
mdlocation += (int)strlen(pseudocopyright) + 1;
} else
output_ezword(0, output_format_byte, out_file);
if (description != NULL) {
output_ezword(mdlocation, output_format_byte, out_file);
mdlocation += (int)strlen(description) + 1;
} else
output_ezword(0, output_format_byte, out_file);
if (version != NULL) {
output_ezword(mdlocation, output_format_byte, out_file);
mdlocation += (int)strlen(version) + 1;
} else
output_ezword(0, output_format_byte, out_file);
if (codepage != NULL) {
output_ezword(mdlocation, output_format_byte, out_file);
mdlocation += (int)strlen(codepage) + 1;
} else
output_ezword(0, output_format_byte, out_file);
if (font_pack_name != NULL)
write_string(font_pack_name, out_file);
if (author != NULL)
write_string(author, out_file);
if (pseudocopyright != NULL)
write_string(pseudocopyright, out_file);
if (description != NULL)
write_string(description, out_file);
if (version != NULL)
write_string(version, out_file);
if (codepage != NULL)
write_string(codepage, out_file);
}
for (int i = 0; i < fonts_loaded; i++) {
current_font = fonts[i];
serialize_font(current_font, output_format_byte, out_file);
fonts[i] = NULL;
free_fnt(current_font);
}
current_font = NULL;
} else {
switch (output_format) {
case output_c_array:
c_array_data.file = out_file;
c_array_data.row_counter = 0;
c_array_data.first_line = true;
serialize_font(current_font, output_format_c_array, &c_array_data);
print_newline(out_file);
break;
case output_asm_array:
fprintf(out_file, ".header:"); print_newline(out_file);
fprintf(out_file, "\tdb\t0 ; font format version"); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; height", current_font->height); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; glyph count", current_font->total_glyphs & 0xFF); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; first glyph", current_font->first_glyph); print_newline(out_file);
fprintf(out_file, "\tdl\t.widthsTable - .header ; offset to widths table"); print_newline(out_file);
fprintf(out_file, "\tdl\t.bitmapsTable - .header ; offset to bitmaps offsets table"); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; italics space adjust", current_font->italic_space_adjust); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; suggested blank space above", current_font->space_above); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; suggested blank space below", current_font->space_below); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; weight (boldness/thinness)", current_font->weight); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; style field", current_font->style); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; capital height", current_font->cap_height); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; lowercase x height", current_font->x_height); print_newline(out_file);
fprintf(out_file, "\tdb\t%i ; baseline height", current_font->baseline_height); print_newline(out_file);
fprintf(out_file, ".widthsTable: ; start of widths table"); print_newline(out_file);
for (int i = 0; i < current_font->total_glyphs; i++) {
fprintf(out_file, "\tdb\t%i ; Code point $%02X %c", current_font->widths_table[i], i + current_font->first_glyph, i + current_font->first_glyph);
print_newline(out_file);
}
fprintf(out_file, ".bitmapsTable: ; start of table of offsets to bitmaps"); print_newline(out_file);
for (int i = 0; i < current_font->total_glyphs; i++) {
int width = current_font->widths_table[i];
fprintf(out_file, "\tdw\t.glyph_%02X - .header", i + current_font->first_glyph);
if (width <= 16)
fprintf(out_file, " - %i", 3 - byte_columns(width));
fprintf(out_file, "; %c", i + current_font->first_glyph);
print_newline(out_file);
}
for (int i = 0; i < current_font->total_glyphs; i++) {
int width = current_font->widths_table[i];
int bwidth = byte_columns(width);
fprintf(out_file, ".glyph_%02X: ; %c", i + current_font->first_glyph, i + current_font->first_glyph); print_newline(out_file);
int byte = 0;
for (int row = 0; row < current_font->height; row++) {
switch (bwidth) {
case 1:
fprintf(out_file, "\tdb\t");
break;
case 2:
fprintf(out_file, "\tdw\t");
break;
case 3:
fprintf(out_file, "\tdl\t");
break;
default:
throw_error(internal_error, "byte_columns failed to give 1, 2, or 3. That should not happen.");
break;
}
/* The format requires omitting the least-significant byte(s) if they're unused. */
for (int col = 0; col < bwidth; col++) {
int b = current_font->bitmaps[i]->bytes[byte++];
for (int bit = 0; bit < 8; bit++, b <<= 1)
if (b & 0x80)
fprintf(out_file, "1");
else
fprintf(out_file, "0");
}
fprintf(out_file, "b");
print_newline(out_file);
}
}
break;
case output_binary_blob:
serialize_font(current_font, output_format_byte, out_file);
break;
case output_unspecified:
throw_error(bad_options, "-o: No output format specified.");
break;
default:
throw_error(internal_error, "-o: Someone attempted to add a new output format without actually coding it.");
break;
}
free_fnt(current_font);
current_font = NULL;
fonts[0] = NULL;
}
printf("Output size: %li bytes; conversion finished.\n", ftell(out_file));
fclose(out_file);
return 0;
}