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.
-
ZenX2
- Citizen
- Posts: 89
- Joined: Wed Nov 17, 2010 5:35 am
Post
by ZenX2 » Fri Feb 03, 2012 11:24 pm
I'm working on making a virtual terminal so I can make roguelikes and other textual things.
(Screenshot)
It does most of the things I need it to, except being able to draw box-drawing character and other utf-8 glyphs.
My question is, how would I be able to draw other characters besides letters? Would I just stick them in a string like letters?
-
tentus
- Inner party member
- Posts: 1058
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
-
Contact:
Post
by tentus » Sat Feb 04, 2012 12:12 am
In 0.7.2 I think you can go up to character 255 (ÿ) like so:
Code: Select all
love.graphics.print("\255", 0, 0)
-- or
love.graphics.print("ÿ", 0, 0)
This breaks things in 0.8.0 though. It only goes up to \127.
Why? I haven't the foggiest.
-
hryx
- Party member
- Posts: 110
- Joined: Mon Mar 29, 2010 2:28 am
- Location: SF<CA<USA
Post
by hryx » Sat Feb 04, 2012 6:46 am
tentus wrote:This breaks things in 0.8.0 though. It only goes up to \127.
Why? I haven't the foggiest.
0.8.0 switches strings to UTF-8, whose characters have variable length (in terms of bytes). The first 7 bits of UTF-8 constitute the ASCII character set. The 8th bit is used to tell Unicode that the character needs more bits to be described properly.
Characters like ÿ are in the ANSI character set and use up that 8th bit, and therefore in UTF-8 they are placed in higher bytes. ASCII is a subset of UTF-8; ANSI is not.
This explanation should help you visualize it.
-
Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
-
Contact:
Post
by Robin » Sat Feb 04, 2012 1:00 pm
A simple solution would be to just do
Code: Select all
love.graphics.print("thìngś likæ thỹs¶¥×¤€", x, y)
If you do that, you must use UTF-8 as the encoding for your source files.
-
utunnels
- Citizen
- Posts: 75
- Joined: Fri Jan 06, 2012 5:20 pm
Post
by utunnels » Sat Feb 04, 2012 1:48 pm
May I ask a related question?
Since we have utf-8 support now, how to get a single character from a utf-8 string? The lua function string.byte doesn't work obviously.
---
Edit*
OK, I just found a long article about that on the lua site. It appears there's no easy way, except interpreting the string byte by byte or using some complex regular expressions.
http://lua-users.org/wiki/LuaUnicode
-
Ellohir
- Party member
- Posts: 235
- Joined: Sat Oct 22, 2011 11:12 pm
Post
by Ellohir » Sat Feb 04, 2012 2:29 pm
You can easily use "string.sub", taking a substring starting and ending at the first character. For example:
Code: Select all
function love.draw()
x = string.sub("¥×¤€ìng? likæ th?s", 1, 1)
love.graphics.print(x, 400, 300)
end
Prints ¥.
-
utunnels
- Citizen
- Posts: 75
- Joined: Fri Jan 06, 2012 5:20 pm
Post
by utunnels » Sat Feb 04, 2012 2:41 pm
I don't think that always works, if the character is utf-8 encoded (for example that ÿ takes 2 bytes and any Chinese character takes 3 bytes).
I figured it out a minute ago though:
Code: Select all
function utf8iterate(str)
return string.gfind(str, "([%z\1-\127\194-\244][\128-\191]*)")
end
...blahblah
local line = 0
for c in utf8iterate(someutf8string) do
love.graphics.print(c, 10, 40+26*line)
line = line + 1
end
-
MarekkPie
- Inner party member
- Posts: 587
- Joined: Wed Dec 28, 2011 4:48 pm
-
Contact:
Post
by MarekkPie » Sat Feb 04, 2012 5:27 pm
Is that...
...REGEX!?!?!?!?!?
(runs away screaming)
-
Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
-
Contact:
Post
by Robin » Sat Feb 04, 2012 7:49 pm
MarekkPie wrote:Is that...
...REGEX!?!?!?!?!?
Nope.
Chuck Testa.
It's called patterns and less powerful than regular expressions. But they look a bit like them.
-
hryx
- Party member
- Posts: 110
- Joined: Mon Mar 29, 2010 2:28 am
- Location: SF<CA<USA
Post
by hryx » Sun Feb 05, 2012 1:51 am
utunnels wrote:Code: Select all
function utf8iterate(str)
return string.gfind(str, "([%z\1-\127\194-\244][\128-\191]*)")
end
...blahblah
local line = 0
for c in utf8iterate(someutf8string) do
love.graphics.print(c, 10, 40+26*line)
line = line + 1
end
Well that's no fun. I understand why string.sub() wouldn't work like you'd think in this case, but there should be an equally simple way to grab a subset of a string. I find that it's a common task.
What about a new LoveString type? New string module?
Code: Select all
s = love.string.sub('Hello, 世界', 5, 8)
love.graphics.print(s, 40, 40) --> "o, 世"
Not that anybody wants a new module or type.
Users browsing this forum: Exabot [Bot] and 7 guests