More font rendering cleanup

This commit is contained in:
Georg Hagen
2025-03-02 19:25:41 +01:00
parent 6c796bb3a0
commit fdea7ce0ba
7 changed files with 52 additions and 67 deletions

View File

@@ -11,11 +11,6 @@
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void BitmapFontAtlasGenerator::GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset)
{
GenerateAtlas(Utils::ReadFile(fontFile), charset);
}
void BitmapFontAtlasGenerator::GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) void BitmapFontAtlasGenerator::GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset)
{ {
Generate(fontData.AsBytes(), charset); Generate(fontData.AsBytes(), charset);
@@ -88,7 +83,7 @@ namespace OpenVulkano::Scene
// 2) render glyph in FillGlyphsInfo with FT_LOAD_RENDER | FT_LOAD_TARGET_LCD mode; // 2) render glyph in FillGlyphsInfo with FT_LOAD_RENDER | FT_LOAD_TARGET_LCD mode;
// 3) take into account all mentioned things above for proper mapping. // 3) take into account all mentioned things above for proper mapping.
FT_GlyphSlot slot = face->glyph; FT_GlyphSlot slot = face->glyph;
GlyphForPacking& glyph = allGlyphs.emplace_back(codepoint, ScaleGlyphSize(slot->bitmap.width, slot->bitmap.rows)); allGlyphs.emplace_back(codepoint, ScaleGlyphSize(slot->bitmap.width, slot->bitmap.rows));
area += slot->bitmap.rows * slot->bitmap.width; area += slot->bitmap.rows * slot->bitmap.width;
} }
std::sort(allGlyphs.begin(), allGlyphs.end(), std::sort(allGlyphs.begin(), allGlyphs.end(),

View File

@@ -15,13 +15,13 @@ namespace OpenVulkano::Scene
class FontPixelSizeConfig class FontPixelSizeConfig
{ {
public: public:
FontPixelSizeConfig(float size = 24.f, float dpi = 72.f, bool isPixelSize = true) FontPixelSizeConfig(const float size = 24.f, const float dpi = 72.f, const bool isPixelSize = true)
: m_size(size), m_dpi(dpi), m_isPixelSize(isPixelSize) : m_size(size), m_dpi(dpi), m_isPixelSize(isPixelSize)
{ {
} }
void SetSize(float size) { m_size = size; } void SetSize(const float size) { m_size = size; }
void SetDpi(float dpi) { m_dpi = dpi; } void SetDpi(const float dpi) { m_dpi = dpi; }
void SetIsPixelSize(bool isPixelSize) { m_isPixelSize = isPixelSize; } void SetIsPixelSize(const bool isPixelSize) { m_isPixelSize = isPixelSize; }
[[nodiscard]] float GetSize() const { return m_size; } [[nodiscard]] float GetSize() const { return m_size; }
[[nodiscard]] float GetDpi() const { return m_dpi; } [[nodiscard]] float GetDpi() const { return m_dpi; }
[[nodiscard]] bool GetIsPixelSize() const { return m_isPixelSize; } [[nodiscard]] bool GetIsPixelSize() const { return m_isPixelSize; }
@@ -34,6 +34,9 @@ namespace OpenVulkano::Scene
class BitmapFontAtlasGenerator : public FontAtlasGeneratorBase class BitmapFontAtlasGenerator : public FontAtlasGeneratorBase
{ {
FontPixelSizeConfig m_pixelSizeConfig;
SubpixelLayout m_subpixelLayout;
public: public:
BitmapFontAtlasGenerator(FontPixelSizeConfig config = FontPixelSizeConfig(), BitmapFontAtlasGenerator(FontPixelSizeConfig config = FontPixelSizeConfig(),
std::optional<SubpixelLayout> subpixelLayout = std::nullopt) std::optional<SubpixelLayout> subpixelLayout = std::nullopt)
@@ -42,18 +45,21 @@ namespace OpenVulkano::Scene
, m_subpixelLayout(subpixelLayout.value_or(SubpixelLayout::UNKNOWN)) , m_subpixelLayout(subpixelLayout.value_or(SubpixelLayout::UNKNOWN))
{ {
} }
[[deprecated]] void GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset) override;
void GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) override; void GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) override;
private: private:
void Generate(const std::span<const uint8_t>& fontData, const std::set<uint32_t>& chset); void Generate(const std::span<const uint8_t>& fontData, const std::set<uint32_t>& chset);
void FillGlyphsInfo(const std::vector<GlyphForPacking>& allGlyphs, const FtFaceRecPtr& face, double scaleFactor); void FillGlyphsInfo(const std::vector<GlyphForPacking>& allGlyphs, const FtFaceRecPtr& face, double scaleFactor);
void FillSubpixelData(const FT_Bitmap& bitmap, const GlyphForPacking& glyph); void FillSubpixelData(const FT_Bitmap& bitmap, const GlyphForPacking& glyph);
FT_Int32 GetGlyphRenderMode() const; FT_Int32 GetGlyphRenderMode() const;
// tmp function // tmp function
Math::Vector2ui ScaleGlyphSize(unsigned int w, unsigned int h) const; Math::Vector2ui ScaleGlyphSize(unsigned int w, unsigned int h) const;
std::pair<std::vector<GlyphForPacking>, double> InitGlyphsForPacking(const std::set<uint32_t>& chset, const FtFaceRecPtr& face); std::pair<std::vector<GlyphForPacking>, double> InitGlyphsForPacking(const std::set<uint32_t>& chset, const FtFaceRecPtr& face);
private:
FontPixelSizeConfig m_pixelSizeConfig;
SubpixelLayout m_subpixelLayout;
}; };
} }

