@@ -1336,40 +1336,45 @@ void ImFontAtlas::CustomRectCalcUV(const CustomRect* rect, ImVec2* out_uv_min, I
1336
1336
1337
1337
bool ImFontAtlas::Build ()
1338
1338
{
1339
- IM_ASSERT (ConfigData.Size > 0 );
1339
+ return ImFontAtlasBuildWithStbTruetype (this );
1340
+ }
1340
1341
1341
- ImFontAtlasBuildRegisterDefaultCustomRects (this );
1342
+ bool ImFontAtlasBuildWithStbTruetype (ImFontAtlas* atlas)
1343
+ {
1344
+ IM_ASSERT (atlas->ConfigData .Size > 0 );
1342
1345
1343
- TexID = NULL ;
1344
- TexWidth = TexHeight = 0 ;
1345
- TexUvWhitePixel = ImVec2 (0 , 0 );
1346
- ClearTexData ();
1346
+ ImFontAtlasBuildRegisterDefaultCustomRects (atlas);
1347
+
1348
+ atlas->TexID = NULL ;
1349
+ atlas->TexWidth = atlas->TexHeight = 0 ;
1350
+ atlas->TexUvWhitePixel = ImVec2 (0 , 0 );
1351
+ atlas->ClearTexData ();
1347
1352
1348
1353
// Count glyphs/ranges
1349
1354
int total_glyphs_count = 0 ;
1350
1355
int total_ranges_count = 0 ;
1351
- for (int input_i = 0 ; input_i < ConfigData.Size ; input_i++)
1356
+ for (int input_i = 0 ; input_i < atlas-> ConfigData .Size ; input_i++)
1352
1357
{
1353
- ImFontConfig& cfg = ConfigData[input_i];
1358
+ ImFontConfig& cfg = atlas-> ConfigData [input_i];
1354
1359
if (!cfg.GlyphRanges )
1355
- cfg.GlyphRanges = GetGlyphRangesDefault ();
1360
+ cfg.GlyphRanges = atlas-> GetGlyphRangesDefault ();
1356
1361
for (const ImWchar* in_range = cfg.GlyphRanges ; in_range[0 ] && in_range[1 ]; in_range += 2 , total_ranges_count++)
1357
1362
total_glyphs_count += (in_range[1 ] - in_range[0 ]) + 1 ;
1358
1363
}
1359
1364
1360
1365
// We need a width for the skyline algorithm. Using a dumb heuristic here to decide of width. User can override TexDesiredWidth and TexGlyphPadding if they wish.
1361
1366
// Width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
1362
- TexWidth = (TexDesiredWidth > 0 ) ? TexDesiredWidth : (total_glyphs_count > 4000 ) ? 4096 : (total_glyphs_count > 2000 ) ? 2048 : (total_glyphs_count > 1000 ) ? 1024 : 512 ;
1363
- TexHeight = 0 ;
1367
+ atlas-> TexWidth = (atlas-> TexDesiredWidth > 0 ) ? atlas-> TexDesiredWidth : (total_glyphs_count > 4000 ) ? 4096 : (total_glyphs_count > 2000 ) ? 2048 : (total_glyphs_count > 1000 ) ? 1024 : 512 ;
1368
+ atlas-> TexHeight = 0 ;
1364
1369
1365
1370
// Start packing
1366
1371
const int max_tex_height = 1024 *32 ;
1367
1372
stbtt_pack_context spc;
1368
- stbtt_PackBegin (&spc, NULL , TexWidth, max_tex_height, 0 , TexGlyphPadding, NULL );
1373
+ stbtt_PackBegin (&spc, NULL , atlas-> TexWidth , max_tex_height, 0 , atlas-> TexGlyphPadding , NULL );
1369
1374
stbtt_PackSetOversampling (&spc, 1 , 1 );
1370
1375
1371
1376
// Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
1372
- ImFontAtlasBuildPackCustomRects (this , spc.pack_info );
1377
+ ImFontAtlasBuildPackCustomRects (atlas , spc.pack_info );
1373
1378
1374
1379
// Initialize font information (so we can error without any cleanup)
1375
1380
struct ImFontTempBuildData
@@ -1379,12 +1384,13 @@ bool ImFontAtlas::Build()
1379
1384
stbtt_pack_range* Ranges;
1380
1385
int RangesCount;
1381
1386
};
1382
- ImFontTempBuildData* tmp_array = (ImFontTempBuildData*)ImGui::MemAlloc ((size_t )ConfigData.Size * sizeof (ImFontTempBuildData));
1383
- for (int input_i = 0 ; input_i < ConfigData.Size ; input_i++)
1387
+ ImFontTempBuildData* tmp_array = (ImFontTempBuildData*)ImGui::MemAlloc ((size_t )atlas-> ConfigData .Size * sizeof (ImFontTempBuildData));
1388
+ for (int input_i = 0 ; input_i < atlas-> ConfigData .Size ; input_i++)
1384
1389
{
1385
- ImFontConfig& cfg = ConfigData[input_i];
1390
+ ImFontConfig& cfg = atlas-> ConfigData [input_i];
1386
1391
ImFontTempBuildData& tmp = tmp_array[input_i];
1387
- IM_ASSERT (cfg.DstFont && (!cfg.DstFont ->IsLoaded () || cfg.DstFont ->ContainerAtlas == this ));
1392
+ IM_ASSERT (cfg.DstFont && (!cfg.DstFont ->IsLoaded () || cfg.DstFont ->ContainerAtlas == atlas));
1393
+
1388
1394
const int font_offset = stbtt_GetFontOffsetForIndex ((unsigned char *)cfg.FontData , cfg.FontNo );
1389
1395
IM_ASSERT (font_offset >= 0 );
1390
1396
if (!stbtt_InitFont (&tmp.FontInfo , (unsigned char *)cfg.FontData , font_offset))
@@ -1401,9 +1407,9 @@ bool ImFontAtlas::Build()
1401
1407
memset (buf_ranges, 0 , total_ranges_count * sizeof (stbtt_pack_range));
1402
1408
1403
1409
// First font pass: pack all glyphs (no rendering at this point, we are working with rectangles in an infinitely tall texture at this point)
1404
- for (int input_i = 0 ; input_i < ConfigData.Size ; input_i++)
1410
+ for (int input_i = 0 ; input_i < atlas-> ConfigData .Size ; input_i++)
1405
1411
{
1406
- ImFontConfig& cfg = ConfigData[input_i];
1412
+ ImFontConfig& cfg = atlas-> ConfigData [input_i];
1407
1413
ImFontTempBuildData& tmp = tmp_array[input_i];
1408
1414
1409
1415
// Setup ranges
@@ -1436,23 +1442,23 @@ bool ImFontAtlas::Build()
1436
1442
// Extend texture height
1437
1443
for (int i = 0 ; i < n; i++)
1438
1444
if (tmp.Rects [i].was_packed )
1439
- TexHeight = ImMax (TexHeight, tmp.Rects [i].y + tmp.Rects [i].h );
1445
+ atlas-> TexHeight = ImMax (atlas-> TexHeight , tmp.Rects [i].y + tmp.Rects [i].h );
1440
1446
}
1441
1447
IM_ASSERT (buf_rects_n == total_glyphs_count);
1442
1448
IM_ASSERT (buf_packedchars_n == total_glyphs_count);
1443
1449
IM_ASSERT (buf_ranges_n == total_ranges_count);
1444
1450
1445
1451
// Create texture
1446
- TexHeight = ImUpperPowerOfTwo (TexHeight);
1447
- TexPixelsAlpha8 = (unsigned char *)ImGui::MemAlloc (TexWidth * TexHeight);
1448
- memset (TexPixelsAlpha8, 0 , TexWidth * TexHeight);
1449
- spc.pixels = TexPixelsAlpha8;
1450
- spc.height = TexHeight;
1452
+ atlas-> TexHeight = ImUpperPowerOfTwo (atlas-> TexHeight );
1453
+ atlas-> TexPixelsAlpha8 = (unsigned char *)ImGui::MemAlloc (atlas-> TexWidth * atlas-> TexHeight );
1454
+ memset (atlas-> TexPixelsAlpha8 , 0 , atlas-> TexWidth * atlas-> TexHeight );
1455
+ spc.pixels = atlas-> TexPixelsAlpha8 ;
1456
+ spc.height = atlas-> TexHeight ;
1451
1457
1452
1458
// Second pass: render font characters
1453
- for (int input_i = 0 ; input_i < ConfigData.Size ; input_i++)
1459
+ for (int input_i = 0 ; input_i < atlas-> ConfigData .Size ; input_i++)
1454
1460
{
1455
- ImFontConfig& cfg = ConfigData[input_i];
1461
+ ImFontConfig& cfg = atlas-> ConfigData [input_i];
1456
1462
ImFontTempBuildData& tmp = tmp_array[input_i];
1457
1463
stbtt_PackSetOversampling (&spc, cfg.OversampleH , cfg.OversampleV );
1458
1464
stbtt_PackFontRangesRenderIntoRects (&spc, &tmp.FontInfo , tmp.Ranges , tmp.RangesCount , tmp.Rects );
@@ -1465,9 +1471,9 @@ bool ImFontAtlas::Build()
1465
1471
buf_rects = NULL ;
1466
1472
1467
1473
// Third pass: setup ImFont and glyphs for runtime
1468
- for (int input_i = 0 ; input_i < ConfigData.Size ; input_i++)
1474
+ for (int input_i = 0 ; input_i < atlas-> ConfigData .Size ; input_i++)
1469
1475
{
1470
- ImFontConfig& cfg = ConfigData[input_i];
1476
+ ImFontConfig& cfg = atlas-> ConfigData [input_i];
1471
1477
ImFontTempBuildData& tmp = tmp_array[input_i];
1472
1478
ImFont* dst_font = cfg.DstFont ; // We can have multiple input fonts writing into a same destination font (when using MergeMode=true)
1473
1479
@@ -1477,7 +1483,7 @@ bool ImFontAtlas::Build()
1477
1483
1478
1484
float ascent = unscaled_ascent * font_scale;
1479
1485
float descent = unscaled_descent * font_scale;
1480
- ImFontAtlasBuildSetupFont (this , dst_font, &cfg, ascent, descent);
1486
+ ImFontAtlasBuildSetupFont (atlas , dst_font, &cfg, ascent, descent);
1481
1487
float off_x = cfg.GlyphOffset .x ;
1482
1488
float off_y = cfg.GlyphOffset .y + (float )(int )(dst_font->Ascent + 0 .5f );
1483
1489
@@ -1497,7 +1503,7 @@ bool ImFontAtlas::Build()
1497
1503
1498
1504
stbtt_aligned_quad q;
1499
1505
float dummy_x = 0 .0f , dummy_y = 0 .0f ;
1500
- stbtt_GetPackedQuad (range.chardata_for_range , TexWidth, TexHeight, char_idx, &dummy_x, &dummy_y, &q, 0 );
1506
+ stbtt_GetPackedQuad (range.chardata_for_range , atlas-> TexWidth , atlas-> TexHeight , char_idx, &dummy_x, &dummy_y, &q, 0 );
1501
1507
1502
1508
dst_font->Glyphs .resize (dst_font->Glyphs .Size + 1 );
1503
1509
ImFont::Glyph& glyph = dst_font->Glyphs .back ();
@@ -1514,7 +1520,7 @@ bool ImFontAtlas::Build()
1514
1520
1515
1521
if (cfg.PixelSnapH )
1516
1522
glyph.XAdvance = (float )(int )(glyph.XAdvance + 0 .5f );
1517
- dst_font->MetricsTotalSurface += (int )((glyph.U1 - glyph.U0 ) * TexWidth + 1 .99f ) * (int )((glyph.V1 - glyph.V0 ) * TexHeight + 1 .99f ); // +1 to account for average padding, +0.99 to round
1523
+ dst_font->MetricsTotalSurface += (int )((glyph.U1 - glyph.U0 ) * atlas-> TexWidth + 1 .99f ) * (int )((glyph.V1 - glyph.V0 ) * atlas-> TexHeight + 1 .99f ); // +1 to account for average padding, +0.99 to round
1518
1524
}
1519
1525
}
1520
1526
cfg.DstFont ->BuildLookupTable ();
@@ -1526,7 +1532,7 @@ bool ImFontAtlas::Build()
1526
1532
ImGui::MemFree (tmp_array);
1527
1533
1528
1534
// Render into our custom data block
1529
- ImFontAtlasBuildRenderDefaultTexData (this );
1535
+ ImFontAtlasBuildRenderDefaultTexData (atlas );
1530
1536
1531
1537
return true ;
1532
1538
}
0 commit comments