better handling of abusive sign input,

avoid breaking old signs' word wrapping

also increase the sign input limit to 600 chars, to allow for ample amounts of
color codes.
This commit is contained in:
Vanessa Ezekowitz 2014-08-11 21:06:12 -04:00
parent 8e76593982
commit 5d73e994a7

View File

@ -160,6 +160,11 @@ local SIGN_WIDTH
local CHARS_PER_LINE = 30 local CHARS_PER_LINE = 30
local NUMBER_OF_LINES = 6 local NUMBER_OF_LINES = 6
-- 6 rows, max 80 chars per, plus a bit of fudge to
-- avoid excess trimming (e.g. due to color codes)
local MAX_INPUT_CHARS = 600
-- This holds the individual character widths. -- This holds the individual character widths.
-- Indexed by the actual character (e.g. charwidth["A"]) -- Indexed by the actual character (e.g. charwidth["A"])
local charwidth = { } local charwidth = { }
@ -171,7 +176,7 @@ local CHARDB_FILE = minetest.get_worldpath().."/signs_lib_chardb"
local function trim_input(text) local function trim_input(text)
local txt_len = string.len(text) local txt_len = string.len(text)
text_trimmed = string.sub(text, 1, math.min(480, txt_len)) -- 6 rows, max 80 chars per text_trimmed = string.sub(text, 1, math.min(MAX_INPUT_CHARS, txt_len))
return text_trimmed return text_trimmed
end end
@ -371,15 +376,16 @@ local function make_line_texture(line, lineno)
width = width + w + 1 width = width + w + 1
if width >= (SIGN_WIDTH - charwidth[" "]) then if width >= (SIGN_WIDTH - charwidth[" "]) then
width = 0 width = 0
break
else else
maxw = math_max(width, maxw) maxw = math_max(width, maxw)
end end
if #chars < MAX_INPUT_CHARS then
table.insert(chars, { table.insert(chars, {
off=ch_offs, off=ch_offs,
tex=FONT_FMT_SIMPLE:format(c:byte()), tex=FONT_FMT_SIMPLE:format(c:byte()),
col=("%X"):format(cur_color), col=("%X"):format(cur_color),
}) })
end
ch_offs = ch_offs + w ch_offs = ch_offs + w
end end
end end
@ -420,6 +426,7 @@ local function make_line_texture(line, lineno)
end end
table.insert(texture, (":%d,%d=hdf_20.png"):format(xpos + word.w, ypos)) table.insert(texture, (":%d,%d=hdf_20.png"):format(xpos + word.w, ypos))
xpos = xpos + word.w + charwidth[" "] xpos = xpos + word.w + charwidth[" "]
if xpos >= (SIGN_WIDTH + charwidth[" "]) then break end
end end
table.insert(texture, fill_line(xpos, ypos, maxw, "n")) table.insert(texture, fill_line(xpos, ypos, maxw, "n"))