// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/pcf.c
// Function: handler_metrics
static void handler_metrics(deark *c, lctx *d, struct table_entry *te)
{
	i64 pos = te->offset;
	int saved_indent_level;
	i64 nmetrics;
	i64 k;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "metrics table at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	if(!read_and_check_format_field(c, d, te, pos)) goto done;
	pos += 4;

	nmetrics = dbuf_getu16x(c->infile, pos, te->fmt.is_le);
	pos += 2;
	de_dbg(c, "number of metrics: %d", (int)nmetrics);

	d->num_chars = nmetrics;

	// Allocate chars array, and set defaults
	d->chars = de_mallocarray(c, d->num_chars, sizeof(struct char_info));
	for(k=0; k<d->num_chars; k++) {
		d->chars[k].codepoint = DE_CODEPOINT_INVALID;
	}

	for(k=0; k<d->num_chars; k++) {
		int leftsb, rightsb;
		int char_width;
		int char_desc;
		unsigned int char_attr;
		struct char_info *ci;

		de_dbg2(c, "char[%d]", (int)k);
		de_dbg_indent(c, 1);
		ci = &d->chars[k];

		if(te->fmt.gross_format==GFMT_COMPRESSED_METRICS) {
			leftsb = (int)de_getbyte_p(&pos) - 0x80;
			rightsb = (int)de_getbyte_p(&pos) - 0x80;
			char_width = (int)de_getbyte_p(&pos) - 0x80;
			ci->ascent = (int)de_getbyte_p(&pos) - 0x80;
			char_desc = (int)de_getbyte_p(&pos) - 0x80;
			char_attr = 0;
		}
		else {
			leftsb = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			rightsb = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			char_width = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			ci->ascent = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			char_desc = (int)dbuf_geti16x(c->infile, pos, te->fmt.is_le); pos += 2;
			char_attr = (int)dbuf_getu16x(c->infile, pos, te->fmt.is_le); pos += 2;
		}

		if(c->debug_level>=2) {
			de_dbg2(c, "bearing (l, r): %d, %d", leftsb, rightsb);
			de_dbg2(c, "width: %d", char_width);
			de_dbg2(c, "ascent, descent: %d, %d", ci->ascent, char_desc);
			de_dbg2(c, "attributes: %u", char_attr);
		}
		ci->width_raw = rightsb - leftsb;
		ci->height_raw = ci->ascent + char_desc;
		if(c->debug_level>=2) {
			de_dbg(c, "raw bitmap dimensions: %d"DE_CHAR_TIMES"%d",
				ci->width_raw, ci->height_raw);
		}

		// TODO: Are these calculations correct?
		ci->extraspace_l = (i16)leftsb;
		if(ci->extraspace_l<0) ci->extraspace_l=0;
		ci->extraspace_r = (i16)(char_width - ci->width_raw - (int)ci->extraspace_l);
		if(ci->extraspace_r<0) ci->extraspace_r=0;

		de_dbg_indent(c, -1);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r12
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movq 0x30(%rdx), %r14
movq %r14, 0x8(%rsp)
leaq 0x2c(%rsp), %rsi
callq 0xdebae
leaq 0x742c8(%rip), %rsi     # 0x12c9c4
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xb8f61
testl %eax, %eax
je 0xb8a3b
leaq 0x4(%r14), %rsi
movq 0x18(%rbx), %rdi
movl 0x10(%r12), %edx
callq 0xe41b1
movq %rax, %r15
addq $0x6, %r14
movq %r14, 0x8(%rsp)
leaq 0x7428a(%rip), %rsi     # 0x12c9d9
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rsp), %r13
movq %r15, 0x10(%r13)
pushq $0x18
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdf66f
movq %rax, 0x18(%r13)
movq 0x10(%r13), %rcx
testq %rcx, %rcx
cmovgq %rcx, %r14
addq $0x4, %rax
subq $0x1, %r14
jb 0xb879b
movl $0xfffffff, (%rax)      # imm = 0xFFFFFFF
addq $0x18, %rax
jmp 0xb8789
pushq $0x16
popq %r14
xorl %edx, %edx
movq %r12, 0x48(%rsp)
movq %rbx, 0x30(%rsp)
cmpq %rcx, %rdx
jge 0xb8a3b
movq %rbx, %rdi
leaq 0x74231(%rip), %rsi     # 0x12c9ef
movq %rdx, 0x50(%rsp)
xorl %eax, %eax
callq 0xde9f1
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r13
cmpl $0x100, 0x4(%r12)       # imm = 0x100
movq 0x18(%rbx), %rdi
movq %r13, 0x20(%rsp)
movq %r14, 0x38(%rsp)
jne 0xb8867
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %r12d
addl $-0x80, %r12d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %eax
addl $-0x80, %eax
movq %rax, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %r15d
addl $-0x80, %r15d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %eax
addl $-0x80, %eax
movl %eax, -0x6(%r13,%r14)
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %ebp
addl $-0x80, %ebp
xorl %eax, %eax
jmp 0xb891e
movq 0x8(%rsp), %rsi
movl 0x10(%r12), %edx
callq 0xe41be
movq %r12, %rcx
movq %rax, %r12
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%rcx), %edx
movq %r13, %rbp
movq %rcx, %r13
callq 0xe41be
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r13), %edx
callq 0xe41be
movq %rax, %r15
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r13), %edx
callq 0xe41be
movl %eax, -0x6(%rbp,%r14)
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r13), %edx
callq 0xe41be
movq %rax, %rbp
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
movq %rsi, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r13), %edx
callq 0xe41b1
addq $0x2, 0x8(%rsp)
cmpl $0x2, (%rbx)
jl 0xb899a
movq %rbx, %rdi
leaq 0x740cb(%rip), %rsi     # 0x12c9f8
movl %r12d, %edx
movq 0x10(%rsp), %rcx
movq %rax, 0x40(%rsp)
xorl %eax, %eax
callq 0xde9f1
movq %rbx, %rdi
leaq 0x596bc(%rip), %rsi     # 0x112007
movl %r15d, %edx
xorl %eax, %eax
callq 0xde9f1
movq 0x38(%rsp), %r14
movq 0x20(%rsp), %r13
movl -0x6(%r13,%r14), %edx
movq %rbx, %rdi
leaq 0x740a1(%rip), %rsi     # 0x12ca0f
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde9f1
movq %rbx, %rdi
leaq 0x740a6(%rip), %rsi     # 0x12ca27
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0xde9f1
movq 0x10(%rsp), %rdx
cmpl $0x2, (%rbx)
setge %al
jmp 0xb89ab
xorl %eax, %eax
movq 0x38(%rsp), %r14
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %rdx
subl %r12d, %edx
movl %edx, -0xe(%r13,%r14)
addl -0x6(%r13,%r14), %ebp
movl %ebp, -0xa(%r13,%r14)
testb %al, %al
je 0xb89e5
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x74066(%rip), %rsi     # 0x12ca36
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x20(%rsp), %r13
movl -0xe(%r13,%r14), %edx
jmp 0xb89ea
movq 0x30(%rsp), %rbx
testw %r12w, %r12w
movl $0x0, %eax
cmovsl %eax, %r12d
movw %r12w, -0x2(%r13,%r14)
addl %edx, %r12d
subl %r12d, %r15d
testw %r15w, %r15w
cmovsl %eax, %r15d
movw %r15w, (%r13,%r14)
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
addq $0x18, %r14
movq 0x48(%rsp), %r12
jmp 0xb87ab
movl 0x2c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq