Getting number of lines of printf
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
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
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.
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.
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.
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.
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
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.
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?
Re: Getting number of lines of printf
Yes, please. I'd add it to the tracker if I could.
Who is online
Users browsing this forum: No registered users and 209 guests