// File: /workspace/github_c_makefile_star2_O2/ggerganov[P]font-rasterizer/main.c
// Function: main
int main(int argc, const char * argv[]) {
    if (argc < 3) {
        printf("usage: %s font.ttf height [width]\n", argv[0]);
        printf("\n");
        printf("       rasterizes the provided ttf font into cells with specified height in pixels\n");
        printf("       if width is provided, it overrides the auto detected value\n");
        printf("       the generated raster data is printed on the standard output\n");
        return -1;
    }

    const char * fontFName = argv[1];
    int height = atoi(argv[2]);
    int width = argc > 3 ? atoi(argv[3]) : 0;

    unsigned char* fontBuffer;

    /* load font file */
    {
        long size;
        FILE* fontFile = fopen(argv[1], "rb");
        fseek(fontFile, 0, SEEK_END);
        size = ftell(fontFile);
        fseek(fontFile, 0, SEEK_SET);

        fontBuffer = malloc(size);

        fread(fontBuffer, size, 1, fontFile);
        fclose(fontFile);
    }

    /* prepare font */
    stbtt_fontinfo info;
    if (!stbtt_InitFont(&info, fontBuffer, 0)) {
        printf("failed to load ttf font\n");
        return -2;
    }

    int b_w = 0; /* bitmap width */
    int b_h = height; /* bitmap height */
    int c_w = width; /* char width */
    int c_h = height; /* char height */

    /* calculate font scaling */
    float scale = stbtt_ScaleForPixelHeight(&info, c_h);

    if (c_w == 0) {
        /* determine max char width */
        for (unsigned char ch = 0; ch < 128; ++ch) {
            int ax;
            int lsb;
            stbtt_GetCodepointHMetrics(&info, ch, &ax, &lsb);
            if (c_w < ax*scale) {
                c_w = ax*scale;
            }
        }
    }

    b_w = 128*c_w;

    /* create a bitmap for the phrase */
    unsigned char* bitmap = calloc(b_w*b_h, sizeof(unsigned char));

    int x = 0, maxx = 0;
    int ascent, descent, lineGap;
    stbtt_GetFontVMetrics(&info, &ascent, &descent, &lineGap);

    ascent *= scale;
    descent *= scale;

    /* begin C constants output */
    printf("static const int FONT_SIZE_X = %d;\n", c_w);
    printf("static const int FONT_SIZE_Y = %d;\n\n", c_h);
    printf("static const unsigned char kFontRaster[128][FONT_SIZE_Y*FONT_SIZE_X] = {\n");

    for (unsigned char ch = 0; ch < 128; ++ch) {
        /* how wide is this character */
        int ax;
        int lsb;
        stbtt_GetCodepointHMetrics(&info, ch, &ax, &lsb);

        /* 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, ch, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);

        /* compute y (different characters have different heights */
        int y = ascent + c_y1;
        if (y < 0) y = 0;

        /* render character (stride and offset is important here) */
        int byteOffset = x + (lsb*scale) + (y*b_w);
        stbtt_MakeCodepointBitmap(&info, bitmap + byteOffset, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, ch);

        /* output C array data */
        printf("    {");
        for (int yy = 0; yy < c_h; ++yy) {
            for (int xx = 0; xx < c_w; ++xx) {
                int v = bitmap[byteOffset + (yy - y)*b_w + xx];
                printf(" %3d,", v);
                if (v > 0 && xx > maxx) {
                    maxx = xx;
                }
            }
        }
        printf(" }, // U+%04d\n", ch);

        /* advance x */
        //x += ax*scale; // use true character width
        x += c_w; // force mono-space width

        /* add kerning */
        if (ch < 127) {
            int kern;
            kern = stbtt_GetCodepointKernAdvance(&info, ch, ch + 1);
            x += kern*scale;
        }
    }

    printf("};\n");

    if (maxx + 1 < c_w) {
        printf("// warning : max character width was %d. consider lowering the width parameter to this value\n", maxx + 1);
    }

    /* save out a 1 channel image */
    {
        char tmp[128];
        snprintf(tmp, 128, "raster-h%d.png", c_h);
        stbi_write_png(tmp, b_w, b_h, 1, bitmap, b_w);
    }

    free(fontBuffer);
    free(bitmap);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0x8f7a
movq (%rbx), %rsi
leaq 0x4ea9(%rip), %rdi      # 0xdde9
xorl %eax, %eax
callq 0x10a0
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x5783(%rip), %rdi      # 0xe6d9
callq 0x1050
leaq 0x57ca(%rip), %rdi      # 0xe72c
callq 0x1050
leaq 0x5800(%rip), %rdi      # 0xe76e
callq 0x1050
pushq $-0x1
jmp 0x909a
movl %edi, %r14d
movq 0x10(%rbx), %rdi
callq 0x11e0
movl %eax, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x3, %r14d
je 0x8f9a
movq 0x18(%rbx), %rdi
callq 0x11e0
movl %eax, %r12d
movq 0x8(%rbx), %rdi
leaq 0x4e64(%rip), %rsi      # 0xde0c
callq 0x11c0
movq %rax, %rbx
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1190
movq %rbx, %rdi
callq 0x1140
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1190
movq %r14, %rdi
callq 0x1170
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x1060
movq %rbx, %rdi
callq 0x1080
leaq -0x130(%rbp), %rdi
movq %r15, -0x70(%rbp)
movq %r15, %rsi
xorl %edx, %edx
callq 0x6ff3
testl %eax, %eax
je 0x908c
movl -0x30(%rbp), %r13d
cvtsi2ss %r13d, %xmm0
leaq -0x130(%rbp), %rdi
callq 0x4b6c
movl %r12d, %eax
testl %r12d, %r12d
movss %xmm0, -0x2c(%rbp)
jne 0x90a4
xorl %ebx, %ebx
leaq -0x130(%rbp), %r14
leaq -0x1b0(%rbp), %r15
leaq -0x3c(%rbp), %r12
xorl %eax, %eax
cmpl $0x80, %ebx
je 0x90a0
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
movq %r12, %rcx
movl %eax, %r13d
callq 0x4950
movss -0x2c(%rbp), %xmm0
cvtsi2ss %r13d, %xmm1
cvtsi2ssl -0x1b0(%rbp), %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x9084
movl %r13d, %eax
jmp 0x9088
cvttss2si %xmm2, %eax
incl %ebx
jmp 0x9049
leaq 0x55e2(%rip), %rdi      # 0xe675
callq 0x1050
pushq $-0x2
popq %rbx
jmp 0x9370
movl -0x30(%rbp), %r13d
movl %eax, %r12d
shll $0x7, %r12d
movl %eax, %r15d
movl %r12d, %eax
imull %r13d, %eax
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x1130
movq %rax, -0x48(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x3c(%rbp), %rbx
leaq -0x90(%rbp), %r14
leaq -0x8c(%rbp), %rcx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4982
cvtsi2ssl (%rbx), %xmm0
movss -0x2c(%rbp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, (%rbx)
cvtsi2ssl (%r14), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, (%r14)
leaq 0x4cfd(%rip), %rdi      # 0xde0f
xorl %ebx, %ebx
movl %r15d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x4d0e(%rip), %rdi      # 0xde33
movl %r13d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x5557(%rip), %rdi      # 0xe68d
callq 0x1050
testl %r15d, %r15d
movl %r13d, %eax
movl $0x0, %r13d
movl %r15d, -0x38(%rbp)
cmovgl %r15d, %r13d
movl %r12d, -0x34(%rbp)
movslq %r12d, %rcx
movq %rcx, -0x68(%rbp)
testl %eax, %eax
cmovlel %ebx, %eax
movq %rax, -0x80(%rbp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpl $0x80, %r12d
je 0x92fb
leaq -0x130(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
leaq -0x1b0(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x4950
movq %r14, %rdi
movl %r12d, %esi
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
leaq -0x5c(%rbp), %rdx
leaq -0x58(%rbp), %rcx
leaq -0x54(%rbp), %r8
leaq -0x50(%rbp), %r9
callq 0x4d64
movl -0x58(%rbp), %eax
movl -0x3c(%rbp), %r14d
addl %eax, %r14d
movl %r15d, -0x4c(%rbp)
cvtsi2ss %r15d, %xmm0
testl %r14d, %r14d
cvtsi2ssl -0x60(%rbp), %xmm1
movl $0x0, %ecx
cmovlel %ecx, %r14d
movss -0x2c(%rbp), %xmm3
mulss %xmm3, %xmm1
movl %r14d, %ecx
imull -0x34(%rbp), %ecx
cvtsi2ss %ecx, %xmm2
addss %xmm0, %xmm1
addss %xmm1, %xmm2
cvttss2si %xmm2, %ecx
movq %r12, %r9
movslq %ecx, %r12
movq -0x48(%rbp), %rcx
leaq (%rcx,%r12), %rsi
movl -0x54(%rbp), %edx
subl -0x5c(%rbp), %edx
movl -0x50(%rbp), %ecx
subl %eax, %ecx
leaq -0x130(%rbp), %rdi
movq -0x68(%rbp), %r15
movl %r15d, %r8d
movaps %xmm3, %xmm0
movaps %xmm3, %xmm1
movq %r9, -0x78(%rbp)
callq 0x61a9
leaq 0x4c1f(%rip), %rdi      # 0xde58
xorl %eax, %eax
callq 0x10a0
imulq %r15, %r14
subq %r14, %r12
addq -0x48(%rbp), %r12
xorl %eax, %eax
cmpq -0x80(%rbp), %rax
je 0x929c
movq %rax, -0x88(%rbp)
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x928c
movzbl (%r12,%r15), %r14d
leaq 0x4bf0(%rip), %rdi      # 0xde5e
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
cmpl %ebx, %r15d
movl %ebx, %eax
cmovgl %r15d, %eax
testl %r14d, %r14d
cmovnel %eax, %ebx
incq %r15
jmp 0x925d
movq -0x88(%rbp), %rax
incq %rax
addq -0x68(%rbp), %r12
jmp 0x924d
leaq 0x4bc1(%rip), %rdi      # 0xde64
movq -0x78(%rbp), %r12
movl %r12d, %esi
xorl %eax, %eax
callq 0x10a0
movl -0x4c(%rbp), %r15d
addl -0x38(%rbp), %r15d
movl $0x80, %r14d
cmpl $0x7f, %r12d
je 0x92f3
leal 0x1(%r12), %r14d
leaq -0x130(%rbp), %rdi
movl %r12d, %esi
movl %r14d, %edx
callq 0x48fa
cvtsi2ss %eax, %xmm0
cvtsi2ss %r15d, %xmm1
mulss -0x2c(%rbp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %r15d
movl %r14d, %r12d
jmp 0x916b
leaq 0x53d4(%rip), %rdi      # 0xe6d6
callq 0x1050
incl %ebx
cmpl -0x38(%rbp), %ebx
jge 0x931e
leaq 0x4b5e(%rip), %rdi      # 0xde73
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x4bac(%rip), %rdx      # 0xded1
xorl %ebx, %ebx
leaq -0x1b0(%rbp), %r14
movl $0x80, %esi
movq %r14, %rdi
movl -0x30(%rbp), %r15d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rcx
movq %r14, %rdi
movl -0x34(%rbp), %esi
movl %r15d, %edx
movq -0x48(%rbp), %r14
movq %r14, %r8
movl %esi, %r9d
callq 0x2cb9
movq -0x70(%rbp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movl %ebx, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq