Newlines for rotated texts

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Newlines for rotated texts

Post by osuf oboys »

love.graphics.draw(string, x, y) supports newlines and breaks as one might expect. love.graphics.draw(string, x, y, angle) does not make new lines and instead prints "\n" as squares, even if angle is 0. Is this complicated to amend or perhaps even intended?

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
On a related note, might we get an overloaded love.graphics.draw(particlesystem, x, y, [angle, [sx, sy]]) (optional arguments) even if the latter three arguments are not yet supported? It would make the construction of modules overloading the draw function much easier.

Thanks!
Attachments
stringDrawingBehavior.love
(268 Bytes) Downloaded 98 times
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Newlines for rotated texts

Post by osuf oboys »

In the meanwhile, here's a softlöve fix. Just copy to a file and include, syntax unchanged. See the attached love file for a demonstration. The third from the left texts are drawn with the original three-argument version.

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
Edit: Updated.
Attachments
newlineStringDrawing.love
Fix: newlines in string drawing
(987 Bytes) Downloaded 87 times
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Newlines for rotated texts

Post by osuf oboys »

Two other newline-related functions that I needed for my project.
util.getFontWidth does the same as Font:getWidth(), except it takes the maximum over all lines, where lines are separated by newlines (\n) and any number of carriage returns (\r).
util.splitTextByFontWidth(font, text, limit) takes a line as input and inserts newlines such that each line is at most limit wide, with the exception of continuous sequences without spaces. In effect, this does the same as love.graphics.drawf() except that you here get the string as a return argument instead of printing it to the screen, and newlines are supported.

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
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 207 guests