Algorithm for printf wrap amount?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Algorithm for printf wrap amount?

Post by Zilarrezko »

I've worked with a GUI type deal, and wanted to reinvent some basic concept objects like sliders and text boxes and the like. Though I stopped and dropped it when I couldn't figure out the algorithm for when the printf wraps a word down and how many pixels it wraps vertically for a text box type thing (It was for a text box that has text that is longer than the box, so the excess gets cut off by stencils and the y offset of the text would be controlled by a slider).

tl;dr:
Anyone know how to get the amount of pixels vertically that printf uses when wrapping?
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Algorithm for printf wrap amount?

Post by Positive07 »

Okey first the short explanation: read [wiki]Font[/wiki] there are all the methods you need.

Long explanation:

You can use the function [wiki]Font:getWrap[/wiki]("your text",limit) to get how many lines long the text is gonna be:

Code: Select all

local text,limit = "This is the text passed to the function",100 --This is the width of the control
local font = love.graphics.getFont()
local rw,lines = font:getWrap(text,limit)
Then you can get the height of a line, there are three different things here, there is the height you can get with [wiki]Font:getHeight[/wiki] which is the one you pass to [wiki]love.graphics.newFont[/wiki], but this is not the real height, the real height can be calculated with [wiki]Font:getAscent[/wiki] and [wiki]Font:getDescent[/wiki], if you sum them up you get the complete height of the font. Then there is also [wiki]Font:getLineHeight[/wiki], this is the data that will tell you if there is spacing between two lines of text, but this is not measured in pixels, so you need to multiply it with the REAL height of the font:

Code: Select all

lineheight = font:getLineHeight() * (Font:getAscent() + Font:getDescent())
This is the total height of a line of text, if you want to know the spacing just get the difference:

Code: Select all

spacing = lineheight - (Font:getAscent() + Font:getDescent())
If you then multiply the lineheight by the number of lines you get the height!!

Code: Select all

height = lines * lineheight
Also you can use "rw" which is that other variable you can get from Font:getWrap, that is the real width, since your text may not cover the whole width of the element, you can use this to shrink your control automatically

NOTE: Remember to always leave some padding so that it looks right
Last edited by Positive07 on Fri Jan 09, 2015 7:56 pm, edited 2 times in total.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Algorithm for printf wrap amount?

Post by Zilarrezko »

Don't waste your typing finger's man haha. I got it now, the font:getWrap, my bad. Thanks! +1
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Algorithm for printf wrap amount?

Post by Positive07 »

Haha I thought you would notice on you own! I wrote it anyway, for anyone having this issue... I recommend you read it anyway, since I put the method to get the height of a line in there too
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests