// File: /workspace/github_cpp_makefile_star2_O1/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 %r9, %rbx
movl %ecx, %ebp
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movzbl 0x57(%rsp), %eax
movl %eax, %ecx
xorl $0xf, %ecx
testb %al, %al
cmovnsl %ecx, %ebx
testl %ebx, %ebx
je 0x1e409
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %r12
movb 0x90(%rsp), %r13b
movl $0xffffff, %ecx         # imm = 0xFFFFFF
andl 0x484(%rdi), %ecx
xorl %edx, %edx
cmpl $0x7, 0x8(%rsi)
sete %dl
shll $0x18, %edx
testb %al, %al
leaq 0x48(%rsp), %r11
cmovsq %r8, %r11
leal (%rdx,%rcx), %esi
addl $0xff000000, %esi       # imm = 0xFF000000
xorps %xmm2, %xmm2
xorl %r8d, %r8d
xorl %r10d, %r10d
movq %rbx, 0x40(%rsp)
movl %r14d, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
movq %r11, 0x10(%rsp)
movslq %r10d, %rax
movzbl (%r11,%rax), %r9d
cvttss2si %xmm2, %ecx
testb %r13b, %r13b
je 0x1e24e
movzbl 0x488(%r12), %edi
movl %ecx, %eax
cltd
idivl %edi
subl %edx, %ecx
cmpl $0xa, %r9d
jne 0x1e273
movzbl 0x480(%r12), %eax
movzbl 0x488(%r12), %ecx
imull %eax, %ecx
addl %ecx, %ebp
xorps %xmm2, %xmm2
jmp 0x1e3fb
movq %r10, 0x18(%rsp)
movq %r8, 0x20(%rsp)
addl %r14d, %ecx
cmpb $0x20, %r9b
movq %r9, 0x38(%rsp)
jae 0x1e2c8
movss %xmm2, 0xc(%rsp)
movq 0x490(%r12), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %ebp, %r8d
callq *0x498(%r12)
cmpl $0x7, 0x8(%r15)
movl 0x484(%r12), %esi
jne 0x1e396
andl $0xffffff, %esi         # imm = 0xFFFFFF
jmp 0x1e39c
movzbl 0x488(%r12), %ebx
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
movl %ecx, %r14d
xorl %edi, %edi
testq %rbx, %rbx
je 0x1e35d
movq %rdi, %r8
imulq %rbx, %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(%r15)
jbe 0x1e355
imull 0xc(%r15), %r11d
andl $-0x4, %r11d
addq 0x10(%r15), %r11
movq %r14, %r12
xorl %r13d, %r13d
movzbl (%r9), %eax
btl %r13d, %eax
jae 0x1e349
movq %r12, %rbp
movq %rbx, %rax
testl %ebp, %ebp
js 0x1e341
cmpl %ebp, (%r15)
jbe 0x1e341
movl %ebp, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %esi, (%r11,%rcx,4)
incq %rbp
decq %rax
jne 0x1e32c
incq %r13
addq %rbx, %r12
cmpq $0x8, %r13
jne 0x1e31c
incq %r10
cmpq %rbx, %r10
jne 0x1e2fc
incq %rdi
cmpq $0x8, %rdi
jne 0x1e2dc
movq 0x40(%rsp), %rbx
movl 0xc(%rsp), %ebp
movl 0x28(%rsp), %r14d
movq 0x30(%rsp), %r12
movb 0x90(%rsp), %r13b
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r10
jmp 0x1e3b1
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(%r12,%rdx), %ecx
movzbl 0x488(%r12), %eax
imull %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
addss %xmm0, %xmm2
cmpl $0x20, %edx
jne 0x1e3e1
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 %rbx, %r10
jne 0x1e22d
jmp 0x1e40c
xorl %r8d, %r8d
movl %r8d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq