Getting number of lines of printf

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.
User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Getting number of lines of printf

Post by Lap » Fri Jun 11, 2010 7:21 am

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.

User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Getting number of lines of printf

Post by nevon » Fri Jun 11, 2010 7:30 am

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

User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: Getting number of lines of printf

Post by Lap » Fri Jun 11, 2010 7:59 am

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.

User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Getting number of lines of printf

Post by nevon » Fri Jun 11, 2010 8:54 am

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.

User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: Getting number of lines of printf

Post by Luiji » Fri Jun 11, 2010 4:33 pm

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.
Good bye.

User avatar
Elvashi
Prole
Posts: 45
Joined: Sat Jul 04, 2009 9:17 am
Location: Australia

Re: Getting number of lines of printf

Post by Elvashi » Fri Jun 11, 2010 8:28 pm

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.
"We could make a program for doing this for you, but that is for the LÖVE IDE, planned to be released in March 2142." ~mike
Networking with UDP uLove Proposal CCG: Gangrene

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Getting number of lines of printf

Post by bartbes » Fri Jun 11, 2010 8:57 pm

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.

User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: Getting number of lines of printf

Post by Lap » Sat Jun 12, 2010 5:06 am

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.
Last edited by Lap on Sat Jun 12, 2010 11:31 am, edited 1 time in total.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Getting number of lines of printf

Post by bartbes » Sat Jun 12, 2010 9:45 am

Well, I guess I can add a function to calculate it, should I consider this a feature request?

User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: Getting number of lines of printf

Post by Lap » Sat Jun 12, 2010 11:31 am

Yes, please. I'd add it to the tracker if I could.

Post Reply

Who is online

Users browsing this forum: No registered users and 71 guests