Accept input from scandinavian characters æøåäöå

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
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Accept input from scandinavian characters æøåäöå

Post 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?
User avatar
kalium
Prole
Posts: 9
Joined: Fri Jul 06, 2012 6:36 pm
Location: Rio de Janeiro

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

Post 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.
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

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

Post 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
Attachments
texttest.love
(38.41 KiB) Downloaded 239 times
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

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

Post 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.
Shallow indentations.
hexawing
Prole
Posts: 1
Joined: Wed Oct 10, 2012 3:17 pm

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

Post by hexawing »

Aha, can use Chinese finally!
thanks!
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

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

Post 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.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

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

Post 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.
Shallow indentations.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

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

Post 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.
Attachments
LUBETest.love
LUBE chat app with unicode support
(215.05 KiB) Downloaded 290 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post by bartbes »

Sadly, backspace doesn't work with unicode characters, though. Otherwise, nice work!
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

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

Post 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...
Post Reply

Who is online

Users browsing this forum: No registered users and 128 guests