Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit e585204

Browse files
committed
ImFontAtlas: heuristic increase texture width up to 4096 with 4000+ glyphs. Various comments (ocornut#491)
1 parent 3922988 commit e585204

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

extra_fonts/README.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
The code in imgui.cpp embeds a copy of 'ProggyClean.ttf' that you can use without any external files.
33
Those are only provided as a convenience, you can load your own .TTF files.
44

5+
Fonts are rasterized in a single texture at the time of calling either of io.Fonts.GetTexDataAsAlpha8()/GetTexDataAsRGBA32()/Build().
6+
57
---------------------------------
68
LOADING INSTRUCTIONS
79
---------------------------------
@@ -20,10 +22,17 @@
2022

2123
ImFontConfig config;
2224
config.OversampleH = 3;
23-
config.OversampleV = 3;
25+
config.OversampleV = 1;
2426
config.GlyphExtraSpacing.x = 1.0f;
2527
io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels, &config);
2628

29+
If you have very large number of glyphs or multiple fonts:
30+
31+
- Mind the fact that some graphics drivers have texture size limitation.
32+
- Set io.Fonts.TexDesiredWidth to specify a texture width to minimize texture height (see comment in ImFontAtlas::Build function).
33+
- You may reduce oversampling, e.g. config.OversampleH = 2 or 1.
34+
- Reduce glyphs ranges, consider calculating them based on your source data if this is possible.
35+
2736
Combine two fonts into one:
2837

2938
// Load main font

imgui.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@
359359
360360
Q: How can I load multiple fonts?
361361
A: Use the font atlas to pack them into a single texture:
362+
(Read extra_fonts/README.txt and the code in ImFontAtlas for more details.)
362363
363364
ImGuiIO& io = ImGui::GetIO();
364365
ImFont* font0 = io.Fonts->AddFontDefault();
@@ -371,7 +372,7 @@
371372
// Options
372373
ImFontConfig config;
373374
config.OversampleH = 3;
374-
config.OversampleV = 3;
375+
config.OversampleV = 1;
375376
config.GlyphExtraSpacing.x = 1.0f;
376377
io.Fonts->LoadFromFileTTF("myfontfile.ttf", size_pixels, &config);
377378
@@ -383,8 +384,6 @@
383384
io.Fonts->LoadFromFileTTF("fontawesome-webfont.ttf", 16.0f, &config, ranges);
384385
io.Fonts->LoadFromFileTTF("myfontfile.ttf", size_pixels, NULL, &config, io.Fonts->GetGlyphRangesJapanese());
385386
386-
Read extra_fonts/README.txt or ImFontAtlas class for more details.
387-
388387
Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic?
389388
A: When loading a font, pass custom Unicode ranges to specify the glyphs to load. ImGui will support UTF-8 encoding across the board.
390389
Character input depends on you passing the right character code to io.AddInputCharacter(). The example applications do that.

imgui_draw.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,9 @@ bool ImFontAtlas::Build()
12441244
}
12451245
}
12461246

1247-
// Start packing
1248-
TexWidth = (TexDesiredWidth > 0) ? TexDesiredWidth : (total_glyph_count > 2000) ? 2048 : (total_glyph_count > 1000) ? 1024 : 512; // Width doesn't actually matters much but some API/GPU have texture size limitations, and increasing width can decrease height.
1247+
// Start packing. We need a known width for the skyline algorithm. Using a cheap heuristic here to decide of width. User can override TexDesiredWidth if they wish.
1248+
// After packing is done, width shouldn't matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
1249+
TexWidth = (TexDesiredWidth > 0) ? TexDesiredWidth : (total_glyph_count > 4000) ? 4096 : (total_glyph_count > 2000) ? 2048 : (total_glyph_count > 1000) ? 1024 : 512;
12491250
TexHeight = 0;
12501251
const int max_tex_height = 1024*32;
12511252
stbtt_pack_context spc;

0 commit comments

Comments
 (0)