From: Jean-Philippe Bruyère Date: Sun, 26 Dec 2021 00:38:00 +0000 (+0100) Subject: vkvg_load_font_from_path, usefull if run without fontconfig X-Git-Tag: v0.3.0-beta~68 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=10ad80c0939a63f47241cef0148267c95e0acf8a;p=jp%2Fvkvg.git vkvg_load_font_from_path, usefull if run without fontconfig --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 4209246..bbb80be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -209,8 +209,6 @@ FUNCTION (setup_lib LibName) TARGET_INCLUDE_DIRECTORIES(${LibName} PRIVATE ${Vulkan_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} - ${HarfBuzz_INCLUDE_DIRS} - ${Fontconfig_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/vkh/include @@ -219,10 +217,16 @@ FUNCTION (setup_lib LibName) TARGET_LINK_LIBRARIES(${LibName} ${Vulkan_LIBRARIES} ${FREETYPE_LIBRARIES} - ${HarfBuzz_LIBRARIES} - ${Fontconfig_LIBRARIES} vkh_static ) + IF (VKVG_USE_HARFBUZZ) + TARGET_INCLUDE_DIRECTORIES(${LibName} PRIVATE + ${HarfBuzz_INCLUDE_DIRS} + ) + TARGET_LINK_LIBRARIES(${LibName} + ${HarfBuzz_LIBRARIES} + ) + ENDIF () IF (VKVG_USE_FONTCONFIG) TARGET_INCLUDE_DIRECTORIES(${LibName} PRIVATE ${Fontconfig_INCLUDE_DIRS} diff --git a/include/vkvg.h b/include/vkvg.h index 1e0c58f..da8bc6a 100644 --- a/include/vkvg.h +++ b/include/vkvg.h @@ -1368,10 +1368,11 @@ void vkvg_select_font_face (VkvgContext ctx, const char* name); * @brief Select a new font by providing its file path. * * @param ctx a valid vkvg @ref context - * @param name A valid font file path. + * @param path A valid font file path. + * @param name A short name to select this font afteward */ vkvg_public -void vkvg_select_font_path (VkvgContext ctx, const char* path); +void vkvg_load_font_from_path (VkvgContext ctx, const char* path, const char *name); /** * @brief * diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 5ce32a8..c6f495b 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -920,10 +920,11 @@ void vkvg_select_font_face (VkvgContext ctx, const char* name){ return; _select_font_face (ctx, name); } -void vkvg_select_font_path (VkvgContext ctx, const char* path){ +void vkvg_load_font_from_path (VkvgContext ctx, const char* path, const char* name){ if (ctx->status) return; - //_select_font_path (ctx, path); + _add_new_font_identity(ctx, path, name); + _select_font_face (ctx, name); } void vkvg_set_font_size (VkvgContext ctx, uint32_t size){ if (ctx->status) diff --git a/src/vkvg_fonts.c b/src/vkvg_fonts.c index 1bfee07..c2e82b6 100644 --- a/src/vkvg_fonts.c +++ b/src/vkvg_fonts.c @@ -189,10 +189,10 @@ void _destroy_font_cache (VkvgDevice dev){ } free (f->sizes); free(f->fontFile); - for (uint32_t j = 0; j < f->fcNamesCount; j++) - free (f->fcNames[j]); - if (f->fcNamesCount > 0) - free (f->fcNames); + for (uint32_t j = 0; j < f->namesCount; j++) + free (f->names[j]); + if (f->namesCount > 0) + free (f->names); } free(cache->fonts); @@ -333,8 +333,29 @@ _char_ref* _prepare_char (VkvgDevice dev, _vkvg_font_t* f, FT_UInt gindex){ dev->fontCache->stagingX += bmpWidth; return cr; } -void _select_font_path (VkvgContext ctx, const char* fontFile){ - ctx->currentFont = NULL; +void _font_add_name (_vkvg_font_identity_t* font, const char* name, int nameLength) { + if (++font->namesCount == 1) + font->names = (char**) malloc (sizeof(char*)); + else + font->names = (char**) realloc (font->names, font->namesCount * sizeof(char*)); + + font->names[font->namesCount-1] = (char*)calloc(nameLength, sizeof (char)); + strcpy (font->names[font->namesCount-1], name); +} +void _add_new_font_identity (VkvgContext ctx, const char* fontFile, const char* name){ + _font_cache_t* cache = (_font_cache_t*)ctx->pSurf->dev->fontCache; + if (++cache->fontsCount == 1) + cache->fonts = (_vkvg_font_identity_t*) malloc (cache->fontsCount * sizeof(_vkvg_font_identity_t)); + else + 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; + nf.fontFile = (char*)malloc (fflength * sizeof(char)); + strcpy (nf.fontFile, fontFile); + _font_add_name (&nf, name, fflength); + + cache->fonts[cache->fontsCount-1] = nf; } //select current font for context void _select_font_face (VkvgContext ctx, const char* name){ @@ -344,15 +365,6 @@ void _select_font_face (VkvgContext ctx, const char* name){ ctx->currentFont = NULL; ctx->currentFontSize = NULL; } -void _font_add_fc_name (_vkvg_font_identity_t* font, const char* fcname, int nameLength) { - if (++font->fcNamesCount == 1) - font->fcNames = (char**) malloc (sizeof(char*)); - else - font->fcNames = (char**) realloc (font->fcNames, font->fcNamesCount * sizeof(char*)); - - font->fcNames[font->fcNamesCount-1] = (char*)calloc(nameLength, sizeof (char)); - strcpy (font->fcNames[font->fcNamesCount-1], fcname); -} _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx, _vkvg_font_identity_t* font, FT_F26Dot6 charSize) { for (uint32_t i = 0; i < font->sizeCount; ++i) { if (font->sizes[i].charSize == charSize) @@ -368,7 +380,7 @@ _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx, _vkvg_font_identity_t* font->sizes = (_vkvg_font_t*) realloc (font->sizes, font->sizeCount * sizeof(_vkvg_font_t)); _vkvg_font_t newSize = {.charSize = charSize}; - FT_CHECK_RESULT(FT_New_Face(cache->library, font->fontFile, 0, &newSize.face)); + FT_CHECK_RESULT(FT_New_Face (cache->library, font->fontFile, 0, &newSize.face)); FT_CHECK_RESULT(FT_Set_Char_Size(newSize.face, 0, newSize.charSize, dev->hdpi, dev->vdpi )); #ifdef VKVG_USE_HARFBUZZ @@ -393,8 +405,8 @@ _vkvg_font_t* _find_or_create_font_size (VkvgContext ctx, _vkvg_font_identity_t* _vkvg_font_identity_t* _tryFindFontByName (VkvgContext ctx){ _font_cache_t* cache = (_font_cache_t*)ctx->pSurf->dev->fontCache; for (int i = 0; i < cache->fontsCount; ++i) { - for (uint32_t j = 0; j < cache->fonts[i].fcNamesCount; j++) { - if (strcmp (cache->fonts[i].fcNames[j], ctx->selectedFontName) == 0) + for (uint32_t j = 0; j < cache->fonts[i].namesCount; j++) { + if (strcmp (cache->fonts[i].names[j], ctx->selectedFontName) == 0) return &cache->fonts[i]; } } @@ -419,28 +431,14 @@ _vkvg_font_identity_t* _tryResolveFontNameWithFontConfig (VkvgContext ctx) { for (int i = 0; i < cache->fontsCount; ++i) { if (strcmp (cache->fonts[i].fontFile, fontFile) == 0) { int fflength = strlen(fontFile) + 1; - _font_add_fc_name (&cache->fonts[i], ctx->selectedFontName, fflength); + _font_add_name (&cache->fonts[i], ctx->selectedFontName, fflength); resolvedFont = &cache->fonts[i]; break;; } } if (!resolvedFont) { //if not found, create a new vkvg_font - cache->fontsCount++; - - if (cache->fontsCount == 1) - cache->fonts = (_vkvg_font_identity_t*) malloc (cache->fontsCount * sizeof(_vkvg_font_identity_t)); - else - 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; - nf.fontFile = (char*)malloc (fflength * sizeof(char)); - strcpy (nf.fontFile, fontFile); - _font_add_fc_name (&nf, ctx->selectedFontName, fflength); - - cache->fonts[cache->fontsCount-1] = nf; + _add_new_font_identity(cache, fontFile, ctx->selectedFontName); resolvedFont = &cache->fonts[cache->fontsCount-1]; } } @@ -466,8 +464,8 @@ void _update_current_font (VkvgContext ctx) { if (ctx->selectedFontName[0] == 0) _select_font_face (ctx, "sans"); - ctx->currentFont = _find_or_create_font (ctx); - ctx->currentFontSize = _find_or_create_font_size (ctx, ctx->currentFont, ctx->selectedCharSize); + ctx->currentFont = _find_or_create_font (ctx); + ctx->currentFontSize = _find_or_create_font_size (ctx, ctx->currentFont, ctx->selectedCharSize); } } @@ -545,6 +543,7 @@ void _create_text_run (VkvgContext ctx, const char* text, VkvgText textRun) { #else int textByteLength = strlen (text); if (textByteLength > 0) { + setlocale(LC_ALL, ""); size_t wsize = mbstowcs(NULL, text, 0); wchar_t *tmp = (wchar_t*)malloc((wsize+1) * sizeof (wchar_t)); textRun->glyph_count = mbstowcs (tmp, text, wsize); diff --git a/src/vkvg_fonts.h b/src/vkvg_fonts.h index 02b0a7f..4d78c76 100644 --- a/src/vkvg_fonts.h +++ b/src/vkvg_fonts.h @@ -91,11 +91,11 @@ typedef struct { /* Font identification structure */ typedef struct { - char** fcNames; /* Resolved Input names to this font by fontConfig */ - uint32_t fcNamesCount; /* Count of resolved names by fontConfig */ - char* fontFile; /* Font file full path*/ - uint32_t sizeCount; /* available font size loaded */ - _vkvg_font_t* sizes; /* loaded font size array */ + char** names; /* Resolved Input names to this font by fontConfig */ + uint32_t namesCount; /* Count of resolved names by fontConfig */ + char* fontFile; /* Font file full path*/ + uint32_t sizeCount; /* available font size loaded */ + _vkvg_font_t* sizes; /* loaded font size array */ }_vkvg_font_identity_t; // Font cache global structure, entry point for all font related operations. @@ -149,7 +149,7 @@ void _init_fonts_cache (VkvgDevice dev); void _destroy_font_cache (VkvgDevice dev); //Select current font for context from font name, create new font entry in cache if required void _select_font_face (VkvgContext ctx, const char* name); -void _select_font_path (VkvgContext ctx, const char* fontFile); +void _add_new_font_identity (VkvgContext ctx, const char* fontFile, const char *name); //Draw text void _show_text (VkvgContext ctx, const char* text); //Get text dimmensions diff --git a/tests/data/DancingScript-Regular.ttf b/tests/data/DancingScript-Regular.ttf new file mode 100644 index 0000000..fbb0a5e Binary files /dev/null and b/tests/data/DancingScript-Regular.ttf differ diff --git a/tests/data/DroidSansMono.ttf b/tests/data/DroidSansMono.ttf new file mode 100644 index 0000000..bdad1e3 Binary files /dev/null and b/tests/data/DroidSansMono.ttf differ diff --git a/tests/text.c b/tests/text.c index 9b2e952..fdd47d6 100644 --- a/tests/text.c +++ b/tests/text.c @@ -194,6 +194,20 @@ void simple_text () { print_boxed (ctx, "ANOTHER ONE TO CHECK..", 50,80,20); vkvg_destroy (ctx); } +void font_file_path () { + VkvgContext ctx = vkvg_create(surf); + + vkvg_set_source_rgb (ctx, 0, 0, 0); + vkvg_paint (ctx); + vkvg_set_source_rgb (ctx, 1, 1, 1); + vkvg_load_font_from_path (ctx, "data/DancingScript-Regular.ttf", "droid"); + print_boxed (ctx, "This is a test string!", 50,20,12); + print_boxed (ctx, "This is a test string!", 50,50,20); + print_boxed (ctx, "ANOTHER ONE TO CHECK..", 50,80,20); + print_boxed (ctx, "this is another string to check if ligature are well set", 10,120,20); + vkvg_destroy (ctx); +} + void random_size () { VkvgContext ctx = vkvg_create(surf); vkvg_clear(ctx); @@ -236,6 +250,7 @@ int main(int argc, char *argv[]) { no_test_size = true; //vkvg_log_level = VKVG_LOG_INFO; PERFORM_TEST (simple_text, argc, argv); + PERFORM_TEST (font_file_path, argc, argv); PERFORM_TEST (single_font_and_size, argc, argv); PERFORM_TEST (random_size, argc, argv); PERFORM_TEST (random_font_and_size, argc, argv);