From 1d33ea939f702e0fb1b4201f1032ee63993ba3e4 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 29 May 2026 18:10:29 +0200 Subject: [PATCH] DrawList: added ImDrawListFlags_NoTextPixelSnap to disable snapping of AddText() coordinates for a given scope. (#3437, #9417, #2291) This may evolve into a per-call ImDrawFlags option as well. + clip_rect.Max.y early out may be applied before truncation. --- docs/CHANGELOG.txt | 10 ++++++---- imgui.h | 1 + imgui_draw.cpp | 20 +++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 1c5535835..932ebe129 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -50,15 +50,17 @@ Other Changes: - Clicking on a window's empty-space to move/focus a window checks for lack of queued focus request. (#9382) - InputText: - - Added style.InputTextCursorSize to configure cursor/caret thickness. (#7031, #9409) - This is automatically scaled by style.ScaleAllSizes(). + - Added `style.InputTextCursorSize` to configure cursor/caret thickness. (#7031, #9409) + This is automatically scaled by `style.ScaleAllSizes()`. - Fonts: - Added `IMGUI_DISABLE_DEFAULT_FONT_BITMAP`/`IMGUI_DISABLE_DEFAULT_FONT_VECTOR` to disable embedding either fonts separately. (#9407) - - Tweak CalcTextSize() awkward width rounding/ceiling code to reduce floating-point + - Tweak `CalcTextSize()` awkward width rounding/ceiling code to reduce floating-point imprecisions altering the result by 1 even at relatively small width. (#791) - DrawList: - - Minor optimization to AddLine(), AddLineH(), AddLineV() functions. (#4091) + - Minor optimization to `AddLine()`, `AddLineH()`, `AddLineV()` functions. (#4091) + - Added `ImDrawListFlags_NoTextPixelSnap` to disable snapping of AddText() + coordinates for a given scope. (#3437, #9417, #2291) - Demo: - Extract 'Widgets->Tree Nodes->Selectable Nodes' out of the 'Advanced' demo for clarity (manual reimplementation of basic selection). diff --git a/imgui.h b/imgui.h index 2aa8cb0d7..050237048 100644 --- a/imgui.h +++ b/imgui.h @@ -3279,6 +3279,7 @@ enum ImDrawListFlags_ ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). ImDrawListFlags_AntiAliasedFill = 1 << 2, // Enable anti-aliased edge around filled shapes (rounded rectangles, circles). ImDrawListFlags_AllowVtxOffset = 1 << 3, // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. + ImDrawListFlags_NoTextPixelSnap = 1 << 4, // [Internal] Disable automatically snapping AddText() calls to pixel boundaries. }; // Draw command list diff --git a/imgui_draw.cpp b/imgui_draw.cpp index b112f471d..a78a35ac0 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -5764,8 +5764,13 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, Im if (glyph->Colored) col |= ~IM_COL32_A_MASK; float scale = (size >= 0.0f) ? (size / baked->Size) : 1.0f; - float x = IM_TRUNC(pos.x); - float y = IM_TRUNC(pos.y); + float x = pos.x; + float y = pos.y; + if ((draw_list->Flags & ImDrawListFlags_NoTextPixelSnap) == 0) + { + x = IM_TRUNC(x); + y = IM_TRUNC(y); + } float x1 = x + glyph->X0 * scale; float x2 = x + glyph->X1 * scale; @@ -5797,12 +5802,17 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, Im // DO NOT CALL DIRECTLY THIS WILL CHANGE WILDLY IN 2026. Use ImDrawList::AddText(). void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, ImDrawTextFlags flags) { - // Align to be pixel perfect begin: - float x = IM_TRUNC(pos.x); - float y = IM_TRUNC(pos.y); + // Align to be pixel perfect + float x = pos.x; + float y = pos.y; if (y > clip_rect.w) return; + if ((draw_list->Flags & ImDrawListFlags_NoTextPixelSnap) == 0) + { + x = IM_TRUNC(x); + y = IM_TRUNC(y); + } if (!text_end) text_end = text_begin + ImStrlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.