// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-font.c
// Function: de_font_paint_character_idx
void de_font_paint_character_idx(deark *c, de_bitmap *img,
	struct de_bitmap_font *font, i64 char_idx,
	i64 xpos, i64 ypos, de_color fgcol, de_color bgcol,
	unsigned int flags)
{
	struct de_bitmap_font_char *ch;

	if(char_idx<0 || char_idx>=font->num_chars) return;
	ch = &font->char_array[char_idx];
	if(!is_valid_char(ch)) return;
	if(ch->width > font->nominal_width) return;
	if(ch->height > font->nominal_height) return;

	// Paint a "canvas" for the char, if needed.

	// If the "extraspace" feature is used, paint an additional canvas of
	// a different color.
	if(!(flags&DE_PAINTFLAG_TRNSBKGD) && (ch->extraspace_l || ch->extraspace_r)) {
		i64 canvas_x, canvas_y;
		i64 canvas_w, canvas_h;

		canvas_x = xpos;
		canvas_y = ypos+ch->v_offset;
		canvas_w = (i64)ch->extraspace_l + (i64)ch->width + (i64)ch->extraspace_r;
		canvas_h = ch->height;
		// (We don't need to support both the "extraspace" and the VGA9COL
		// feature at the same time.)

		if(ch->width==0 || ch->height==0) {
			// Bit of a hack. If we have a zero-size (spacer-only) character,
			// paint the extraspace as the full height the cell.
			// (In principle, maybe it should be painted this way for *all*
			// characters, but I'm not sure how to make that look good.)
			canvas_y = ypos;
			canvas_h = font->nominal_height;
		}

		if(canvas_y + canvas_h > ypos + font->nominal_height) {
			goto after_extraspace;
		}

		de_bitmap_rect(img, canvas_x, canvas_y,
			canvas_w, canvas_h, DE_MAKE_RGB(192,255,192), 0);
	}

after_extraspace:

	if(ch->width<=0 || ch->height<=0) goto after_foreground; // Nothing to do

	// Paint the canvas for the main part of the character.
	if(!(flags&DE_PAINTFLAG_TRNSBKGD)) {
		i64 canvas_x, canvas_y;
		i64 canvas_w, canvas_h;

		canvas_x = xpos + ch->extraspace_l;
		canvas_y = ypos+ch->v_offset;
		canvas_w = ch->width;
		if((flags&DE_PAINTFLAG_VGA9COL) && ch->width==8) {
			canvas_w++;
		}
		canvas_h = ch->height;

		if(canvas_y + canvas_h > ypos + font->nominal_height) {
			goto after_foreground;
		}
		de_bitmap_rect(img, canvas_x, canvas_y,
			canvas_w, canvas_h, bgcol, 0);
	}

	// Paint the character foreground.
	paint_character_internal(c, img, ch,
		ch->extraspace_l + xpos, ch->v_offset + ypos, fgcol, flags);

after_foreground:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x10(%rsp)
testq %rcx, %rcx
js 0xe9e2c
movq %rdx, %r13
cmpq %rcx, 0x18(%rdx)
jle 0xe9e2c
movq %r9, %r15
movq %r8, %r12
imulq $0x28, %rcx, %r14
addq 0x20(%r13), %r14
movq %r14, %rdi
callq 0xe9fdb
testl %eax, %eax
je 0xe9e2c
movl 0x8(%r14), %eax
cmpl (%r13), %eax
jg 0xe9e2c
movl 0xc(%r14), %r8d
movl 0x4(%r13), %edi
cmpl %edi, %r8d
jg 0xe9e2c
movl 0x90(%rsp), %ebp
testb $0x1, %bpl
jne 0xe9e02
movswq 0x14(%r14), %rcx
movzwl 0x16(%r14), %esi
movl %ecx, %edx
orw %si, %dx
je 0xe9e02
movslq %edi, %r9
testl %eax, %eax
je 0xe9dc9
testl %r8d, %r8d
je 0xe9dc9
movslq 0x10(%r14), %rdx
addq %r15, %rdx
movl %r8d, %edi
jmp 0xe9dcc
movq %r15, %rdx
movslq %edi, %r8
leaq (%rdx,%r8), %rdi
addq %r15, %r9
cmpq %r9, %rdi
jg 0xe9e02
cltq
movswq %si, %rsi
addq %rax, %rcx
addq %rsi, %rcx
andl $0x0, (%rsp)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movl $0xffc0ffc0, %r9d       # imm = 0xFFC0FFC0
callq 0xe8292
movl 0x8(%r14), %eax
testl %eax, %eax
jle 0xe9e2c
movslq 0xc(%r14), %r8
testq %r8, %r8
jle 0xe9e2c
testb $0x1, %bpl
jne 0xe9e3b
movslq 0x10(%r14), %rdx
addq %r15, %rdx
leaq (%rdx,%r8), %rcx
movslq 0x4(%r13), %rsi
addq %r15, %rsi
cmpq %rsi, %rcx
jle 0xe9e42
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %ebx
andl $0x2, %ebx
jmp 0xe9e7e
movl %ebp, %ebx
andl $0x2, %ebx
movswq 0x14(%r14), %rsi
addq %r12, %rsi
cmpl $0x8, %eax
movl %eax, %eax
movq %rax, %rcx
pushq $0x9
popq %rdi
cmoveq %rdi, %rcx
movl 0x88(%rsp), %r9d
testl %ebx, %ebx
cmoveq %rax, %rcx
andl $0x0, (%rsp)
movq 0x10(%rsp), %rdi
callq 0xe8292
movl 0x8(%r14), %eax
movswq 0x14(%r14), %rcx
addq %r12, %rcx
movq %rcx, 0x30(%rsp)
movslq 0x10(%r14), %rcx
addq %r15, %rcx
movq %rcx, 0x20(%rsp)
cltq
testl %ebx, %ebx
setne %cl
cmpl $0x8, %eax
sete %r13b
andb %cl, %r13b
pushq $0x9
popq %rcx
cmoveq %rax, %rcx
xorl %esi, %esi
testq %rcx, %rcx
movl $0x0, %ebx
movq %rcx, 0x40(%rsp)
cmovgq %rcx, %rbx
xorb $0x1, %r13b
movl $0xffffff50, %edi       # imm = 0xFFFFFF50
pushq $0x7
popq %r8
movslq 0xc(%r14), %rax
cmpq %rax, %rsi
jge 0xe9e2c
testb $0x10, %bpl
jne 0xe9efa
movq %rsi, 0x18(%rsp)
testb $0x20, %bpl
je 0xe9f05
addq %rsi, %rax
cqto
pushq $0x2
popq %rcx
idivq %rcx
jmp 0xe9f00
movq %rsi, %rax
shrq %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq %rsi, 0x28(%rsp)
addq %rsi, %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0xe9fce
movq %r12, %rax
testb $0x4, %bpl
jne 0xe9f44
testb $0x8, %bpl
je 0xe9f47
movq 0x40(%rsp), %rax
addq %r12, %rax
cqto
pushq $0x2
popq %rcx
idivq %rcx
jmp 0xe9f47
shrq %rax
cmpq $0x8, %rax
setne %cl
orb %r13b, %cl
jne 0xe9f62
movl (%r14), %ecx
addl %edi, %ecx
movq %r8, %rax
cmpl $0x30, %ecx
jb 0xe9f67
jmp 0xe9fc6
testq %rax, %rax
js 0xe9fc6
movslq 0x8(%r14), %rcx
cmpq %rcx, %rax
jge 0xe9fc6
movq 0x18(%r14), %rcx
imulq 0x18(%rsp), %rcx
movq %rax, %rdx
shrq $0x3, %rdx
addq 0x20(%r14), %rcx
movzbl (%rdx,%rcx), %ecx
notb %al
movzbl %al, %eax
andl $0x7, %eax
btl %eax, %ecx
jae 0xe9fc6
movq 0x30(%rsp), %rax
leaq (%rax,%r12), %rsi
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl 0x80(%rsp), %ecx
movl %ebp, %r15d
movq %r8, %rbp
callq 0xe728c
movq %rbp, %r8
movl %r15d, %ebp
movl $0xffffff50, %edi       # imm = 0xFFFFFF50
incq %r12
jmp 0xe9f1a
movq 0x28(%rsp), %rsi
incq %rsi
jmp 0xe9ecf