Doc: Tweak and extra mention of AddCustomRectFontGlyph + made the example register two rectangles.

This commit is contained in:
omar 2019-06-26 12:15:32 +02:00
parent eb3e271c24
commit 4b95e7c2f3
2 changed files with 16 additions and 8 deletions

View File

@ -844,6 +844,8 @@ CODE
main font. Then you can refer to icons within your strings.
You may want to see ImFontConfig::GlyphMinAdvanceX to make your icon look monospace to facilitate alignment.
(Read the 'misc/fonts/README.txt' file for more details about icons font loading.)
With some extra effort, you may use colorful icon by registering custom rectangle space inside the font atlas,
and copying your own graphics data into it. See misc/fonts/README.txt about using the AddCustomRectFontGlyph API.
Q: How can I load multiple fonts?
A: Use the font atlas to pack them into a single texture:

View File

@ -212,9 +212,11 @@ texture, and blit/copy any graphics data of your choice into those rectangles.
Pseudo-code:
// Add font, then register one custom 13x13 rectangle mapped to glyph 'a' of this font
// Add font, then register two custom 13x13 rectangles mapped to glyph 'a' and 'b' of this font
ImFont* font = io.Fonts->AddFontDefault();
int rect_id = io.Fonts->AddCustomRectFontGlyph(font, 'a', 13, 13, 13+1);
int rect_ids[2];
rect_ids[0] = io.Fonts->AddCustomRectFontGlyph(font, 'a', 13, 13, 13+1);
rect_ids[1] = io.Fonts->AddCustomRectFontGlyph(font, 'b', 13, 13, 13+1);
// Build atlas
io.Fonts->Build();
@ -224,9 +226,12 @@ Pseudo-code:
int tex_width, tex_height;
io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_width, &tex_height);
// Fill the custom rectangle with red pixels (in reality you would draw/copy your bitmap data here)
for (int rect_n = 0; rect_n < IM_ARRAYSIZE(rect_ids); rect_n++)
{
int rect_id = rects_ids[rect_n];
if (const ImFontAtlas::CustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id))
{
// Fill the custom rectangle with red pixels (in reality you would draw/copy your bitmap data here!)
for (int y = 0; y < rect->Height; y++)
{
ImU32* p = (ImU32*)tex_pixels + (rect->Y + y) * tex_width + (rect->X);
@ -234,6 +239,7 @@ Pseudo-code:
*p++ = IM_COL32(255, 0, 0, 255);
}
}
}
---------------------------------------