Code: Select all
string = "1\n12\n123\n1234\n12345"
font = love.graphics.newFont(love.default_font)
love.graphics.setFont(font)
function draw()
love.graphics.draw(string, 10, 10)
love.graphics.draw(string, 110, 10, 0)
end
Thanks!
Code: Select all
string = "1\n12\n123\n1234\n12345"
font = love.graphics.newFont(love.default_font)
love.graphics.setFont(font)
function draw()
love.graphics.draw(string, 10, 10)
love.graphics.draw(string, 110, 10, 0)
end
Code: Select all
--[[ Fix: Newlines in string drawing
Author: osuf oboys
Version: 1, Jan 31, 2009
--]]
function string.lineiter(s)
s = s.."\n"
return s:gmatch("([^\r\n]*)\r*\n\r*")
end
love.graphics.default_draw = love.graphics.draw
function love.graphics.draw(elem, x, y, ...)
if not type(elem) == "string" then
return love.graphics.default_draw(elem, x, y, unpack(arg))
end
local angle, sx, sy = unpack(arg)
angle = angle or 0
sx = sx or 1
sy = sy or sx
local c = love.graphics:getFont():getHeight() * love.graphics:getFont():getLineHeight()
for line in string.lineiter(elem) do
love.graphics.default_draw(line, x, y, angle, sx, sy)
x = x - sx * c * math.sin(math.rad(angle))
y = y + sy * c * math.cos(math.rad(angle))
end
end
Code: Select all
util = {}
-- Supports newlines
function util.getFontWidth(font, text)
font = font or love.graphics.getFont()
local maxwidth = 0
for line in string.lineiter(text) do
maxwidth = math.max(maxwidth, font:getWidth(line))
end
return maxwidth
end
-- Supports newlines
function util.splitTextByFontWidth(font, text, width)
font = font or love.graphics.getFont()
local s = ""
for line in string.lineiter(text) do
if util.getFontWidth(font, line) <= width then
s = s..line.."\n"
else
local tmps = ""
for token in line:gmatch("%s*[^%s]+") do
if tmps:len() == 0 or util.getFontWidth(font, tmps..token) <= width then
tmps = tmps..token
else
s = s..tmps.."\n"
--TODO remove preceding whitespaces
tmps = token
end
end
s = s..tmps.."\n"
end
end
return s:sub(1,s:len()-1)
end
Users browsing this forum: No registered users and 11 guests