Page 2 of 2

Re: Getting number of lines of printf

Posted: Sat Jun 12, 2010 1:39 pm
by bartbes
There now is a getWrap function returning the maximum width and the number of lines, the code I used to test it:

Code: Select all

function dbg(...)
	print(...)
	return ...
end

function love.load()
	ma_string = "THIS SHOULD WRAP-A-LOT, BUT NOT THERE!"
	font = love.graphics.newFont(12)
	love.graphics.setFont(font)
	width, lines = dbg(font:getWrap(ma_string, 15))
	height = (lines+1)*(font:getHeight()+font:getLineHeight())
end

function love.draw()
	love.graphics.rectangle("line", 100, 100, width, height)
	love.graphics.printf(ma_string, 100, 100, 15)
end
Result:
Success.png
Success.png (2.37 KiB) Viewed 1720 times

Re: Getting number of lines of printf

Posted: Sat Jun 12, 2010 3:59 pm
by Lap
Well that was the fastest feature request to implementation time I can remember. Thanks.

Side question: Does the latest dev version use the new way of printing. I remember someone saying the starting x,y for text meant something different in the next version? I was waiting for this kind of function to be added to love and for whatever the new print method to be implemented before I went and tweaked all the text based stuff in my game.

Re: Getting number of lines of printf

Posted: Sat Jun 12, 2010 4:51 pm
by bartbes
It's top-left now, as is all drawing.

Re: Getting number of lines of printf

Posted: Sun Jun 13, 2010 3:21 am
by Jasoco
So this means I'll be able to make my dialog bubbles dynamically sized?

Re: Getting number of lines of printf

Posted: Sun Jun 13, 2010 7:00 am
by bartbes
Yes.

Re: Getting number of lines of printf

Posted: Wed Nov 03, 2010 1:09 am
by luminosity
I think I'm missing something in this function. Test code:

Code: Select all

function love.load()
    font = love.graphics.newFont(12)
    love.graphics.setFont(font)
    lines = 0
    text = ""
    timePassed = 0
end

function love.draw()
    love.graphics.setColor(255, 255, 255)
    love.graphics.printf(text, 0, 0, 1000, "left")
end	

function love.update(dt)
    timePassed = timePassed + dt
    if timePassed > 1 then
        timePassed = timePassed - 1
        lines = lines + 1
        if #text > 0 then
            text = text .. "\n"
        end
        text = text .. 'a'
        local _, wrapLines = font:getWrap(text, 1000)
        print('Lines', lines, 'Wrap lines', wrapLines, _)
    end
end
Using the 0.7 beta on Windows 7.

The text takes up more and more lines, but the lines variable stays 1, while width increases. Has the function changed since this post? I couldn't find it in the wiki.

Edit

Okay so I went and looked at the C source, like I probably should have originally. It appears getWrap is intended to only find the wrap for a piece of text without newlines in it, hence my weird results. I was thrown off by assuming it was intended to measure the height of any string you could hand to love.graphics.print. I've rewritten my font rendering to bear this in mind and it works perfectly now.

Should I edit the documentation to add this method, with a quick explanation of this to ensure other people don't trip over this problem in future?