Fonts: Added some details about using custom colorful icons.

This commit is contained in:
omar
2019-05-28 20:17:15 +02:00
parent 70d9f79312
commit c487bc52a2
2 changed files with 44 additions and 4 deletions

View File

@ -26,6 +26,7 @@ If you have other loading/merging/adding fonts, you can post on the Dear ImGui "
- Fonts Loading Instructions
- FreeType rasterizer, Small font sizes
- Building Custom Glyph Ranges
- Using custom colorful icons
- Embedding Fonts in Source Code
- Credits/Licences for fonts included in this folder
- Fonts Links
@ -198,6 +199,43 @@ For example: for a game where your script is known, if you can feed your entire
io.Fonts->Build(); // Build the atlas while 'ranges' is still in scope and not deleted.
---------------------------------------
USING CUSTOM COLORFUL ICONS
---------------------------------------
(This is a BETA api, use if you are familiar with dear imgui and with your rendering back-end)
You can use the ImFontAtlas::AddCustomRect() and ImFontAtlas::AddCustomRectFontGlyph() api to register rectangles
that will be packed into the font atlas texture. Register them before building the atlas, then call Build().
You can then use ImFontAtlas::GetCustomRectByIndex(int) to query the position/size of your rectangle within the
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
ImFont* font = io.Fonts->AddFontDefault();
int rect_id = io.Fonts->AddCustomRectFontGlyph(font, 'a', 13, 13, 13+1);
// Build atlas
io.Fonts->Build();
// Retrieve texture in RGBA format
unsigned char* tex_pixels = NULL;
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)
if (const ImFontAtlas::CustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id))
{
for (int y = 0; y < rect->Height; y++)
{
ImU32* p = (ImU32*)tex_pixels + (rect->Y + y) * tex_width + (rect->X);
for (int x = rect->Width; x > 0; x--)
*p++ = IM_COL32(255, 0, 0, 255);
}
}
---------------------------------------
EMBEDDING FONTS IN SOURCE CODE
---------------------------------------