Compare commits

..

No commits in common. "fd80931624e5b342bed331dce2849c0ec8ad3efd" and "e549ce7332458655563c5702e84ea8ecf6545d05" have entirely different histories.

5 changed files with 24 additions and 57 deletions

View File

@ -10,7 +10,7 @@ libtoml_dep = dependency(
exe = executable( exe = executable(
'smeargle', 'smeargle',
['src/main.c', 'src/font.c'], 'src/main.c',
dependencies: libtoml_dep, dependencies: libtoml_dep,
install: true install: true
) )

View File

@ -4,29 +4,25 @@
#include "util.h" #include "util.h"
#include "font.h" #include "font.h"
font_t font_create(char *font_name, char *filename) { font_t font_create(char *name, char *filename) {
FILE *fp; FILE *fp;
font_t font; font_t font;
char errbuf[200];
map_t *map;
printf("Processing %s...\n", font_name); printf("Processing %s...\n", name);
fp = fopen(filename, "r"); fp = fopen(filename, "r");
if (!fp) { if (!fp) {
char str[200]; char errbuf[200];
snprintf(str, 200, "can't open %s: ", filename); snprintf(errbuf, 200, "can't open %s: ", filename);
error(str, strerror(errno)); error(errbuf, strerror(errno));
} }
toml_table_t *table = toml_parse_file(fp, errbuf, 200); toml_table_t *table = toml_parse_file(fp);
fclose(fp); fclose(fp);
#define TOML_GET_KEY(var, key, func) toml_datum_t (var) = func(table, (key));\ #define TOML_GET_KEY((var, key, func)) toml_datum_t *(var) = func(table, key);\
if (!(var).ok) {\ if (!var.ok) {\
char str[200];\ error("key '" key "' missing from", filename); \
snprintf(str, 200, "key '%s' missing from", (key)); \
error(str, filename); \
} }
TOML_GET_KEY(name, "name", toml_string_in) TOML_GET_KEY(name, "name", toml_string_in)
@ -35,25 +31,21 @@ font_t font_create(char *font_name, char *filename) {
TOML_GET_KEY(width, "width", toml_int_in) TOML_GET_KEY(width, "width", toml_int_in)
TOML_GET_KEY(height, "height", toml_int_in) TOML_GET_KEY(height, "height", toml_int_in)
#undef TOML_GET_KEY #undef TOML_GET_KEY
toml_table_t *mapping = toml_table_in(table, "map"); toml_table_t *mapping = toml_table_in(table, "map")
if (!mapping.ok) {
error("map missing for font ", font);
}
font.name = name.u.s; font.name = name.u.s;
font.toml_filename = image_filename.u.s; font.filename = image_filename.u.s;
font.bits_per_pixel = bpp.u.i; font.bits_per_pixel = bpp;
font.width = width.u.i; font.width = width;
font.height = height.u.i; font.height = height;
font.map_entries = map_create(mapping, &map); font.map_entries = map_create(mapping, *map);
font.map = map;
return font; return font;
} }
void font_destroy(font_t font) {
map_destroy(font.map);
free(font.name);
free(font.toml_filename);
}
unsigned int map_create(toml_table_t *table, map_t **map) { unsigned int map_create(toml_table_t *table, map_t **map) {
map_t *node; map_t *node;
map_t *next; map_t *next;
@ -63,15 +55,15 @@ unsigned int map_create(toml_table_t *table, map_t **map) {
toml_datum_t spcidx = toml_int_at(space, 0); toml_datum_t spcidx = toml_int_at(space, 0);
toml_datum_t spcwid = toml_int_at(space, 1); toml_datum_t spcwid = toml_int_at(space, 1);
if (!spcidx.ok) { if (!spcidx.ok) {
error("missing or inappropriate index for glyph: ' '", ""); error("missing or inappropriate index for glyph: ", key);
} }
if (!spcwid.ok) { if (!spcwid.ok) {
error("missing or inappropriate index for glyph: ' '", ""); error("missing or inappropriate index for glyph: ", key);
} }
node = root; node = root;
for (nodes = 1; ; nodes++) { for (nodes = 1; ; nodes++) {
const char *key = toml_key_in(table, nodes); char *key = toml_key_in(table, nodes);
if (!key) break; if (!key) break;
toml_array_t *val = toml_array_in(table, key); toml_array_t *val = toml_array_in(table, key);
toml_datum_t index = toml_int_at(val, 0); toml_datum_t index = toml_int_at(val, 0);
@ -90,18 +82,4 @@ unsigned int map_create(toml_table_t *table, map_t **map) {
node->next = next; node->next = next;
node = next; node = next;
} }
node->next = NULL;
*map = root;
return nodes;
}
void map_destroy(map_t *map) {
map_t *node = map;
map_t *next;
while ((next = node->next) != NULL) {
free(node);
node = next;
}
} }

View File

@ -1,7 +1,7 @@
typedef struct map_t { typedef struct map_t {
const char *glyph; char *glyph;
unsigned int index; unsigned int pos;
unsigned int width; unsigned int width;
struct map_t *next; struct map_t *next;
} map_t; } map_t;
@ -15,8 +15,3 @@ typedef struct {
unsigned int map_entries; unsigned int map_entries;
map_t *map; map_t *map;
} font_t; } font_t;
font_t font_create(char *name, char *filename);
void font_destroy(font_t font);
unsigned int map_create(toml_table_t *table, map_t **map);
void map_destroy(map_t *map);

View File

@ -6,8 +6,6 @@
#include <toml.h> #include <toml.h>
#include "util.h"
#define ERRBUFSZ 200 #define ERRBUFSZ 200
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -1,11 +1,7 @@
#include <sys/errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
static void error(const char *msg, const char *msg1) { static void error(const char *msg, const char *msg1) {
fprintf(stderr, "Error: %s%s\n", msg, msg1 ? msg1 : ""); fprintf(stderr, "Error: %s%s\n", msg, msg1 ? msg1 : "");
exit(1); exit(1);
} }