Determining the width of a character/word

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
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Determining the width of a character/word

Post by mickeyjm »

I'm trying to create something that prints coloured text whe supplied with a table in the format of:

Code: Select all

{"String",{R,G,B},"String 2"...}
So far I can get it to print the strings in the correct colour but I need a way to determine the width of the previous strings so words dont overlap. I tried just counting the length of the string but LOVE's default font doesnt seem to be fixed width. Do I need to find a fixed width font or is there a way to calculate the size of a string (in pixels)?
Your screen is very zoomed in...
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Determining the width of a character/word

Post by Nixola »

I think it's font:getWidth(text) or something similar
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Determining the width of a character/word

Post by mickeyjm »

thx, but does this work with no preset font?

EDIT: Duurr I forgot about love.graphics.getFont()
Your screen is very zoomed in...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Determining the width of a character/word

Post by Robin »

I think love.graphics.getFont() will automatically create the default font in future versions of LÖVE, but doesn't currently. So for now: no, you will need to create the font.
Help us help you: attach a .love.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Determining the width of a character/word

Post by Roland_Yonaba »

mickeyjm wrote:I'm trying to create something that prints coloured text whe supplied with a table in the format of:

Code: Select all

{"String",{R,G,B},"String 2"...}
So far I can get it to print the strings in the correct colour but I need a way to determine the width of the previous strings so words dont overlap. I tried just counting the length of the string but LOVE's default font doesnt seem to be fixed width. Do I need to find a fixed width font or is there a way to calculate the size of a string (in pixels)?
Based on love's graphics.lua script:

Code: Select all

	local _newFont = love.graphics.newFont
	love.graphics.newFont = function(font, size)
		if type(font) == "number" or not font then
			size = font
			font = vera_ttf
		end
		return _newFont(font, size or 12)
	end
Why not use 12*string.length ?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Determining the width of a character/word

Post by kikito »

Roland_Yonaba wrote: Why not use 12*string.length ?
Because if love ever changes its default font size to 13, that code will stop working.
When I write def I mean function.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Determining the width of a character/word

Post by Nixola »

Because not every character has the same width
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Determining the width of a character/word

Post by bartbes »

Roland_Yonaba wrote: Why not use 12*string.length ?
Because 12 is the font height in points.
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: Determining the width of a character/word

Post by Nsmurf »

Just use a fixed width font.

I did something like that in Source Code: The Game.
OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Determining the width of a character/word

Post by Robin »

Seriously, what's wrong with Font:getWidth()?
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 98 guests