Get widest character of font?

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
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Get widest character of font?

Post by TheOddByte »

Hello, I'm wondering how I can get the width of the font. It's kind of hard for me to explain what I mean, so I'll show it in code

Code: Select all

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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Get widest character of font?

Post by Positive07 »

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.
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
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Get widest character of font?

Post by zorg »

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.
Me and my stuff :3True 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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Get widest character of font?

Post by Positive07 »

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 in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Get widest character of font?

Post by ivan »

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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Get widest character of font?

Post by Positive07 »

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 in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Get widest character of font?

Post by zorg »

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 :3True 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.
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Re: Get widest character of font?

Post by TheOddByte »

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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Get widest character of font?

Post by Positive07 »

Then you can make sure that is monospaced... by checking if multiple characters have the same width and reject the font otherwise
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
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Re: Get widest character of font?

Post by TheOddByte »

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?

Code: Select all

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?

Code: Select all

local function isMonospaced( font )
     return font:getWidth( "I" ) == font:getWidth( "M" )
end
What's the object-oriented way to get wealthy? Inheritance.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest