imgui_freetype: removed anonymous namespace + extracting two functions outside of ImGui_ImplFreeType_FontSrcData.

This commit is contained in:
ocornut
2025-04-26 17:24:38 +02:00
committed by ocornut
parent 6a455e1281
commit 42e7bb80b6
+9 -14
View File
@@ -110,8 +110,6 @@ static FT_Error ImGuiLunasvgPortPresetSlot(FT_GlyphSlot slot, FT_Bool cache, FT_
#define FT_CEIL(X) (((X + 63) & -64) / 64) // From SDL_ttf: Handy routines for converting from fixed point #define FT_CEIL(X) (((X + 63) & -64) / 64) // From SDL_ttf: Handy routines for converting from fixed point
#define FT_SCALEFACTOR 64.0f #define FT_SCALEFACTOR 64.0f
namespace
{
// Glyph metrics: // Glyph metrics:
// -------------- // --------------
// //
@@ -157,8 +155,6 @@ namespace
// Initialize from an external data buffer. Doesn't copy data, and you must ensure it stays valid up to this object lifetime. // Initialize from an external data buffer. Doesn't copy data, and you must ensure it stays valid up to this object lifetime.
bool InitFont(FT_Library ft_library, ImFontConfig* src, ImGuiFreeTypeBuilderFlags extra_user_flags); bool InitFont(FT_Library ft_library, ImFontConfig* src, ImGuiFreeTypeBuilderFlags extra_user_flags);
void CloseFont(); void CloseFont();
const FT_Glyph_Metrics* LoadGlyph(uint32_t in_codepoint);
void BlitGlyph(const FT_Bitmap* ft_bitmap, uint32_t* dst, uint32_t dst_pitch);
ImGui_ImplFreeType_FontSrcData() { memset((void*)this, 0, sizeof(*this)); } ImGui_ImplFreeType_FontSrcData() { memset((void*)this, 0, sizeof(*this)); }
~ImGui_ImplFreeType_FontSrcData() { CloseFont(); } ~ImGui_ImplFreeType_FontSrcData() { CloseFont(); }
@@ -223,9 +219,9 @@ namespace
} }
} }
const FT_Glyph_Metrics* ImGui_ImplFreeType_FontSrcData::LoadGlyph(uint32_t codepoint) static const FT_Glyph_Metrics* ImGui_ImplFreeType_LoadGlyph(ImGui_ImplFreeType_FontSrcData* src_data, uint32_t codepoint)
{ {
uint32_t glyph_index = FT_Get_Char_Index(FtFace, codepoint); uint32_t glyph_index = FT_Get_Char_Index(src_data->FtFace, codepoint);
if (glyph_index == 0) if (glyph_index == 0)
return nullptr; return nullptr;
@@ -234,12 +230,12 @@ namespace
// - https://github.com/ocornut/imgui/issues/4567 // - https://github.com/ocornut/imgui/issues/4567
// - https://github.com/ocornut/imgui/issues/4566 // - https://github.com/ocornut/imgui/issues/4566
// You can use FreeType 2.10, or the patched version of 2.11.0 in VcPkg, or probably any upcoming FreeType version. // You can use FreeType 2.10, or the patched version of 2.11.0 in VcPkg, or probably any upcoming FreeType version.
FT_Error error = FT_Load_Glyph(FtFace, glyph_index, LoadFlags); FT_Error error = FT_Load_Glyph(src_data->FtFace, glyph_index, src_data->LoadFlags);
if (error) if (error)
return nullptr; return nullptr;
// Need an outline for this to work // Need an outline for this to work
FT_GlyphSlot slot = FtFace->glyph; FT_GlyphSlot slot = src_data->FtFace->glyph;
#if defined(IMGUI_ENABLE_FREETYPE_LUNASVG) || defined(IMGUI_ENABLE_FREETYPE_PLUTOSVG) #if defined(IMGUI_ENABLE_FREETYPE_LUNASVG) || defined(IMGUI_ENABLE_FREETYPE_PLUTOSVG)
IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE || slot->format == FT_GLYPH_FORMAT_BITMAP || slot->format == FT_GLYPH_FORMAT_SVG); IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE || slot->format == FT_GLYPH_FORMAT_BITMAP || slot->format == FT_GLYPH_FORMAT_SVG);
#else #else
@@ -250,9 +246,9 @@ namespace
#endif // IMGUI_ENABLE_FREETYPE_LUNASVG #endif // IMGUI_ENABLE_FREETYPE_LUNASVG
// Apply convenience transform (this is not picking from real "Bold"/"Italic" fonts! Merely applying FreeType helper transform. Oblique == Slanting) // Apply convenience transform (this is not picking from real "Bold"/"Italic" fonts! Merely applying FreeType helper transform. Oblique == Slanting)
if (UserFlags & ImGuiFreeTypeBuilderFlags_Bold) if (src_data->UserFlags & ImGuiFreeTypeBuilderFlags_Bold)
FT_GlyphSlot_Embolden(slot); FT_GlyphSlot_Embolden(slot);
if (UserFlags & ImGuiFreeTypeBuilderFlags_Oblique) if (src_data->UserFlags & ImGuiFreeTypeBuilderFlags_Oblique)
{ {
FT_GlyphSlot_Oblique(slot); FT_GlyphSlot_Oblique(slot);
//FT_BBox bbox; //FT_BBox bbox;
@@ -264,7 +260,7 @@ namespace
return &slot->metrics; return &slot->metrics;
} }
void ImGui_ImplFreeType_FontSrcData::BlitGlyph(const FT_Bitmap* ft_bitmap, uint32_t* dst, uint32_t dst_pitch) static void ImGui_ImplFreeType_BlitGlyph(const FT_Bitmap* ft_bitmap, uint32_t* dst, uint32_t dst_pitch)
{ {
IM_ASSERT(ft_bitmap != nullptr); IM_ASSERT(ft_bitmap != nullptr);
const uint32_t w = ft_bitmap->width; const uint32_t w = ft_bitmap->width;
@@ -313,7 +309,6 @@ namespace
IM_ASSERT(0 && "FreeTypeFont::BlitGlyph(): Unknown bitmap pixel mode!"); IM_ASSERT(0 && "FreeTypeFont::BlitGlyph(): Unknown bitmap pixel mode!");
} }
} }
} // namespace
// FreeType memory allocation callbacks // FreeType memory allocation callbacks
static void* FreeType_Alloc(FT_Memory /*memory*/, long size) static void* FreeType_Alloc(FT_Memory /*memory*/, long size)
@@ -492,7 +487,7 @@ ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontCon
bd_font_data->BakedLastActivated = baked; bd_font_data->BakedLastActivated = baked;
} }
const FT_Glyph_Metrics* metrics = bd_font_data->LoadGlyph(codepoint); const FT_Glyph_Metrics* metrics = ImGui_ImplFreeType_LoadGlyph(bd_font_data, codepoint);
if (metrics == NULL) if (metrics == NULL)
return NULL; return NULL;
@@ -530,7 +525,7 @@ ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontCon
// Render pixels to our temporary buffer // Render pixels to our temporary buffer
atlas->Builder->TempBuffer.resize(w * h * 4); atlas->Builder->TempBuffer.resize(w * h * 4);
uint32_t* temp_buffer = (uint32_t*)atlas->Builder->TempBuffer.Data; uint32_t* temp_buffer = (uint32_t*)atlas->Builder->TempBuffer.Data;
bd_font_data->BlitGlyph(ft_bitmap, temp_buffer, w); ImGui_ImplFreeType_BlitGlyph(ft_bitmap, temp_buffer, w);
const float offsets_scale = baked->Size / baked->ContainerFont->Sources[0]->SizePixels; const float offsets_scale = baked->Size / baked->ContainerFont->Sources[0]->SizePixels;
float font_off_x = (src->GlyphOffset.x * offsets_scale); float font_off_x = (src->GlyphOffset.x * offsets_scale);