Skip to content

[feat] Add support for loading OpenType fonts from memory.#82

Open
notmarek wants to merge 5 commits intoNiLuJe:masterfrom
KindleModding:in-memory-fonts
Open

[feat] Add support for loading OpenType fonts from memory.#82
notmarek wants to merge 5 commits intoNiLuJe:masterfrom
KindleModding:in-memory-fonts

Conversation

@notmarek
Copy link

@notmarek notmarek commented Jan 17, 2025

This change is Reviewable

notmarek and others added 3 commits January 17, 2025 20:43
Co-authored-by: NiLuJe <ninuje@gmail.com>
Co-authored-by: NiLuJe <ninuje@gmail.com>
Co-authored-by: NiLuJe <ninuje@gmail.com>
fclose(f);
}
int result = add_ot_font_data(data, style, ot_fonts);
free(data);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a problem ;).

(The data is still live, stbtt doesn't make a copy, it just keeps a reference to that same pointer).

// Then, get the offset to the first font
const int fontoffset = stbtt_GetFontOffsetForIndex(data, 0);
if (fontoffset == -1) {
free(data);
Copy link
Owner

@NiLuJe NiLuJe Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the same vein, this needs to be free(font_info->data);

Which implies that we take ownership of the pointer, which might be a problem for your usecase (e.g., pointer to static content), so this may need to be more complex to add a way to say "nope, this data is immutable, don't ever try to free it".

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weeelll, except font_info->data only exists after stbtt_InitFont ;p.

The point about ownership still stands, though ;p.

// First, check if we can actually find a recognizable font format in the data...
const int fontcount = stbtt_GetNumberOfFonts(data);
if (fontcount == 0) {
free(data);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

stbtt_fontinfo* font_info = calloc(1U, sizeof(stbtt_fontinfo));
if (!font_info) {
PFWARN("Error allocating stbtt_fontinfo struct: %m");
free(data);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants