// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movq 0x30(%rdx), %rax
movq %rax, (%rsp)
leaq 0x14(%rsp), %rsi
callq 0xe958c
movq (%rsp), %rdx
leaq 0x78da3(%rip), %rsi     # 0x139934
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq (%rsp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xc13e2
testl %eax, %eax
je 0xc0e86
movq (%rsp), %rsi
addq $0x4, %rsi
movq %rsi, (%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
callq 0xef570
movq %rax, %r14
addq $0x2, (%rsp)
leaq 0x78d62(%rip), %rsi     # 0x139949
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, 0x10(%r12)
movl $0x18, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xea259
movq %rax, 0x18(%r12)
movq 0x10(%r12), %rcx
testq %rcx, %rcx
jle 0xc0c2b
addq $0x4, %rax
movl $0xfffffff, (%rax)      # imm = 0xFFFFFFF
addq $0x18, %rax
decq %rcx
jne 0xc0c1c
cmpq $0x0, 0x10(%r12)
jle 0xc0e86
movl $0x16, %ebp
xorl %edx, %edx
movq %r15, 0x18(%rsp)
movq %r12, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x78d0d(%rip), %rsi     # 0x13995f
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0xe93cf
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r12), %r13
cmpl $0x100, 0x4(%r15)       # imm = 0x100
movq 0x18(%rbx), %rdi
jne 0xc0cee
movq %rsp, %r14
movq %r14, %rsi
callq 0xef14d
movzbl %al, %r12d
addl $-0x80, %r12d
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %eax
addl $-0x80, %eax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %r15d
addl $-0x80, %r15d
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %eax
addl $-0x80, %eax
movl %eax, -0x6(%r13,%rbp)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %r14d
addl $-0x80, %r14d
xorl %eax, %eax
jmp 0xc0d99
movq (%rsp), %rsi
movl 0x10(%r15), %edx
callq 0xef5b7
movq %rax, %r12
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
callq 0xef5b7
movq %rax, 0x8(%rsp)
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r15), %edx
callq 0xef5b7
movq %r15, %rcx
movq %rax, %r15
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%rcx), %edx
movq %rcx, %r14
callq 0xef5b7
movl %eax, -0x6(%r13,%rbp)
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%rbx), %rdi
movl 0x10(%r14), %edx
callq 0xef5b7
movq %rax, %r14
movq (%rsp), %rsi
addq $0x2, %rsi
movq %rsi, (%rsp)
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %rax
movl 0x10(%rax), %edx
callq 0xef570
addq $0x2, (%rsp)
cmpl $0x2, (%rbx)
jl 0xc0dff
movq %rbx, %rdi
leaq 0x78bc0(%rip), %rsi     # 0x139968
movl %r12d, %edx
movq 0x8(%rsp), %rcx
movq %rax, 0x20(%rsp)
xorl %eax, %eax
callq 0xe93cf
movq %rbx, %rdi
leaq 0x5e209(%rip), %rsi     # 0x11efcf
movl %r15d, %edx
xorl %eax, %eax
callq 0xe93cf
movl -0x6(%r13,%rbp), %edx
movq %rbx, %rdi
leaq 0x78ba0(%rip), %rsi     # 0x13997f
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe93cf
movq %rbx, %rdi
leaq 0x78ba4(%rip), %rsi     # 0x139997
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0xe93cf
movq 0x8(%rsp), %rdx
subl %r12d, %edx
movl %edx, -0xe(%r13,%rbp)
addl -0x6(%r13,%rbp), %r14d
movl %r14d, -0xa(%r13,%rbp)
cmpl $0x2, (%rbx)
jl 0xc0e2f
movq %rbx, %rdi
leaq 0x78b81(%rip), %rsi     # 0x1399a6
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe9226
testw %r12w, %r12w
movl $0x0, %eax
cmovsl %eax, %r12d
movw %r12w, -0x2(%r13,%rbp)
addl -0xe(%r13,%rbp), %r12d
subl %r12d, %r15d
testw %r15w, %r15w
cmovsl %eax, %r15d
movw %r15w, (%r13,%rbp)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x30(%rsp), %rdx
incq %rdx
addq $0x18, %rbp
movq 0x28(%rsp), %r12
cmpq 0x10(%r12), %rdx
movq 0x18(%rsp), %r15
jl 0xc0c48
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq