How to use Emoji (and other non ascii) characters

As part of my continued efforts to find alternative ways to generate / use graphics in Codea I came upon the idea of using some of the vector assets that are stored as part of the Apple Emjoi font.

Obviously using these in Codea is easy (just change the keyboard layout), but if you try and copy the code or edit via AirCode then the characters get screwed up (as they have below), this is because they are not in the normal ascii range but are mutlibyte characters - in fact if you do a string.len(“”) with a single emoji char you’ll get a length of four.

Anyway - after a big of Googling (noobies take note, google really is your friend when trying to find out new stuff!), I was able to run up the following code - the value of “str” in the comment block should be the 8 emoji moon characters in sequence - but you can see the same thing with the international ones, and probably Chinese / Japanese Kana & Kanji as well.

The commented code prints out the four byte sequence that makes up each of the characters in “str” so you can “recreate” them again afterwards, each entry in the str[] table is actually a single emoji, now if I wanted to actually use them in a game, I’d render them to an image and blit that - but the code is just a proof of concept.

Enjoy.

--# Main
-- Emoji Demo

-- Use this function to perform your initial setup
function setup()
    print("Hello Emoji!")

--[[
    -- str = "à,á,,ã,ä,å"
    str = "<chars removed as they mess up the post>"
    pattern = "[%z\\1-\\127\\194-\\244][\\128-\\191]*"

    for c in str:gmatch(pattern) do
        print(c:byte(1, -1))
    end
--]]

    str = {}
    str[1] = string.char(240,159,140,145)
    str[2] = string.char(240,159,140,146)
    str[3] = string.char(240,159,140,147)
    str[4] = string.char(240,159,140,148)
    str[5] = string.char(240,159,140,149)
    str[6] = string.char(240,159,140,150)
    str[7] = string.char(240,159,140,151)
    str[8] = string.char(240,159,140,152)

    font("AppleColorEmoji")
    fontSize(300)
end

-- This function gets called once every frame
local f = 1
function draw()
    -- This sets a dark background color
    background(40, 40, 50)

    smooth()
    -- Do your drawing here
    text(str[1+math.floor(f/2)],WIDTH/2,HEIGHT/2)
    f = f + 1
    if f >= 8*2 then f = 1 end

end

Quick follow-up to this since I needed to be able to distinguish emojis from text and was working on the basis that all emojis were four characters long, as per @TechDojo’s post above. Turns out some are 6. I haven’t done extensive testing but certainly all the card suit emojis are 6 characters long.

Did you try googling in this forum?

http://codea.io/talk/discussion/comment/12393

@epicurus101 - Thanks for pointing that out, I’ve seen some that were two and some three. I actually used the commented out bit of code to display the characters I wanted values for and then copied them by hand to a list for later use.