## Getting number of lines of printf

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

### Getting number of lines of printf

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

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

### Re: Getting number of lines of printf

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

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.
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

### Re: Getting number of lines of printf

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.
Elvashi
Prole
Posts: 45
Joined: Sat Jul 04, 2009 9:17 am
Location: Australia

### Re: Getting number of lines of printf

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
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Getting number of lines of printf

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

### Re: Getting number of lines of printf

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.
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Getting number of lines of printf

Well, I guess I can add a function to calculate it, should I consider this a feature request?
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm