Add FontResolver
This commit is contained in:
46
openVulkanoCpp/Host/FontResolver.cpp
Normal file
46
openVulkanoCpp/Host/FontResolver.cpp
Normal file
@@ -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 <array>
|
||||
#include <string_view>
|
||||
#include <filesystem>
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
namespace
|
||||
{
|
||||
static constexpr std::array<std::string_view, 1> FONT_EXTENSIONS = { ".ttf" };
|
||||
static constexpr std::array<std::string_view, 1> FONT_POSTFIXES = { "-Regular" };
|
||||
|
||||
static constexpr std::array<std::string_view, 3> FONT_FILE_CHECKS = { "", FONT_EXTENSIONS[0], "-Regular.ttf" }; // TODO automate
|
||||
}
|
||||
|
||||
Array<char> FontResolver::GetFontData(const std::string& fontName, bool checkSystemFonts)
|
||||
{
|
||||
Array<char> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user