diff --git a/openVulkanoCpp/Host/FontResolver.cpp b/openVulkanoCpp/Host/FontResolver.cpp new file mode 100644 index 0000000..ce2fddd --- /dev/null +++ b/openVulkanoCpp/Host/FontResolver.cpp @@ -0,0 +1,46 @@ +/* + * 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 "FontResolver.hpp" +#include "Base/Utils.hpp" +#include "ResourceLoader.hpp" +#include "SystemFontResolver.hpp" +#include "fmt/format.h" +#include +#include +#include + +namespace OpenVulkano +{ + namespace + { + static constexpr std::array FONT_EXTENSIONS = { ".ttf" }; + static constexpr std::array FONT_POSTFIXES = { "-Regular" }; + + static constexpr std::array FONT_FILE_CHECKS = { "", FONT_EXTENSIONS[0], "-Regular.ttf" }; // TODO automate + } + + Array FontResolver::GetFontData(const std::string& fontName, bool checkSystemFonts) + { + Array data; + for (const auto& ext : FONT_FILE_CHECKS) + { + std::string fName = fmt::format("{}{}", fontName, ext); + data = ResourceLoader::GetInstance().GetResource(fName); + if (!data.Empty()) return data; + if (std::filesystem::exists(fName)) + { + data = Utils::ReadFile(fName, true); + if (!data.Empty()) return data; + } + } + if (checkSystemFonts) + { + data = Utils::ReadFile(SystemFontResolver::GetSystemFontPath(fontName), true); + } + return data; + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Host/FontResolver.hpp b/openVulkanoCpp/Host/FontResolver.hpp new file mode 100644 index 0000000..c87b0e6 --- /dev/null +++ b/openVulkanoCpp/Host/FontResolver.hpp @@ -0,0 +1,19 @@ +/* + * 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/. + */ + +#pragma once + +#include "Data/Containers/Array.hpp" +#include + +namespace OpenVulkano +{ + class FontResolver final + { + public: + [[nodiscard]] static Array GetFontData(const std::string& fontName, bool checkSystemFonts = true); + }; +} diff --git a/openVulkanoCpp/Scene/Text/FontAtlasFactory.cpp b/openVulkanoCpp/Scene/Text/FontAtlasFactory.cpp index cf7439a..6788d22 100644 --- a/openVulkanoCpp/Scene/Text/FontAtlasFactory.cpp +++ b/openVulkanoCpp/Scene/Text/FontAtlasFactory.cpp @@ -7,7 +7,7 @@ #include "FontAtlasFactory.hpp" #include "SdfFontAtlasGenerator.hpp" #include "BitmapFontAtlasGenerator.hpp" -#include "Host/SystemFontResolver.hpp" +#include "Host/FontResolver.hpp" #include "Base/Logger.hpp" #include "Host/ResourceLoader.hpp" #include @@ -45,7 +45,7 @@ namespace OpenVulkano::Scene return atlas; } - const auto& fontData = FindFont(fontIdentifier); + const auto& fontData = FontResolver::GetFontData(fontIdentifier, m_allowSystemFonts); if (fontData.Empty()) { Logger::DATA->warn("Could not find font {}", fontIdentifier); @@ -82,7 +82,7 @@ namespace OpenVulkano::Scene return it->second; } - const auto& fontData = FindFont(fontIdentifier); + const auto& fontData = FontResolver::GetFontData(fontIdentifier, m_allowSystemFonts); if (fontData.Empty()) { Logger::DATA->warn("Could not find font {}", fontIdentifier); @@ -95,22 +95,4 @@ namespace OpenVulkano::Scene if (charset.empty()) m_atlasesCache.emplace(id, bitmapGen.GetAtlas()); return bitmapGen.GetAtlas(); } - - Array FontAtlasFactory::FindFont(const std::string& fontIdentifier) const - { - Array resource = ResourceLoader::GetInstance().GetResource(fontIdentifier); - if (resource.Empty()) - { - if (!std::filesystem::exists(fontIdentifier)) - { - if (!m_allowSystemFonts) - { - return {}; - } - return Utils::ReadFile(SystemFontResolver::GetSystemFontPath(fontIdentifier), true); - } - return Utils::ReadFile(fontIdentifier); - } - return resource; - } } diff --git a/openVulkanoCpp/Scene/Text/FontAtlasFactory.hpp b/openVulkanoCpp/Scene/Text/FontAtlasFactory.hpp index 5c1fd32..43b870e 100644 --- a/openVulkanoCpp/Scene/Text/FontAtlasFactory.hpp +++ b/openVulkanoCpp/Scene/Text/FontAtlasFactory.hpp @@ -44,8 +44,5 @@ namespace OpenVulkano::Scene [[nodiscard]] FontAtlas::Ptr GetFontAtlas(const std::string& fontIdentifier, float ptSize, SubpixelLayout subpixelLayout = SubpixelLayout::UNKNOWN, const std::set& charset = {}) const; - - private: - Array FindFont(const std::string& fontFile) const; }; }