Code: Select all
local exports = {}
local function isHan(char)
-- Simple processing. Not precise
return char >= 0x3000
end
local function isSpace(char)
return char == 0x20
end
local function isBreak(char)
return char == 0xA
end
local function forUChar(s, f)
local i = 1
local len = string.len(s)
while i <= len do
local char
local b = string.byte(s, i)
local d = 0
if b < 0x80 then
char = b
elseif b < 0xc0 then
char = (b - 0xc0) * 64 + (string.byte(s, i + 1) - 0x80)
d = 1
else
char = (b - 0xe0) * 4096 + (string.byte(s, i + 1) - 0x80) * 64 + (string.byte(s, i + 2) - 0x80)
d = 2
end
f(char, string.sub(s, i, i + d))
i = i + d + 1
end
end
local function layout(s, x, y, limit)
local segments = {}
local spaces = {}
local buff = ""
local font = love.graphics.getFont()
local lineHeight = font:getLineHeight() * font:getHeight()
forUChar(s, function(char, ds)
if isHan(char) then
if string.len(buff) > 0 then
table.insert(segments, buff)
table.insert(spaces, "")
buff = ""
end
table.insert(segments, ds)
table.insert(spaces, "")
elseif isBreak(char) then
if string.len(buff) > 0 then
table.insert(segments, buff)
table.insert(spaces, "")
buff = ""
end
table.insert(segments, "\n")
table.insert(spaces, "")
elseif isSpace(char) then
table.insert(segments, buff)
table.insert(spaces, ds)
buff = ""
else
buff = buff .. ds
end
end)
if string.len(buff) > 0 then
table.insert(segments, buff)
table.insert(spaces, "")
buff = ""
end
local position = 0
local line = ""
for i, segment in ipairs(segments) do
position = position + font:getWidth(segment)
if segment ~= "\n" and position <= limit then
line = line .. segment
if position + font:getWidth(spaces[i]) <= limit then
position = position + font:getWidth(spaces[i])
line = line .. spaces[i]
end
else
love.graphics.print(line, x, y)
y = y + lineHeight
if segment == "\n" then
line = ""
position = 0
else
line = segment .. spaces[i]
position = font:getWidth(segment)
end
end
end
love.graphics.print(line, x, y)
end
exports.layout = layout
return exports
Code: Select all
layouter.layout("Wish your dream comes true\n愿你的梦想可以绽放\nあなたの夢を、咲きますように", 100, 100, 200)
三小时的成果,很简单,没有考虑标点周围的特殊行为。