Page 1 of 2

Getting number of lines of printf

Posted: Fri Jun 11, 2010 7:21 am
by Lap
If I am using printf for wordwrap there is no way to accurately determine how many lines a given string will be or how many vertical pixels it will take. I'd like to request this feature be added to future versions of Love.

Re: Getting number of lines of printf

Posted: Fri Jun 11, 2010 7:30 am
by nevon
There is a very cumbersome way.

Code: Select all

font = love.graphics.newFont()
str = "This is a very long string that will surely be split over several lines."
maxwidth = 100
lines = math.ceil(font:getWidth(str)/maxwidth)
vspace  = font:getHeight(str)*lines

Re: Getting number of lines of printf

Posted: Fri Jun 11, 2010 7:59 am
by Lap
The method you have is similar to the one I'm using and both ways work poorly. They are only estimates and are unsatisfactory.

When you estimate like this you frequently get overestimating on some strings which in my case means boxes around text having varying amounts of ugly white space at the end.

Re: Getting number of lines of printf

Posted: Fri Jun 11, 2010 8:54 am
by nevon
Yes, I agree. Text handling is quite poor, but at least it's something to get you by for now. If the issue tracker was up, you could file a feature request.

Re: Getting number of lines of printf

Posted: Fri Jun 11, 2010 4:33 pm
by Luiji
My recommendation: when you have to wrap text like that, use a monospace font. It's also good if you are trying to make your game look retro.

Re: Getting number of lines of printf

Posted: Fri Jun 11, 2010 8:28 pm
by Elvashi
I'm fairly certain I have FRed it in the past, and monoscape is not a solution, at best its a hack*.

Font objects have getWidth(string), which can be used to get the length of a string, it seems safe to assume that love is using the basic "wrap when too long" method, which should be easy enough to implement.

* Unless, ofcause, you want to use a monoscape font in the first place.

Re: Getting number of lines of printf

Posted: Fri Jun 11, 2010 8:57 pm
by bartbes
I just checked the algorithm, whenever it is over the wrap limit it goes back to the last space character and inserts a line break there, if there is no space it just continues on the line. Hope this helps.

Re: Getting number of lines of printf

Posted: Sat Jun 12, 2010 5:06 am
by Lap
It is technically possible to predict the lines, but by doing so you are essentially recreating the entire word wrap function which is a huge waste.

Re: Getting number of lines of printf

Posted: Sat Jun 12, 2010 9:45 am
by bartbes
Well, I guess I can add a function to calculate it, should I consider this a feature request?

Re: Getting number of lines of printf

Posted: Sat Jun 12, 2010 11:31 am
by Lap
Yes, please. I'd add it to the tracker if I could.