mirror of
https://github.com/ocornut/imgui.git
synced 2026-01-11 00:04:24 +00:00
Fonts: rework ImFontLoader::FontBakedLoadGlyph() interface
This commit is contained in:
parent
4dec946ae6
commit
8523cbdf58
3 changed files with 36 additions and 46 deletions
|
|
@ -4393,12 +4393,15 @@ static ImFontGlyph* ImFontBaked_BuildLoadGlyph(ImFontBaked* baked, ImWchar codep
|
||||||
{
|
{
|
||||||
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
|
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
|
||||||
if (!src->GlyphExcludeRanges || ImFontAtlasBuildAcceptCodepointForSource(src, codepoint))
|
if (!src->GlyphExcludeRanges || ImFontAtlasBuildAcceptCodepointForSource(src, codepoint))
|
||||||
if (ImFontGlyph* glyph = loader->FontBakedLoadGlyph(atlas, src, baked, loader_user_data_p, codepoint))
|
{
|
||||||
|
ImFontGlyph glyph_buf;
|
||||||
|
if (loader->FontBakedLoadGlyph(atlas, src, baked, loader_user_data_p, codepoint, &glyph_buf))
|
||||||
{
|
{
|
||||||
// FIXME: Add hooks for e.g. #7962
|
// FIXME: Add hooks for e.g. #7962
|
||||||
glyph->SourceIdx = src_n;
|
glyph_buf.SourceIdx = src_n;
|
||||||
return glyph;
|
return ImFontAtlasBakedAddFontGlyph(atlas, baked, src, &glyph_buf);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
loader_user_data_p += loader->FontBakedSrcLoaderDataSize;
|
loader_user_data_p += loader->FontBakedSrcLoaderDataSize;
|
||||||
src_n++;
|
src_n++;
|
||||||
}
|
}
|
||||||
|
|
@ -4539,14 +4542,14 @@ static bool ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ImFontGlyph* ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, ImWchar codepoint)
|
static bool ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, ImWchar codepoint, ImFontGlyph* out_glyph)
|
||||||
{
|
{
|
||||||
// Search for first font which has the glyph
|
// Search for first font which has the glyph
|
||||||
ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData;
|
ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData;
|
||||||
IM_ASSERT(bd_font_data);
|
IM_ASSERT(bd_font_data);
|
||||||
int glyph_index = stbtt_FindGlyphIndex(&bd_font_data->FontInfo, (int)codepoint);
|
int glyph_index = stbtt_FindGlyphIndex(&bd_font_data->FontInfo, (int)codepoint);
|
||||||
if (glyph_index == 0)
|
if (glyph_index == 0)
|
||||||
return NULL;
|
return false;
|
||||||
|
|
||||||
// Fonts unit to pixels
|
// Fonts unit to pixels
|
||||||
int oversample_h, oversample_v;
|
int oversample_h, oversample_v;
|
||||||
|
|
@ -4564,10 +4567,8 @@ static ImFontGlyph* ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas,
|
||||||
const bool is_visible = (x0 != x1 && y0 != y1);
|
const bool is_visible = (x0 != x1 && y0 != y1);
|
||||||
|
|
||||||
// Prepare glyph
|
// Prepare glyph
|
||||||
ImFontGlyph glyph_in = {};
|
out_glyph->Codepoint = codepoint;
|
||||||
ImFontGlyph* glyph = &glyph_in;
|
out_glyph->AdvanceX = advance * scale_for_layout;
|
||||||
glyph->Codepoint = codepoint;
|
|
||||||
glyph->AdvanceX = advance * scale_for_layout;
|
|
||||||
|
|
||||||
// Pack and retrieve position inside texture atlas
|
// Pack and retrieve position inside texture atlas
|
||||||
// (generally based on stbtt_PackFontRangesRenderIntoRects)
|
// (generally based on stbtt_PackFontRangesRenderIntoRects)
|
||||||
|
|
@ -4580,7 +4581,7 @@ static ImFontGlyph* ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas,
|
||||||
{
|
{
|
||||||
// Pathological out of memory case (TexMaxWidth/TexMaxHeight set too small?)
|
// Pathological out of memory case (TexMaxWidth/TexMaxHeight set too small?)
|
||||||
IM_ASSERT(pack_id != ImFontAtlasRectId_Invalid && "Out of texture memory.");
|
IM_ASSERT(pack_id != ImFontAtlasRectId_Invalid && "Out of texture memory.");
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id);
|
ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id);
|
||||||
|
|
||||||
|
|
@ -4615,21 +4616,16 @@ static ImFontGlyph* ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas,
|
||||||
// Register glyph
|
// Register glyph
|
||||||
// r->x r->y are coordinates inside texture (in pixels)
|
// r->x r->y are coordinates inside texture (in pixels)
|
||||||
// glyph.X0, glyph.Y0 are drawing coordinates from base text position, and accounting for oversampling.
|
// glyph.X0, glyph.Y0 are drawing coordinates from base text position, and accounting for oversampling.
|
||||||
glyph->X0 = x0 * recip_h + font_off_x;
|
out_glyph->X0 = x0 * recip_h + font_off_x;
|
||||||
glyph->Y0 = y0 * recip_v + font_off_y;
|
out_glyph->Y0 = y0 * recip_v + font_off_y;
|
||||||
glyph->X1 = (x0 + (int)r->w) * recip_h + font_off_x;
|
out_glyph->X1 = (x0 + (int)r->w) * recip_h + font_off_x;
|
||||||
glyph->Y1 = (y0 + (int)r->h) * recip_v + font_off_y;
|
out_glyph->Y1 = (y0 + (int)r->h) * recip_v + font_off_y;
|
||||||
glyph->Visible = true;
|
out_glyph->Visible = true;
|
||||||
glyph->PackId = pack_id;
|
out_glyph->PackId = pack_id;
|
||||||
glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, src, glyph);
|
ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, out_glyph, r, bitmap_pixels, ImTextureFormat_Alpha8, w);
|
||||||
ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, glyph, r, bitmap_pixels, ImTextureFormat_Alpha8, w);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, src, glyph);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return glyph;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype()
|
const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype()
|
||||||
|
|
|
||||||
|
|
@ -3669,7 +3669,7 @@ namespace ImGui
|
||||||
|
|
||||||
// Hooks and storage for a given font backend.
|
// Hooks and storage for a given font backend.
|
||||||
// This structure is likely to evolve as we add support for incremental atlas updates.
|
// This structure is likely to evolve as we add support for incremental atlas updates.
|
||||||
// Conceptually this could be in ImGuiPlatformIO, but we are far from ready to make this public.
|
// Conceptually this could be public, but API is still going to be evolve.
|
||||||
struct ImFontLoader
|
struct ImFontLoader
|
||||||
{
|
{
|
||||||
const char* Name;
|
const char* Name;
|
||||||
|
|
@ -3680,7 +3680,7 @@ struct ImFontLoader
|
||||||
bool (*FontSrcContainsGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint);
|
bool (*FontSrcContainsGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint);
|
||||||
bool (*FontBakedInit)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src);
|
bool (*FontBakedInit)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src);
|
||||||
void (*FontBakedDestroy)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src);
|
void (*FontBakedDestroy)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src);
|
||||||
ImFontGlyph* (*FontBakedLoadGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint);
|
bool (*FontBakedLoadGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint, ImFontGlyph* out_glyph);
|
||||||
|
|
||||||
// Size of backend data, Per Baked * Per Source. Buffers are managed by core to avoid excessive allocations.
|
// Size of backend data, Per Baked * Per Source. Buffers are managed by core to avoid excessive allocations.
|
||||||
// FIXME: At this point the two other types of buffers may be managed by core to be consistent?
|
// FIXME: At this point the two other types of buffers may be managed by core to be consistent?
|
||||||
|
|
|
||||||
|
|
@ -473,12 +473,12 @@ void ImGui_ImplFreeType_FontBakedDestroy(ImFontAtlas* atlas, ImFontConfig* src,
|
||||||
bd_baked_data->~ImGui_ImplFreeType_FontSrcBakedData(); // ~IM_PLACEMENT_DELETE()
|
bd_baked_data->~ImGui_ImplFreeType_FontSrcBakedData(); // ~IM_PLACEMENT_DELETE()
|
||||||
}
|
}
|
||||||
|
|
||||||
ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint)
|
bool ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint, ImFontGlyph* out_glyph)
|
||||||
{
|
{
|
||||||
ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
|
ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
|
||||||
uint32_t glyph_index = FT_Get_Char_Index(bd_font_data->FtFace, codepoint);
|
uint32_t glyph_index = FT_Get_Char_Index(bd_font_data->FtFace, codepoint);
|
||||||
if (glyph_index == 0)
|
if (glyph_index == 0)
|
||||||
return NULL;
|
return false;
|
||||||
|
|
||||||
if (bd_font_data->BakedLastActivated != baked) // <-- could use id
|
if (bd_font_data->BakedLastActivated != baked) // <-- could use id
|
||||||
{
|
{
|
||||||
|
|
@ -490,7 +490,7 @@ ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontCon
|
||||||
|
|
||||||
const FT_Glyph_Metrics* metrics = ImGui_ImplFreeType_LoadGlyph(bd_font_data, codepoint);
|
const FT_Glyph_Metrics* metrics = ImGui_ImplFreeType_LoadGlyph(bd_font_data, codepoint);
|
||||||
if (metrics == NULL)
|
if (metrics == NULL)
|
||||||
return NULL;
|
return false;
|
||||||
|
|
||||||
// Render glyph into a bitmap (currently held by FreeType)
|
// Render glyph into a bitmap (currently held by FreeType)
|
||||||
FT_Face face = bd_font_data->FtFace;
|
FT_Face face = bd_font_data->FtFace;
|
||||||
|
|
@ -507,10 +507,8 @@ ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontCon
|
||||||
const float rasterizer_density = src->RasterizerDensity * baked->RasterizerDensity;
|
const float rasterizer_density = src->RasterizerDensity * baked->RasterizerDensity;
|
||||||
|
|
||||||
// Prepare glyph
|
// Prepare glyph
|
||||||
ImFontGlyph glyph_in = {};
|
out_glyph->Codepoint = codepoint;
|
||||||
ImFontGlyph* glyph = &glyph_in;
|
out_glyph->AdvanceX = (slot->advance.x / FT_SCALEFACTOR) / rasterizer_density;
|
||||||
glyph->Codepoint = codepoint;
|
|
||||||
glyph->AdvanceX = (slot->advance.x / FT_SCALEFACTOR) / rasterizer_density;
|
|
||||||
|
|
||||||
// Pack and retrieve position inside texture atlas
|
// Pack and retrieve position inside texture atlas
|
||||||
if (is_visible)
|
if (is_visible)
|
||||||
|
|
@ -542,21 +540,17 @@ ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontCon
|
||||||
// Register glyph
|
// Register glyph
|
||||||
float glyph_off_x = (float)face->glyph->bitmap_left;
|
float glyph_off_x = (float)face->glyph->bitmap_left;
|
||||||
float glyph_off_y = (float)-face->glyph->bitmap_top;
|
float glyph_off_y = (float)-face->glyph->bitmap_top;
|
||||||
glyph->X0 = glyph_off_x * recip_h + font_off_x;
|
out_glyph->X0 = glyph_off_x * recip_h + font_off_x;
|
||||||
glyph->Y0 = glyph_off_y * recip_v + font_off_y;
|
out_glyph->Y0 = glyph_off_y * recip_v + font_off_y;
|
||||||
glyph->X1 = (glyph_off_x + w) * recip_h + font_off_x;
|
out_glyph->X1 = (glyph_off_x + w) * recip_h + font_off_x;
|
||||||
glyph->Y1 = (glyph_off_y + h) * recip_v + font_off_y;
|
out_glyph->Y1 = (glyph_off_y + h) * recip_v + font_off_y;
|
||||||
glyph->Visible = true;
|
out_glyph->Visible = true;
|
||||||
glyph->Colored = (ft_bitmap->pixel_mode == FT_PIXEL_MODE_BGRA);
|
out_glyph->Colored = (ft_bitmap->pixel_mode == FT_PIXEL_MODE_BGRA);
|
||||||
glyph->PackId = pack_id;
|
out_glyph->PackId = pack_id;
|
||||||
glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, src, glyph);
|
ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, out_glyph, r, (const unsigned char*)temp_buffer, ImTextureFormat_RGBA32, w * 4);
|
||||||
ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, glyph, r, (const unsigned char*)temp_buffer, ImTextureFormat_RGBA32, w * 4);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
return true;
|
||||||
glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, src, glyph);
|
|
||||||
}
|
|
||||||
return glyph;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ImGui_ImplFreetype_FontSrcContainsGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint)
|
bool ImGui_ImplFreetype_FontSrcContainsGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue