From: Jean-Philippe Bruyère Date: Wed, 23 Feb 2022 15:16:31 +0000 (+0100) Subject: implement vkvg_load_font_from_memory X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=4cc9696fde5e78c7f398d21f10042e5e0669b200;p=jp%2Fvkvg.git implement vkvg_load_font_from_memory --- diff --git a/include/vkvg.h b/include/vkvg.h index de3dcd9..a068976 100644 --- a/include/vkvg.h +++ b/include/vkvg.h @@ -1497,6 +1497,16 @@ void vkvg_select_font_face (VkvgContext ctx, const char* name); */ vkvg_public void vkvg_load_font_from_path (VkvgContext ctx, const char* path, const char *name); +/** + * @brief Select a new font by providing a pointer on the font file loaded in memory and its size in byte. + * + * @param ctx a valid vkvg @ref context + * @param fontBuffer a pointer to a raw font file loaded in memory. + * @param fontBufferByteSize the size of the font buffer in bytes. + * @param name A short name to select this font afteward + */ +vkvg_public +void vkvg_load_font_from_memory (VkvgContext ctx, unsigned char* fontBuffer, long fontBufferByteSize, const char* name); /** * @brief * diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 868cc6f..a1e5f08 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -1109,7 +1109,18 @@ void vkvg_load_font_from_path (VkvgContext ctx, const char* path, const char* na if (ctx->status) return; RECORD(ctx, VKVG_CMD_SET_FONT_PATH, name); - _font_cache_add_font_identity(ctx, path, name); + _vkvg_font_identity_t* fid = _font_cache_add_font_identity(ctx, path, name); + _font_cache_load_font_file_in_memory (fid); + _select_font_face (ctx, name); +} +void vkvg_load_font_from_memory (VkvgContext ctx, unsigned char* fontBuffer, long fontBufferByteSize, const char* name) { + if (ctx->status) + return; + //RECORD(ctx, VKVG_CMD_SET_FONT_PATH, name); + _vkvg_font_identity_t* fid = _font_cache_add_font_identity (ctx, NULL, name); + fid->fontBuffer = fontBuffer; + fid->fontBufSize = fontBufferByteSize; + _select_font_face (ctx, name); } void vkvg_set_font_size (VkvgContext ctx, uint32_t size){ diff --git a/src/vkvg_fonts.c b/src/vkvg_fonts.c index 1847d99..ea2c489 100644 --- a/src/vkvg_fonts.c +++ b/src/vkvg_fonts.c @@ -378,7 +378,16 @@ void _font_add_name (_vkvg_font_identity_t* font, const char* name, int nameLeng font->names[font->namesCount-1] = (char*)calloc(nameLength, sizeof (char)); strcpy (font->names[font->namesCount-1], name); } -void _font_cache_add_font_identity (VkvgContext ctx, const char* fontFilePath, const char* name){ +void _font_cache_load_font_file_in_memory (_vkvg_font_identity_t* fontId) { + FILE* fontFile = fopen(fontId->fontFile, "rb"); + fseek(fontFile, 0, SEEK_END); + fontId->fontBufSize = ftell(fontFile); /* how long is the file ? */ + fseek(fontFile, 0, SEEK_SET); /* reset */ + fontId->fontBuffer = malloc(fontId->fontBufSize); + fread(fontId->fontBuffer, fontId->fontBufSize, 1, fontFile); + fclose(fontFile); +} +_vkvg_font_identity_t* _font_cache_add_font_identity (VkvgContext ctx, const char* fontFilePath, const char* name){ _font_cache_t* cache = (_font_cache_t*)ctx->dev->fontCache; if (++cache->fontsCount == 1) cache->fonts = (_vkvg_font_identity_t*) malloc (cache->fontsCount * sizeof(_vkvg_font_identity_t)); @@ -386,20 +395,16 @@ void _font_cache_add_font_identity (VkvgContext ctx, const char* fontFilePath, c cache->fonts = (_vkvg_font_identity_t*) realloc (cache->fonts, cache->fontsCount * sizeof(_vkvg_font_identity_t)); _vkvg_font_identity_t nf = {0}; - int fflength = strlen (fontFilePath) + 1; - nf.fontFile = (char*)malloc (fflength * sizeof(char)); - strcpy (nf.fontFile, fontFilePath); - _font_add_name (&nf, name, fflength); + if (fontFilePath) { + int fflength = strlen (fontFilePath) + 1; + nf.fontFile = (char*)malloc (fflength * sizeof(char)); + strcpy (nf.fontFile, fontFilePath); + } - FILE* fontFile = fopen(nf.fontFile, "rb"); - fseek(fontFile, 0, SEEK_END); - nf.fontBufSize = ftell(fontFile); /* how long is the file ? */ - fseek(fontFile, 0, SEEK_SET); /* reset */ - nf.fontBuffer = malloc(nf.fontBufSize); - fread(nf.fontBuffer, nf.fontBufSize, 1, fontFile); - fclose(fontFile); + _font_add_name (&nf, name, strlen (name)); cache->fonts[cache->fontsCount-1] = nf; + return &cache->fonts[cache->fontsCount-1]; } //select current font for context _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx) { @@ -480,7 +485,7 @@ bool _tryResolveFontNameWithFontConfig (VkvgContext ctx, _vkvg_font_identity_t** if (fontFile) { //try find font in cache by path for (int i = 0; i < cache->fontsCount; ++i) { - if (strcmp (cache->fonts[i].fontFile, fontFile) == 0) { + if (cache->fonts[i].fontFile && strcmp (cache->fonts[i].fontFile, fontFile) == 0) { int fflength = strlen(fontFile) + 1; _font_add_name (&cache->fonts[i], ctx->selectedFontName, fflength); *resolvedFont = &cache->fonts[i]; @@ -489,7 +494,8 @@ bool _tryResolveFontNameWithFontConfig (VkvgContext ctx, _vkvg_font_identity_t** } if (!*resolvedFont) { //if not found, create a new vkvg_font - _font_cache_add_font_identity(ctx, fontFile, ctx->selectedFontName); + _vkvg_font_identity_t* fid = _font_cache_add_font_identity(ctx, fontFile, ctx->selectedFontName); + _font_cache_load_font_file_in_memory (fid); *resolvedFont = &cache->fonts[cache->fontsCount-1]; } } diff --git a/src/vkvg_fonts.h b/src/vkvg_fonts.h index 8b45950..2b62848 100644 --- a/src/vkvg_fonts.h +++ b/src/vkvg_fonts.h @@ -189,7 +189,8 @@ typedef struct _vkvg_text_run_t { void _fonts_cache_create (VkvgDevice dev); //Release all ressources of font cache. void _font_cache_destroy (VkvgDevice dev); -void _font_cache_add_font_identity (VkvgContext ctx, const char* fontFile, const char *name); +_vkvg_font_identity_t *_font_cache_add_font_identity (VkvgContext ctx, const char* fontFile, const char *name); +void _font_cache_load_font_file_in_memory (_vkvg_font_identity_t* fontId); //Draw text void _font_cache_show_text (VkvgContext ctx, const char* text); //Get text dimmensions