From 6cdd64b8e786f012fe90f3364bd46d419adfbdd1 Mon Sep 17 00:00:00 2001 From: Buurazu Date: Tue, 8 Feb 2022 03:55:47 -0500 Subject: [PATCH] huge improvement to fitTextToWidth --- eid_api.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/eid_api.lua b/eid_api.lua index eedb5857..be17dfaf 100644 --- a/eid_api.lua +++ b/eid_api.lua @@ -742,6 +742,19 @@ function EID:filterColorMarkup(text, baseKColor) return textPartsTable end +-- A simple function to replace all markup {{ }} with placeholder strings, to use in fitTextToWidth +function EID:replaceAllMarkupWithSpaces(text) + for word in string.gmatch(text, "{{.-}}") do + local lookup = EID:getIcon(word) + if lookup[1] ~= "ERROR" then + text = string.gsub(text, word, EID:generatePlaceholderString(lookup[3]), 1) + else + text = string.gsub(text, word, "", 1) + end + end + return text +end + -- Fits a given string to a specific width -- returns the string as a table of lines function EID:fitTextToWidth(str, textboxWidth, breakUtf8Chars) @@ -815,14 +828,8 @@ function EID:fitTextToWidth(str, textboxWidth, breakUtf8Chars) -- we can break after str[cursor] local word = sub(str, word_begin_index, cursor) - - local colorFiltered = EID:filterColorMarkup(word, EID:getTextColor()) - local filteredWord = {} - for _, filtered in ipairs(colorFiltered) do - table.insert(filteredWord, filtered[1]) - end - local strFiltered, spriteTable = EID:filterIconMarkup(table.concat(filteredWord), 0, 0) - local wordLength = EID:getStrWidth(strFiltered) + local wordFiltered = EID:replaceAllMarkupWithSpaces(word) + local wordLength = EID:getStrWidth(wordFiltered) if curLength + wordLength <= textboxWidth or curLength < 17 then table.insert(text, word)