// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movss %xmm0, 0x2c(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movq %r9, %rax
shrq $0x38, %rax
xorl $0xf, %eax
testq %r9, %r9
movl %r9d, %r12d
cmovnsl %eax, %r12d
testl %r12d, %r12d
je 0x1dc91
movl %ecx, %ebx
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movb 0x90(%rsp), %r13b
movl $0xffffff, %eax         # imm = 0xFFFFFF
andl 0x484(%rdi), %eax
xorl %ecx, %ecx
cmpl $0x7, 0x8(%rsi)
sete %cl
shll $0x18, %ecx
testq %r9, %r9
leaq 0x48(%rsp), %r11
cmovsq %r8, %r11
leal (%rcx,%rax), %esi
addl $0xff000000, %esi       # imm = 0xFF000000
xorps %xmm2, %xmm2
xorl %r8d, %r8d
xorl %r10d, %r10d
movl %edx, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
movq %r12, 0x40(%rsp)
movq %r11, 0x10(%rsp)
movslq %r10d, %rax
movzbl (%r11,%rax), %r9d
cvttss2si %xmm2, %ecx
testb %r13b, %r13b
je 0x1dadf
movzbl 0x488(%r15), %edi
movl %ecx, %eax
cltd
idivl %edi
subl %edx, %ecx
cmpl $0xa, %r9d
jne 0x1db02
movzbl 0x480(%r15), %eax
movzbl 0x488(%r15), %ecx
imull %eax, %ecx
addl %ecx, %ebx
xorps %xmm2, %xmm2
jmp 0x1dc83
movq %r10, 0x18(%rsp)
movq %r8, 0x20(%rsp)
addl %ebp, %ecx
cmpb $0x20, %r9b
movq %r9, 0x38(%rsp)
jae 0x1db53
movss %xmm2, 0xc(%rsp)
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 0x1dc1f
andl $0xffffff, %esi         # imm = 0xFFFFFF
jmp 0x1dc25
movzbl 0x488(%r15), %ebp
movl %ebx, 0xc(%rsp)
movl %ebx, %edx
movl %ecx, %r13d
xorl %edi, %edi
testq %rbp, %rbp
je 0x1dbe7
movq %rdi, %r8
imulq %rbp, %r8
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
leaq (%rax,%rcx,8), %r9
addq %rdi, %r9
xorl %r10d, %r10d
leal (%r10,%rdx), %r11d
addl %r8d, %r11d
cmpl %r11d, 0x4(%r14)
jbe 0x1dbdf
imull 0xc(%r14), %r11d
andl $-0x4, %r11d
addq 0x10(%r14), %r11
movq %r13, %r15
xorl %r12d, %r12d
movzbl (%r9), %eax
btl %r12d, %eax
jae 0x1dbd3
movq %r15, %rbx
movq %rbp, %rax
testl %ebx, %ebx
js 0x1dbcb
cmpl %ebx, (%r14)
jbe 0x1dbcb
movl %ebx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %esi, (%r11,%rcx,4)
incq %rbx
decq %rax
jne 0x1dbb6
incq %r12
addq %rbp, %r15
cmpq $0x8, %r12
jne 0x1dba6
incq %r10
cmpq %rbp, %r10
jne 0x1db86
incq %rdi
cmpq $0x8, %rdi
jne 0x1db66
movl 0xc(%rsp), %ebx
movl 0x28(%rsp), %ebp
movq 0x30(%rsp), %r15
movq 0x40(%rsp), %r12
movb 0x90(%rsp), %r13b
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r10
jmp 0x1dc3a
orl $0xff000000, %esi       # imm = 0xFF000000
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r10
movss 0xc(%rsp), %xmm2
movq 0x38(%rsp), %rdx
movzbl 0x400(%r15,%rdx), %ecx
movzbl 0x488(%r15), %eax
imull %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
addss %xmm0, %xmm2
cmpl $0x20, %edx
jne 0x1dc69
addss 0x2c(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
cvttss2si %xmm1, %rax
cmpl %r8d, %eax
cmoval %eax, %r8d
incq %r10
cmpq %r12, %r10
jne 0x1dabf
jmp 0x1dc94
xorl %r8d, %r8d
movl %r8d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq