// File: /workspace/github_c_makefile_star2_O2/justinmeiners[P]stb-truetype-example/main.c
// Function: main
int main(int argc, const char * argv[])
{
    /* load font file */
    long size;
    unsigned char* fontBuffer;
    
    FILE* fontFile = fopen("font/cmunrm.ttf", "rb");
    fseek(fontFile, 0, SEEK_END);
    size = ftell(fontFile); /* how long is the file ? */
    fseek(fontFile, 0, SEEK_SET); /* reset */
    
    fontBuffer = malloc(size);
    
    fread(fontBuffer, size, 1, fontFile);
    fclose(fontFile);

    /* prepare font */
    stbtt_fontinfo info;
    if (!stbtt_InitFont(&info, fontBuffer, 0))
    {
        printf("failed\n");
    }
    
    int b_w = 512; /* bitmap width */
    int b_h = 128; /* bitmap height */
    int l_h = 64; /* line height */

    /* create a bitmap for the phrase */
    unsigned char* bitmap = calloc(b_w * b_h, sizeof(unsigned char));
    
    /* calculate font scaling */
    float scale = stbtt_ScaleForPixelHeight(&info, l_h);

    char* word = "the quick brown fox";
    
    int x = 0;
       
    int ascent, descent, lineGap;
    stbtt_GetFontVMetrics(&info, &ascent, &descent, &lineGap);
    
    ascent = roundf(ascent * scale);
    descent = roundf(descent * scale);
    
    int i;
    for (i = 0; i < strlen(word); ++i)
    {
        /* how wide is this character */
        int ax;
	int lsb;
        stbtt_GetCodepointHMetrics(&info, word[i], &ax, &lsb);
        /* (Note that each Codepoint call has an alternative Glyph version which caches the work required to lookup the character word[i].) */

        /* get bounding box for character (may be offset to account for chars that dip above or below the line) */
        int c_x1, c_y1, c_x2, c_y2;
        stbtt_GetCodepointBitmapBox(&info, word[i], scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
        
        /* compute y (different characters have different heights) */
        int y = ascent + c_y1;
        
        /* render character (stride and offset is important here) */
        int byteOffset = x + roundf(lsb * scale) + (y * b_w);
        stbtt_MakeCodepointBitmap(&info, bitmap + byteOffset, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, word[i]);

        /* advance x */
        x += roundf(ax * scale);
        
        /* add kerning */
        int kern;
        kern = stbtt_GetCodepointKernAdvance(&info, word[i], word[i + 1]);
        x += roundf(kern * scale);
    }
    
    /* save out a 1 channel image */
    stbi_write_png("out.png", b_w, b_h, 1, bitmap, b_w);
	
    /*
     Note that this example writes each character directly into the target image buffer.
     The "right thing" to do for fonts that have overlapping characters is
     MakeCodepointBitmap to a temporary buffer and then alpha blend that onto the target image.
     See the stb_truetype.h header for more info.
    */
    
    free(fontBuffer);
    free(bitmap);
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
leaq 0x4ee4(%rip), %rdi      # 0xdde9
leaq 0x4eed(%rip), %rsi      # 0xddf9
callq 0x11a0
movq %rax, %r12
movq $0x0, -0x40(%rbp)
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1170
movq %r12, %rdi
callq 0x1120
movq %rax, %r15
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movq %r15, %rdi
callq 0x1150
movq %rax, %r13
pushq $0x1
popq %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x1050
movq %r12, %rdi
callq 0x1080
leaq -0x118(%rbp), %rdi
movq %r13, -0x70(%rbp)
movq %r13, %rsi
xorl %edx, %edx
callq 0x6fc3
testl %eax, %eax
jne 0x8f8d
leaq 0x5625(%rip), %rdi      # 0xe5ad
callq 0x1040
movl $0x10000, %edi          # imm = 0x10000
movq %r14, %rsi
callq 0x1110
movq %rax, -0x68(%rbp)
leaq -0x118(%rbp), %rbx
movss 0x4b5b(%rip), %xmm0     # 0xdb08
movq %rbx, %rdi
callq 0x4b3c
movss %xmm0, -0x2c(%rbp)
leaq -0x5c(%rbp), %r13
leaq -0x78(%rbp), %r15
leaq -0x74(%rbp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x4952
cvtsi2ssl (%r13), %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
cvttss2si %xmm0, %eax
movl %eax, (%r13)
cvtsi2ssl (%r15), %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
cvttss2si %xmm0, %eax
movl %eax, (%r15)
cmpq $0x14, %r14
je 0x9115
leaq 0x4de9(%rip), %rax      # 0xddfc
movsbl -0x1(%r14,%rax), %r13d
leaq -0x118(%rbp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
leaq -0x58(%rbp), %rdx
leaq -0x54(%rbp), %rcx
callq 0x4920
movq %rbx, %rdi
movl %r13d, %esi
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
leaq -0x50(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq -0x44(%rbp), %r9
callq 0x4d34
movl -0x4c(%rbp), %r15d
movl -0x5c(%rbp), %r12d
addl %r15d, %r12d
cvtsi2ssl -0x40(%rbp), %xmm0
movss %xmm0, -0x40(%rbp)
cvtsi2ssl -0x54(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
shll $0x9, %r12d
cvtsi2ss %r12d, %xmm1
addss -0x40(%rbp), %xmm0
addss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movslq %eax, %rsi
addq -0x68(%rbp), %rsi
movl -0x48(%rbp), %edx
subl -0x50(%rbp), %edx
movl -0x44(%rbp), %ecx
subl %r15d, %ecx
movq %rbx, %rdi
movl $0x200, %r8d            # imm = 0x200
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
movl %r13d, %r9d
callq 0x6179
cvtsi2ssl -0x58(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
addss -0x40(%rbp), %xmm0
movaps %xmm0, -0x40(%rbp)
leaq 0x4d21(%rip), %rax      # 0xddfc
movsbl (%r14,%rax), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x48ca
cvtsi2ss %eax, %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
cvttps2dq -0x40(%rbp), %xmm1
cvtdq2ps %xmm1, %xmm1
addss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movq %rax, -0x40(%rbp)
incq %r14
jmp 0x9002
leaq 0x4cf4(%rip), %rdi      # 0xde10
pushq $0x1
popq %rcx
movl $0x200, %esi            # imm = 0x200
movl $0x80, %edx
movq -0x68(%rbp), %rbx
movq %rbx, %r8
movl $0x200, %r9d            # imm = 0x200
callq 0x2c89
movq -0x70(%rbp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq