aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/font.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-08 16:03:02 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-08 16:03:02 +0100
commitc4f84e1969f4c856a5bf0352e99fcb73a4cf56cf (patch)
tree989fbe845a051d10f750bb6baeb0b58823a611bd /src/graphics/font.c
parente3c90b41386986ef53e512994d6e2f7ceadfc177 (diff)
Do not check for valid utf8 in set text, render the invalid utf8 instead. Mmap fonts instead of loading the whole font
Diffstat (limited to 'src/graphics/font.c')
-rw-r--r--src/graphics/font.c39
1 files changed, 17 insertions, 22 deletions
diff --git a/src/graphics/font.c b/src/graphics/font.c
index 175c571..dfffc7e 100644
--- a/src/graphics/font.c
+++ b/src/graphics/font.c
@@ -26,16 +26,16 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c
self->packed_chars = NULL;
self->num_packed_chars = 0;
- mgl_filedata filedata;
- if(mgl_load_file(filepath, &filedata, NULL) != 0) {
+ mgl_memory_mapped_file mapped_file;
+ if(mgl_mapped_file_load(filepath, &mapped_file, &(mgl_memory_mapped_file_load_options){ .readable = true, .writable = false }) != 0) {
fprintf(stderr, "Error: failed to load font %s, error: mgl_load_file failed\n", filepath);
return -1;
}
stbtt_fontinfo font;
- if(!stbtt_InitFont(&font, filedata.data, stbtt_GetFontOffsetForIndex(filedata.data, 0))) {
+ if(!stbtt_InitFont(&font, mapped_file.data, stbtt_GetFontOffsetForIndex(mapped_file.data, 0))) {
fprintf(stderr, "Error: failed to load font %s, error: stbtt_InitFont failed\n", filepath);
- mgl_filedata_free(&filedata);
+ mgl_mapped_file_unload(&mapped_file);
return -1;
}
@@ -43,9 +43,7 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c
self->packed_chars = malloc(self->num_packed_chars * sizeof(stbtt_packedchar));
if(!self->packed_chars) {
fprintf(stderr, "Error: failed to load font %s, error: out of memory\n", filepath);
- mgl_filedata_free(&filedata);
- mgl_font_unload(self);
- return -1;
+ goto error;
}
bool atlas_created = false;
@@ -62,9 +60,7 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c
unsigned char *new_atlas = realloc(self->font_atlas.atlas, self->font_atlas.width * self->font_atlas.height);
if(!new_atlas) {
fprintf(stderr, "Error: failed to load font %s, error: out of memory\n", filepath);
- mgl_filedata_free(&filedata);
- mgl_font_unload(self);
- return -1;
+ goto error;
}
self->font_atlas.atlas = new_atlas;
@@ -72,19 +68,17 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c
if(!stbtt_PackBegin(&pc, self->font_atlas.atlas, self->font_atlas.width, self->font_atlas.height, self->font_atlas.width, 1, NULL)) {
fprintf(stderr, "Error: failed to load font %s, error: stbtt_PackBegin failed\n", filepath);
- mgl_filedata_free(&filedata);
- mgl_font_unload(self);
- return -1;
+ goto error;
}
stbtt_PackSetOversampling(&pc, 2, 2);
- if(!stbtt_PackFontRange(&pc, filedata.data, 0, self->character_size, 0, self->num_packed_chars, self->packed_chars)) {
+ if(!stbtt_PackFontRange(&pc, mapped_file.data, 0, self->character_size, 0, self->num_packed_chars, self->packed_chars)) {
stbtt_PackEnd(&pc);
continue;
}
- /*if(!stbtt_PackFontRange(&pc, filedata.data, 0, self->character_size, 0x00004E00, self->num_packed_chars, self->packed_chars)) {
+ /*if(!stbtt_PackFontRange(&pc, mapped_file.data, 0, self->character_size, 0x00004E00, self->num_packed_chars, self->packed_chars)) {
stbtt_PackEnd(&pc);
continue;
}*/
@@ -96,22 +90,23 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c
if(!atlas_created) {
fprintf(stderr, "Error: failed to load font %s, error: failed to create atlas\n", filepath);
- mgl_filedata_free(&filedata);
- mgl_font_unload(self);
- return -1;
+ goto error;
}
if(mgl_texture_load_from_memory(&self->texture, self->font_atlas.atlas, self->font_atlas.width, self->font_atlas.height, MGL_IMAGE_FORMAT_ALPHA, NULL) != 0) {
fprintf(stderr, "Error: failed to load font %s, error: mgl_texture_load_from_memory failed\n", filepath);
- mgl_filedata_free(&filedata);
- mgl_font_unload(self);
- return -1;
+ goto error;
}
/* TODO: Use stbtt_GetCodepointSDF */
- mgl_filedata_free(&filedata);
+ mgl_mapped_file_unload(&mapped_file);
return 0;
+
+ error:
+ mgl_mapped_file_unload(&mapped_file);
+ mgl_font_unload(self);
+ return -1;
}
void mgl_font_unload(mgl_font *self) {