86 lines
2.1 KiB
C
86 lines
2.1 KiB
C
|
|
||
|
#include <toml.h>
|
||
|
|
||
|
#include "util.h"
|
||
|
#include "font.h"
|
||
|
|
||
|
font_t font_create(char *name, char *filename) {
|
||
|
FILE *fp;
|
||
|
font_t font;
|
||
|
|
||
|
printf("Processing %s...\n", name);
|
||
|
|
||
|
fp = fopen(filename, "r");
|
||
|
|
||
|
if (!fp) {
|
||
|
char errbuf[200];
|
||
|
snprintf(errbuf, 200, "can't open %s: ", filename);
|
||
|
error(errbuf, strerror(errno));
|
||
|
}
|
||
|
toml_table_t *table = toml_parse_file(fp);
|
||
|
fclose(fp);
|
||
|
|
||
|
#define TOML_GET_KEY((var, key, func)) toml_datum_t *(var) = func(table, key);\
|
||
|
if (!var.ok) {\
|
||
|
error("key '" key "' missing from", filename); \
|
||
|
}
|
||
|
|
||
|
TOML_GET_KEY(name, "name", toml_string_in)
|
||
|
TOML_GET_KEY(image_filename, "filename", toml_string_in)
|
||
|
TOML_GET_KEY(bpp, "bits_per_pixel", toml_int_in)
|
||
|
TOML_GET_KEY(width, "width", toml_int_in)
|
||
|
TOML_GET_KEY(height, "height", toml_int_in)
|
||
|
#undef TOML_GET_KEY
|
||
|
toml_table_t *mapping = toml_table_in(table, "map")
|
||
|
if (!mapping.ok) {
|
||
|
error("map missing for font ", font);
|
||
|
}
|
||
|
|
||
|
font.name = name.u.s;
|
||
|
font.filename = image_filename.u.s;
|
||
|
font.bits_per_pixel = bpp;
|
||
|
font.width = width;
|
||
|
font.height = height;
|
||
|
font.map_entries = map_create(mapping, *map);
|
||
|
|
||
|
return font;
|
||
|
}
|
||
|
|
||
|
unsigned int map_create(toml_table_t *table, map_t **map) {
|
||
|
map_t *node;
|
||
|
map_t *next;
|
||
|
unsigned int nodes;
|
||
|
map_t *root = malloc(sizeof(map_t*));
|
||
|
toml_array_t *space = toml_array_in(table, " ");
|
||
|
toml_datum_t spcidx = toml_int_at(space, 0);
|
||
|
toml_datum_t spcwid = toml_int_at(space, 1);
|
||
|
if (!spcidx.ok) {
|
||
|
error("missing or inappropriate index for glyph: ", key);
|
||
|
}
|
||
|
if (!spcwid.ok) {
|
||
|
error("missing or inappropriate index for glyph: ", key);
|
||
|
}
|
||
|
node = root;
|
||
|
|
||
|
for (nodes = 1; ; nodes++) {
|
||
|
char *key = toml_key_in(table, nodes);
|
||
|
if (!key) break;
|
||
|
toml_array_t *val = toml_array_in(table, key);
|
||
|
toml_datum_t index = toml_int_at(val, 0);
|
||
|
toml_datum_t width = toml_int_at(val, 1);
|
||
|
if (!index.ok) {
|
||
|
error("missing or inappropriate index for glyph: ", key);
|
||
|
}
|
||
|
if (!width.ok) {
|
||
|
error("missing or inappropriate width for glyph: ", key);
|
||
|
}
|
||
|
|
||
|
next = malloc(sizeof(map_t *));
|
||
|
next->glyph = key;
|
||
|
next->index = index.u.i;
|
||
|
next->width = width.u.i;
|
||
|
node->next = next;
|
||
|
node = next;
|
||
|
}
|
||
|
}
|