[SOLVED] font.getWidth and Umlauts

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
Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

[SOLVED] font.getWidth and Umlauts

Post by Germanunkol » Fri Jun 22, 2012 4:58 pm

I'm having troubles with font.getWidth(). It crashes when it reaches an umlaut:

My code:

Code: Select all

buttonFont = love.graphics.newFont( 18 )
function love.load(arg)
	titleStr = ""
	fullTitle = "schrödinger"
	buttonFont:getWidth( fullTitle )
	j = 0
	while buttonFont:getWidth( titleStr ) < buttonWidth-20 and #titleStr < #fullTitle do
		print(titleStr.. " " .. buttonFont:getWidth( titleStr ))
		titleStr = fullTitle:sub( 1, j )
		print("new: " .. titleStr)
		j = j+1
	end
end
Output:

Code: Select all

 0
new: 
 0
new: s
s 8
new: sc
sc 17
new: sch
sch 28
new: schr
schr 36
new: schr
Error: main.lua:62: Not enough space
stack traceback:
	[C]: in function 'getWidth'
	main.lua:62: in function 'load'
	[string "boot.lua"]:378: in function <[string "boot.lua"]:373>
	[C]: in function 'xpcall'
What does "not enough space" mean? Am I doing something wrong?
Can someone confirm this? Is it an engine-bug?

Thanks...
Last edited by Germanunkol on Sat Jun 23, 2012 8:20 am, edited 1 time in total.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: font.getWidth and Umlauts

Post by Boolsheet » Fri Jun 22, 2012 5:36 pm

"Not enough space" is an exception thrown by the UTF-8 library used by LÖVE. This can happen when the library encouters an unfinished UTF-8 string. In this case, you chop the UTF-8 code for 'ö' which uses 2 bytes in half with fullTitle:sub( 1, j ). The library sees the first byte and expects another, but is already at the end of the string and throws.

Or something like that anyway. One would expect a "Invalid UTF-8" message here.

Make sure you chop UTF-8 strings a the right places if you want to split them up.
Shallow indentations.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: font.getWidth and Umlauts

Post by Robin » Fri Jun 22, 2012 6:13 pm

From the Lua users wiki:

Code: Select all

for uchar in string.gfind(ustring, "([%z\1-\127\194-\244][\128-\191]*)") do
  -- something
end
It might be useful to write a function for that:

Code: Select all

function utf8iter(str)
    return str:gfind("([%z\1-\127\194-\244][\128-\191]*)")
end
Use it like:

Code: Select all

buttonFont = love.graphics.newFont( 18 )
function love.load(arg)
   titleStr = ""
   fullTitle = "schrödinger"
   for char in utf8iter(fullTitle) do
      titleStr = titleStr .. char
      if buttonFont:getWidth( titleStr ) < buttonWidth-20 then
          print(titleStr.. " " .. buttonFont:getWidth( titleStr ))
      else
          break
      end
   end
end
Help us help you: attach a .love.

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: [SOLVED] font.getWidth and Umlauts

Post by Germanunkol » Sat Jun 23, 2012 8:21 am

Perfect, thank you both!
It works like a charm, using gfind.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

Post Reply

Who is online

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