// File: /workspace/github_cpp_makefile_star2_O2/Alcaro[P]bridges/font.cpp
// Function: font::render(image&, int, int, cstring, float, bool) const
uint32_t font::render(image& img, int32_t x, int32_t y, cstring text, float xspace, bool align) const
{
	uint32_t color;
	if (img.fmt == ifmt_0rgb8888) color = this->color & 0x00FFFFFF;
	else color = this->color | 0xFF000000;
	
	uint32_t maxwidth = 0;
	
	float xoff = 0;
	for (size_t i=0;i<text.length();i++)
	{
		uint8_t ch = text[i];
		int32_t xstart = x + (align ? ((int)xoff/this->scale)*this->scale : (int)xoff);
		
		if (ch == '\n')
		{
			y += this->height * this->scale;
			xoff = 0;
			continue;
		}
		else if (ch <= 0x1F)
		{
			this->fallback(img, *this, xstart, y, ch);
			//refresh the color, in case the fallback changed it
			if (img.fmt == ifmt_0rgb8888) color = this->color & 0x00FFFFFF;
			else color = this->color | 0xFF000000;
		}
		else
		{
			for (size_t yp=0;yp<8;yp++)
			{
				for (size_t yps=0;yps<this->scale;yps++)
				{
					uint32_t yt = y + yp*this->scale + yps;
					if (yt < 0 || yt >= img.height) continue;
					
					uint32_t* targetpx = img.pixels32 + yt*img.stride/sizeof(uint32_t);
					
					for (size_t xp=0;xp<8;xp++)
					{
						if (this->characters[ch][yp] & (1<<xp))
						{
							for (size_t xps=0;xps<this->scale;xps++)
							{
								int32_t xt = xstart + xp*this->scale + xps;
								if (xt < 0 || (uint32_t)xt >= img.width) continue;
								
								targetpx[xt] = color;
							}
						}
					}
				}
			}
		}
		
		xoff += this->width[ch] * this->scale;
		if (ch == ' ') xoff += xspace;
		
		//-scale to remove the inter-letter spacing
		maxwidth = max(maxwidth, xoff-this->scale);
	}
	
	return maxwidth;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movss %xmm0, 0x24(%rsp)
movl %ecx, %ebx
movl %edx, %ebp
movq %rsi, %r14
movb 0x80(%rsp), %r12b
leaq 0x38(%rsp), %r13
movq %r8, (%r13)
movq %r9, 0x8(%r13)
xorl %eax, %eax
cmpl $0x7, 0x8(%rsi)
sete %al
movl $0xffffff, %ecx         # imm = 0xFFFFFF
movq %rdi, 0x8(%rsp)
andl 0x484(%rdi), %ecx
shll $0x18, %eax
leal (%rax,%rcx), %esi
addl $0xff000000, %esi       # imm = 0xFF000000
movq %r9, %rax
shrq $0x38, %rax
xorl $0xf, %eax
testq %r9, %r9
cmovsq %r9, %rax
cmovsq %r8, %r13
movl %eax, %r8d
xorl %r9d, %r9d
xorps %xmm2, %xmm2
xorl %r10d, %r10d
movl %edx, 0x20(%rsp)
movq %r13, 0x30(%rsp)
movq %r8, 0x10(%rsp)
cmpq %r8, %r9
je 0x19044
movslq %r9d, %rax
movzbl (%r13,%rax), %r11d
cvttss2si %xmm2, %ecx
testb %r12b, %r12b
je 0x18e8c
movq 0x8(%rsp), %rax
movzbl 0x488(%rax), %edi
movl %ecx, %eax
cltd
idivl %edi
subl %edx, %ecx
cmpl $0xa, %r11d
jne 0x18eb2
movq 0x8(%rsp), %rcx
movzbl 0x480(%rcx), %eax
movzbl 0x488(%rcx), %ecx
imull %eax, %ecx
addl %ecx, %ebx
xorps %xmm2, %xmm2
jmp 0x1903c
movl %r10d, 0x4(%rsp)
movq %r9, 0x18(%rsp)
addl %ebp, %ecx
cmpb $0x20, %r11b
movq %r11, 0x28(%rsp)
jae 0x18f0a
movq %r11, %r9
movss %xmm2, (%rsp)
movq 0x8(%rsp), %r15
movq 0x490(%r15), %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %ebx, %r8d
callq *0x498(%r15)
cmpl $0x7, 0x8(%r14)
movl 0x484(%r15), %esi
jne 0x18fdb
andl $0xffffff, %esi         # imm = 0xFFFFFF
jmp 0x18fe1
movq 0x8(%rsp), %rax
movzbl 0x488(%rax), %r15d
movl %ebx, (%rsp)
movl %ebx, %edx
movl %ecx, %r13d
xorl %edi, %edi
cmpq $0x8, %rdi
je 0x18fb6
movq %rdi, %r8
imulq %r15, %r8
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rcx,8), %r9
addq %rdi, %r9
xorl %r10d, %r10d
cmpq %r15, %r10
je 0x18fae
leal (%r10,%rdx), %r11d
addl %r8d, %r11d
cmpl %r11d, 0x4(%r14)
jbe 0x18fa9
imull 0xc(%r14), %r11d
andl $-0x4, %r11d
addq 0x10(%r14), %r11
movq %r13, %rbp
xorl %r12d, %r12d
cmpq $0x8, %r12
je 0x18fa9
movzbl (%r9), %eax
btl %r12d, %eax
jae 0x18fa1
movq %rbp, %rbx
movq %r15, %rax
subq $0x1, %rax
jb 0x18fa1
testl %ebx, %ebx
js 0x18f9c
cmpl %ebx, (%r14)
jbe 0x18f9c
movl %ebx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %esi, (%r11,%rcx,4)
incq %rbx
jmp 0x18f81
incq %r12
addq %r15, %rbp
jmp 0x18f6b
incq %r10
jmp 0x18f46
incq %rdi
jmp 0x18f21
movl (%rsp), %ebx
movl 0x20(%rsp), %ebp
movb 0x80(%rsp), %r12b
movq 0x30(%rsp), %r13
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
movl 0x4(%rsp), %r10d
jmp 0x18ff5
orl $0xff000000, %esi       # imm = 0xFF000000
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
movl 0x4(%rsp), %r10d
movss (%rsp), %xmm2
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rdx
movzbl 0x400(%rax,%rdx), %ecx
movzbl 0x488(%rax), %eax
imull %eax, %ecx
cvtsi2ss %ecx, %xmm0
addss %xmm0, %xmm2
cmpq $0x20, %rdx
jne 0x19025
addss 0x24(%rsp), %xmm2
cvtsi2ss %eax, %xmm0
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
cvttss2si %xmm1, %rax
cmpl %r10d, %eax
cmoval %eax, %r10d
incq %r9
jmp 0x18e5e
movl %r10d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq