Correctly break utf8 strings and prefer breaking on space character

This commit is contained in:
AndrejIT 2015-07-06 21:49:06 +03:00
parent 8c50c5bb50
commit 8274582d2a

View File

@ -52,22 +52,70 @@ chat2.send_message = function(player, message, color)
local line1 = nil --allow message to span at most to three lines. more is not ok for public chat. local line1 = nil --allow message to span at most to three lines. more is not ok for public chat.
local line2 = nil local line2 = nil
local line3 = nil local line3 = nil
local symbols = ''
line1 = string.sub(message, 1, chat2.chat_width) for i = 1, #message do
if string.len(message) > chat2.chat_width then if
line2 = " "..string.sub(message, chat2.chat_width, (chat2.chat_width * 2)) bit.band( string.byte(message, i), 20) == 20 and --is space symbol
string.len(symbols) > (chat2.chat_width - 8) and --space have priority for breaking lines
(
not line1 or
not line2 or
not line3
)
then
if not line1 then
line1 = symbols
symbols = ''
elseif not line2 then
line2 = symbols
symbols = ''
elseif not line3 then
line3 = symbols
symbols = ''
end end
if string.len(message) > (chat2.chat_width * 2) then elseif
line3 = " "..string.sub(message, (chat2.chat_width * 2), (chat2.chat_width * 3)) (bit.band( string.byte(message, i), 128) == 0 or bit.band( string.byte(message, i), 192) == 192) and --is ascii or first byte of unicode
string.len(symbols) > (chat2.chat_width - 1) and
(
not line1 or
not line2 or
not line3
)
then
if not line1 then
line1 = symbols
symbols = ''
elseif not line2 then
line2 = symbols
symbols = ''
elseif not line3 then
line3 = symbols
symbols = ''
end
elseif line1 and line2 and line3 then --stop when all three lines filled
break
end
symbols = symbols..message:sub(i,i)
end end
if not line1 and symbols then --when message is shorten than line
line1 = symbols
elseif not line2 and symbols then --when message is shorten than line
line2 = symbols
elseif not line3 and symbols then --when message is shorten than line
line3 = symbols
end
if line1 then
chat2.add_message(player, line1, color) chat2.add_message(player, line1, color)
end
if line2 then if line2 then
chat2.add_message(player, line2, color) chat2.add_message(player, line2, color)
end end
if line3 then if line3 then
chat2.add_message(player, line3, color) chat2.add_message(player, line3, color)
end end
if message ~= '' then if message ~= '' then
chat2.lastmessagetimes[player:get_player_name()] = minetest.get_gametime() chat2.lastmessagetimes[player:get_player_name()] = minetest.get_gametime()
end end