implement system fonts search for Linux

This commit is contained in:
ohyzha
2025-01-17 14:01:24 +02:00
parent 169d6c4129
commit dbc52eff42
4 changed files with 70 additions and 1 deletions

View File

@@ -16,6 +16,9 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <unistd.h> #include <unistd.h>
#include <thread> #include <thread>
#include <filesystem>
#include <functional>
#include <map>
namespace OpenVulkano namespace OpenVulkano
{ {
@@ -397,6 +400,34 @@ namespace OpenVulkano
std::string SystemInfo::GetSystemFontPath(const std::string& fontName) std::string SystemInfo::GetSystemFontPath(const std::string& fontName)
{ {
// fontName -> fontPath
static std::map<std::string, std::string> fontFilesMapping;
if (fontFilesMapping.empty())
{
const std::filesystem::path fontsDir = "/usr/share/fonts/truetype";
std::function<void(const std::filesystem::path)> GetFontFiles;
GetFontFiles = [&](const std::filesystem::path& dir)
{
for (const auto& file : std::filesystem::directory_iterator(dir))
{
if (file.is_directory())
{
GetFontFiles(dir / file);
}
else if (file.path().extension() == ".ttf")
{
// store font name without extension
fontFilesMapping[file.path().stem()] = file.path();
}
}
};
GetFontFiles(fontsDir);
}
if (fontFilesMapping.contains(fontName))
{
return fontFilesMapping.at(fontName);
}
return ""; return "";
} }

View File

@@ -15,6 +15,17 @@ if (APPLE)
list(FILTER SOURCES EXCLUDE REGEX "ExeAppendedZipResourceLoader") list(FILTER SOURCES EXCLUDE REGEX "ExeAppendedZipResourceLoader")
endif() endif()
if (LINUX)
find_program(LSB_RELEASE_EXEC lsb_release)
execute_process(COMMAND ${LSB_RELEASE_EXEC} -is
OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT ${LSB_RELEASE_ID_SHORT} STREQUAL "Ubuntu")
list(FILTER SOURCES EXCLUDE REGEX "*Ubuntu*")
endif()
endif()
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${SOURCES}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${SOURCES})
file(GLOB_RECURSE RESOURCES "${ROOT_FOLDER}/resources/*.rc" "${ROOT_FOLDER}/resources/*.h") file(GLOB_RECURSE RESOURCES "${ROOT_FOLDER}/resources/*.rc" "${ROOT_FOLDER}/resources/*.h")
list(APPEND SOURCES ${RESOURCES}) list(APPEND SOURCES ${RESOURCES})

View File

@@ -0,0 +1,28 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_all.hpp>
#include "Base/Logger.hpp"
#include "Host/SystemInfo.hpp"
#include "Scene/Text/FontAtlasFactory.hpp"
#include <filesystem>
using namespace OpenVulkano;
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");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf");
path = SystemInfo::GetSystemFontPath("UbuntuMono-BI");
REQUIRE(!path.empty());
path = SystemInfo::GetSystemFontPath("NON-EXISTING Font");
REQUIRE(path.empty());
}

View File

@@ -10,7 +10,6 @@
#include <filesystem> #include <filesystem>
#include "Scene/Text/FontAtlasFactory.hpp" #include "Scene/Text/FontAtlasFactory.hpp"
#include "Host/ResourceLoader.hpp"
using namespace OpenVulkano; using namespace OpenVulkano;