MCPcopy Create free account
hub / github.com/ErrorAtLine0/infinipaint / FontData

Method FontData

src/FontData.cpp:49–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47#include <filesystem>
48
49FontData::FontData()
50{
51
52#ifdef __EMSCRIPTEN__
53 defaultFontMgr = SkFontMgr_New_Custom_Directory("data/fonts");
54#elif __ANDROID__
55 {
56 std::vector<sk_sp<SkData>> fontFiles;
57 int globCount;
58 std::filesystem::path fontFolderPath("data/fonts");
59 char** filesInPath = SDL_GlobDirectory(fontFolderPath.c_str(), nullptr, 0, &globCount);
60 if(filesInPath) {
61 for(int i = 0; i < globCount; i++) {
62 std::filesystem::path filePath = fontFolderPath / std::filesystem::path(filesInPath[i]);
63 SDL_PathInfo fileInfo;
64 if(SDL_GetPathInfo(filePath.c_str(), &fileInfo) && fileInfo.type == SDL_PATHTYPE_FILE) {
65 std::string strData = read_file_to_string(filePath);
66 fontFiles.emplace_back(SkData::MakeWithCopy(strData.data(), strData.size()));
67 }
68 }
69 SDL_free(filesInPath);
70 defaultFontMgr = SkFontMgr_New_Custom_Data(SkSpan(fontFiles.data(), fontFiles.size()));
71 }
72 }
73 localFontMgr = SkFontMgr_New_AndroidNDK(true, SkFontScanner_Make_FreeType());
74#elif __APPLE__
75 for(const auto& dirEntry : std::filesystem::recursive_directory_iterator("data/fonts")) {
76 if(dirEntry.is_regular_file()) {
77 std::string urlStr = std::string(std::filesystem::canonical(dirEntry.path()).string());
78 CFURLRef fontURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(urlStr.c_str()), urlStr.size(), false);
79 CTFontManagerRegisterFontsForURL(fontURL, CTFontManagerScope::kCTFontManagerScopeProcess, nullptr);
80 CFRelease(fontURL);
81 }
82 }
83 defaultFontMgr = SkFontMgr_New_CoreText(nullptr);
84#elif _WIN32
85 localFontMgr = SkFontMgr_New_DirectWrite();
86 std::vector<std::wstring> fontPaths;
87 for(const auto& dirEntry : std::filesystem::recursive_directory_iterator(std::filesystem::path(L"data\\fonts"))) {
88 if(dirEntry.is_regular_file())
89 fontPaths.emplace_back(dirEntry.path().wstring());
90 }
91 fontSetManagerWindows.CreateFontSetUsingLocalFontFiles(fontPaths);
92 fontSetManagerWindows.CreateFontCollectionFromFontSet();
93
94 IDWriteFactory* fac = fontSetManagerWindows.IDWriteFactory5_IsAvailable() ? fontSetManagerWindows.m_dwriteFactory5.Get() : fontSetManagerWindows.m_dwriteFactory3.Get();
95 defaultFontMgr = SkFontMgr_New_DirectWrite(fac, fontSetManagerWindows.m_customFontCollection.Get());
96#else
97 localFontMgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType());
98 defaultFontMgr = SkFontMgr_New_Custom_Directory("data/fonts");
99#endif
100 map["Roboto"] = defaultFontMgr->makeFromFile("data/fonts/Roboto-variable.ttf");
101
102 collection = sk_make_sp<skia::textlayout::FontCollection>();
103 collection->setDefaultFontManager(defaultFontMgr, std::vector<SkString>{SkString{"Roboto"}, SkString{"Noto Emoji"}, SkString{"Amiri"}});
104 if(localFontMgr)
105 collection->setDynamicFontManager(localFontMgr);
106 std::string s = "🙂";

Callers

nothing calls this directly

Calls 11

read_file_to_stringFunction · 0.85
canonicalClass · 0.85
c_strMethod · 0.80
dataMethod · 0.45
sizeMethod · 0.45
stringMethod · 0.45
GetMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected