love.graphics.printf 'ing a string with multiple spaces

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.
Post Reply
chute
Prole
Posts: 18
Joined: Sat Sep 17, 2011 6:49 pm

love.graphics.printf 'ing a string with multiple spaces

Post by chute »

Hi guys,

I think this one will be quick. When I use the following code:

Code: Select all

text = "Test                         Text" --the extra spaces here are intentional
love.graphics.printf(text, 10, 10, 100, "center")
it prints "Test Text" no matter how many spaces are in between the two words. love.graphics.print doesn't have this problem, so I assume it has something to do with the formatting code of printf.

Is there any way to force multiple spaces through printf?

Thanks!
User avatar
kexisse
Citizen
Posts: 56
Joined: Wed Jun 13, 2012 2:52 pm

Re: love.graphics.printf 'ing a string with multiple spaces

Post by kexisse »

It's most likely that "center" alignment is trying to justify the text, making it spread out between your text area of size 100px.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: love.graphics.printf 'ing a string with multiple spaces

Post by Roland_Yonaba »

Yeah, definitely.

Maybe you can hardcode a text centering function. Assuming a default font width, and a number limit:

Code: Select all

function center(text,number_limit)
    return (number_limit/2)-(text:len()*default_font_width)/2
end

function love.draw()
   text = "whatever"
   love.graphics.print(text,center(text),y)
end
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: love.graphics.printf 'ing a string with multiple spaces

Post by Boolsheet »

You're right, the code checks for the normal space. You can use one of the other space characters that unicode provides and it won't break into a newline.

Code: Select all

NBSP = "\194\160"
text = "Test".. NBSP:rep(25).. "Text"
You can of course use UTF-8 encoding with your text editor and write the character directly, just don't forget to save without the BOM.
Shallow indentations.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: love.graphics.printf 'ing a string with multiple spaces

Post by Roland_Yonaba »

Boolsheet wrote:You're right, the code checks for the normal space. You can use one of the other space characters that unicode provides and it won't break into a newline.

Code: Select all

NBSP = "\194\160"
text = "Test".. NBSP:rep(25).. "Text"
You can of course use UTF-8 encoding with your text editor and write the character directly, just don't forget to save without the BOM.
Seems it doesn't works... I just get the word "Text" printed. -_-
Attachments
main.lua
(144 Bytes) Downloaded 156 times
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: love.graphics.printf 'ing a string with multiple spaces

Post by Boolsheet »

Roland_Yonaba wrote:Seems it doesn't works... I just get the word "Text" printed. -_-
Move it some more to the right? :P
Shallow indentations.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: love.graphics.printf 'ing a string with multiple spaces

Post by Roland_Yonaba »

Gosh...
Sankyu for the tip, BTW!
chute
Prole
Posts: 18
Joined: Sat Sep 17, 2011 6:49 pm

Re: love.graphics.printf 'ing a string with multiple spaces

Post by chute »

Boolsheet wrote:You're right, the code checks for the normal space. You can use one of the other space characters that unicode provides and it won't break into a newline.

Code: Select all

NBSP = "\194\160"
text = "Test".. NBSP:rep(25).. "Text"
You can of course use UTF-8 encoding with your text editor and write the character directly, just don't forget to save without the BOM.
This helped. I forgot to mention it was an image font. I added \194\160 to the end of the image font and alternated the two spaces (like "Test \194\160 \194\160 \194\160 Text".

Thanks!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests