Skip to content

Commit 069c715

Browse files
author
okay
committed
[kobo][text] add ability to embed font in rmkit binaries
by defining EMBED_FONT_H, its possible to embed a font into the generated binary. EMBED_FONT_H needs to include a ttf font and define two macros: EMBED_FONT_NAME and EMBED_FONT_LEN This reverts commit ea6a540.
1 parent 539df31 commit 069c715

File tree

3 files changed

+43222
-15
lines changed

3 files changed

+43222
-15
lines changed

src/rmkit/fb/stb_text.cpy

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,47 @@
66

77
#include "../defines.h"
88
#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
1017

1118

1219
#define FONT_SIZE 24
20+
#define FONT_BUFFER_SIZE 24<<20
1321
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]
1524
static stbtt_fontinfo font;
1625
extern bool did_setup = false
1726

1827
static void setup_font():
1928
if !did_setup:
2029
const char *filename = getenv("RMKIT_DEFAULT_FONT");
30+
bool embedded_font = false
2131
if filename == NULL:
2232
#ifdef REMARKABLE
2333
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";
2734
#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
2938
#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);
3839

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
3950
stbtt_InitFont(&font, font_buffer, 0);
4051
did_setup = true
4152

0 commit comments

Comments
 (0)