View File

@@ -11,6 +11,11 @@
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void IFontAtlasGenerator::GenerateAtlas(const std::filesystem::path& fontFile, const std::set<uint32_t>& charset)
{
GenerateAtlas(Utils::ReadFile(fontFile), charset);
}
std::pair<FtLibraryRecPtr, FtFaceRecPtr> std::pair<FtLibraryRecPtr, FtFaceRecPtr>
FontAtlasGeneratorBase::InitFreetype(const std::span<const uint8_t>& data) FontAtlasGeneratorBase::InitFreetype(const std::span<const uint8_t>& data)
{ {

View File

@@ -22,14 +22,20 @@ namespace OpenVulkano::Scene
public: public:
FontAtlasGeneratorBase(const int channelsCount) : m_channelsCount(channelsCount) {} FontAtlasGeneratorBase(const int channelsCount) : m_channelsCount(channelsCount) {}
[[nodiscard]] const std::shared_ptr<FontAtlas>& GetAtlas() const final { return m_atlasData; } [[nodiscard]] const std::shared_ptr<FontAtlas>& GetAtlas() const final { return m_atlasData; }
[[nodiscard]] int GetAtlasChannelsCount() const { return m_channelsCount; } [[nodiscard]] int GetAtlasChannelsCount() const { return m_channelsCount; }
static size_t LoadAllGlyphs(std::set<uint32_t>& chars, const std::span<const uint8_t>& fontData) { auto [lib, face] = InitFreetype(fontData); return LoadAllGlyphs(chars, face); } static size_t LoadAllGlyphs(std::set<uint32_t>& chars, const std::span<const uint8_t>& fontData) { auto [lib, face] = InitFreetype(fontData); return LoadAllGlyphs(chars, face); }
static size_t LoadAllGlyphs(std::set<uint32_t>& chars, const FtFaceRecPtr& face); static size_t LoadAllGlyphs(std::set<uint32_t>& chars, const FtFaceRecPtr& face);
protected: protected:
void SetGlyphData(GlyphInfo& info, Math::Vector2d bearing, Math::Vector2d size, const Math::AABB& aabb, double advance); void SetGlyphData(GlyphInfo& info, Math::Vector2d bearing, Math::Vector2d size, const Math::AABB& aabb, double advance);
[[nodiscard]] static std::string GetFreetypeErrorDescription(FT_Error error); [[nodiscard]] static std::string GetFreetypeErrorDescription(FT_Error error);
[[nodiscard]] static std::pair<FtLibraryRecPtr, FtFaceRecPtr> InitFreetype(const std::span<const uint8_t>& data); [[nodiscard]] static std::pair<FtLibraryRecPtr, FtFaceRecPtr> InitFreetype(const std::span<const uint8_t>& data);
}; };
} }

View File

@@ -6,11 +6,10 @@
#pragma once #pragma once
#include <Data/Containers/Array.hpp> #include "Base/Wrapper.hpp"
#include <string> #include "Data/Containers/Array.hpp"
#include <optional> #include <filesystem>
#include <set> #include <set>
#include <memory>
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
@@ -20,8 +19,11 @@ namespace OpenVulkano::Scene
{ {
public: public:
virtual ~IFontAtlasGenerator() = default; virtual ~IFontAtlasGenerator() = default;
[[deprecated]] virtual void GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset) = 0;
void GenerateAtlas(const std::filesystem::path& fontFile, const std::set<uint32_t>& charset);
virtual void GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) = 0; virtual void GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) = 0;
virtual const std::shared_ptr<FontAtlas>& GetAtlas() const = 0;
virtual const Ptr<FontAtlas>& GetAtlas() const = 0;
}; };
} }

