Displaying text works on emscripten version

This commit is contained in:
Yousef Khadadeh 2025-11-11 19:52:45 +03:00
parent cdbff3c043
commit b8fb00e218
6 changed files with 38 additions and 15 deletions

View file

@ -290,7 +290,7 @@ class ConanSkia(ConanFile):
self.options.enable_fontmgr_android = enabled self.options.enable_fontmgr_android = enabled
if self.options.enable_fontmgr_custom_directory == None: 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 self.options.enable_fontmgr_custom_directory = enabled
if self.options.enable_fontmgr_custom_embedded == None: if self.options.enable_fontmgr_custom_embedded == None:

View file

@ -1,5 +1,5 @@
#include "FontData.hpp" #include "FontData.hpp"
//#include <include/ports/SkFontMgr_directory.h> #include <include/ports/SkFontMgr_directory.h>
#include <include/ports/SkFontMgr_empty.h> #include <include/ports/SkFontMgr_empty.h>
#include <include/core/SkFontMetrics.h> #include <include/core/SkFontMetrics.h>
#include <include/core/SkTextBlob.h> #include <include/core/SkTextBlob.h>
@ -18,19 +18,24 @@
FontData::FontData() FontData::FontData()
{ {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
mgr = SkFontMgr_New_Custom_Empty(); localFontMgr = SkFontMgr_New_Custom_Empty();
#elif _WIN32 #elif _WIN32
mgr = SkFontMgr_New_GDI(); localFontMgr = SkFontMgr_New_GDI();
#elif __APPLE__ #elif __APPLE__
mgr = SkFontMgr_New_Custom_Empty(); localFontMgr = SkFontMgr_New_Custom_Empty();
#elif __linux__ #elif __linux__
mgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType()); localFontMgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType());
#else #else
mgr = SkFontMgr_New_Custom_Empty(); localFontMgr = SkFontMgr_New_Custom_Empty();
#endif #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<skia::textlayout::FontCollection>(); collection = sk_make_sp<skia::textlayout::FontCollection>();
collection->setDefaultFontManager(mgr, {SkString{"Roboto"}}); collection->setDefaultFontManager(defaultFontMgr, {SkString{"Roboto"}});
collection->setDynamicFontManager(localFontMgr);
} }
FontData::~FontData() { FontData::~FontData() {

View file

@ -10,7 +10,8 @@ using namespace Eigen;
struct FontData { struct FontData {
FontData(); FontData();
sk_sp<SkFontMgr> mgr; sk_sp<SkFontMgr> defaultFontMgr;
sk_sp<SkFontMgr> localFontMgr;
sk_sp<skia::textlayout::FontCollection> collection; sk_sp<skia::textlayout::FontCollection> collection;
std::unordered_map<std::string, sk_sp<SkTypeface>> map; std::unordered_map<std::string, sk_sp<SkTypeface>> map;
~FontData(); ~FontData();

View file

@ -5,6 +5,7 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <include/core/SkFont.h> #include <include/core/SkFont.h>
#include <include/core/SkFontMgr.h> #include <include/core/SkFontMgr.h>
#include <include/core/SkTypeface.h>
#include <modules/skparagraph/include/FontCollection.h> #include <modules/skparagraph/include/FontCollection.h>
#include <modules/svg/include/SkSVGDOM.h> #include <modules/svg/include/SkSVGDOM.h>
#include <Helpers/SCollision.hpp> #include <Helpers/SCollision.hpp>
@ -108,7 +109,6 @@ struct UpdateInputData {
SkFont get_font(float fSize) const; SkFont get_font(float fSize) const;
uint16_t fontSize = 16; uint16_t fontSize = 16;
sk_sp<SkTypeface> textTypeface;
sk_sp<skia::textlayout::FontCollection> fontCollection; sk_sp<skia::textlayout::FontCollection> fontCollection;
@ -116,7 +116,11 @@ struct UpdateInputData {
bool hoverObstructed = false; bool hoverObstructed = false;
bool acceptingTextInput = false; bool acceptingTextInput = false;
float deltaTime = 0.0f; float deltaTime = 0.0f;
sk_sp<SkFontMgr> textFontMgr;
sk_sp<SkFontMgr> textFontMgrLocal;
sk_sp<SkFontMgr> textFontMgrFallback;
sk_sp<SkTypeface> textTypeface;
DefaultStringArena* strArena; DefaultStringArena* strArena;
std::shared_ptr<Theme> theme; std::shared_ptr<Theme> theme;
std::unordered_map<std::string, sk_sp<SkSVGDOM>> svgData; std::unordered_map<std::string, sk_sp<SkSVGDOM>> svgData;

View file

@ -10,9 +10,21 @@
bool GUIStuff::FontPicker::update(UpdateInputData& io, std::string* fontName, GUIManager* gui) { bool GUIStuff::FontPicker::update(UpdateInputData& io, std::string* fontName, GUIManager* gui) {
if(sortedFontList.empty()) { if(sortedFontList.empty()) {
auto icu = SkUnicodes::ICU::Make(); 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; 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()); std::string familyName(familyNameSkString.c_str(), familyNameSkString.size());
sortedFontList.emplace_back(familyName); sortedFontList.emplace_back(familyName);
std::transform(familyName.begin(), familyName.end(), familyName.begin(), ::tolower); std::transform(familyName.begin(), familyName.end(), familyName.begin(), ::tolower);

View file

@ -39,7 +39,8 @@ Toolbar::Toolbar(MainProgram& initMain):
main(initMain) main(initMain)
{ {
io->textTypeface = main.fonts.map["Roboto"]; 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; io->fontCollection = main.fonts.collection;
load_default_palette(); load_default_palette();