// File: Alcaro[P]bridges/font.cpp
// Function: font::render_justified(image&, int, int, unsigned int, unsigned int, cstring, bool) const
void font::render_justified(image& img, int32_t x, int32_t y, uint32_t width1, uint32_t width2, cstring text, bool align) const
{
	size_t linestart = 0;
	size_t numspaces = 0;
	
	uint32_t linewidth = 0;
	for (size_t i=0;i<=text.length();i++)
	{
		if (i == text.length() || text[i] == '\n')
		{
			linewidth *= this->scale;
			
			float spacesize;
			if (linewidth >= width1 && linewidth <= width2) spacesize = ((width2 - linewidth) / (float)numspaces);
			else spacesize = 0;
			
			this->render(img, x, y, text.substr(linestart, i), spacesize, align);
			
			y += this->height * this->scale;
			linestart = i+1;
			linewidth = 0;
			numspaces = 0;
			continue;
		}
		
		if (text[i] == ' ') numspaces++;
		linewidth += this->width[(uint8_t)text[i]];
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movb 0x20(%rbp), %al
leaq 0x10(%rbp), %r10
movq %r10, -0xb0(%rbp)
movq %rdi, -0x48(%rbp)
movq %rsi, -0x50(%rbp)
movl %edx, -0x54(%rbp)
movl %ecx, -0x58(%rbp)
movl %r8d, -0x5c(%rbp)
movl %r9d, -0x60(%rbp)
andb $0x1, %al
movb %al, -0x61(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%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 0x4795c
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 0x47969
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0xc0(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpq %rcx, %rax
ja 0x47bf9
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 0x479d7
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 0x479e4
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0xd0(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpq %rcx, %rax
je 0x47a14
movq -0xb0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
callq 0xa1c0
movsbl %al, %eax
cmpl $0xa, %eax
jne 0x47b8a
movq -0xa8(%rbp), %rax
movzbl 0x488(%rax), %eax
imull -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jb 0x47ab5
movl -0x7c(%rbp), %eax
cmpl -0x60(%rbp), %eax
ja 0x47ab5
movl -0x60(%rbp), %eax
movl -0x7c(%rbp), %ecx
subl %ecx, %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, -0xdc(%rbp)
movq -0x78(%rbp), %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 0x47a97
movss -0xd8(%rbp), %xmm0
movss %xmm0, -0xd4(%rbp)
movss -0xdc(%rbp), %xmm0
movss -0xd4(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x8c(%rbp)
jmp 0x47ac0
xorps %xmm0, %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 -0x70(%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
movb -0x61(%rbp), %al
movq -0xa0(%rbp), %r8
movq -0x98(%rbp), %r9
andb $0x1, %al
movzbl %al, %eax
movl %eax, (%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, -0x70(%rbp)
movl $0x0, -0x7c(%rbp)
movq $0x0, -0x78(%rbp)
jmp 0x47be2
movq -0xb0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
callq 0xa1c0
movsbl %al, %eax
cmpl $0x20, %eax
jne 0x47bb3
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0xb0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
callq 0xa1c0
movb %al, %cl
movq -0xa8(%rbp), %rax
movzbl %cl, %ecx
movzbl 0x400(%rax,%rcx), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x47901
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)