local function getWidth( font )
local width = 0
for i = 32, 127 do
local w = font:getWidth( string.char( i ) )
if w > width then
width = w
end
end
return width
end
Basically I want to get the width of the widest character so that I can fit all characters in a grid correctly, is there a better way to do this?
What's the object-oriented way to get wealthy? Inheritance.
One is whether the font is monospaced or not; if it is, then technically all characters should be the same width.
(There is a caveat though, some fonts say they're monospaced but they're also kerning dependent, so without that, the glyphs may not be the same width at all; not sure if this is relevant with Löve or not, though)
The second is that if it's not monospaced, then you'll have to draw out each glyph offset by various amounts to make it look monospaced, if you want them to fit into a regular grid.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
That is the only use case I can think of getting the widest character width. Generally you want to dynamically get the character width of the character you are currently drawing.
for i, person inipairs(everybody) do [tab]ifnot person.obey then person:setObey(true) end end
love.system.openURL(github.com/pablomayobre)
TheOddByte wrote: ↑Mon Feb 20, 2017 4:05 pm
Basically I want to get the width of the widest character so that I can fit all characters in a grid correctly, is there a better way to do this?
I wouldn't do it in Lua since this is the sort of metric that is not expected to change.
Another option is to use FontForge.
Just open up your font then select all glyphs (compact encoding) and you can then see (or modify) the exact width of each glyph.
Like Zorg said, changing the width will ruin the font unless it's already monospaced.
If you prefer using Lua, then just get the width of the letter 'M' and you'll be fine.
You could also get a rough estimate using the W character which is most of the time the widest and then multiply that by a certain amount so that even if there are bigger characters they will fit, and they will fit in the grid with some margins between them.
If you want them to be one against the other like in a console, to draw graphics with text, then use a monospaced font or an image font
for i, person inipairs(everybody) do [tab]ifnot person.obey then person:setObey(true) end end
love.system.openURL(github.com/pablomayobre)
Do note that even if you're adamant on not using a monospaced or an image font, due to you needing to draw all symbols separately because of the varying offsets you'd need to apply per-symbol, the amount of draw calls will skyrocket pretty fast.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Positive07 wrote: ↑Mon Feb 20, 2017 5:02 pm
Just a question, why do you need that value?
I'm asking since you may be approaching the problem wrong. And depending on your answer, your solution may be right.
To clarify what I need this for, I'm creating a virtual computer inside LÖVE that I later plan on adding into the actual game I'm creating, I need the width so that I can align the characters correctly onto that virtual screen which is set up as a grid.
zorg wrote: ↑Mon Feb 20, 2017 9:17 pm
Basically, there are two things to consider;
One is whether the font is monospaced or not; if it is, then technically all characters should be the same width.
(There is a caveat though, some fonts say they're monospaced but they're also kerning dependent, so without that, the glyphs may not be the same width at all; not sure if this is relevant with Löve or not, though)
The second is that if it's not monospaced, then you'll have to draw out each glyph offset by various amounts to make it look monospaced, if you want them to fit into a regular grid.
If you look at my answer above you'll probably understand better why I can't really predict if it's a monospaced font or not, because I want the user to be able to set the font to use for that virtual screen.
What's the object-oriented way to get wealthy? Inheritance.
Positive07 wrote: ↑Tue Feb 21, 2017 6:50 pm
Then you can make sure that is monospaced... by checking if multiple characters have the same width and reject the font otherwise
So I'd do that with almost the identical code I'm using? Like instead of setting the width to the widest character found I'd return false if the width doesn't match the other characters?
local function isMonospaced( font )
local width = font:getWidth( string.char( 32 ) )
for i = 33, 127 do
local w = font:getWidth( string.char( i ) )
if w ~= width then
return false
end
end
return true
end
Or are you suggesting a shorter solution like this?