// File: /workspace/github_c_makefile_star2_O3/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 0xf5484
movq %rdx, %r12
cmpq %rcx, 0x18(%rdx)
jle 0xf5484
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 0xf54f2
testl %eax, %eax
je 0xf5484
movl 0x8(%r13), %eax
cmpl (%r12), %eax
jg 0xf5484
movl 0xc(%r13), %r8d
movl 0x4(%r12), %edi
cmpl %edi, %r8d
jg 0xf5484
testb $0x1, %bpl
jne 0xf5459
movswq 0x14(%r13), %rcx
movzwl 0x16(%r13), %esi
movl %ecx, %edx
orw %si, %dx
je 0xf5459
movslq %edi, %r9
testl %eax, %eax
je 0xf541f
testl %r8d, %r8d
je 0xf541f
movslq 0x10(%r13), %rdx
addq %rbx, %rdx
movl %r8d, %edi
jmp 0xf5422
movq %rbx, %rdx
movslq %edi, %r8
leaq (%rdx,%r8), %rdi
addq %rbx, %r9
cmpq %r9, %rdi
jg 0xf5459
cltq
movswq %si, %rsi
addq %rax, %rcx
addq %rsi, %rcx
movl $0x0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movl $0xffc0ffc0, %r9d       # imm = 0xFFC0FFC0
callq 0xf3654
movl 0x8(%r13), %eax
testl %eax, %eax
jle 0xf5484
movslq 0xc(%r13), %r8
testq %r8, %r8
jle 0xf5484
testb $0x1, %bpl
jne 0xf54c5
movslq 0x10(%r13), %rdx
addq %rbx, %rdx
leaq (%rdx,%r8), %rcx
movslq 0x4(%r12), %rsi
addq %rbx, %rsi
cmpq %rsi, %rcx
jle 0xf5493
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 %eax, %eax
movl $0x9, %ecx
cmovneq %rax, %rcx
testb $0x2, %bpl
cmoveq %rax, %rcx
movl $0x0, (%rsp)
movq %r14, %rdi
movl 0x10(%rsp), %r9d
callq 0xf3654
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 0xf5535