// File: stetre[P]moonfonts/src/fonts.c
// Function: Load
static int Load(lua_State *L)
    {
    int width, height;
    unsigned int i, codepoint;
    unsigned char **pixels ; /* [height][width] */
    stb_fontchar *fontchar; /* [num_chars] */ 
    const char *name = luaL_checkstring(L, 1);
    int height_pow2 = optboolean(L, 2, 0);

    font_t *font = font_search(L, name);
    if(!font)
        return 0;

    width = font->width;
    height = height_pow2 ? font->height_pow2 : font->height;
    pixels = (unsigned char**)Malloc(L, sizeof(unsigned char) * width * height);
    fontchar = (stb_fontchar*)Malloc(L, sizeof(stb_fontchar) * font->num_chars);
    font->create(fontchar, (unsigned char (*)[])(pixels), height);

    lua_newtable(L);

    lua_pushstring(L, font->name);
    lua_setfield(L, -2, "name");

    lua_pushlstring(L, (const char*)pixels, sizeof(unsigned char) * width * height);
    lua_setfield(L, -2, "pixels");

    lua_pushinteger(L, width);
    lua_setfield(L, -2, "width");
    lua_pushinteger(L, height);
    lua_setfield(L, -2, "height");
    lua_pushinteger(L, font->line_spacing);
    lua_setfield(L, -2, "line_spacing");
    lua_pushinteger(L, font->num_chars);
    lua_setfield(L, -2, "num_chars");
/*
    lua_pushinteger(L, font->first_char);
    lua_setfield(L, -2, "first_char");
*/

    lua_newtable(L); /* chars data */

    for(i=0; i < font->num_chars; i++)
        {
        codepoint = i + font->first_char;
        pushfontchar(L, &fontchar[i], codepoint);
        lua_seti(L, -2, codepoint);
        }
    lua_setfield(L, -2, "fontchar");

    Free(L, (char*)pixels);
    Free(L, (char*)fontchar);
    return 1;
    }

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x10e0
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x11e0
movl %eax, 0x1c(%rsp)
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x131f0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x12f9f
movl $0x0, 0x54(%rsp)
jmp 0x131e6
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x12fc0
movq 0x10(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x12fcc
movq 0x10(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x48(%rsp), %rdi
movslq 0x44(%rsp), %rsi
shlq $0x0, %rsi
movslq 0x40(%rsp), %rax
imulq %rax, %rsi
callq 0x1070
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rax
movl 0x1c(%rax), %eax
movl %eax, %esi
shlq $0x6, %rsi
callq 0x1070
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x40(%rsp), %edx
callq *%rax
movq 0x48(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x1090
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
callq 0x11d0
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x19ea2e(%rip), %rdx    # 0x1b1a8a
callq 0x10a0
movq 0x48(%rsp), %rdi
movq 0x30(%rsp), %rsi
movslq 0x44(%rsp), %rdx
shlq $0x0, %rdx
movslq 0x40(%rsp), %rax
imulq %rax, %rdx
callq 0x1160
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x198f99(%rip), %rdx    # 0x1ac02c
callq 0x10a0
movq 0x48(%rsp), %rdi
movslq 0x44(%rsp), %rsi
callq 0x10c0
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x198f7b(%rip), %rdx    # 0x1ac033
callq 0x10a0
movq 0x48(%rsp), %rdi
movslq 0x40(%rsp), %rsi
callq 0x10c0
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x198f5c(%rip), %rdx    # 0x1ac039
callq 0x10a0
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, %esi
callq 0x10c0
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x198f39(%rip), %rdx    # 0x1ac040
callq 0x10a0
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rax
movl 0x1c(%rax), %eax
movl %eax, %esi
callq 0x10c0
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x198f1c(%rip), %rdx    # 0x1ac04d
callq 0x10a0
movq 0x48(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x1090
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0x131aa
movl 0x3c(%rsp), %eax
movq 0x10(%rsp), %rcx
addl 0x18(%rcx), %eax
movl %eax, 0x38(%rsp)
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x3c(%rsp), %eax
shlq $0x6, %rax
addq %rax, %rsi
movl 0x38(%rsp), %edx
callq 0x13320
movq 0x48(%rsp), %rdi
movl 0x38(%rsp), %eax
movl %eax, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x10f0
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1314c
movq 0x48(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x198e9c(%rip), %rdx    # 0x1ac057
callq 0x10a0
movq 0x48(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x1190
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1190
movl $0x1, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nop