Skip to content

Commit 0be4f66

Browse files
committed
ImFontAtlas: Shuffling some code inside Build() to make upcoming diffs less confusing (nb: we might break compat with forks of Build() like #618)
1 parent 34d09f2 commit 0be4f66

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

TODO.txt

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
2424
- window: increase minimum size of a window with menus or fix the menu rendering so that it doesn't look odd.
2525
- window: double-clicking on title bar to minimize isn't consistent, perhaps move to single-click on left-most collapse icon?
2626
- window: expose contents size. (#1045)
27+
- window: GetWindowSize() returns (0,0) when not calculated? (#1045)
2728
!- scrolling: allow immediately effective change of scroll after Begin() if we haven't appended items yet.
2829
- scrolling/clipping: separator on the initial position of a window is not visible (cursorpos.y <= clippos.y)
2930

imgui_draw.cpp

+30-30
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ void ImDrawData::ScaleClipRects(const ImVec2& scale)
10341034
}
10351035

10361036
//-----------------------------------------------------------------------------
1037-
// ImFontAtlas
1037+
// ImFontConfig
10381038
//-----------------------------------------------------------------------------
10391039

10401040
ImFontConfig::ImFontConfig()
@@ -1055,6 +1055,10 @@ ImFontConfig::ImFontConfig()
10551055
memset(Name, 0, sizeof(Name));
10561056
}
10571057

1058+
//-----------------------------------------------------------------------------
1059+
// ImFontAtlas
1060+
//-----------------------------------------------------------------------------
1061+
10581062
ImFontAtlas::ImFontAtlas()
10591063
{
10601064
TexID = NULL;
@@ -1282,40 +1286,19 @@ bool ImFontAtlas::Build()
12821286
TexUvWhitePixel = ImVec2(0, 0);
12831287
ClearTexData();
12841288

1285-
struct ImFontTempBuildData
1286-
{
1287-
stbtt_fontinfo FontInfo;
1288-
stbrp_rect* Rects;
1289-
stbtt_pack_range* Ranges;
1290-
int RangesCount;
1291-
};
1292-
ImFontTempBuildData* tmp_array = (ImFontTempBuildData*)ImGui::MemAlloc((size_t)ConfigData.Size * sizeof(ImFontTempBuildData));
1293-
1294-
// Initialize font information early (so we can error without any cleanup) + count glyphs
1289+
// Count glyphs/ranges
12951290
int total_glyph_count = 0;
12961291
int total_glyph_range_count = 0;
12971292
for (int input_i = 0; input_i < ConfigData.Size; input_i++)
12981293
{
12991294
ImFontConfig& cfg = ConfigData[input_i];
1300-
ImFontTempBuildData& tmp = tmp_array[input_i];
1301-
1302-
IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == this));
1303-
const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo);
1304-
IM_ASSERT(font_offset >= 0);
1305-
if (!stbtt_InitFont(&tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset))
1306-
return false;
1307-
1308-
// Count glyphs
13091295
if (!cfg.GlyphRanges)
13101296
cfg.GlyphRanges = GetGlyphRangesDefault();
1311-
for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2)
1312-
{
1297+
for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2, total_glyph_range_count++)
13131298
total_glyph_count += (in_range[1] - in_range[0]) + 1;
1314-
total_glyph_range_count++;
1315-
}
13161299
}
13171300

1318-
// Start packing. We need a known width for the skyline algorithm. Using a cheap heuristic here to decide of width. User can override TexDesiredWidth and TexGlyphPadding if they wish.
1301+
// Start packing. We need a known width for the skyline algorithm. Using a dumb heuristic here to decide of width. User can override TexDesiredWidth and TexGlyphPadding if they wish.
13191302
// After packing is done, width shouldn't matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
13201303
TexWidth = (TexDesiredWidth > 0) ? TexDesiredWidth : (total_glyph_count > 4000) ? 4096 : (total_glyph_count > 2000) ? 2048 : (total_glyph_count > 1000) ? 1024 : 512;
13211304
TexHeight = 0;
@@ -1332,6 +1315,26 @@ bool ImFontAtlas::Build()
13321315
if (extra_rects[i].was_packed)
13331316
TexHeight = ImMax(TexHeight, extra_rects[i].y + extra_rects[i].h);
13341317

1318+
// Initialize font information (so we can error without any cleanup)
1319+
struct ImFontTempBuildData
1320+
{
1321+
stbtt_fontinfo FontInfo;
1322+
stbrp_rect* Rects;
1323+
stbtt_pack_range* Ranges;
1324+
int RangesCount;
1325+
};
1326+
ImFontTempBuildData* tmp_array = (ImFontTempBuildData*)ImGui::MemAlloc((size_t)ConfigData.Size * sizeof(ImFontTempBuildData));
1327+
for (int input_i = 0; input_i < ConfigData.Size; input_i++)
1328+
{
1329+
ImFontConfig& cfg = ConfigData[input_i];
1330+
ImFontTempBuildData& tmp = tmp_array[input_i];
1331+
IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == this));
1332+
const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo);
1333+
IM_ASSERT(font_offset >= 0);
1334+
if (!stbtt_InitFont(&tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset))
1335+
return false;
1336+
}
1337+
13351338
// Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0)
13361339
int buf_packedchars_n = 0, buf_rects_n = 0, buf_ranges_n = 0;
13371340
stbtt_packedchar* buf_packedchars = (stbtt_packedchar*)ImGui::MemAlloc(total_glyph_count * sizeof(stbtt_packedchar));
@@ -1350,11 +1353,8 @@ bool ImFontAtlas::Build()
13501353
// Setup ranges
13511354
int glyph_count = 0;
13521355
int glyph_ranges_count = 0;
1353-
for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2)
1354-
{
1356+
for (const ImWchar* in_range = cfg.GlyphRanges; in_range[0] && in_range[1]; in_range += 2, glyph_ranges_count++)
13551357
glyph_count += (in_range[1] - in_range[0]) + 1;
1356-
glyph_ranges_count++;
1357-
}
13581358
tmp.Ranges = buf_ranges + buf_ranges_n;
13591359
tmp.RangesCount = glyph_ranges_count;
13601360
buf_ranges_n += glyph_ranges_count;
@@ -1392,7 +1392,7 @@ bool ImFontAtlas::Build()
13921392
spc.pixels = TexPixelsAlpha8;
13931393
spc.height = TexHeight;
13941394

1395-
// Second pass: render characters
1395+
// Second pass: render font characters
13961396
for (int input_i = 0; input_i < ConfigData.Size; input_i++)
13971397
{
13981398
ImFontConfig& cfg = ConfigData[input_i];

0 commit comments

Comments
 (0)