diff options
Diffstat (limited to 'libskrift_open_font.c')
-rw-r--r-- | libskrift_open_font.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/libskrift_open_font.c b/libskrift_open_font.c new file mode 100644 index 0000000..1eb8ff9 --- /dev/null +++ b/libskrift_open_font.c @@ -0,0 +1,50 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +int +libskrift_open_font(LIBSKRIFT_FONT **fontp, FILE *fp) +{ + int saved_errno; + char *mem = NULL, *new; + size_t size = 0, off = 0, r; + + *fontp = calloc(1, sizeof(**fontp)); + + saved_errno = errno; + errno = 0; + for (;;) { + if (off + 2048 > size) { + size += 8192; + new = realloc(mem, size); + if (!new) { + free(mem); + return -1; + } + mem = new; + } + r = fread(&mem[off], 1, size - off, fp); + if (!r) + break; + off += r; + } + if (errno) { + free(mem); + return -1; + } + + size = off; + new = realloc(mem, size); + if (new) + mem = new; + errno = saved_errno; + + if (libskrift_open_font_mem(fontp, mem, size)) { + free(mem); + return -1; + } + + (*fontp)->memory_free = mem; + (*fontp)->memory_size = size; + + return 0; +} |