Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dummy glyphs in font atlas #1703

Closed
r-lyeh opened this issue Mar 27, 2018 · 7 comments
Closed

Dummy glyphs in font atlas #1703

r-lyeh opened this issue Mar 27, 2018 · 7 comments
Milestone

Comments

@r-lyeh
Copy link

r-lyeh commented Mar 27, 2018

Hello omar,

image

I've found about a few thousand dummy glyphs in my font atlas when loading a CJK font.
See attached pic.

Is there a quick way to detect them and prevent them included in the final font atlas?
I would rather like a '?'/unused glyph in those dummy boxes instead :)

Ty!

PS: ImGui 1.60 beta

@r-lyeh r-lyeh changed the title Many dummy glyphs in font atlas Dummy glyphs in font atlas Mar 27, 2018
@ocornut
Copy link
Owner

ocornut commented Mar 27, 2018

Hello,
Yes this indeed an issue with the current code. Will tackle it shortly. This is essentially waiting for the same solution as #1671.
The stbtt_PackFontRangesGatherRects() call in stb_truetype don't check for missing glyph which ends up rendering the first glyph of the font, we need to refactor a bit of the font building function.

@r-lyeh
Copy link
Author

r-lyeh commented Mar 27, 2018

Aha great! Ty for pointing that out.
I'll leave the issue open for you as a reminder.
For now, and as a workaround, I've just discarded glyph as well, only if both dummy_x and dummy_y are 0.

@r-lyeh
Copy link
Author

r-lyeh commented Mar 27, 2018

2nd try: I've just tweaked stb_truetype to forbid baking of missing glyphs. Feels hacky but texture atlas fits nicely!

image

@r-lyeh
Copy link
Author

r-lyeh commented Mar 27, 2018

diff --git a/source/editor/3rd/@ocornut/imgui_draw.cpp b/source/editor/3rd/@ocornut/imgui_draw.cpp
index a2d0215..090fd24 100644
--- a/source/editor/3rd/@ocornut/imgui_draw.cpp
+++ b/source/editor/3rd/@ocornut/imgui_draw.cpp
@@ -1780,7 +1780,7 @@ bool    ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
         buf_rects_n += font_glyphs_count;
         stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV);
         int n = stbtt_PackFontRangesGatherRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects);
-        IM_ASSERT(n == font_glyphs_count);
+        font_glyphs_count = n; // IM_ASSERT(n == font_glyphs_count);
         stbrp_pack_rects((stbrp_context*)spc.pack_info, tmp.Rects, n);

         // Extend texture height
@@ -1858,6 +1858,7 @@ bool    ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
                 stbtt_aligned_quad q;
                 float dummy_x = 0.0f, dummy_y = 0.0f;
                 stbtt_GetPackedQuad(range.chardata_for_range, atlas->TexWidth, atlas->TexHeight, char_idx, &dummy_x, &dummy_y, &q, 0);
+                //if( dummy_x != 0 ) // @r-lyeh: see https://github.com/ocornut/imgui/issues/1671
                 dst_font->AddGlyph((ImWchar)codepoint, q.x0 + off_x, q.y0 + off_y, q.x1 + off_x, q.y1 + off_y, q.s0, q.t0, q.s1, q.t1, pc.xadvance);
             }
         }
diff --git a/source/editor/3rd/@ocornut/stb_truetype.h b/source/editor/3rd/@ocornut/stb_truetype.h
index f65deb5..7ab78ff 100644
--- a/source/editor/3rd/@ocornut/stb_truetype.h
+++ b/source/editor/3rd/@ocornut/stb_truetype.h
@@ -3645,6 +3645,7 @@ static int stbtt_BakeFontBitmap_internal(unsigned char *data, int offset,  // fo
    for (i=0; i < num_chars; ++i) {
       int advance, lsb, x0,y0,x1,y1,gw,gh;
       int g = stbtt_FindGlyphIndex(&f, first_char + i);
+      if(!g) continue; //@r-lyeh
       stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
       stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
       gw = x1-x0;
@@ -3968,6 +3969,7 @@ STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stb
          int x0,y0,x1,y1;
          int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
          int glyph = stbtt_FindGlyphIndex(info, codepoint);
+         if(!glyph) continue; //@r-lyeh
          stbtt_GetGlyphBitmapBoxSubpixel(info,glyph,
                                          scale * spc->h_oversample,
                                          scale * spc->v_oversample,
@@ -4032,6 +4034,7 @@ STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const
             int advance, lsb, x0,y0,x1,y1;
             int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
             int glyph = stbtt_FindGlyphIndex(info, codepoint);
+            if(!glyph) continue; //@r-lyeh
             stbrp_coord pad = (stbrp_coord) spc->padding;

@ocornut ocornut modified the milestones: v1.60, v1.61 Apr 4, 2018
ocornut added a commit that referenced this issue May 7, 2018
…nd 2) allow merging fonts with overlapping ranges. Demo: Fixed displaying ? instead of greyed out empty box. (#1671, #1703)
@ocornut
Copy link
Owner

ocornut commented May 7, 2018

@r-lyeh This should be fixed in master now, could you confirm it does what you want with your font?

(Note that it was a minor bug that the Demo displayed ? at all here, the greyed out rectangles were meant to be left empty)

I submitted my modification to nothings/stb, I'm not sure it'll be merged as is (perhaps as an option to the packer) but either way I'll follow on that and hopefully the next version of stb_truetype will match our mod.

@r-lyeh
Copy link
Author

r-lyeh commented May 8, 2018

Cool. I'll let you know!

@r-lyeh
Copy link
Author

r-lyeh commented May 8, 2018

No problems found :)

@r-lyeh r-lyeh closed this as completed May 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants