reimplement system fonts retrieval for Linux using fontconfig
This commit is contained in:
96
.vscode/settings.json
vendored
Normal file
96
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"csetjmp": "cpp",
|
||||
"csignal": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"*.ipp": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"thread": "cpp",
|
||||
"*.inc": "cpp",
|
||||
"any": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"hash_map": "cpp",
|
||||
"hash_set": "cpp",
|
||||
"strstream": "cpp",
|
||||
"barrier": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"bitset": "cpp",
|
||||
"cfenv": "cpp",
|
||||
"charconv": "cpp",
|
||||
"chrono": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"complex": "cpp",
|
||||
"concepts": "cpp",
|
||||
"coroutine": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"deque": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"regex": "cpp",
|
||||
"source_location": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"rope": "cpp",
|
||||
"slist": "cpp",
|
||||
"fstream": "cpp",
|
||||
"future": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"latch": "cpp",
|
||||
"limits": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ranges": "cpp",
|
||||
"scoped_allocator": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"shared_mutex": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"syncstream": "cpp",
|
||||
"typeindex": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"valarray": "cpp",
|
||||
"variant": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -5,17 +5,56 @@
|
||||
*/
|
||||
|
||||
#include "Host/SystemFontResolver.hpp"
|
||||
#include <filesystem>
|
||||
#include "Base/Logger.hpp"
|
||||
#include <fontconfig/fontconfig.h>
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
std::string SystemFontResolver::GetSystemFontPath(const std::string& fontName)
|
||||
{
|
||||
// fontName -> fontPath
|
||||
static std::map<std::string, std::string> fontFilesMapping = ReadSystemFonts();
|
||||
auto it = fontFilesMapping.find(fontName);
|
||||
if (it != fontFilesMapping.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> SystemFontResolver::ReadSystemFonts()
|
||||
{
|
||||
FcConfig* config = FcInitLoadConfigAndFonts();
|
||||
FcPattern* pat = FcPatternCreate();
|
||||
FcObjectSet* os = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, NULL);
|
||||
FcFontSet* fs = FcFontList(config, pat, os);
|
||||
if (!fs)
|
||||
{
|
||||
Logger::DATA->warn("Could not get system fonts");
|
||||
FcFontSetDestroy(fs);
|
||||
FcObjectSetDestroy(os);
|
||||
FcPatternDestroy(pat);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> fontFilesMapping;
|
||||
for (int i = 0; i < fs->nfont; ++i)
|
||||
{
|
||||
FcPattern* font = fs->fonts[i];
|
||||
FcChar8* file, *style, *family;
|
||||
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch &&
|
||||
FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch &&
|
||||
FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
|
||||
{
|
||||
fontFilesMapping[std::string(reinterpret_cast<char*>(family)) + " " +
|
||||
std::string(reinterpret_cast<char*>(style))]
|
||||
= std::string(reinterpret_cast<char*>(file));
|
||||
}
|
||||
}
|
||||
FcFontSetDestroy(fs);
|
||||
FcObjectSetDestroy(os);
|
||||
FcPatternDestroy(pat);
|
||||
FcConfigDestroy(config);
|
||||
return fontFilesMapping;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
*/
|
||||
|
||||
#include <catch2/catch_all.hpp>
|
||||
#include "Host/SystemFontResolver.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include "Host/SystemInfo.hpp"
|
||||
#include "Scene/Text/FontAtlasFactory.hpp"
|
||||
#include <filesystem>
|
||||
|
||||
using namespace OpenVulkano;
|
||||
@@ -17,12 +16,12 @@ TEST_CASE("Search system fonts")
|
||||
Logger::SetupLogger("", "tests.log");
|
||||
|
||||
// assume these fonts are present since they are default
|
||||
std::filesystem::path path = SystemInfo::GetSystemFontPath("Ubuntu-R");
|
||||
std::string path = SystemFontResolver::GetSystemFontPath("Ubuntu Regular");
|
||||
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf");
|
||||
|
||||
path = SystemInfo::GetSystemFontPath("UbuntuMono-BI");
|
||||
REQUIRE(!path.empty());
|
||||
path = SystemFontResolver::GetSystemFontPath("Ubuntu Mono Bold Italic");
|
||||
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/UbuntuMono-BI.ttf");
|
||||
|
||||
path = SystemInfo::GetSystemFontPath("NON-EXISTING Font");
|
||||
path = SystemFontResolver::GetSystemFontPath("NON-EXISTING Font");
|
||||
REQUIRE(path.empty());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user