mirror of
https://github.com/ocornut/imgui.git
synced 2026-05-31 14:25:49 +08:00
imgui_freetype: removed anonymous namespace + extracting two functions outside of ImGui_ImplFreeType_FontSrcData.
This commit is contained in:
@@ -110,55 +110,51 @@ 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:
|
//
|
||||||
// --------------
|
// xmin xmax
|
||||||
//
|
// | |
|
||||||
// xmin xmax
|
// |<-------- width -------->|
|
||||||
// | |
|
// | |
|
||||||
// |<-------- width -------->|
|
// | +-------------------------+----------------- ymax
|
||||||
// | |
|
// | | ggggggggg ggggg | ^ ^
|
||||||
// | +-------------------------+----------------- ymax
|
// | | g:::::::::ggg::::g | | |
|
||||||
// | | ggggggggg ggggg | ^ ^
|
// | | g:::::::::::::::::g | | |
|
||||||
// | | g:::::::::ggg::::g | | |
|
// | | g::::::ggggg::::::gg | | |
|
||||||
// | | g:::::::::::::::::g | | |
|
// | | g:::::g g:::::g | | |
|
||||||
// | | g::::::ggggg::::::gg | | |
|
// offsetX -|-------->| g:::::g g:::::g | offsetY |
|
||||||
// | | g:::::g g:::::g | | |
|
// | | g:::::g g:::::g | | |
|
||||||
// offsetX -|-------->| g:::::g g:::::g | offsetY |
|
// | | g::::::g g:::::g | | |
|
||||||
// | | g:::::g g:::::g | | |
|
// | | g:::::::ggggg:::::g | | |
|
||||||
// | | g::::::g g:::::g | | |
|
// | | g::::::::::::::::g | | height
|
||||||
// | | g:::::::ggggg:::::g | | |
|
// | | gg::::::::::::::g | | |
|
||||||
// | | g::::::::::::::::g | | height
|
// baseline ---*---------|---- gggggggg::::::g-----*-------- |
|
||||||
// | | gg::::::::::::::g | | |
|
// / | | g:::::g | |
|
||||||
// baseline ---*---------|---- gggggggg::::::g-----*-------- |
|
// origin | | gggggg g:::::g | |
|
||||||
// / | | g:::::g | |
|
// | | g:::::gg gg:::::g | |
|
||||||
// origin | | gggggg g:::::g | |
|
// | | g::::::ggg:::::::g | |
|
||||||
// | | g:::::gg gg:::::g | |
|
// | | gg:::::::::::::g | |
|
||||||
// | | g::::::ggg:::::::g | |
|
// | | ggg::::::ggg | |
|
||||||
// | | gg:::::::::::::g | |
|
// | | gggggg | v
|
||||||
// | | ggg::::::ggg | |
|
// | +-------------------------+----------------- ymin
|
||||||
// | | gggggg | v
|
// | |
|
||||||
// | +-------------------------+----------------- ymin
|
// |------------- advanceX ----------->|
|
||||||
// | |
|
|
||||||
// |------------- advanceX ----------->|
|
|
||||||
|
|
||||||
// Stored in ImFontAtlas::FontLoaderData. ALLOCATED BY US.
|
// Stored in ImFontAtlas::FontLoaderData. ALLOCATED BY US.
|
||||||
struct ImGui_ImplFreeType_Data
|
struct ImGui_ImplFreeType_Data
|
||||||
{
|
{
|
||||||
FT_Library Library;
|
FT_Library Library;
|
||||||
FT_MemoryRec_ MemoryManager;
|
FT_MemoryRec_ MemoryManager;
|
||||||
ImGui_ImplFreeType_Data() { memset((void*)this, 0, sizeof(*this)); }
|
ImGui_ImplFreeType_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Stored in ImFontConfig::FontLoaderData. ALLOCATED BY US.
|
// Stored in ImFontConfig::FontLoaderData. ALLOCATED BY US.
|
||||||
struct ImGui_ImplFreeType_FontSrcData
|
struct ImGui_ImplFreeType_FontSrcData
|
||||||
{
|
{
|
||||||
// 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(); }
|
||||||
|
|
||||||
@@ -167,17 +163,17 @@ namespace
|
|||||||
ImGuiFreeTypeBuilderFlags UserFlags; // = ImFontConfig::FontBuilderFlags
|
ImGuiFreeTypeBuilderFlags UserFlags; // = ImFontConfig::FontBuilderFlags
|
||||||
FT_Int32 LoadFlags;
|
FT_Int32 LoadFlags;
|
||||||
ImFontBaked* BakedLastActivated;
|
ImFontBaked* BakedLastActivated;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Stored in ImFontBaked::FontLoaderDatas: pointer to SourcesCount instances of this. ALLOCATED BY CORE.
|
// Stored in ImFontBaked::FontLoaderDatas: pointer to SourcesCount instances of this. ALLOCATED BY CORE.
|
||||||
struct ImGui_ImplFreeType_FontSrcBakedData
|
struct ImGui_ImplFreeType_FontSrcBakedData
|
||||||
{
|
{
|
||||||
FT_Size FtSize; // This represent a FT_Face with a given size.
|
FT_Size FtSize; // This represent a FT_Face with a given size.
|
||||||
ImGui_ImplFreeType_FontSrcBakedData() { memset((void*)this, 0, sizeof(*this)); }
|
ImGui_ImplFreeType_FontSrcBakedData() { memset((void*)this, 0, sizeof(*this)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
bool ImGui_ImplFreeType_FontSrcData::InitFont(FT_Library ft_library, ImFontConfig* src, ImGuiFreeTypeBuilderFlags extra_font_builder_flags)
|
bool ImGui_ImplFreeType_FontSrcData::InitFont(FT_Library ft_library, ImFontConfig* src, ImGuiFreeTypeBuilderFlags extra_font_builder_flags)
|
||||||
{
|
{
|
||||||
FT_Error error = FT_New_Memory_Face(ft_library, (uint8_t*)src->FontData, (uint32_t)src->FontDataSize, (uint32_t)src->FontNo, &FtFace);
|
FT_Error error = FT_New_Memory_Face(ft_library, (uint8_t*)src->FontData, (uint32_t)src->FontDataSize, (uint32_t)src->FontNo, &FtFace);
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
return false;
|
return false;
|
||||||
@@ -212,20 +208,20 @@ namespace
|
|||||||
LoadFlags |= FT_LOAD_COLOR;
|
LoadFlags |= FT_LOAD_COLOR;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImGui_ImplFreeType_FontSrcData::CloseFont()
|
void ImGui_ImplFreeType_FontSrcData::CloseFont()
|
||||||
{
|
{
|
||||||
if (FtFace)
|
if (FtFace)
|
||||||
{
|
{
|
||||||
FT_Done_Face(FtFace);
|
FT_Done_Face(FtFace);
|
||||||
FtFace = nullptr;
|
FtFace = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@@ -262,10 +258,10 @@ 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;
|
||||||
const uint32_t h = ft_bitmap->rows;
|
const uint32_t h = ft_bitmap->rows;
|
||||||
@@ -312,8 +308,7 @@ namespace
|
|||||||
default:
|
default:
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user