16 const bool pressed = [&] {
18 return ImGui::Button(label, ImVec2(button_size, button_size));
20 if (!ImGui::IsItemVisible())
23 unsigned int codepoint = 0;
24 ImTextCharFromUtf8(&codepoint, label,
nullptr);
25 auto* baked = ImGui::GetFontBaked();
26 const auto* glyph = baked->FindGlyph(
static_cast<ImWchar
>(codepoint));
27 if (!glyph || !glyph->Visible)
30 const float font_scale = ImGui::GetFontSize() / baked->Size;
31 const float width = (glyph->X1 - glyph->X0) * font_scale;
32 const float height = (glyph->Y1 - glyph->Y0) * font_scale;
33 if (width <= 0 || height <= 0)
35 const ImVec2 min = ImGui::GetItemRectMin();
36 const ImVec2 max = ImGui::GetItemRectMax();
38 const float inset = ImGui::GetFontSize() / 8.0f;
39 const float available = std::max(1.0f, button_size - 2 * inset);
40 const float fit = std::min({1.0f, available / width, available / height});
41 const ImVec2 glyph_size(width * fit, height * fit);
42 const ImVec2 glyph_min((min.x + max.x - glyph_size.x) * 0.5f,
43 (min.y + max.y - glyph_size.y) * 0.5f);
44 ImGui::GetWindowDrawList()->AddImage(
45 ImGui::GetFont()->OwnerAtlas->TexRef, glyph_min,
46 ImVec2(glyph_min.x + glyph_size.x, glyph_min.y + glyph_size.y),
47 ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1),
48 ImGui::GetColorU32(ImGuiCol_Text));