From dbc52eff4293986504babd47d3cbb2e51080e608 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Fri, 17 Jan 2025 14:01:24 +0200 Subject: [PATCH] implement system fonts search for Linux --- openVulkanoCpp/Host/Linux/SystemInfo.cpp | 31 +++++++++++++++++++ tests/CMakeLists.txt | 11 +++++++ .../Linux/SystemFontsSearchTestsUbuntu.cpp | 28 +++++++++++++++++ tests/Host/Windows/SystemFontsSearchTests.cpp | 1 - 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp diff --git a/openVulkanoCpp/Host/Linux/SystemInfo.cpp b/openVulkanoCpp/Host/Linux/SystemInfo.cpp index 896a9c8..a63d3f7 100644 --- a/openVulkanoCpp/Host/Linux/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Linux/SystemInfo.cpp @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include namespace OpenVulkano { @@ -397,6 +400,34 @@ namespace OpenVulkano std::string SystemInfo::GetSystemFontPath(const std::string& fontName) { + // fontName -> fontPath + static std::map fontFilesMapping; + if (fontFilesMapping.empty()) + { + const std::filesystem::path fontsDir = "/usr/share/fonts/truetype"; + std::function 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 ""; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 85c0c4f..29345f3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,17 @@ if (APPLE) list(FILTER SOURCES EXCLUDE REGEX "ExeAppendedZipResourceLoader") 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}) file(GLOB_RECURSE RESOURCES "${ROOT_FOLDER}/resources/*.rc" "${ROOT_FOLDER}/resources/*.h") list(APPEND SOURCES ${RESOURCES}) diff --git a/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp b/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp new file mode 100644 index 0000000..6c2e372 --- /dev/null +++ b/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp @@ -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 +#include "Base/Logger.hpp" +#include "Host/SystemInfo.hpp" +#include "Scene/Text/FontAtlasFactory.hpp" +#include + +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()); +} diff --git a/tests/Host/Windows/SystemFontsSearchTests.cpp b/tests/Host/Windows/SystemFontsSearchTests.cpp index 4fdf919..c299d48 100644 --- a/tests/Host/Windows/SystemFontsSearchTests.cpp +++ b/tests/Host/Windows/SystemFontsSearchTests.cpp @@ -10,7 +10,6 @@ #include #include "Scene/Text/FontAtlasFactory.hpp" -#include "Host/ResourceLoader.hpp" using namespace OpenVulkano;