// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
testq %rcx, %rcx
js 0xf5916
movq %rdx, %r12
cmpq %rcx, 0x18(%rdx)
jle 0xf5916
movq %r9, %rbx
movq %r8, %r15
movq %rsi, %r14
movl 0x60(%rsp), %ebp
movl 0x58(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq (%rcx,%rcx,4), %r13
shlq $0x3, %r13
addq 0x20(%r12), %r13
movq %r13, %rdi
callq 0xf5982
testl %eax, %eax
je 0xf5916
movslq 0x8(%r13), %rax
cmpl (%r12), %eax
jg 0xf5916
movl 0xc(%r13), %edi
movl 0x4(%r12), %ecx
cmpl %ecx, %edi
jg 0xf5916
testb $0x1, %bpl
jne 0xf58e6
movswq 0x14(%r13), %rsi
testq %rsi, %rsi
jne 0xf589b
cmpw $0x0, 0x16(%r13)
je 0xf58e6
movslq %ecx, %r9
testl %eax, %eax
je 0xf58b1
testl %edi, %edi
je 0xf58b1
movslq 0x10(%r13), %rdx
addq %rbx, %rdx
movl %edi, %ecx
jmp 0xf58b4
movq %rbx, %rdx
movslq %ecx, %r8
leaq (%rdx,%r8), %rcx
addq %rbx, %r9
cmpq %r9, %rcx
jg 0xf58e6
addq %rax, %rsi
movswq 0x16(%r13), %rcx
addq %rsi, %rcx
movl $0x0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movl $0xffc0ffc0, %r9d       # imm = 0xFFC0FFC0
callq 0xf3cb3
movslq 0x8(%r13), %rax
testq %rax, %rax
jle 0xf5916
movslq 0xc(%r13), %r8
testq %r8, %r8
jle 0xf5916
testb $0x1, %bpl
jne 0xf5955
movslq 0x10(%r13), %rdx
addq %rbx, %rdx
leaq (%rdx,%r8), %rcx
movslq 0x4(%r12), %rsi
addq %rbx, %rsi
cmpq %rsi, %rcx
jle 0xf5925
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movswq 0x14(%r13), %rsi
addq %r15, %rsi
cmpl $0x8, %eax
movl $0x9, %ecx
cmovneq %rax, %rcx
testb $0x2, %bpl
cmoveq %rax, %rcx
movl $0x0, (%rsp)
movq %r14, %rdi
movl 0x10(%rsp), %r9d
callq 0xf3cb3
movswq 0x14(%r13), %rdx
addq %r15, %rdx
movslq 0x10(%r13), %rcx
addq %rbx, %rcx
movq %r14, %rdi
movq %r13, %rsi
movl 0x14(%rsp), %r8d
movl %ebp, %r9d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf59d6