ImFont: Added FindGlyphNoFallback. Fixed MergeMode broken by 1ef1acbd8d

This commit is contained in:
omar 2018-03-10 10:33:12 +01:00
parent 351b3fa7b0
commit 1f6ad7a894
4 changed files with 22 additions and 16 deletions

View File

@ -1784,6 +1784,7 @@ struct ImFont
IMGUI_API void ClearOutputData(); IMGUI_API void ClearOutputData();
IMGUI_API void BuildLookupTable(); IMGUI_API void BuildLookupTable();
IMGUI_API const ImFontGlyph*FindGlyph(ImWchar c) const; IMGUI_API const ImFontGlyph*FindGlyph(ImWchar c) const;
IMGUI_API const ImFontGlyph*FindGlyphNoFallback(ImWchar c) const;
IMGUI_API void SetFallbackChar(ImWchar c); IMGUI_API void SetFallbackChar(ImWchar c);
float GetCharAdvance(ImWchar c) const { return ((int)c < IndexAdvanceX.Size) ? IndexAdvanceX[(int)c] : FallbackAdvanceX; } float GetCharAdvance(ImWchar c) const { return ((int)c < IndexAdvanceX.Size) ? IndexAdvanceX[(int)c] : FallbackAdvanceX; }
bool IsLoaded() const { return ContainerAtlas != NULL; } bool IsLoaded() const { return ContainerAtlas != NULL; }

View File

@ -2222,13 +2222,11 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size))
{ {
// Display all glyphs of the fonts in separate pages of 256 characters // Display all glyphs of the fonts in separate pages of 256 characters
const ImFontGlyph* glyph_fallback = font->FallbackGlyph; // Forcefully/dodgily make FindGlyph() return NULL on fallback, which isn't the default behavior.
font->FallbackGlyph = NULL;
for (int base = 0; base < 0x10000; base += 256) for (int base = 0; base < 0x10000; base += 256)
{ {
int count = 0; int count = 0;
for (int n = 0; n < 256; n++) for (int n = 0; n < 256; n++)
count += font->FindGlyph((ImWchar)(base + n)) ? 1 : 0; count += font->FindGlyphNoFallback((ImWchar)(base + n)) ? 1 : 0;
if (count > 0 && ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base+255, count, count > 1 ? "glyphs" : "glyph")) if (count > 0 && ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base+255, count, count > 1 ? "glyphs" : "glyph"))
{ {
float cell_spacing = style.ItemSpacing.y; float cell_spacing = style.ItemSpacing.y;
@ -2239,7 +2237,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
{ {
ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size.x + cell_spacing), base_pos.y + (n / 16) * (cell_size.y + cell_spacing)); ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size.x + cell_spacing), base_pos.y + (n / 16) * (cell_size.y + cell_spacing));
ImVec2 cell_p2(cell_p1.x + cell_size.x, cell_p1.y + cell_size.y); ImVec2 cell_p2(cell_p1.x + cell_size.x, cell_p1.y + cell_size.y);
const ImFontGlyph* glyph = font->FindGlyph((ImWchar)(base+n)); const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base+n));
draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255,255,255,100) : IM_COL32(255,255,255,50)); draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255,255,255,100) : IM_COL32(255,255,255,50));
font->RenderChar(draw_list, cell_size.x, cell_p1, ImGui::GetColorU32(ImGuiCol_Text), (ImWchar)(base+n)); // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions available to generate a string. font->RenderChar(draw_list, cell_size.x, cell_p1, ImGui::GetColorU32(ImGuiCol_Text), (ImWchar)(base+n)); // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions available to generate a string.
if (glyph && ImGui::IsMouseHoveringRect(cell_p1, cell_p2)) if (glyph && ImGui::IsMouseHoveringRect(cell_p1, cell_p2))
@ -2257,7 +2255,6 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
ImGui::TreePop(); ImGui::TreePop();
} }
} }
font->FallbackGlyph = glyph_fallback;
ImGui::TreePop(); ImGui::TreePop();
} }
ImGui::TreePop(); ImGui::TreePop();

View File

@ -1841,7 +1841,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
continue; continue;
const int codepoint = range.first_unicode_codepoint_in_range + char_idx; const int codepoint = range.first_unicode_codepoint_in_range + char_idx;
if (cfg.MergeMode && dst_font->FindGlyph((unsigned short)codepoint)) if (cfg.MergeMode && dst_font->FindGlyphNoFallback((unsigned short)codepoint))
continue; continue;
stbtt_aligned_quad q; stbtt_aligned_quad q;
@ -2205,8 +2205,7 @@ void ImFont::BuildLookupTable()
IndexLookup[(int)tab_glyph.Codepoint] = (unsigned short)(Glyphs.Size-1); IndexLookup[(int)tab_glyph.Codepoint] = (unsigned short)(Glyphs.Size-1);
} }
FallbackGlyph = NULL; FallbackGlyph = FindGlyphNoFallback(FallbackChar);
FallbackGlyph = FindGlyph(FallbackChar);
FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f; FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f;
for (int i = 0; i < max_codepoint + 1; i++) for (int i = 0; i < max_codepoint + 1; i++)
if (IndexAdvanceX[i] < 0.0f) if (IndexAdvanceX[i] < 0.0f)
@ -2268,13 +2267,22 @@ void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst)
const ImFontGlyph* ImFont::FindGlyph(ImWchar c) const const ImFontGlyph* ImFont::FindGlyph(ImWchar c) const
{ {
if (c < IndexLookup.Size) if (c >= IndexLookup.Size)
{ return FallbackGlyph;
const unsigned short i = IndexLookup[c]; const unsigned short i = IndexLookup[c];
if (i != (unsigned short)-1) if (i == (unsigned short)-1)
return &Glyphs.Data[i]; return FallbackGlyph;
} return &Glyphs.Data[i];
return FallbackGlyph; }
const ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c) const
{
if (c >= IndexLookup.Size)
return NULL;
const unsigned short i = IndexLookup[c];
if (i == (unsigned short)-1)
return NULL;
return &Glyphs.Data[i];
} }
const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const

View File

@ -333,7 +333,7 @@ bool ImGuiFreeType::BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags)
{ {
for (uint32_t codepoint = in_range[0]; codepoint <= in_range[1]; ++codepoint) for (uint32_t codepoint = in_range[0]; codepoint <= in_range[1]; ++codepoint)
{ {
if (cfg.MergeMode && dst_font->FindGlyph((unsigned short)codepoint)) if (cfg.MergeMode && dst_font->FindGlyphNoFallback((unsigned short)codepoint))
continue; continue;
FT_Glyph ft_glyph = NULL; FT_Glyph ft_glyph = NULL;