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)
This commit is contained in:
1 parent
0663b79ddd
commit
5029525f2a
8 files changed
+134
-92
No files matched your search
+24
-17
@@ -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
|
||||
|
||||
Reference in new issue
Block a user