diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2026-10-07 07:51:02 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2026-10-07 07:51:02 +0200 |
| commit | a49732e00946dc22018ff11c19eb6ee0b109b547 (patch) | |
| tree | 512db32c38dc21bdd7b50d7fd0f127c24887d1e1 | |
| parent | 0d91d23613163ff39c4395b59aef8f0ab9e9e7d7 (diff) | |
FontFace and FontBuffer arrays
| -rw-r--r-- | src/vkvg_fonts.c | 86 | ||||
| -rw-r--r-- | src/vkvg_fonts.h | 21 |
2 files changed, 56 insertions, 51 deletions
diff --git a/src/vkvg_fonts.c b/src/vkvg_fonts.c index df9b104..e8df2cc 100644 --- a/src/vkvg_fonts.c +++ b/src/vkvg_fonts.c @@ -115,7 +115,7 @@ void _fonts_cache_create(VkvgDevice dev, const char* fontDirs) { cache->hostBuff = (uint8_t*)malloc(buffLength); - cache->fontBuffers = array_create_vkvg_font_buffer_t(4); + cache->fontBuffers = array_create_FontBuffer(4); dev->fontCache = cache; } @@ -271,33 +271,35 @@ void _font_buffer_destroy (vkvg_font_face_t* face) { FT_Done_Face(face->face); mtx_destroy(&face->mutex); } -void _font_face_destroy (vkvg_font_face_t* face) { +void _font_face_destroy (FontFace face) { array_destroy_VkvgFont(&face->sizes); array_destroy_uint64_t(&face->queryHashes); FT_Done_Face(face->face); mtx_destroy(&face->mutex); - array_del_vkvg_font_face_t(&face->fontBuffer->faces, face->index); + array_del_FontFace(&face->fontBuffer->faces, face->index); + free(face); } void _font_cache_destroy(VkvgDevice dev) { _font_cache_t* cache = (_font_cache_t*)dev->fontCache; free(cache->hostBuff); - vkvg_font_buffer_t* buffs = cache->fontBuffers.elements; + FontBuffer* buffs = cache->fontBuffers.elements; for (int i = 0; i < cache->fontBuffers.count; ++i) { - vkvg_font_face_t* faces = buffs[i].faces.elements; - for (int j = 0; j < buffs[i].faces.count; ++j) { - VkvgFont *fonts = faces[j].sizes.elements; - for (int k = 0; k < faces[j].sizes.count; ++k) { + FontFace* faces = buffs[i]->faces.elements; + for (int j = 0; j < buffs[i]->faces.count; ++j) { + VkvgFont *fonts = faces[j]->sizes.elements; + for (int k = 0; k < faces[j]->sizes.count; ++k) { vkvg_font_destroy(fonts[k]); } // _font_face_destroy(&faces[j]); } - free(buffs[i].buffer); - array_destroy_vkvg_font_face_t(&buffs[i].faces); + free(buffs[i]->buffer); + array_destroy_FontFace(&buffs[i]->faces); + free(buffs[i]); } - array_destroy_vkvg_font_buffer_t(&cache->fontBuffers); + array_destroy_FontBuffer(&cache->fontBuffers); LOG(VKVG_LOG_INFO, "Font pages in cache: %d\n", cache->texPages.count); for (int i = 0; i < cache->texPages.count; ++i) { @@ -354,6 +356,7 @@ char_ref _prepare_char(VkvgDevice dev, VkvgText tr, uint32_t gindex) { FT_CHECK_RESULT(FT_Render_Glyph(f->face->glyph, FT_RENDER_MODE_LCD)); #else FT_CHECK_RESULT(FT_Load_Glyph(f->face->face, gindex, FT_LOAD_RENDER)); + #endif FT_GlyphSlot slot = f->face->face->glyph; @@ -470,16 +473,16 @@ FcPattern* _get_font_config_pattern(VkvgContext ctx, const char* fontQueryStr, . return request; } #define get_font_config_pattern(...) _get_font_config_pattern(__VA_ARGS__, NULL)*/ -bool tryGetFontFaceFromQueryHash(_font_cache_t* cache, uint64_t queryHash, vkvg_font_face_t **const face) { +bool tryGetFontFaceFromQueryHash(_font_cache_t* cache, uint64_t queryHash, FontFace *const face) { *face = NULL; - vkvg_font_buffer_t* buffs = cache->fontBuffers.elements; + FontBuffer* buffs = cache->fontBuffers.elements; for (int i = 0; i < cache->fontBuffers.count; ++i) { - vkvg_font_face_t* faces = buffs[i].faces.elements; - for (int j = 0; j < buffs[i].faces.count; ++j) { - uint64_t* queries = faces[j].queryHashes.elements; - for (int k = 0; k < faces[j].queryHashes.count; ++k) { + FontFace* faces = buffs[i]->faces.elements; + for (int j = 0; j < buffs[i]->faces.count; ++j) { + uint64_t* queries = faces[j]->queryHashes.elements; + for (int k = 0; k < faces[j]->queryHashes.count; ++k) { if (queries[k] == queryHash) { - *face = &faces[j]; + *face = faces[j]; return true; } } @@ -487,11 +490,11 @@ bool tryGetFontFaceFromQueryHash(_font_cache_t* cache, uint64_t queryHash, vkvg_ } return false; } -bool tryGetFontBufferFromPathHash(_font_cache_t* cache, uint64_t pathHash, vkvg_font_buffer_t **const fontBuff) { +bool tryGetFontBufferFromPathHash(_font_cache_t* cache, uint64_t pathHash, FontBuffer *const fontBuff) { *fontBuff = NULL; for (int i = 0; i < cache->fontBuffers.count; ++i) { - if (cache->fontBuffers.elements[i].fontPathHash == pathHash) { - *fontBuff = &cache->fontBuffers.elements[i]; + if (cache->fontBuffers.elements[i]->fontPathHash == pathHash) { + *fontBuff = cache->fontBuffers.elements[i]; return true; } @@ -500,7 +503,7 @@ bool tryGetFontBufferFromPathHash(_font_cache_t* cache, uint64_t pathHash, vkvg_ } #ifdef VKVG_USE_FONTCONFIG -bool _tryResolveFontNameWithFontConfig(VkvgDevice dev, const char* fontString, vkvg_font_face_t **const face) { +bool _tryResolveFontNameWithFontConfig(VkvgDevice dev, const char* fontString, FontFace *const face) { _font_cache_t* cache = dev->fontCache; uint64_t queryHash = fnv1a_64_str(fontString); *face = NULL; @@ -527,49 +530,50 @@ bool _tryResolveFontNameWithFontConfig(VkvgDevice dev, const char* fontString, v if (tryGetFontBufferFromPathHash(cache, fontFileHash, &buffPtr)) { for (int f = 0; f < buffPtr->faces.count; ++f) { - if (buffPtr->faces.elements[f].face->face_index == index) { - *face = &buffPtr->faces.elements[f]; + if (buffPtr->faces.elements[f]->face->face_index == index) { + *face = buffPtr->faces.elements[f]; array_add_uint64_t (&(*face)->queryHashes, queryHash); return true; } } LOG(VKVG_LOG_INFO, "Font buffer without a face\n"); } else { - vkvg_font_buffer_t buff = {fontFileHash}; + buffPtr = (FontBuffer)calloc(1, sizeof(vkvg_font_buffer_t)); + buffPtr->fontPathHash = fontFileHash; FILE *file = fopen(fontFile, "rb"); if (!file) { LOG(VKVG_LOG_ERR, "Error opening font file: %s\n", fontFile); return false; } fseek(file, 0, SEEK_END); - buff.bufferSize = ftell(file); + buffPtr->bufferSize = ftell(file); fseek(file, 0, SEEK_SET); - buff.buffer = malloc(buff.bufferSize); - if (!buff.buffer) { + buffPtr->buffer = malloc(buffPtr->bufferSize); + if (!buffPtr->buffer) { LOG(VKVG_LOG_ERR, "Error opening font file: %s\n", fontFile); fclose(file); return false; } - unsigned long readBytes = fread(buff.buffer, 1, buff.bufferSize, file); + unsigned long readBytes = fread(buffPtr->buffer, 1, buffPtr->bufferSize, file); fclose(file); - if (readBytes != buff.bufferSize) { + if (readBytes != buffPtr->bufferSize) { return false; - } - buff.faces = array_create_vkvg_font_face_t(2); - array_add_vkvg_font_buffer_t(&cache->fontBuffers, buff); - buffPtr = &cache->fontBuffers.elements[cache->fontBuffers.count - 1]; + buffPtr->faces = array_create_FontFace(2); + array_add_FontBuffer(&cache->fontBuffers, buffPtr); } - vkvg_font_face_t newFace = { buffPtr->faces.count, buffPtr }; - newFace.queryHashes = array_create_uint64_t(2); - array_add_uint64_t(&newFace.queryHashes, queryHash); + FontFace newFace = (FontFace)calloc(1, sizeof(vkvg_font_face_t)); + newFace->index = buffPtr->faces.count; + newFace->fontBuffer = buffPtr; + newFace->queryHashes = array_create_uint64_t(2); + array_add_uint64_t(&newFace->queryHashes, queryHash); if (dev->threadAware) - mtx_init(&newFace.mutex, mtx_plain); + mtx_init(&newFace->mutex, mtx_plain); #ifdef VKVG_USE_FREETYPE - FT_Error res = FT_New_Memory_Face(cache->library, buffPtr->buffer, buffPtr->bufferSize, index, &newFace.face); + FT_Error res = FT_New_Memory_Face(cache->library, buffPtr->buffer, buffPtr->bufferSize, index, &newFace->face); if (res) { LOG(VKVG_LOG_ERR, "FreeType error 0x%x creating memory face in %s at line %d\n", res, __FILE__, __LINE__); return false; @@ -590,8 +594,8 @@ bool _tryResolveFontNameWithFontConfig(VkvgDevice dev, const char* fontString, v newSize.descent = roundf(newSize.scale * font->descent); newSize.lineGap = roundf(newSize.scale * font->lineGap); #endif - array_add_vkvg_font_face_t(&buffPtr->faces, newFace); - *face = &buffPtr->faces.elements[buffPtr->faces.count - 1]; + array_add_FontFace(&buffPtr->faces, newFace); + *face = newFace; return true; } FcPatternDestroy(request); diff --git a/src/vkvg_fonts.h b/src/vkvg_fonts.h index 5ac51d3..a643609 100644 --- a/src/vkvg_fonts.h +++ b/src/vkvg_fonts.h @@ -183,12 +183,13 @@ typedef struct _tex_page_t { CTOR_ARRAY(tex_page_t) -typedef struct _vkvg_font_buffer_t vkvg_font_buffer_t; +typedef struct _vkvg_font_buffer_t* FontBuffer; +typedef struct _vkvg_font_face_t* FontFace; /* Font identification structure */ -typedef struct { +typedef struct _vkvg_font_face_t{ uint32_t index; /* index in face list of containing font buffer */ - vkvg_font_buffer_t* fontBuffer; + FontBuffer fontBuffer; array_uint64_t queryHashes; #ifdef VKVG_USE_FREETYPE FT_Face face; /* FreeType face*/ @@ -204,16 +205,16 @@ typedef struct { #endif } vkvg_font_face_t; -CTOR_ARRAY(vkvg_font_face_t) +CTOR_ARRAY(FontFace) typedef struct _vkvg_font_buffer_t { uint64_t fontPathHash; size_t bufferSize; - unsigned char* buffer; - array_vkvg_font_face_t faces; + unsigned char* buffer; + array_FontFace faces; } vkvg_font_buffer_t; -CTOR_ARRAY(vkvg_font_buffer_t) +CTOR_ARRAY(FontBuffer) DIAGNOSTIC_RESTORE_UNUSED @@ -221,8 +222,8 @@ typedef struct _vkvg_font_t { vkvg_status_t status; atomic_int references; // reference count - vkvg_font_face_t *face; - uint32_t eltIndex; // index in face->sizes[] array + FontFace face; + uint32_t eltIndex; // index in face->sizes[] array #ifdef VKVG_USE_FREETYPE FT_F26Dot6 charSize; /* Font size in Point as fixed float 26.6 */ FT_Size ftSize; /* FT size rec */ @@ -267,7 +268,7 @@ typedef struct { VkFence uploadFence; /* Signaled when upload is finished */ mtx_t mutex; /* font cache global mutex, used only if device is in thread aware mode (see: vkvg_device_set_thread_aware) */ - array_vkvg_font_buffer_t fontBuffers; + array_FontBuffer fontBuffers; array_tex_page_t texPages; } _font_cache_t; |