diff options
author | Michael Pavone <pavone@retrodev.com> | 2018-03-25 12:01:49 -0700 |
---|---|---|
committer | Michael Pavone <pavone@retrodev.com> | 2018-03-25 12:01:49 -0700 |
commit | a5d5c017589945008024f111c06894f5724d6c3b (patch) | |
tree | a06db423e81d53aee66cfa0b0ca366bd57d71dc8 /nuklear_ui/font.c | |
parent | 15af9462392967b6adf7ba6ff4f7ff778cf10eb3 (diff) | |
parent | b6feecb6c12566e9ea826cb67701008f9e423eea (diff) |
Merged nuklear_ui into default
Diffstat (limited to 'nuklear_ui/font.c')
-rw-r--r-- | nuklear_ui/font.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/nuklear_ui/font.c b/nuklear_ui/font.c new file mode 100644 index 0000000..006ae48 --- /dev/null +++ b/nuklear_ui/font.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include "../util.h" +#include "sfnt.h" + +char *default_font_path(void) +{ + FILE *fc_pipe = popen("fc-match -f '%{file}'", "r"); + if (!fc_pipe) { + return NULL; + } + size_t buf_size = 128; + char *buffer = NULL; + size_t total = 0, read = 0; + do { + total += read; + buf_size *= 2; + buffer = realloc(buffer, buf_size); + if (!buffer) { + return NULL; + } + read = fread(buffer, 1, buf_size - total, fc_pipe); + } while (read == (buf_size - total)); + total += read; + buffer[total] = 0; + + return buffer; +} + +uint8_t *default_font(uint32_t *size_out) +{ + char *path = default_font_path(); + if (!path) { + goto error; + } + FILE *f = fopen(path, "rb"); + if (!f) { + goto error; + } + long size = file_size(f); + uint8_t *buffer = malloc(size); + if (size != fread(buffer, 1, size, f)) { + fclose(f); + goto error; + } + fclose(f); + sfnt_container *sfnt = load_sfnt(buffer, size); + if (!sfnt) { + free(buffer); + goto error; + } + return sfnt_flatten(sfnt->tables, size_out); +error: + //TODO: try to find a suitable font in /usr/share/fonts as a fallback + return NULL; +}
\ No newline at end of file |