Mercurial > repos > blastem
comparison nuklear_ui/font_web.c @ 2629:537a2f3b880d
Add font_web.c that was missed on a previous commit
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 23 Feb 2025 00:08:45 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2628:32ce6c588bc9 | 2629:537a2f3b880d |
---|---|
1 #include <stdint.h> | |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <stddef.h> | |
5 #include "sfnt.h" | |
6 #include "../util.h" | |
7 | |
8 uint8_t *default_font(uint32_t *size_out) | |
9 { | |
10 sfnt_container *sfnt = NULL; | |
11 FILE *f = fopen("DroidSans.ttf", "rb"); | |
12 if (!f) { | |
13 fprintf(stderr, "Failed to open font file DroidSans.ttf\n"); | |
14 return NULL; | |
15 } | |
16 long size = file_size(f); | |
17 uint8_t *buffer = malloc(size); | |
18 if (size != fread(buffer, 1, size, f)) { | |
19 fprintf(stderr, "Failed to read font file\n"); | |
20 goto cleanup; | |
21 } | |
22 fclose(f); | |
23 f = NULL; | |
24 sfnt = load_sfnt(buffer, size); | |
25 if (!sfnt) { | |
26 fprintf(stderr, "File does not contain SFNT resources\n"); | |
27 goto cleanup; | |
28 } | |
29 for (uint8_t j = 0; j < sfnt->num_fonts; j++) | |
30 { | |
31 if (sfnt_has_truetype_glyphs(sfnt->tables + j)) { | |
32 return sfnt_flatten(sfnt->tables + j, size_out); | |
33 } | |
34 fprintf(stderr, "Font %s in file doesn't have TrueType glyphs\n", sfnt_name(sfnt->tables + j, SFNT_POSTSCRIPT)); | |
35 } | |
36 cleanup: | |
37 if (sfnt) { | |
38 sfnt_free(sfnt); | |
39 } else { | |
40 free(buffer); | |
41 } | |
42 if (f) { | |
43 fclose(f); | |
44 } | |
45 return NULL; | |
46 } |