Page 1 of 2

Box-drawing characters/other UTF-8?

Posted: Fri Feb 03, 2012 11:24 pm
by ZenX2
I'm working on making a virtual terminal so I can make roguelikes and other textual things.

(Screenshot)
Image

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?

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 12:12 am
by tentus
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.

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 6:46 am
by hryx
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.

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 1:00 pm
by Robin
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.

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 1:48 pm
by utunnels
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

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 2:29 pm
by Ellohir
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 ¥.

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 2:41 pm
by utunnels
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

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 5:27 pm
by MarekkPie
Is that...

...REGEX!?!?!?!?!?

(runs away screaming)

Re: Box-drawing characters/other UTF-8?

Posted: Sat Feb 04, 2012 7:49 pm
by Robin
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.

Re: Box-drawing characters/other UTF-8?

Posted: Sun Feb 05, 2012 1:51 am
by hryx
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.