From 4b95e7c2f3d5793bf73c7e8b4c130696010d2f19 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 26 Jun 2019 12:15:32 +0200 Subject: [PATCH] Doc: Tweak and extra mention of AddCustomRectFontGlyph + made the example register two rectangles. --- imgui.cpp | 2 ++ misc/fonts/README.txt | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 0df747f8a..79c2de90f 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -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: diff --git a/misc/fonts/README.txt b/misc/fonts/README.txt index 010a72070..cf953cf6f 100644 --- a/misc/fonts/README.txt +++ b/misc/fonts/README.txt @@ -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,14 +226,18 @@ 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) - if (const ImFontAtlas::CustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id)) + for (int rect_n = 0; rect_n < IM_ARRAYSIZE(rect_ids); rect_n++) { - for (int y = 0; y < rect->Height; y++) + int rect_id = rects_ids[rect_n]; + if (const ImFontAtlas::CustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id)) { - 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); + // 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); + for (int x = rect->Width; x > 0; x--) + *p++ = IM_COL32(255, 0, 0, 255); + } } }