|
6 | 6 |
|
7 | 7 | #include "../defines.h" |
8 | 8 | #include "../../vendor/stb/stb_truetype.h" |
9 | | -#include "../assets.h" |
| 9 | + |
| 10 | +#if !defined(REMARKABLE) | defined(FONT_EMBED_H) |
| 11 | +#ifndef FONT_EMBED_H |
| 12 | +#define FONT_EMBED_H "../rmkit/font_embed.h" |
| 13 | +#endif |
| 14 | + |
| 15 | +#include FONT_EMBED_H |
| 16 | +#endif |
10 | 17 |
|
11 | 18 |
|
12 | 19 | #define FONT_SIZE 24 |
| 20 | +#define FONT_BUFFER_SIZE 24<<20 |
13 | 21 | namespace stbtext: |
14 | | - static unsigned char font_buffer[24<<20] |
| 22 | + // TODO: fix the max size read to prevent overflows (or just abort on really large files) |
| 23 | + static unsigned char font_buffer[FONT_BUFFER_SIZE] |
15 | 24 | static stbtt_fontinfo font; |
16 | 25 | extern bool did_setup = false |
17 | 26 |
|
18 | 27 | static void setup_font(): |
19 | 28 | if !did_setup: |
20 | 29 | const char *filename = getenv("RMKIT_DEFAULT_FONT"); |
| 30 | + bool embedded_font = false |
21 | 31 | if filename == NULL: |
22 | 32 | #ifdef REMARKABLE |
23 | 33 | filename = "/usr/share/fonts/ttf/noto/NotoMono-Regular.ttf"; |
24 | | - // TODO: fix the max size read to prevent overflows (or just abort on really large files) |
25 | | - #elif KOBO |
26 | | - filename = "/usr/local/rmkit/NotoMono-Regular.ttf"; |
27 | 34 | #else |
28 | | - filename = "src/vendor/NotoSansMono-Regular.ttf"; |
| 35 | + memcpy(font_buffer, FONT_EMBED_NAME, FONT_EMBED_LEN) |
| 36 | + font_buffer[FONT_EMBED_LEN] = 0 |
| 37 | + embedded_font = true |
29 | 38 | #endif |
30 | | - pass |
31 | | - |
32 | | - FILE * file = fopen(filename, "rb"); |
33 | | - if file == NULL: |
34 | | - debug "Unable to read font file: ", filename |
35 | | - return; |
36 | | - _ := fread(font_buffer, 1, 24<<20, file); |
37 | | - fclose(file); |
38 | 39 |
|
| 40 | + if filename: |
| 41 | + FILE * file = fopen(filename, "rb"); |
| 42 | + if file == NULL: |
| 43 | + debug "Unable to read font file: ", filename |
| 44 | + return; |
| 45 | + _ := fread(font_buffer, 1, FONT_BUFFER_SIZE, file); |
| 46 | + fclose(file); |
| 47 | + else if !embedded_font: |
| 48 | + debug "No font specified and no embedded font available!" |
| 49 | + return |
39 | 50 | stbtt_InitFont(&font, font_buffer, 0); |
40 | 51 | did_setup = true |
41 | 52 |
|
|
0 commit comments