Rework some text related functions

This commit is contained in:
Georg Hagen
2025-03-01 20:00:27 +01:00
parent bfff861673
commit 0a027b8bb7
8 changed files with 43 additions and 43 deletions

View File

@@ -12,39 +12,32 @@
namespace OpenVulkano::Scene
{
std::pair<FtLibraryRecPtr, FtFaceRecPtr>
FontAtlasGeneratorBase::InitFreetype(const std::variant<std::string, Array<char>>& source)
FontAtlasGeneratorBase::InitFreetype(const std::span<const uint8_t>& data)
{
std::pair<FtLibraryRecPtr, FtFaceRecPtr> result;
FT_Library library;
auto error = FT_Init_FreeType(&library);
if (error)
{
throw std::runtime_error(fmt::format("Could not initalize freetype library. {}", GetFreetypeErrorDescription(error)));
throw std::runtime_error(fmt::format("Could not initialize freetype library. Error: {}", GetFreetypeErrorDescription(error)));
}
result.first = FtLibraryRecPtr(library);
FT_Face face;
if (std::holds_alternative<std::string>(source))
error = FT_New_Memory_Face(library, data.data(), data.size(), 0, &face);
if (error)
{
error = FT_New_Face(library, std::get<0>(source).c_str(), 0, &face);
}
else
{
auto& arr = std::get<1>(source);
error = FT_New_Memory_Face(library, (const FT_Byte*) (arr.Data()), arr.Size(), 0, &face);
}
if (error == FT_Err_Unknown_File_Format)
{
throw std::runtime_error("Unknown font file format\n");
}
else if (error)
{
throw std::runtime_error(fmt::format("Font file could not be opened or read or it's corrupted. {}", GetFreetypeErrorDescription(error)));
if (error == FT_Err_Unknown_File_Format) throw std::runtime_error("Unknown font file format");
throw std::runtime_error(fmt::format("Font file could not be read or is corrupted. Error: {}", GetFreetypeErrorDescription(error)));
}
result.second = FtFaceRecPtr(face);
// some fancy font without unicode charmap
if (face->charmap == nullptr)
{
throw std::runtime_error("Selected font doesn't contain unicode charmap");
}
return { FtLibraryRecPtr(library), FtFaceRecPtr(face) };
return result;
}
std::string FontAtlasGeneratorBase::GetFreetypeErrorDescription(FT_Error error)
@@ -88,18 +81,16 @@ namespace OpenVulkano::Scene
info.advance = advance;
}
std::set<uint32_t> FontAtlasGeneratorBase::LoadAllGlyphs(const std::variant<std::string, Array<char>>& data)
size_t FontAtlasGeneratorBase::LoadAllGlyphs(std::set<uint32_t>& chars, const FtFaceRecPtr& face)
{
const auto& [lib, face] = InitFreetype(data);
std::set<uint32_t> s;
chars.clear();
FT_UInt glyphIndex;
FT_ULong unicode = FT_Get_First_Char(face.get(), &glyphIndex);
while (glyphIndex != 0)
{
s.insert(unicode);
chars.insert(unicode);
unicode = FT_Get_Next_Char(face.get(), unicode, &glyphIndex);
}
return s;
return chars.size();
}
}