From b8fb00e218a97b84a2bff1e12552ac58ee5e506a Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Tue, 11 Nov 2025 19:52:45 +0300 Subject: [PATCH] Displaying text works on emscripten version --- .../conan-skia/recipes/skia/all/conanfile.py | 2 +- src/FontData.cpp | 21 ++++++++++++------- src/FontData.hpp | 3 ++- src/GUIStuff/Elements/Element.hpp | 8 +++++-- src/GUIStuff/Elements/FontPicker.cpp | 16 ++++++++++++-- src/Toolbar.cpp | 3 ++- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/conan/conan-skia/recipes/skia/all/conanfile.py b/conan/conan-skia/recipes/skia/all/conanfile.py index f402f31..197d62f 100644 --- a/conan/conan-skia/recipes/skia/all/conanfile.py +++ b/conan/conan-skia/recipes/skia/all/conanfile.py @@ -290,7 +290,7 @@ class ConanSkia(ConanFile): self.options.enable_fontmgr_android = enabled if self.options.enable_fontmgr_custom_directory == None: - enabled = self.options.use_freetype and arch != "wasm" + enabled = self.options.use_freetype self.options.enable_fontmgr_custom_directory = enabled if self.options.enable_fontmgr_custom_embedded == None: diff --git a/src/FontData.cpp b/src/FontData.cpp index b39bb25..99f4125 100644 --- a/src/FontData.cpp +++ b/src/FontData.cpp @@ -1,5 +1,5 @@ #include "FontData.hpp" -//#include +#include #include #include #include @@ -18,19 +18,24 @@ FontData::FontData() { #ifdef __EMSCRIPTEN__ - mgr = SkFontMgr_New_Custom_Empty(); + localFontMgr = SkFontMgr_New_Custom_Empty(); #elif _WIN32 - mgr = SkFontMgr_New_GDI(); + localFontMgr = SkFontMgr_New_GDI(); #elif __APPLE__ - mgr = SkFontMgr_New_Custom_Empty(); + localFontMgr = SkFontMgr_New_Custom_Empty(); #elif __linux__ - mgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType()); + localFontMgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType()); #else - mgr = SkFontMgr_New_Custom_Empty(); + localFontMgr = SkFontMgr_New_Custom_Empty(); #endif - map["Roboto"] = mgr->makeFromFile("data/fonts/Roboto-variable.ttf"); + + defaultFontMgr = SkFontMgr_New_Custom_Directory("data/fonts"); + + map["Roboto"] = defaultFontMgr->makeFromFile("data/fonts/Roboto-variable.ttf"); + collection = sk_make_sp(); - collection->setDefaultFontManager(mgr, {SkString{"Roboto"}}); + collection->setDefaultFontManager(defaultFontMgr, {SkString{"Roboto"}}); + collection->setDynamicFontManager(localFontMgr); } FontData::~FontData() { diff --git a/src/FontData.hpp b/src/FontData.hpp index 1248b54..806fa49 100644 --- a/src/FontData.hpp +++ b/src/FontData.hpp @@ -10,7 +10,8 @@ using namespace Eigen; struct FontData { FontData(); - sk_sp mgr; + sk_sp defaultFontMgr; + sk_sp localFontMgr; sk_sp collection; std::unordered_map> map; ~FontData(); diff --git a/src/GUIStuff/Elements/Element.hpp b/src/GUIStuff/Elements/Element.hpp index e5694e9..0b43ccc 100644 --- a/src/GUIStuff/Elements/Element.hpp +++ b/src/GUIStuff/Elements/Element.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -108,7 +109,6 @@ struct UpdateInputData { SkFont get_font(float fSize) const; uint16_t fontSize = 16; - sk_sp textTypeface; sk_sp fontCollection; @@ -116,7 +116,11 @@ struct UpdateInputData { bool hoverObstructed = false; bool acceptingTextInput = false; float deltaTime = 0.0f; - sk_sp textFontMgr; + + sk_sp textFontMgrLocal; + sk_sp textFontMgrFallback; + sk_sp textTypeface; + DefaultStringArena* strArena; std::shared_ptr theme; std::unordered_map> svgData; diff --git a/src/GUIStuff/Elements/FontPicker.cpp b/src/GUIStuff/Elements/FontPicker.cpp index 9fe6803..afae1cc 100644 --- a/src/GUIStuff/Elements/FontPicker.cpp +++ b/src/GUIStuff/Elements/FontPicker.cpp @@ -10,9 +10,21 @@ bool GUIStuff::FontPicker::update(UpdateInputData& io, std::string* fontName, GUIManager* gui) { if(sortedFontList.empty()) { auto icu = SkUnicodes::ICU::Make(); - for(int i = 0; i < io.textFontMgr->countFamilies(); i++) { + for(int i = 0; i < io.textFontMgrLocal->countFamilies(); i++) { SkString familyNameSkString; - io.textFontMgr->getFamilyName(i, &familyNameSkString); + io.textFontMgrLocal->getFamilyName(i, &familyNameSkString); + if(familyNameSkString.isEmpty()) + continue; + std::string familyName(familyNameSkString.c_str(), familyNameSkString.size()); + sortedFontList.emplace_back(familyName); + std::transform(familyName.begin(), familyName.end(), familyName.begin(), ::tolower); + sortedFontListLowercase.emplace_back(familyName); + } + for(int i = 0; i < io.textFontMgrFallback->countFamilies(); i++) { + SkString familyNameSkString; + io.textFontMgrFallback->getFamilyName(i, &familyNameSkString); + if(familyNameSkString.isEmpty()) + continue; std::string familyName(familyNameSkString.c_str(), familyNameSkString.size()); sortedFontList.emplace_back(familyName); std::transform(familyName.begin(), familyName.end(), familyName.begin(), ::tolower); diff --git a/src/Toolbar.cpp b/src/Toolbar.cpp index bf88301..650809f 100644 --- a/src/Toolbar.cpp +++ b/src/Toolbar.cpp @@ -39,7 +39,8 @@ Toolbar::Toolbar(MainProgram& initMain): main(initMain) { io->textTypeface = main.fonts.map["Roboto"]; - io->textFontMgr = main.fonts.mgr; + io->textFontMgrLocal = main.fonts.localFontMgr; + io->textFontMgrFallback = main.fonts.defaultFontMgr; io->fontCollection = main.fonts.collection; load_default_palette();