// File: Alcaro[P]bridges/font.cpp
// Function: font::render_wrap(image&, int, int, unsigned int, cstring) const
void font::render_wrap(image& img, int32_t x, int32_t y, uint32_t width, cstring text) const
{
	size_t linestart = 0;
	size_t numspaces = 0;
	
	size_t lastspace = 0; // index to the string
	uint32_t widthlastspace = 0; // text width (pixels) from linestart to lastspace
	
	uint32_t linewidth = 0; // text width (pixels) from linestart to i
	
	for (size_t i=0;i<=text.length();i++)
	{
		if (i == text.length() || text[i] == '\n' || linewidth >= width)
		{
			float spacesize = 0;
			if (linewidth >= width)
			{
				i = lastspace;
				linewidth = widthlastspace;
				spacesize = ((width - linewidth) / (float)(numspaces-1));
			}
			
			this->render(img, x, y, text.substr(linestart, i), spacesize, true);
			
			y += this->height * this->scale;
			linestart = i+1;
			linewidth = 0;
			numspaces = 0;
			continue;
		}
		
		if (text[i] == ' ')
		{
			lastspace = i;
			widthlastspace = linewidth;
			numspaces++;
		}
		linewidth += this->width[(uint8_t)text[i]] * this->scale;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
leaq 0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq %rdi, -0x48(%rbp)
movq %rsi, -0x50(%rbp)
movl %edx, -0x54(%rbp)
movl %ecx, -0x58(%rbp)
movl %r8d, -0x5c(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0xb0(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x47ccf
movq -0xb8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x24(%rbp)
jmp 0x47cdc
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0xc0(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpq %rcx, %rax
ja 0x47f7e
movq -0xb0(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, -0xd0(%rbp)
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x47d4a
movq -0xc8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x47d57
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0xd0(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpq %rcx, %rax
je 0x47d8f
movq -0xb0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
callq 0xa1c0
movsbl %al, %eax
cmpl $0xa, %eax
je 0x47d8f
movl -0x80(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jb 0x47ef6
xorps %xmm0, %xmm0
movss %xmm0, -0x8c(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jb 0x47e30
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x5c(%rbp), %eax
movl -0x80(%rbp), %ecx
subl %ecx, %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, -0xdc(%rbp)
movq -0x70(%rbp), %rax
decq %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0xd8(%rbp)
testq %rax, %rax
movss %xmm0, -0xd4(%rbp)
js 0x47e14
movss -0xd8(%rbp), %xmm0
movss %xmm0, -0xd4(%rbp)
movss -0xdc(%rbp), %xmm0
movss -0xd4(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x8c(%rbp)
movq -0xb0(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0xe4(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %esi
movq -0x88(%rbp), %rax
movl %eax, %edx
callq 0x9f90
movq -0xf0(%rbp), %rsi
movl -0xe4(%rbp), %ecx
movq -0xa8(%rbp), %rdi
movq %rax, %r8
movq %rdx, %rax
movl -0xe0(%rbp), %edx
movq %r8, -0xa0(%rbp)
movq %rax, -0x98(%rbp)
movss -0x8c(%rbp), %xmm0
movq -0xa0(%rbp), %r8
movq -0x98(%rbp), %r9
movl $0x1, (%rsp)
callq 0x473f0
movq -0xa8(%rbp), %rcx
movzbl 0x480(%rcx), %eax
movzbl 0x488(%rcx), %ecx
imull %ecx, %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
jmp 0x47f67
movq -0xb0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
callq 0xa1c0
movsbl %al, %eax
cmpl $0x20, %eax
jne 0x47f30
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0xb0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
callq 0xa1c0
movq -0xa8(%rbp), %rcx
movzbl %al, %eax
movzbl 0x400(%rcx,%rax), %eax
movzbl 0x488(%rcx), %ecx
imull %ecx, %eax
addl -0x80(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x47c74
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)