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;
|
||||
}
|
||||
}
|
||||
19
openVulkanoCpp/Host/FontResolver.hpp
Normal file
19
openVulkanoCpp/Host/FontResolver.hpp
Normal file
@@ -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 <string>
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
class FontResolver final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] static Array<char> GetFontData(const std::string& fontName, bool checkSystemFonts = true);
|
||||
};
|
||||
}
|
||||
@@ -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 <utility>
|
||||
@@ -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<char> FontAtlasFactory::FindFont(const std::string& fontIdentifier) const
|
||||
{
|
||||
Array<char> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,8 +44,5 @@ namespace OpenVulkano::Scene
|
||||
[[nodiscard]] FontAtlas::Ptr GetFontAtlas(const std::string& fontIdentifier, float ptSize,
|
||||
SubpixelLayout subpixelLayout = SubpixelLayout::UNKNOWN,
|
||||
const std::set<uint32_t>& charset = {}) const;
|
||||
|
||||
private:
|
||||
Array<char> FindFont(const std::string& fontFile) const;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user