View File

@@ -17,12 +17,6 @@ namespace OpenVulkano::Scene
SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::sdfDefaultConfig = { 42, 1.0, 5 }; SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::sdfDefaultConfig = { 42, 1.0, 5 };
SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::msdfDefaultConfig = { 32, 1.0, 3 }; SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::msdfDefaultConfig = { 32, 1.0, 3 };
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& inCs)
{
GenerateAtlas(Utils::ReadFile(fontFile), inCs);
}
template<int Channels> SdfFontAtlasGeneratorGeneric<Channels>::SdfFontAtlasGeneratorGeneric() : FontAtlasGeneratorBase(Channels) template<int Channels> SdfFontAtlasGeneratorGeneric<Channels>::SdfFontAtlasGeneratorGeneric() : FontAtlasGeneratorBase(Channels)
{ {
if constexpr (Channels == 1) m_config = SdfFontAtlasGeneratorConfig::sdfDefaultConfig; if constexpr (Channels == 1) m_config = SdfFontAtlasGeneratorConfig::sdfDefaultConfig;
@@ -39,7 +33,7 @@ namespace OpenVulkano::Scene
std::set<uint32_t> fallback; std::set<uint32_t> fallback;
if (inCs.empty()) if (inCs.empty())
{ {
FontAtlasGeneratorBase::LoadAllGlyphs(fallback, fontData.AsBytes()); LoadAllGlyphs(fallback, fontData.AsBytes());
} }
const auto& charset = inCs.empty() ? fallback : inCs; const auto& charset = inCs.empty() ? fallback : inCs;
std::for_each(charset.begin(), charset.end(), [&](uint32_t unicode) { s.add(unicode); }); std::for_each(charset.begin(), charset.end(), [&](uint32_t unicode) { s.add(unicode); });
@@ -47,43 +41,21 @@ namespace OpenVulkano::Scene
} }
template<int Channels> template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile, const msdf_atlas::Charset& charset) void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::filesystem::path& fontFile, const msdf_atlas::Charset& charset)
{ {
// TODO: dynamic atlas and add only those symbols which are not present yet in current atlas auto data = Utils::ReadFile(fontFile);
msdfgen::FreetypeHandle* ft; GenerateAtlas(reinterpret_cast<const uint8_t*>(data.Data()), data.Size(), charset);
msdfgen::FontHandle* font;
InitFreetypeFromFile(ft, font, fontFile);
Generate(ft, font, charset);
} }
template<int Channels> template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const msdfgen::byte* fontData, int length, const msdf_atlas::Charset& charset) void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const msdfgen::byte* fontData, int length, const msdf_atlas::Charset& charset)
{ { // TODO: dynamic atlas and add only those symbols which are not present yet in current atlas
msdfgen::FreetypeHandle* ft; msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font; msdfgen::FontHandle* font;
InitFreetypeFromBuffer(ft, font, fontData, length); InitFreetypeFromBuffer(ft, font, fontData, length);
Generate(ft, font, charset); Generate(ft, font, charset);
} }
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromFile(msdfgen::FreetypeHandle*& ft,
msdfgen::FontHandle*& font,
const std::string& fontFile)
{
ft = msdfgen::initializeFreetype();
if (!ft)
{
throw std::runtime_error("Failed to initialize freetype");
}
font = loadFont(ft, fontFile.data());
if (!font)
{
deinitializeFreetype(ft);
ft = nullptr;
throw std::runtime_error(fmt::format("Failed to load font from file {0}", fontFile.data()));
}
}
template<int Channels> template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft, void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft,
msdfgen::FontHandle*& font, msdfgen::FontHandle*& font,
@@ -113,7 +85,7 @@ namespace OpenVulkano::Scene
if constexpr (Channels == 3) if constexpr (Channels == 3)
{ {
const double maxCornerAngle = 3.0; constexpr double maxCornerAngle = 3.0;
for (msdf_atlas::GlyphGeometry& glyph : glyphsGeometry) for (msdf_atlas::GlyphGeometry& glyph : glyphsGeometry)
{ {
glyph.edgeColoring(&msdfgen::edgeColoringByDistance, maxCornerAngle, 0); glyph.edgeColoring(&msdfgen::edgeColoringByDistance, maxCornerAngle, 0);
@@ -162,7 +134,7 @@ namespace OpenVulkano::Scene
} }
else else
{ {
const msdfgen::BitmapConstRef<msdfgen::byte, 1>& storage = generator.atlasStorage(); const msdfgen::BitmapConstRef<msdfgen::byte>& storage = generator.atlasStorage();
memcpy(m_atlasData->GetTexture()->textureBuffer, storage.pixels, width * height); memcpy(m_atlasData->GetTexture()->textureBuffer, storage.pixels, width * height);
} }

View File

@@ -12,14 +12,11 @@
#include <msdfgen.h> #include <msdfgen.h>
#include <msdf-atlas-gen/msdf-atlas-gen.h> #include <msdf-atlas-gen/msdf-atlas-gen.h>
#include <string> #include <string>
#include <optional>
#include <map>
#include <variant> #include <variant>
#define MSDFGEN_AVAILABLE 1 #define MSDFGEN_AVAILABLE 1
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
struct SdfFontAtlasGeneratorConfig struct SdfFontAtlasGeneratorConfig
{ {
static SdfFontAtlasGeneratorConfig sdfDefaultConfig; static SdfFontAtlasGeneratorConfig sdfDefaultConfig;
@@ -32,32 +29,34 @@ namespace OpenVulkano::Scene
template<int Channels> template<int Channels>
class SdfFontAtlasGeneratorGeneric final : public FontAtlasGeneratorBase class SdfFontAtlasGeneratorGeneric final : public FontAtlasGeneratorBase
{ {
private:
using SdfGenerator = msdf_atlas::ImmediateAtlasGenerator<float, 1, msdf_atlas::sdfGenerator, using SdfGenerator = msdf_atlas::ImmediateAtlasGenerator<float, 1, msdf_atlas::sdfGenerator,
msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 1>>; msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 1>>;
using MsdfGenerator = msdf_atlas::ImmediateAtlasGenerator<float, 3, msdf_atlas::msdfGenerator, using MsdfGenerator = msdf_atlas::ImmediateAtlasGenerator<float, 3, msdf_atlas::msdfGenerator,
msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 3>>; msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 3>>;
public: public:
using Generator = std::conditional<Channels == 1, SdfGenerator, MsdfGenerator>::type; using Generator = typename std::conditional<Channels == 1, SdfGenerator, MsdfGenerator>::type;
using Config = SdfFontAtlasGeneratorConfig; using Config = SdfFontAtlasGeneratorConfig;
static constexpr int channelsCount = (Channels == 1 ? 1 : 4); static constexpr int channelsCount = (Channels == 1 ? 1 : 4);
SdfFontAtlasGeneratorGeneric(); SdfFontAtlasGeneratorGeneric();
[[deprecated]] void GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset) override;
void GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) override; void GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset) override;
[[deprecated]] void GenerateAtlas(const std::string& fontFile,
const msdf_atlas::Charset& charset = msdf_atlas::Charset::ASCII); void GenerateAtlas(const std::filesystem::path& fontFile, const msdf_atlas::Charset& charset = msdf_atlas::Charset::ASCII);
void GenerateAtlas(const msdfgen::byte* fontData, int length, void GenerateAtlas(const msdfgen::byte* fontData, int length, const msdf_atlas::Charset& charset = msdf_atlas::Charset::ASCII);
const msdf_atlas::Charset& charset = msdf_atlas::Charset::ASCII);
void SetGeneratorConfig(const Config& config) { m_config = config; } void SetGeneratorConfig(const Config& config) { m_config = config; }
Config& GetGeneratorConfig() { return m_config; } Config& GetGeneratorConfig() { return m_config; }
private: private:
void InitFreetypeFromFile(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font, const std::string& file);
void InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font, void InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font,
const msdfgen::byte* fontData, int length); const msdfgen::byte* fontData, int length);
void Generate(msdfgen::FreetypeHandle* ft, msdfgen::FontHandle* font, const msdf_atlas::Charset& chset); void Generate(msdfgen::FreetypeHandle* ft, msdfgen::FontHandle* font, const msdf_atlas::Charset& chset);
private:
Config m_config; Config m_config;
}; };
using SdfFontAtlasGenerator = SdfFontAtlasGeneratorGeneric<1>; using SdfFontAtlasGenerator = SdfFontAtlasGeneratorGeneric<1>;
using MsdfFontAtlasGenerator = SdfFontAtlasGeneratorGeneric<3>; using MsdfFontAtlasGenerator = SdfFontAtlasGeneratorGeneric<3>;
} }