diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2022-02-23 15:37:20 +0100 |
|---|---|---|
| committer | j-p <jp_bruyere@hotmail.com> | 2022-02-26 11:05:24 +0100 |
| commit | bdfe4b11b13eae31e8f3563dcda50d2edc708cba (patch) | |
| tree | 9c47e5135ed7e759d0ec2444067b7f9bcbd51500 /src | |
| parent | 19bc61899bac5837ad0445eab23d0d952e001ef7 (diff) | |
use font file buffer and FT_New_Memory_Face
Diffstat (limited to 'src')
| -rw-r--r-- | src/vkvg_fonts.c | 25 | ||||
| -rw-r--r-- | src/vkvg_fonts.h | 3 |
2 files changed, 14 insertions, 14 deletions
diff --git a/src/vkvg_fonts.c b/src/vkvg_fonts.c index 736e5e9..edbcfe5 100644 --- a/src/vkvg_fonts.c +++ b/src/vkvg_fonts.c @@ -251,9 +251,7 @@ void _font_cache_destroy (VkvgDevice dev){ free (f->names[j]); if (f->namesCount > 0) free (f->names); -#ifndef VKVG_USE_FREETYPE free (f->fontBuffer); -#endif } free(cache->fonts); @@ -380,7 +378,7 @@ 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* fontFile, const char* name){ +void _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)); @@ -388,11 +386,19 @@ void _font_cache_add_font_identity (VkvgContext ctx, const char* fontFile, const 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 (fontFile) + 1; + int fflength = strlen (fontFilePath) + 1; nf.fontFile = (char*)malloc (fflength * sizeof(char)); - strcpy (nf.fontFile, fontFile); + strcpy (nf.fontFile, fontFilePath); _font_add_name (&nf, name, fflength); + 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); + cache->fonts[cache->fontsCount-1] = nf; } //select current font for context @@ -400,13 +406,6 @@ _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx) { _vkvg_font_identity_t* font = ctx->currentFont; #ifndef VKVG_USE_FREETYPE if (!font->stbInfo.data) { - FILE* fontFile = fopen(font->fontFile, "rb"); - fseek(fontFile, 0, SEEK_END); - long ffsize = ftell(fontFile); /* how long is the file ? */ - fseek(fontFile, 0, SEEK_SET); /* reset */ - font->fontBuffer = malloc(ffsize); - fread(font->fontBuffer, ffsize, 1, fontFile); - fclose(fontFile); if (!stbtt_InitFont(&font->stbInfo, font->fontBuffer, 0)) printf("failed\n"); stbtt_GetFontVMetrics(&font->stbInfo, &font->ascent, &font->descent, &font->lineGap); @@ -426,7 +425,7 @@ _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx) { VkvgDevice dev = ctx->dev; #ifdef VKVG_USE_FREETYPE _font_cache_t* cache = (_font_cache_t*)ctx->dev->fontCache; - FT_CHECK_RESULT(FT_New_Face (cache->library, font->fontFile, 0, &newSize.face)); + FT_CHECK_RESULT(FT_New_Memory_Face (cache->library, font->fontBuffer, font->fontBufSize, 0, &newSize.face)); FT_CHECK_RESULT(FT_Set_Char_Size(newSize.face, 0, newSize.charSize, dev->hdpi, dev->vdpi )); newSize.charLookup = (_char_ref**)calloc (newSize.face->num_glyphs, sizeof(_char_ref*)); diff --git a/src/vkvg_fonts.h b/src/vkvg_fonts.h index d0a6dac..8b45950 100644 --- a/src/vkvg_fonts.h +++ b/src/vkvg_fonts.h @@ -112,9 +112,10 @@ typedef struct { typedef struct { char** names; /* Resolved Input names to this font by fontConfig or custom name set by @ref vkvg_load_from_path*/ uint32_t namesCount; /* Count of resolved names by fontConfig */ + unsigned char* fontBuffer; /* stb_truetype in memory buffer */ + long fontBufSize;/* */ char* fontFile; /* Font file full path*/ #ifndef VKVG_USE_FREETYPE - unsigned char* fontBuffer; /* stb_truetype in memory buffer */ stbtt_fontinfo stbInfo; /* stb_truetype structure */ int ascent; /* unscalled stb font metrics */ int descent; |