Page 1 of 2

Accept input from scandinavian characters æøåäöå

Posted: Mon Jul 30, 2012 10:16 pm
by NÖÖB
Hey, when I run the example at https://love2d.org/wiki/love.keypressed, after commenting "if unicode > 31 and unicode < 127 then ..." and try to press æ, ø or å on my keyboard, the program crashes. Is it possible to make Löve accept these keys, and display them?

Re: Accept input from scandinavian characters æøåäöå

Posted: Tue Jul 31, 2012 3:15 am
by kalium
Try making sure the font you are using supports those characters.
If it does, try specifying the unicode to include those characters' codes.

Re: Accept input from scandinavian characters æøåäöå

Posted: Tue Jul 31, 2012 10:10 pm
by NÖÖB
The following gives the error "graphics.lua:1265: Decoding error: Not enough space". I've uploaded a .love containing a font with the æøå characters..

Code: Select all

function love.load()
	font = love.graphics.newFont("op.ttf", 12)
	love.graphics.setFont(font)
end


function love.draw()
	love.graphics.print("Æ", 0,0 )
end

Re: Accept input from scandinavian characters æøåäöå

Posted: Tue Jul 31, 2012 10:18 pm
by Boolsheet
Almost all of the LÖVE API takes now UTF-8 strings. The error you see is a bit misleading, but comes from the UTF-8 library. It means you have passed an invalid UTF-8 string to love.graphics.print. Encode your Lua file to UTF-8 without BOM, then this should work.

If you want to display the unicode text the user wrote, you have to encode the string in UTF-8 as well. I think there's a thread in this forum with some code related to this (edit: Oops, that was not related to user input). There should also be pure Lua code for handling UTF-8 on the internet somewhere.

Re: Accept input from scandinavian characters æøåäöå

Posted: Fri Oct 12, 2012 2:06 am
by hexawing
Aha, can use Chinese finally!
thanks!

Re: Accept input from scandinavian characters æøåäöå

Posted: Sat Oct 13, 2012 10:35 am
by Przemator
I have a problem. I have changed the encoding of my main.lua to UTF-8 without BOM.

However, the following line gives error:

Code: Select all

love.graphics.print(string.char(243), 400, 100)
Decoding error: Not enough space

Please note, that this works:

Code: Select all

love.graphics.print("ó", 400, 80)
Also, when I press ó on my keyboard, love.keypressed returns the unicode value of 243, so I am sure this is the correct value.

Finally, printing string.char(243) to console returns some different character.

How can I make it possible to store user input in unicode and display it in LOVE? Please help.

Re: Accept input from scandinavian characters æøåäöå

Posted: Sat Oct 13, 2012 11:58 am
by Boolsheet
"Decoding error: Not enough space" is a misleading error message. It should be "Invalid UTF-8".

string.char(243) just returns "\243", one byte. Everything above 127 needs more than one byte to encode in UTF-8. Lua is not aware of unicode or any of its encodings, but I'm sure there are some pure-Lua UTF-8 libraries floating around on the internet. They help you encode the unicode value into a UTF-8 string which you then can pass to love.graphics.print.

print to the console is an entirely different beast. On Windows, it's probably the current code page set for the environment, definitely not unicode. On Linux, it may depend on the system locale.

Re: Accept input from scandinavian characters æøåäöå

Posted: Sat Oct 13, 2012 4:38 pm
by Przemator
Boolsheet you have really inspired me to do a bit of searching. Finally I found the Unicode to UTF converter.

The link:

http://developer.coronalabs.com/code/ut ... on-utility

The code:

Code: Select all

function CodeToUTF8 (Unicode)
    if (Unicode <= 0x7F) then return string.char(Unicode); end;

    if (Unicode <= 0x7FF) then
      local Byte0 = 0xC0 + math.floor(Unicode / 0x40);
      local Byte1 = 0x80 + (Unicode % 0x40);
      return string.char(Byte0, Byte1);
    end;

    if (Unicode <= 0xFFFF) then
      local Byte0 = 0xE0 +  math.floor(Unicode / 0x1000);
      local Byte1 = 0x80 + (math.floor(Unicode / 0x40) % 0x40);
      local Byte2 = 0x80 + (Unicode % 0x40);
      return string.char(Byte0, Byte1, Byte2);
    end;

    return "";                                   -- ignore UTF-32 for the moment
  end;

  function CodeFromUTF8 (UTF8)
    local Byte0 = string.byte(UTF8,1);
    if (math.floor(Byte0 / 0x80) == 0) then return Byte0; end;

    local Byte1 = string.byte(UTF8,2) % 0x40;
    if (math.floor(Byte0 / 0x20) == 0x06) then
      return (Byte0 % 0x20)*0x40 + Byte1;
    end;

    local Byte2 = string.byte(UTF8,3) % 0x40;
    if (math.floor(Byte0 / 0x10) == 0x0E) then
      return (Byte0 % 0x10)*0x1000 + Byte1*0x40 + Byte2;
    end;

    local Byte3 = string.byte(UTF8,4) % 0x40;
    if (math.floor(Byte0 / 0x08) == 0x1E) then
      return (Byte0 % 0x08)*0x40000 + Byte1*0x1000 + Byte2*0x40 + Byte3;
    end;
  end;
The code does not support all Unicode characters, but its sufficient for most purposes.

I also created a small app, which is my test to see how LUBE works. It's a chat app with unicode support. Hopefully it can be helpful to someone. Every message sent from the client to the server is then distributed to all clients.

Re: Accept input from scandinavian characters æøåäöå

Posted: Sat Oct 13, 2012 5:53 pm
by bartbes
Sadly, backspace doesn't work with unicode characters, though. Otherwise, nice work!

Re: Accept input from scandinavian characters æøåäöå

Posted: Sat Oct 13, 2012 8:21 pm
by Przemator
bartbes wrote:Sadly, backspace doesn't work with unicode characters, though. Otherwise, nice work!
thanks. oh you've found a bug :D. right, with unicode, the backspace might be tricky...