Box-drawing characters/other UTF-8?

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
ZenX2
Citizen
Posts: 89
Joined: Wed Nov 17, 2010 5:35 am

Box-drawing characters/other UTF-8?

Post 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?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

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

Post 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.
Kurosuke needs beta testers
User avatar
hryx
Party member
Posts: 110
Joined: Mon Mar 29, 2010 2:28 am
Location: SF<CA<USA

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

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

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

Post 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
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

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

Post 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 ¥.
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

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

Post 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
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

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

Post by MarekkPie »

Is that...

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

(runs away screaming)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
User avatar
hryx
Party member
Posts: 110
Joined: Mon Mar 29, 2010 2:28 am
Location: SF<CA<USA

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

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

Who is online

Users browsing this forum: Bing [Bot], dusoft and 45 guests