mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 02:16:43 +00:00
word wrap
This commit is contained in:
54
luigi/multiline.lua
Normal file
54
luigi/multiline.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
local Multiline = {}
|
||||
|
||||
function Multiline.wrap (font, text, limit)
|
||||
local lines = {{ width = 0 }}
|
||||
local advance = 0
|
||||
|
||||
local function append (word, space)
|
||||
local wordAdvance = font:getAdvance(word)
|
||||
local spaceAdvance = font:getAdvance(space)
|
||||
local words = lines[#lines]
|
||||
if advance + wordAdvance > limit then
|
||||
advance = wordAdvance + spaceAdvance
|
||||
lines[#lines + 1] = { width = advance, word, space }
|
||||
else
|
||||
advance = advance + wordAdvance + spaceAdvance
|
||||
words.width = advance
|
||||
words[#words + 1] = word
|
||||
words[#words + 1] = space
|
||||
end
|
||||
end
|
||||
|
||||
local function appendFrag (frag, isFirst)
|
||||
if isFirst then
|
||||
append(frag, '')
|
||||
else
|
||||
local wordAdvance = font:getAdvance(frag)
|
||||
lines[#lines + 1] = { width = wordAdvance, frag }
|
||||
advance = wordAdvance
|
||||
end
|
||||
end
|
||||
|
||||
local leadSpace = text:match '^ +'
|
||||
|
||||
if leadSpace then
|
||||
append('', leadSpace)
|
||||
end
|
||||
|
||||
for word, space in text:gmatch '([^ ]+)( *)' do
|
||||
if word:match '\n' then
|
||||
local isFirst = true
|
||||
for frag in (word .. '\n'):gmatch '([^\n]*)\n' do
|
||||
appendFrag(frag, isFirst)
|
||||
isFirst = false
|
||||
end
|
||||
append('', space)
|
||||
else
|
||||
append(word, space)
|
||||
end
|
||||
end
|
||||
|
||||
return lines
|
||||
end
|
||||
|
||||
return Multiline
|
||||
Reference in New Issue
Block a user