Accept input from scandinavian characters æøåäöå
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Accept input from scandinavian characters æøåäöå
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 æøåäöå
Try making sure the font you are using supports those characters.
If it does, try specifying the unicode to include those characters' codes.
If it does, try specifying the unicode to include those characters' codes.
Re: Accept input from scandinavian characters æøåäöå
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 308 times
Re: Accept input from scandinavian characters æøåäöå
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.
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.
Re: Accept input from scandinavian characters æøåäöå
Aha, can use Chinese finally!
thanks!
thanks!
Re: Accept input from scandinavian characters æøåäöå
I have a problem. I have changed the encoding of my main.lua to UTF-8 without BOM.
However, the following line gives error:
Decoding error: Not enough space
Please note, that this works:
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.
However, the following line gives error:
Code: Select all
love.graphics.print(string.char(243), 400, 100)
Please note, that this works:
Code: Select all
love.graphics.print("ó", 400, 80)
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 æøåäöå
"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.
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.
Re: Accept input from scandinavian characters æøåäöå
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:
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.
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;
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 329 times
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Accept input from scandinavian characters æøåäöå
Sadly, backspace doesn't work with unicode characters, though. Otherwise, nice work!
Re: Accept input from scandinavian characters æøåäöå
thanks. oh you've found a bug . right, with unicode, the backspace might be tricky...bartbes wrote:Sadly, backspace doesn't work with unicode characters, though. Otherwise, nice work!
Who is online
Users browsing this forum: Semrush [Bot] and 12 guests