different improvements
This commit is contained in:
@@ -7,73 +7,100 @@
|
||||
#include "FontAtlasFactory.hpp"
|
||||
#include "Scene/SdfFontAtlasGenerator.hpp"
|
||||
#include "Scene/BitmapFontAtlasGenerator.hpp"
|
||||
#include "Host/SystemInfo.hpp"
|
||||
#include "Host/SystemFontResolver.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include "Host/ResourceLoader.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
FontAtlas::Ptr FontAtlasFactory::GetFontAtlasScalable(const std::string& fontIdentifier,
|
||||
const std::set<uint32_t>& charset, bool msdf) const
|
||||
FontAtlasFactory::FontIdentifier::FontIdentifier(const std::string& font_, const std::set<uint32_t>& charset,
|
||||
SubpixelLayout subpixelLayout_, float ptSize_,
|
||||
FontAtlasType atlasType_)
|
||||
: font(font_), subpixelLayout(subpixelLayout_), ptSize(ptSize_), atlasType(atlasType_)
|
||||
{
|
||||
const std::string fileName = FindFont(fontIdentifier);
|
||||
if (fileName.empty())
|
||||
std::for_each(charset.begin(), charset.end(), [&](uint32_t c) { charsetHash ^= c; });
|
||||
}
|
||||
|
||||
bool FontAtlasFactory::FontIdentifier::FontIdentifier::operator<(const FontIdentifier& other) const
|
||||
{
|
||||
return std::tie(atlasType, charsetHash, ptSize, subpixelLayout, font)
|
||||
< std::tie(other.atlasType, other.charsetHash, other.ptSize, other.subpixelLayout, other.font);
|
||||
}
|
||||
|
||||
FontAtlas::Ptr FontAtlasFactory::GetFontAtlasScalable(const std::string& fontIdentifier, bool msdf,
|
||||
const std::set<uint32_t>& charset) const
|
||||
{
|
||||
const auto& fontData = FindFont(fontIdentifier);
|
||||
if (fontData.Empty())
|
||||
{
|
||||
Logger::DATA->warn("Could not find font {}", fontIdentifier);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::set<uint32_t>& setRef = (charset.empty() ? FontAtlasGeneratorBase::LoadAllGlyphs(fontIdentifier) : charset);
|
||||
FontIdentifier id(fontIdentifier, setRef, SubpixelLayout::UNKNOWN, 0, msdf);
|
||||
if (m_atlasesCache.contains(id))
|
||||
const std::set<uint32_t>& setRef = (charset.empty() ? FontAtlasGeneratorBase::LoadAllGlyphs(fontData) : charset);
|
||||
FontIdentifier id(fontIdentifier, setRef, SubpixelLayout::UNKNOWN, 0,
|
||||
msdf ? FontAtlasType::MSDF : FontAtlasType::SDF);
|
||||
|
||||
auto it = m_atlasesCache.find(id);
|
||||
if (it != m_atlasesCache.end())
|
||||
{
|
||||
return m_atlasesCache.at(id);
|
||||
return it->second;
|
||||
}
|
||||
|
||||
if (msdf)
|
||||
{
|
||||
MsdfFontAtlasGenerator msdfGen;
|
||||
msdfGen.GenerateAtlas(fileName, setRef);
|
||||
m_atlasesCache[id] = msdfGen.GetAtlas();
|
||||
return m_atlasesCache.at(id);
|
||||
msdfGen.GenerateAtlas(fontData, setRef);
|
||||
return m_atlasesCache.insert({ id, msdfGen.GetAtlas() }).first->second;
|
||||
}
|
||||
SdfFontAtlasGenerator sdfGen;
|
||||
sdfGen.GenerateAtlas(fileName, setRef);
|
||||
m_atlasesCache[id] = sdfGen.GetAtlas();
|
||||
return m_atlasesCache.at(id);
|
||||
sdfGen.GenerateAtlas(fontData, setRef);
|
||||
return m_atlasesCache.insert({ id, sdfGen.GetAtlas() }).first->second;
|
||||
}
|
||||
|
||||
FontAtlas::Ptr FontAtlasFactory::GetFontAtlas(const std::string& fontIdentifier, float ptSize,
|
||||
const std::set<uint32_t>& charset,
|
||||
std::optional<SubpixelLayout> subpixelLayout) const
|
||||
std::optional<SubpixelLayout> subpixelLayout,
|
||||
const std::set<uint32_t>& charset) const
|
||||
{
|
||||
const std::string fileName = FindFont(fontIdentifier);
|
||||
if (fileName.empty())
|
||||
const auto& fontData = FindFont(fontIdentifier);
|
||||
if (fontData.Empty())
|
||||
{
|
||||
Logger::DATA->warn("Could not find font {}", fontIdentifier);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::set<uint32_t>& setRef = (charset.empty() ? FontAtlasGeneratorBase::LoadAllGlyphs(fontIdentifier) : charset);
|
||||
FontIdentifier id(fontIdentifier, setRef, subpixelLayout.value_or(SubpixelLayout::UNKNOWN), ptSize, false);
|
||||
if (m_atlasesCache.contains(id))
|
||||
const std::set<uint32_t>& setRef = (charset.empty() ? FontAtlasGeneratorBase::LoadAllGlyphs(fontData) : charset);
|
||||
FontIdentifier id(fontIdentifier, setRef, subpixelLayout.value_or(SubpixelLayout::UNKNOWN), ptSize,
|
||||
subpixelLayout ? FontAtlasType::BITMAP_SUBPIXEL : FontAtlasType::BITMAP);
|
||||
|
||||
auto it = m_atlasesCache.find(id);
|
||||
if (it != m_atlasesCache.end())
|
||||
{
|
||||
return m_atlasesCache.at(id);
|
||||
return it->second;
|
||||
}
|
||||
|
||||
FontPixelSizeConfig cfg(ptSize);
|
||||
BitmapFontAtlasGenerator bitmapGen(cfg, subpixelLayout);
|
||||
bitmapGen.GenerateAtlas(fileName, setRef);
|
||||
m_atlasesCache[id] = bitmapGen.GetAtlas();
|
||||
return m_atlasesCache.at(id);
|
||||
bitmapGen.GenerateAtlas(fontData, setRef);
|
||||
return m_atlasesCache.insert({ id, bitmapGen.GetAtlas() }).first->second;
|
||||
}
|
||||
|
||||
std::string FontAtlasFactory::FindFont(const std::string& fontIdentifier) const
|
||||
Array<char> FontAtlasFactory::FindFont(const std::string& fontIdentifier) const
|
||||
{
|
||||
if (!std::filesystem::exists(fontIdentifier))
|
||||
Array<char> resource = ResourceLoader::GetInstance().GetResource(fontIdentifier);
|
||||
if (resource.Empty())
|
||||
{
|
||||
if (!m_allowSystemFonts)
|
||||
if (!std::filesystem::exists(fontIdentifier))
|
||||
{
|
||||
return "";
|
||||
if (!m_allowSystemFonts)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
return Utils::ReadFile(SystemFontResolver::GetSystemFontPath(fontIdentifier));
|
||||
}
|
||||
return SystemInfo::GetSystemFontPath(fontIdentifier);
|
||||
return Utils::ReadFile(fontIdentifier);
|
||||
}
|
||||
return fontIdentifier;
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user