From 5029525f2ae07374877a0f7d4ccad9e28009079b Mon Sep 17 00:00:00 2001 From: 4DBug <4DBug@github.com> Date: Sat, 17 Oct 2026 08:43:13 -0500 Subject: [PATCH] Less garbage per frame: scalar text-size path (ImFontCalcTextSizeXY, ImGui.CalcTextSizeXY) for TextEx/ButtonEx, scratch rects in ButtonEx (160 -> 103 KB/frame on demo+stress) --- build/ImGui.linemap.json | 2 +- build/ImGui.luau | 74 +++++++++++++++++++++-------------- build/ImGuiTyped.luau | 1 + build/imgui_love.linemap.json | 2 +- build/imgui_love.lua | 74 +++++++++++++++++++++-------------- imgui.lua | 19 +++++---- imgui_draw.lua | 41 +++++++++++-------- imgui_widgets.lua | 13 +++--- 8 files changed, 134 insertions(+), 92 deletions(-) diff --git a/build/ImGui.linemap.json b/build/ImGui.linemap.json index 03ba5c9..26c9bda 100644 --- a/build/ImGui.linemap.json +++ b/build/ImGui.linemap.json @@ -1 +1 @@ -[[46, "imgui.lua"], [22648, "imgui_demo.lua"], [34505, "imgui_draw.lua"], [42835, "imgui_h.lua"], [45838, "roblox/raster.lua"], [46314, "roblox/textures.lua"], [46485, "roblox/layers.lua"], [46822, "roblox/overlays.lua"], [46903, "roblox/render.lua"], [47748, "roblox/parallel.lua"], [48147, "imgui_impl_roblox.lua"], [48648, "imgui_impl_roblox_fonts.lua"], [55051, "imgui_internal.lua"], [59017, "imgui_tables.lua"], [64109, "imgui_widgets.lua"], [76347, "imstb_rectpack.lua"], [77011, "imstb_textedit.lua"], [78553, "imstb_truetype.lua"], [81341, "imstd_minstdio.lua"]] \ No newline at end of file +[[46, "imgui.lua"], [22651, "imgui_demo.lua"], [34508, "imgui_draw.lua"], [42845, "imgui_h.lua"], [45848, "roblox/raster.lua"], [46324, "roblox/textures.lua"], [46495, "roblox/layers.lua"], [46832, "roblox/overlays.lua"], [46913, "roblox/render.lua"], [47758, "roblox/parallel.lua"], [48157, "imgui_impl_roblox.lua"], [48658, "imgui_impl_roblox_fonts.lua"], [55061, "imgui_internal.lua"], [59027, "imgui_tables.lua"], [64119, "imgui_widgets.lua"], [76360, "imstb_rectpack.lua"], [77024, "imstb_textedit.lua"], [78566, "imstb_truetype.lua"], [81354, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/ImGui.luau b/build/ImGui.luau index 0465d94..7419497 100644 --- a/build/ImGui.luau +++ b/build/ImGui.luau @@ -14705,10 +14705,10 @@ end --- @param hide_text_after_double_hash? bool --- @param wrap_width? float --- @return ImVec2 +-- [Luau] scalar CalcTextSizeEx(): returns x, y without allocating an ImVec2 (hot widget paths) @native -function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) - if hide_text_after_double_hash == nil then hide_text_after_double_hash = false end - if wrap_width == nil then wrap_width = -1.0 end +function ImGui.CalcTextSizeXY(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + if wrap_width == nil then wrap_width = -1.0 end local g = GImGui @@ -14719,19 +14719,22 @@ function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double text_display_end = text_end end - local font = g.Font local font_size = g.FontSize if text == "" or (text_end and text_end <= text_begin) then - return ImVec2(0.0, font_size) + return 0.0, font_size end - local text_size = ImFontCalcTextSizeEx(font, font_size, 3.4028234663853e+38 --[[FLT_MAX]], wrap_width, text, text_begin, text_display_end, text_display_end, nil, 0 --[[ImDrawTextFlags.None]]) -- [Luau] inline font:CalcTextSizeA() + local x, y = ImFontCalcTextSizeXY(g.Font, font_size, 3.4028234663853e+38 --[[FLT_MAX]], wrap_width, text, text_begin, text_display_end, text_display_end, nil, 0 --[[ImDrawTextFlags.None]]) -- [Luau] inline font:CalcTextSizeA() -- Round -- (see 7b0bf230, 4622fa4b6, #791 for details about this.) -- FIXME: Add a way to disable this. - text_size.x = math.ceil(text_size.x) -- [Luau] == ImCeilFast() for all finite values, builtin + return math.ceil(x), y -- [Luau] == ImCeilFast() for all finite values, builtin +end - return text_size +@native +function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + local x, y = ImGui.CalcTextSizeXY(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + return ImVec2(x, y) end -- Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() @@ -37970,7 +37973,8 @@ end local TEXT_SIZE_CACHE_MAX = 4096 @native -function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) +-- [Luau] scalar core of ImFontCalcTextSizeEx(): returns size.x, size.y, remaining (no ImVec2 allocated) +function ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) local text_is_str = type(text) == "string" -- [Luau] cache for the common case (whole string, single pass, no wrap/limit): per baked font, text -> unscaled width, line count. -- Advances of a baked font never change once loaded, so entries stay valid. @@ -37987,7 +37991,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end local e = cache[text] if e then - return ImVec2(e[1] * (size / baked.Size), e[2] * size), text_len1 + return e[1] * (size / baked.Size), e[2] * size, text_len1 end cache_key = text elseif cacheable and wrap_width > 0.0 then @@ -38003,7 +38007,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi if by_wrap == nil then by_wrap = {}; by_size[wrap_width] = by_wrap end local e = by_wrap[text] if e then - return ImVec2(e[1], e[2]), e[3] + return e[1], e[2], e[3] end wrap_cache = by_wrap end @@ -38018,7 +38022,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi local line_height = size local scale = line_height / baked.Size - local text_size = ImVec2() + local tx, ty = 0.0, 0.0 local line_width = 0.0 local word_wrap_enabled = (wrap_width > 0.0) @@ -38038,10 +38042,10 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end if s >= word_wrap_eol then - if text_size.x < line_width then - text_size.x = line_width + if tx < line_width then + tx = line_width end - text_size.y = text_size.y + line_height + ty = ty + line_height line_width = 0.0 s = ImTextCalcWordWrapNextLineStart(text, s, text_end, flags) -- Wrapping skips upcoming blanks if bit32.band(flags, 4 --[[ImDrawTextFlags.StopOnNewLine]]) ~= 0 then @@ -38066,8 +38070,8 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end if c == 10 then -- '\n' - text_size.x = ImMax(text_size.x, line_width) - text_size.y = text_size.y + line_height + if tx < line_width then tx = line_width end + ty = ty + line_height line_width = 0.0 if bit32.band(flags, 4 --[[ImDrawTextFlags.StopOnNewLine]]) ~= 0 then stop = true; break @@ -38099,30 +38103,36 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi if stop then break end end - if (text_size.x < line_width) then - text_size.x = line_width + if (tx < line_width) then + tx = line_width end if out_offset then -- offset allow for the possibility of sitting after a trailing \n out_offset.x = line_width - out_offset.y = text_size.y + line_height + out_offset.y = ty + line_height end - if (line_width > 0 or text_size.y == 0.0) then -- whereas size.y will ignore the trailing \n - text_size.y = text_size.y + line_height + if (line_width > 0 or ty == 0.0) then -- whereas size.y will ignore the trailing \n + ty = ty + line_height end local out_remaining = s if cache_key then - baked._TextSizeCache[cache_key] = { text_size.x / scale, text_size.y / line_height } + baked._TextSizeCache[cache_key] = { tx / scale, ty / line_height } baked._TextSizeCacheN = baked._TextSizeCacheN + 1 elseif wrap_cache then - wrap_cache[text] = { text_size.x, text_size.y, out_remaining } + wrap_cache[text] = { tx, ty, out_remaining } baked._TextSizeCacheN = baked._TextSizeCacheN + 1 end - return text_size, out_remaining + return tx, ty, out_remaining +end + +-- ImVec2 version (upstream signature) +function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) + local x, y, remaining = ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) + return ImVec2(x, y), remaining end --- @param size float @@ -64218,13 +64228,13 @@ function ImGui.TextEx(text, text_end, flags) -- [Luau] scalar text_pos, scratch bb (ItemAdd() copies it, RenderTextWrapped() only reads it) local text_pos_x, text_pos_y = dc.CursorPos.x, dc.CursorPos.y + dc.CurrLineTextBaseOffset local wrap_width = wrap_enabled and ImGui.CalcWrapWidthForPos(dc.CursorPos, wrap_pos_x) or 0.0 - local text_size = ImGui.CalcTextSizeEx(text, 1, text_end, false, wrap_width) + local text_size_x, text_size_y = ImGui.CalcTextSizeXY(text, 1, text_end, false, wrap_width) local bb = TextEx_bb local bb_min, bb_max = bb.Min, bb.Max bb_min.x = text_pos_x; bb_min.y = text_pos_y - bb_max.x = text_pos_x + text_size.x; bb_max.y = text_pos_y + text_size.y - ImGui.ItemSize(text_size, 0.0) + bb_max.x = text_pos_x + text_size_x; bb_max.y = text_pos_y + text_size_y + ImGui.ItemSize(bb, 0.0) -- [Luau] the bb's size == text_size if not ImGui.ItemAdd(bb, 0) then return end @@ -64852,6 +64862,7 @@ end --- @param size_arg? ImVec2 --- @param flags? ImGuiButtonFlags --- @return bool +local ButtonEx_bb, ButtonEx_label_size = ImRect(), ImVec2() local ButtonEx_text_min, ButtonEx_text_max = ImVec2(), ImVec2() @native @@ -64868,7 +64879,8 @@ function ImGui.ButtonEx(label, size_arg, flags) local style = g.Style local id = window:GetID(label) local label_end = ImGui.FindRenderedTextEnd(label) - local label_size = ImGui.CalcTextSize(label, label_end, false) + local label_size = ButtonEx_label_size -- [Luau] scratch: only read below (RenderTextClipped text_size_if_known) + label_size.x, label_size.y = ImGui.CalcTextSizeXY(label, 1, label_end, false) -- [Luau] scalar pos, no temporary ImVec2 local pos_x, pos_y = window.DC.CursorPos.x, window.DC.CursorPos.y @@ -64878,7 +64890,8 @@ function ImGui.ButtonEx(label, size_arg, flags) end local size = ImGui.CalcItemSize(size_arg, label_size.x + frame_padding.x * 2.0, label_size.y + frame_padding.y * 2.0) - local bb = ImRect(pos_x, pos_y, pos_x + size.x, pos_y + size.y) + local bb = ButtonEx_bb -- [Luau] scratch: ItemAdd() copies it, ButtonBehavior()/Render*() only read it + bb.Min.x, bb.Min.y, bb.Max.x, bb.Max.y = pos_x, pos_y, pos_x + size.x, pos_y + size.y ImGui.ItemSize(size, frame_padding.y) if not ImGui.ItemAdd(bb, id) then return false @@ -81855,6 +81868,7 @@ return { ImFontBaked_BuildLoadGlyph = ImFontBaked_BuildLoadGlyph, ImFontBaked_BuildLoadGlyphAdvanceX = ImFontBaked_BuildLoadGlyphAdvanceX, ImFontCalcTextSizeEx = ImFontCalcTextSizeEx, + ImFontCalcTextSizeXY = ImFontCalcTextSizeXY, ImFontCalcWordWrapPositionEx = ImFontCalcWordWrapPositionEx, ImFontCalcWordWrapPositionEx_Uncached = ImFontCalcWordWrapPositionEx_Uncached, ImFontConfig = ImFontConfig, diff --git a/build/ImGuiTyped.luau b/build/ImGuiTyped.luau index cfaa28a..8aedd13 100644 --- a/build/ImGuiTyped.luau +++ b/build/ImGuiTyped.luau @@ -70,6 +70,7 @@ export type ImGuiAPI = { CalcRoundingFlagsForRectInRect: (...any) -> ...any, CalcTextSize: (text: string, text_end: string?, hide_text_after_double_hash: boolean?, wrap_width: number?) -> ImVec2, CalcTextSizeEx: (...any) -> ...any, + CalcTextSizeXY: (...any) -> ...any, CalcTypematicRepeatAmount: (...any) -> ...any, CalcWindowNextAutoFitSize: (...any) -> ...any, CalcWrapWidthForPos: (...any) -> ...any, diff --git a/build/imgui_love.linemap.json b/build/imgui_love.linemap.json index da1731b..6b26349 100644 --- a/build/imgui_love.linemap.json +++ b/build/imgui_love.linemap.json @@ -1 +1 @@ -[[183, "imgui.lua"], [22785, "imgui_demo.lua"], [34642, "imgui_draw.lua"], [42972, "imgui_h.lua"], [45973, "imgui_impl_love.lua"], [46517, "imgui_internal.lua"], [50483, "imgui_tables.lua"], [55575, "imgui_widgets.lua"], [67813, "imstb_rectpack.lua"], [68477, "imstb_textedit.lua"], [70019, "imstb_truetype.lua"], [72807, "imstd_minstdio.lua"]] \ No newline at end of file +[[183, "imgui.lua"], [22788, "imgui_demo.lua"], [34645, "imgui_draw.lua"], [42982, "imgui_h.lua"], [45983, "imgui_impl_love.lua"], [46527, "imgui_internal.lua"], [50493, "imgui_tables.lua"], [55585, "imgui_widgets.lua"], [67826, "imstb_rectpack.lua"], [68490, "imstb_textedit.lua"], [70032, "imstb_truetype.lua"], [72820, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/imgui_love.lua b/build/imgui_love.lua index 4359005..2671527 100644 --- a/build/imgui_love.lua +++ b/build/imgui_love.lua @@ -14842,10 +14842,10 @@ end --- @param hide_text_after_double_hash? bool --- @param wrap_width? float --- @return ImVec2 +-- [Luau] scalar CalcTextSizeEx(): returns x, y without allocating an ImVec2 (hot widget paths) -function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) - if hide_text_after_double_hash == nil then hide_text_after_double_hash = false end - if wrap_width == nil then wrap_width = -1.0 end +function ImGui.CalcTextSizeXY(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + if wrap_width == nil then wrap_width = -1.0 end local g = GImGui @@ -14856,19 +14856,22 @@ function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double text_display_end = text_end end - local font = g.Font local font_size = g.FontSize if text == "" or (text_end and text_end <= text_begin) then - return ImVec2(0.0, font_size) + return 0.0, font_size end - local text_size = ImFontCalcTextSizeEx(font, font_size, 3.4028234663853e+38 --[[FLT_MAX]], wrap_width, text, text_begin, text_display_end, text_display_end, nil, 0 --[[ImDrawTextFlags.None]]) -- [Luau] inline font:CalcTextSizeA() + local x, y = ImFontCalcTextSizeXY(g.Font, font_size, 3.4028234663853e+38 --[[FLT_MAX]], wrap_width, text, text_begin, text_display_end, text_display_end, nil, 0 --[[ImDrawTextFlags.None]]) -- [Luau] inline font:CalcTextSizeA() -- Round -- (see 7b0bf230, 4622fa4b6, #791 for details about this.) -- FIXME: Add a way to disable this. - text_size.x = math.ceil(text_size.x) -- [Luau] == ImCeilFast() for all finite values, builtin + return math.ceil(x), y -- [Luau] == ImCeilFast() for all finite values, builtin +end - return text_size + +function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + local x, y = ImGui.CalcTextSizeXY(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + return ImVec2(x, y) end -- Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() @@ -38107,7 +38110,8 @@ end local TEXT_SIZE_CACHE_MAX = 4096 -function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) +-- [Luau] scalar core of ImFontCalcTextSizeEx(): returns size.x, size.y, remaining (no ImVec2 allocated) +function ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) local text_is_str = type(text) == "string" -- [Luau] cache for the common case (whole string, single pass, no wrap/limit): per baked font, text -> unscaled width, line count. -- Advances of a baked font never change once loaded, so entries stay valid. @@ -38124,7 +38128,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end local e = cache[text] if e then - return ImVec2(e[1] * (size / baked.Size), e[2] * size), text_len1 + return e[1] * (size / baked.Size), e[2] * size, text_len1 end cache_key = text elseif cacheable and wrap_width > 0.0 then @@ -38140,7 +38144,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi if by_wrap == nil then by_wrap = {}; by_size[wrap_width] = by_wrap end local e = by_wrap[text] if e then - return ImVec2(e[1], e[2]), e[3] + return e[1], e[2], e[3] end wrap_cache = by_wrap end @@ -38155,7 +38159,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi local line_height = size local scale = line_height / baked.Size - local text_size = ImVec2() + local tx, ty = 0.0, 0.0 local line_width = 0.0 local word_wrap_enabled = (wrap_width > 0.0) @@ -38175,10 +38179,10 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end if s >= word_wrap_eol then - if text_size.x < line_width then - text_size.x = line_width + if tx < line_width then + tx = line_width end - text_size.y = text_size.y + line_height + ty = ty + line_height line_width = 0.0 s = ImTextCalcWordWrapNextLineStart(text, s, text_end, flags) -- Wrapping skips upcoming blanks if bit32.band(flags, 4 --[[ImDrawTextFlags.StopOnNewLine]]) ~= 0 then @@ -38203,8 +38207,8 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end if c == 10 then -- '\n' - text_size.x = ImMax(text_size.x, line_width) - text_size.y = text_size.y + line_height + if tx < line_width then tx = line_width end + ty = ty + line_height line_width = 0.0 if bit32.band(flags, 4 --[[ImDrawTextFlags.StopOnNewLine]]) ~= 0 then stop = true; break @@ -38236,30 +38240,36 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi if stop then break end end - if (text_size.x < line_width) then - text_size.x = line_width + if (tx < line_width) then + tx = line_width end if out_offset then -- offset allow for the possibility of sitting after a trailing \n out_offset.x = line_width - out_offset.y = text_size.y + line_height + out_offset.y = ty + line_height end - if (line_width > 0 or text_size.y == 0.0) then -- whereas size.y will ignore the trailing \n - text_size.y = text_size.y + line_height + if (line_width > 0 or ty == 0.0) then -- whereas size.y will ignore the trailing \n + ty = ty + line_height end local out_remaining = s if cache_key then - baked._TextSizeCache[cache_key] = { text_size.x / scale, text_size.y / line_height } + baked._TextSizeCache[cache_key] = { tx / scale, ty / line_height } baked._TextSizeCacheN = baked._TextSizeCacheN + 1 elseif wrap_cache then - wrap_cache[text] = { text_size.x, text_size.y, out_remaining } + wrap_cache[text] = { tx, ty, out_remaining } baked._TextSizeCacheN = baked._TextSizeCacheN + 1 end - return text_size, out_remaining + return tx, ty, out_remaining +end + +-- ImVec2 version (upstream signature) +function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) + local x, y, remaining = ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) + return ImVec2(x, y), remaining end --- @param size float @@ -55684,13 +55694,13 @@ function ImGui.TextEx(text, text_end, flags) -- [Luau] scalar text_pos, scratch bb (ItemAdd() copies it, RenderTextWrapped() only reads it) local text_pos_x, text_pos_y = dc.CursorPos.x, dc.CursorPos.y + dc.CurrLineTextBaseOffset local wrap_width = wrap_enabled and ImGui.CalcWrapWidthForPos(dc.CursorPos, wrap_pos_x) or 0.0 - local text_size = ImGui.CalcTextSizeEx(text, 1, text_end, false, wrap_width) + local text_size_x, text_size_y = ImGui.CalcTextSizeXY(text, 1, text_end, false, wrap_width) local bb = TextEx_bb local bb_min, bb_max = bb.Min, bb.Max bb_min.x = text_pos_x; bb_min.y = text_pos_y - bb_max.x = text_pos_x + text_size.x; bb_max.y = text_pos_y + text_size.y - ImGui.ItemSize(text_size, 0.0) + bb_max.x = text_pos_x + text_size_x; bb_max.y = text_pos_y + text_size_y + ImGui.ItemSize(bb, 0.0) -- [Luau] the bb's size == text_size if not ImGui.ItemAdd(bb, 0) then return end @@ -56318,6 +56328,7 @@ end --- @param size_arg? ImVec2 --- @param flags? ImGuiButtonFlags --- @return bool +local ButtonEx_bb, ButtonEx_label_size = ImRect(), ImVec2() local ButtonEx_text_min, ButtonEx_text_max = ImVec2(), ImVec2() @@ -56334,7 +56345,8 @@ function ImGui.ButtonEx(label, size_arg, flags) local style = g.Style local id = window:GetID(label) local label_end = ImGui.FindRenderedTextEnd(label) - local label_size = ImGui.CalcTextSize(label, label_end, false) + local label_size = ButtonEx_label_size -- [Luau] scratch: only read below (RenderTextClipped text_size_if_known) + label_size.x, label_size.y = ImGui.CalcTextSizeXY(label, 1, label_end, false) -- [Luau] scalar pos, no temporary ImVec2 local pos_x, pos_y = window.DC.CursorPos.x, window.DC.CursorPos.y @@ -56344,7 +56356,8 @@ function ImGui.ButtonEx(label, size_arg, flags) end local size = ImGui.CalcItemSize(size_arg, label_size.x + frame_padding.x * 2.0, label_size.y + frame_padding.y * 2.0) - local bb = ImRect(pos_x, pos_y, pos_x + size.x, pos_y + size.y) + local bb = ButtonEx_bb -- [Luau] scratch: ItemAdd() copies it, ButtonBehavior()/Render*() only read it + bb.Min.x, bb.Min.y, bb.Max.x, bb.Max.y = pos_x, pos_y, pos_x + size.x, pos_y + size.y ImGui.ItemSize(size, frame_padding.y) if not ImGui.ItemAdd(bb, id) then return false @@ -73321,6 +73334,7 @@ return { ImFontBaked_BuildLoadGlyph = ImFontBaked_BuildLoadGlyph, ImFontBaked_BuildLoadGlyphAdvanceX = ImFontBaked_BuildLoadGlyphAdvanceX, ImFontCalcTextSizeEx = ImFontCalcTextSizeEx, + ImFontCalcTextSizeXY = ImFontCalcTextSizeXY, ImFontCalcWordWrapPositionEx = ImFontCalcWordWrapPositionEx, ImFontCalcWordWrapPositionEx_Uncached = ImFontCalcWordWrapPositionEx_Uncached, ImFontConfig = ImFontConfig, diff --git a/imgui.lua b/imgui.lua index 37fca78..218087e 100644 --- a/imgui.lua +++ b/imgui.lua @@ -14660,10 +14660,10 @@ end --- @param hide_text_after_double_hash? bool --- @param wrap_width? float --- @return ImVec2 +-- [Luau] scalar CalcTextSizeEx(): returns x, y without allocating an ImVec2 (hot widget paths) @native -function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) - if hide_text_after_double_hash == nil then hide_text_after_double_hash = false end - if wrap_width == nil then wrap_width = -1.0 end +function ImGui.CalcTextSizeXY(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + if wrap_width == nil then wrap_width = -1.0 end local g = GImGui @@ -14674,19 +14674,22 @@ function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double text_display_end = text_end end - local font = g.Font local font_size = g.FontSize if text == "" or (text_end and text_end <= text_begin) then - return ImVec2(0.0, font_size) + return 0.0, font_size end - local text_size = ImFontCalcTextSizeEx(font, font_size, FLT_MAX, wrap_width, text, text_begin, text_display_end, text_display_end, nil, ImDrawTextFlags.None) -- [Luau] inline font:CalcTextSizeA() + local x, y = ImFontCalcTextSizeXY(g.Font, font_size, FLT_MAX, wrap_width, text, text_begin, text_display_end, text_display_end, nil, ImDrawTextFlags.None) -- [Luau] inline font:CalcTextSizeA() -- Round -- (see 7b0bf230, 4622fa4b6, #791 for details about this.) -- FIXME: Add a way to disable this. - text_size.x = math.ceil(text_size.x) -- [Luau] == ImCeilFast() for all finite values, builtin + return math.ceil(x), y -- [Luau] == ImCeilFast() for all finite values, builtin +end - return text_size +@native +function ImGui.CalcTextSizeEx(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + local x, y = ImGui.CalcTextSizeXY(text, text_begin, text_end, hide_text_after_double_hash, wrap_width) + return ImVec2(x, y) end -- Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() diff --git a/imgui_draw.lua b/imgui_draw.lua index a7c0176..4dd6209 100644 --- a/imgui_draw.lua +++ b/imgui_draw.lua @@ -3466,7 +3466,8 @@ end local TEXT_SIZE_CACHE_MAX = 4096 @native -function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) +-- [Luau] scalar core of ImFontCalcTextSizeEx(): returns size.x, size.y, remaining (no ImVec2 allocated) +function ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) local text_is_str = type(text) == "string" -- [Luau] cache for the common case (whole string, single pass, no wrap/limit): per baked font, text -> unscaled width, line count. -- Advances of a baked font never change once loaded, so entries stay valid. @@ -3483,7 +3484,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end local e = cache[text] if e then - return ImVec2(e[1] * (size / baked.Size), e[2] * size), text_len1 + return e[1] * (size / baked.Size), e[2] * size, text_len1 end cache_key = text elseif cacheable and wrap_width > 0.0 then @@ -3499,7 +3500,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi if by_wrap == nil then by_wrap = {}; by_size[wrap_width] = by_wrap end local e = by_wrap[text] if e then - return ImVec2(e[1], e[2]), e[3] + return e[1], e[2], e[3] end wrap_cache = by_wrap end @@ -3514,7 +3515,7 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi local line_height = size local scale = line_height / baked.Size - local text_size = ImVec2() + local tx, ty = 0.0, 0.0 local line_width = 0.0 local word_wrap_enabled = (wrap_width > 0.0) @@ -3534,10 +3535,10 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end if s >= word_wrap_eol then - if text_size.x < line_width then - text_size.x = line_width + if tx < line_width then + tx = line_width end - text_size.y = text_size.y + line_height + ty = ty + line_height line_width = 0.0 s = ImTextCalcWordWrapNextLineStart(text, s, text_end, flags) -- Wrapping skips upcoming blanks if bit32.band(flags, ImDrawTextFlags.StopOnNewLine) ~= 0 then @@ -3562,8 +3563,8 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi end if c == 10 then -- '\n' - text_size.x = ImMax(text_size.x, line_width) - text_size.y = text_size.y + line_height + if tx < line_width then tx = line_width end + ty = ty + line_height line_width = 0.0 if bit32.band(flags, ImDrawTextFlags.StopOnNewLine) ~= 0 then stop = true; break @@ -3595,30 +3596,36 @@ function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begi if stop then break end end - if (text_size.x < line_width) then - text_size.x = line_width + if (tx < line_width) then + tx = line_width end if out_offset then -- offset allow for the possibility of sitting after a trailing \n out_offset.x = line_width - out_offset.y = text_size.y + line_height + out_offset.y = ty + line_height end - if (line_width > 0 or text_size.y == 0.0) then -- whereas size.y will ignore the trailing \n - text_size.y = text_size.y + line_height + if (line_width > 0 or ty == 0.0) then -- whereas size.y will ignore the trailing \n + ty = ty + line_height end local out_remaining = s if cache_key then - baked._TextSizeCache[cache_key] = { text_size.x / scale, text_size.y / line_height } + baked._TextSizeCache[cache_key] = { tx / scale, ty / line_height } baked._TextSizeCacheN = baked._TextSizeCacheN + 1 elseif wrap_cache then - wrap_cache[text] = { text_size.x, text_size.y, out_remaining } + wrap_cache[text] = { tx, ty, out_remaining } baked._TextSizeCacheN = baked._TextSizeCacheN + 1 end - return text_size, out_remaining + return tx, ty, out_remaining +end + +-- ImVec2 version (upstream signature) +function ImFontCalcTextSizeEx(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) + local x, y, remaining = ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begin, text_end_display, text_end, out_offset, flags) + return ImVec2(x, y), remaining end --- @param size float diff --git a/imgui_widgets.lua b/imgui_widgets.lua index d464e58..ebeeaef 100644 --- a/imgui_widgets.lua +++ b/imgui_widgets.lua @@ -110,13 +110,13 @@ function ImGui.TextEx(text, text_end, flags) -- [Luau] scalar text_pos, scratch bb (ItemAdd() copies it, RenderTextWrapped() only reads it) local text_pos_x, text_pos_y = dc.CursorPos.x, dc.CursorPos.y + dc.CurrLineTextBaseOffset local wrap_width = wrap_enabled and ImGui.CalcWrapWidthForPos(dc.CursorPos, wrap_pos_x) or 0.0 - local text_size = ImGui.CalcTextSizeEx(text, 1, text_end, false, wrap_width) + local text_size_x, text_size_y = ImGui.CalcTextSizeXY(text, 1, text_end, false, wrap_width) local bb = TextEx_bb local bb_min, bb_max = bb.Min, bb.Max bb_min.x = text_pos_x; bb_min.y = text_pos_y - bb_max.x = text_pos_x + text_size.x; bb_max.y = text_pos_y + text_size.y - ImGui.ItemSize(text_size, 0.0) + bb_max.x = text_pos_x + text_size_x; bb_max.y = text_pos_y + text_size_y + ImGui.ItemSize(bb, 0.0) -- [Luau] the bb's size == text_size if not ImGui.ItemAdd(bb, 0) then return end @@ -744,6 +744,7 @@ end --- @param size_arg? ImVec2 --- @param flags? ImGuiButtonFlags --- @return bool +local ButtonEx_bb, ButtonEx_label_size = ImRect(), ImVec2() local ButtonEx_text_min, ButtonEx_text_max = ImVec2(), ImVec2() @native @@ -760,7 +761,8 @@ function ImGui.ButtonEx(label, size_arg, flags) local style = g.Style local id = window:GetID(label) local label_end = ImGui.FindRenderedTextEnd(label) - local label_size = ImGui.CalcTextSize(label, label_end, false) + local label_size = ButtonEx_label_size -- [Luau] scratch: only read below (RenderTextClipped text_size_if_known) + label_size.x, label_size.y = ImGui.CalcTextSizeXY(label, 1, label_end, false) -- [Luau] scalar pos, no temporary ImVec2 local pos_x, pos_y = window.DC.CursorPos.x, window.DC.CursorPos.y @@ -770,7 +772,8 @@ function ImGui.ButtonEx(label, size_arg, flags) end local size = ImGui.CalcItemSize(size_arg, label_size.x + frame_padding.x * 2.0, label_size.y + frame_padding.y * 2.0) - local bb = ImRect(pos_x, pos_y, pos_x + size.x, pos_y + size.y) + local bb = ButtonEx_bb -- [Luau] scratch: ItemAdd() copies it, ButtonBehavior()/Render*() only read it + bb.Min.x, bb.Min.y, bb.Max.x, bb.Max.y = pos_x, pos_y, pos_x + size.x, pos_y + size.y ImGui.ItemSize(size, frame_padding.y) if not ImGui.ItemAdd(bb, id) then return false