added support for newline of UTF8 characters
This commit is contained in:
1 parent
0f1412ea5a
commit
f7afbada19
3 files changed
+96
-17
No files matched your search
+91
-16
@@ -720,28 +720,103 @@ end
|
|||||||
|
|
||||||
-- Fits a given string to a specific width
|
-- Fits a given string to a specific width
|
||||||
-- returns the string as a table of lines
|
-- returns the string as a table of lines
|
||||||
function EID:fitTextToWidth(str, textboxWidth)
|
function EID:fitTextToWidth(str, textboxWidth, breakUtf8Chars)
|
||||||
local formatedLines = {}
|
local formatedLines = {}
|
||||||
local curLength = 0
|
local curLength = 0
|
||||||
local text = ""
|
local text = ""
|
||||||
for word in string.gmatch(str, "([^%s]+)") do
|
|
||||||
local colorFiltered = EID:filterColorMarkup(word, EID:getTextColor())
|
|
||||||
local filteredWord = ""
|
|
||||||
for _, filtered in ipairs(colorFiltered) do
|
|
||||||
filteredWord = filteredWord .. filtered[1]
|
|
||||||
end
|
|
||||||
local strFiltered, spriteTable = EID:filterIconMarkup(filteredWord, 0, 0)
|
|
||||||
local wordLength = EID:getStrWidth(strFiltered)
|
|
||||||
|
|
||||||
if curLength + wordLength <= textboxWidth or curLength < 12 then
|
local cursor = 1
|
||||||
text = text .. word .. " "
|
local word_begin_index = 1
|
||||||
curLength = curLength + wordLength
|
|
||||||
else
|
local byte = string.byte -- for speed up
|
||||||
table.insert(formatedLines, text)
|
local sub = string.sub
|
||||||
text = word .. " "
|
local byte_space = string.byte(' ')
|
||||||
curLength = wordLength
|
|
||||||
|
while cursor <= #str do
|
||||||
|
-- ascii word: 0x0xxxxxxx
|
||||||
|
-- utf8 word (sequence): 0x11xxxxxx 0x10xxxxxx 0x10xxxxxx ... 0x10xxxxxx
|
||||||
|
-- see https://en.wikipedia.org/wiki/UTF-8
|
||||||
|
-- we can only break after space, or before 0x11xxxxxx
|
||||||
|
local can_break_after_cursor = false
|
||||||
|
local cur, next = byte(str,cursor), byte(str,cursor+1)
|
||||||
|
if
|
||||||
|
-- cond#1: we can break at the end of string
|
||||||
|
cursor == #str or
|
||||||
|
-- cond#2: we can break after space
|
||||||
|
cur == byte_space or
|
||||||
|
-- handle utf8 characters
|
||||||
|
(breakUtf8Chars and(
|
||||||
|
-- cond#3: we can break if the next character is 0x11xxxxxx
|
||||||
|
((next & 0xC0) == 0xC0) or
|
||||||
|
-- cond#4: we can also break if the current is 0x10xxxxxx while the next is ascii but not space
|
||||||
|
(((cur & 0xC0) == 0x80) and (next~= byte_space and (next & 0x80) == 0x00))
|
||||||
|
))
|
||||||
|
then
|
||||||
|
|
||||||
|
--[[
|
||||||
|
-------------------ascii only---------------
|
||||||
|
word will be separated by spaces.
|
||||||
|
wordA |wordB |wordC |^%@&Q%#^&#@!! |aksldj
|
||||||
|
↑
|
||||||
|
cond#2
|
||||||
|
we may break after every space.
|
||||||
|
spaces | | | | | |woops
|
||||||
|
↑
|
||||||
|
cond#2
|
||||||
|
-------------------UTF8 only----------------
|
||||||
|
word is 0x11xxxxxx followed by several 0x10xxxxxx until the next 0x11xxxxxx (not included)
|
||||||
|
你|好|,|世|界|!|↑|↑|↑|↑
|
||||||
|
↑
|
||||||
|
cond#3
|
||||||
|
byte data of a word:
|
||||||
|
[0x11xxxxxx,0x10xxxxxx,0x10xxxxxx, (next is 0x11xxxxxx, cond#3)]
|
||||||
|
--------------------ascii->utf8------------
|
||||||
|
utf8 word must start with 0x11xxxxxx, so cond#3 split before it.
|
||||||
|
word|世|界
|
||||||
|
| ↑
|
||||||
|
↑ cond#3
|
||||||
|
cond#3
|
||||||
|
|
||||||
|
world |means |世|界
|
||||||
|
↑
|
||||||
|
cond#2
|
||||||
|
-------------------utf8->ascii--------------
|
||||||
|
世|界|是|world
|
||||||
|
| ↑
|
||||||
|
↑ cond#4
|
||||||
|
cond#3
|
||||||
|
世|界|是 |world
|
||||||
|
| ↑
|
||||||
|
↑ cond#4
|
||||||
|
cond#3
|
||||||
|
]]
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
filteredWord = filteredWord .. filtered[1]
|
||||||
|
end
|
||||||
|
local strFiltered, spriteTable = EID:filterIconMarkup(filteredWord, 0, 0)
|
||||||
|
local wordLength = EID:getStrWidth(strFiltered)
|
||||||
|
|
||||||
|
if curLength + wordLength <= textboxWidth or curLength < 12 then
|
||||||
|
text = text .. word
|
||||||
|
curLength = curLength + wordLength
|
||||||
|
else
|
||||||
|
table.insert(formatedLines, text)
|
||||||
|
text = word
|
||||||
|
curLength = wordLength
|
||||||
|
end
|
||||||
|
|
||||||
|
-- next word starts here
|
||||||
|
word_begin_index = cursor + 1
|
||||||
end
|
end
|
||||||
|
cursor = cursor + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(formatedLines, text)
|
table.insert(formatedLines, text)
|
||||||
return formatedLines
|
return formatedLines
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -498,3 +498,7 @@ EID.GoldenTrinketData = {
|
|||||||
-- Lil Clot, Swallowed M80, The Twins (default text), Cricket Leg (17%, 1 in 6), Apollyon's Best Friend, Broken Glasses
|
-- Lil Clot, Swallowed M80, The Twins (default text), Cricket Leg (17%, 1 in 6), Apollyon's Best Friend, Broken Glasses
|
||||||
[176] = 1, [178] = {t={50},mult=2}, [183] = 0, [185] = 17, [186] = 1, [187] = {t={50,50}, mult=2},
|
[176] = 1, [178] = {t={50},mult=2}, [183] = 0, [185] = 17, [186] = 1, [187] = {t={50,50}, mult=2},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EID.BreakUtf8CharsLanguage = {
|
||||||
|
['zh_cn'] = true
|
||||||
|
}
|
||||||
@@ -512,7 +512,7 @@ function EID:printBulletPoints(description, renderPos)
|
|||||||
local textScale = Vector(EID.Scale, EID.Scale)
|
local textScale = Vector(EID.Scale, EID.Scale)
|
||||||
description = EID:replaceShortMarkupStrings(description)
|
description = EID:replaceShortMarkupStrings(description)
|
||||||
for line in string.gmatch(description, "([^#]+)") do
|
for line in string.gmatch(description, "([^#]+)") do
|
||||||
local formatedLines = EID:fitTextToWidth(line, textboxWidth)
|
local formatedLines = EID:fitTextToWidth(line, textboxWidth, EID.BreakUtf8CharsLanguage[EID.Config["Language"]])
|
||||||
local textColor = EID:getTextColor()
|
local textColor = EID:getTextColor()
|
||||||
for i, lineToPrint in ipairs(formatedLines) do
|
for i, lineToPrint in ipairs(formatedLines) do
|
||||||
-- render bulletpoint
|
-- render bulletpoint
|
||||||
|
|||||||
Reference in new issue
Block a user