// File: /workspace/github_c_makefile_star2_O3/smilingthax[P]libfontembed/embed_sfnt.c
// Function: emb_otf_get_pdf_fontdescr
void emb_otf_get_pdf_fontdescr(OTF_FILE *otf,EMB_PDF_FONTDESCR *ret) // {{{
{
  int len;

//  TODO
//  ... fill in struct
  char *head=otf_get_table(otf,OTF_TAG('h','e','a','d'),&len);
  assert(head); // version is 1.0 from otf_load
  ret->bbxmin=get_SHORT(head+36)*1000/otf->unitsPerEm;
  ret->bbymin=get_SHORT(head+38)*1000/otf->unitsPerEm;
  ret->bbxmax=get_SHORT(head+40)*1000/otf->unitsPerEm;
  ret->bbymax=get_SHORT(head+42)*1000/otf->unitsPerEm;
  const int macStyle=get_USHORT(head+44);
  free(head);

  char *post=otf_get_table(otf,OTF_TAG('p','o','s','t'),&len);
  assert(post);
  const unsigned int post_version=get_ULONG(post);
  // check length
  assert( (post_version!=0x00010000)||(len==32) );
  assert( (post_version!=0x00020000)||(len>=34+2*otf->numGlyphs) );
  assert( (post_version!=0x00025000)||(len==35+otf->numGlyphs) );
  assert( (post_version!=0x00030000)||(len==32) );
  assert( (post_version!=0x00020000)||(get_USHORT(post+32)==otf->numGlyphs) ); // v4?
//  assert( (post_version==0x00030000)==(!!(otf->flags&OTF_F_FMT_CFF)) ); // ghostscript embedding does this..
  // TODO: v4 (apple) :  uint16 reencoding[numGlyphs]
  if ( (post_version==0x00010000)||
       (post_version==0x00020000)||
       (post_version==0x00025000)||
       (post_version==0x00030000)||
       (post_version==0x00040000) ) {
    ret->italicAngle=get_LONG(post+4)>>16;
    if (get_ULONG(post+12)>0) { // monospaced
      ret->flags|=1;
    }
  } else {
    fprintf(stderr,"WARNING: no italicAngle, no monospaced flag\n");
  }
  free(post);

  char *os2=otf_get_table(otf,OTF_TAG('O','S','/','2'),&len);
  if (os2) {
    const unsigned short os2_version=get_USHORT(os2);
    // check len
    assert( (os2_version!=0x0000)||(len==78) );
    assert( (os2_version!=0x0001)||(len==86) );
    assert( (os2_version<0x0002)||(os2_version>0x0004)||(len==96) );
    if (os2_version<=0x0004) {

      // from PDF14Deltas.pdf, pg 113
      const int weightClass=get_USHORT(os2+4);
      ret->stemV=50+weightClass*weightClass/(65*65); // TODO, really bad
//printf("a %d\n",weightClass);

      if (ret->supplement>=0) { // cid
        ret->panose=ret->data;
        memcpy(ret->panose,os2+30,12); // sFamilyClass + panose
      }
      const unsigned short fsSelection=get_USHORT(os2+62);
      if (fsSelection&0x01) { // italic
        ret->flags|=0x0040;
      }
      if ( (fsSelection&0x10)&&(weightClass>600) ) { // force bold
        ret->flags|=0x0400;
      }
      const unsigned char family_class=get_USHORT(os2+30)>>8;
      if (family_class==10) { // script
        ret->flags|=0x0008;
      }
      if (family_class!=8) { // not sans-serif
        ret->flags|=0x0002;
      }

      ret->avgWidth=get_SHORT(os2+2)*1000/otf->unitsPerEm;
      ret->ascent=get_SHORT(os2+68)*1000/otf->unitsPerEm;
      ret->descent=get_SHORT(os2+70)*1000/otf->unitsPerEm;
      if (os2_version>=0x0002) {
        ret->xHeight=get_SHORT(os2+86)*1000/otf->unitsPerEm;
        ret->capHeight=get_SHORT(os2+88)*1000/otf->unitsPerEm;
      } // else capHeight fixed later
    } else {
      free(os2);
      os2=NULL;
    }
  } else {
    fprintf(stderr,"WARNING: no OS/2 table\n");
    // e.g. subsetted font from ghostscript // e.g. CFF
  }
  if (os2) {
    free(os2);
  } else { // TODO (if(CFF))
    fprintf(stderr,"WARNING: no ascent/descent, capHeight, stemV, flags\n");
    if (macStyle&0x01) { // force bold - just do it on bold
      ret->flags|=0x0400;
    }
    if (macStyle&0x02) { // italic
      ret->flags|=0x0004;
    }
    //  ... flags TODO? (Serif, Script, Italic, AllCap,SmallCap, ForceBold)
  }

// ? maybe get ascent,descent,capHeight,xHeight,stemV directly from cff
  // Fallbacks
  if ( (!ret->ascent)||(!ret->descent) ) {
    char *hhea=otf_get_table(otf,OTF_TAG('h','h','e','a'),&len);
    if (hhea) {
      ret->ascent=get_SHORT(hhea+4)*1000/otf->unitsPerEm;
      ret->descent=get_SHORT(hhea+6)*1000/otf->unitsPerEm;
    }
    free(hhea);
  }
  if (!ret->stemV) { // TODO? use name
    const unsigned short d_gid=otf_from_unicode(otf,'.');
    if (d_gid) { // stemV=bbox['.'].width;
      len=otf_get_glyph(otf,d_gid);
      assert(len>=10);
      ret->stemV=(get_SHORT(otf->gly+6)-get_SHORT(otf->gly+2))*1000/otf->unitsPerEm;
    } else {
      if (macStyle&1) { // bold
        ret->stemV=165;
      } else {
        ret->stemV=109; // TODO... unserious values...
      }
    }
  }
  if (!ret->capHeight) { // TODO? only reqd. for fonts with latin...
    ret->capHeight=ret->ascent;
    // TODO: OTF spec says:  use metrics of 'H' (0 if not available)
  }
  if (0) { // TODO? uses only adobe latin standard? ?? e.g. Type1
    ret->flags|=0x0020;
  } else {
    ret->flags|=0x0004;
  }
  // TODO SmallCap by font name(?)

// TODO ;   ? cid ?
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x4(%rsp), %rdx
movl $0x68656164, %esi       # imm = 0x68656164
callq 0x423c
testq %rax, %rax
je 0x5a95
movq %rax, %rdi
movzbl 0x25(%rax), %eax
movzbl 0x24(%rdi), %ecx
shll $0x8, %ecx
movswl %cx, %ecx
orl %eax, %ecx
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %ecx
cltd
idivl %ecx
movl %eax, 0xc(%rbx)
movzbl 0x27(%rdi), %eax
movzbl 0x26(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x10(%rbx)
movzbl 0x29(%rdi), %eax
movzbl 0x28(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x14(%rbx)
movzbl 0x2b(%rdi), %eax
movzbl 0x2a(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x18(%rbx)
movzbl 0x2d(%rdi), %ebp
callq 0x21a0
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x706f7374, %esi       # imm = 0x706F7374
callq 0x423c
testq %rax, %rax
je 0x5ab4
movq %rax, %r15
movl (%rax), %eax
bswapl %eax
cmpl $0x10000, %eax          # imm = 0x10000
setne %dl
movl 0x4(%rsp), %ecx
cmpl $0x20, %ecx
sete %sil
orb %dl, %sil
je 0x5ad3
cmpl $0x25000, %eax          # imm = 0x25000
je 0x575d
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x5788
movzwl 0x4c(%r14), %eax
leal 0x22(,%rax,2), %edx
cmpl %edx, %ecx
jl 0x5b4f
movzwl 0x20(%r15), %ecx
rolw $0x8, %cx
cmpw %ax, %cx
je 0x57b7
leaq 0x54b9(%rip), %rdi      # 0xabfe
leaq 0x52fd(%rip), %rsi      # 0xaa49
leaq 0x53ae(%rip), %rcx      # 0xab01
movl $0x68, %edx
callq 0x20e0
movzwl 0x4c(%r14), %eax
addl $0x23, %eax
cmpl %eax, %ecx
je 0x57b7
leaq 0x5433(%rip), %rdi      # 0xaba3
leaq 0x52d2(%rip), %rsi      # 0xaa49
leaq 0x5383(%rip), %rcx      # 0xab01
movl $0x66, %edx
callq 0x20e0
cmpl $0x20, %ecx
sete %cl
cmpl $0x30000, %eax          # imm = 0x30000
setne %dl
orb %cl, %dl
je 0x5b6e
cmpl $0x10000, %eax          # imm = 0x10000
je 0x57b7
cmpl $0x40000, %eax          # imm = 0x40000
je 0x57b7
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x5a8b
movzbl 0x4(%r15), %eax
shll $0x18, %eax
movzbl 0x5(%r15), %ecx
shll $0x10, %ecx
orl %eax, %ecx
sarl $0x10, %ecx
movl %ecx, 0x1c(%rbx)
movl 0xc(%r15), %eax
bswapl %eax
testl %eax, %eax
je 0x57dd
orb $0x1, 0x8(%rbx)
movq %r15, %rdi
callq 0x21a0
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x4f532f32, %esi       # imm = 0x4F532F32
callq 0x423c
testq %rax, %rax
je 0x5a70
movq %rax, %rdi
movzwl (%rax), %ecx
rolw $0x8, %cx
testw %cx, %cx
setne %dl
movl 0x4(%rsp), %eax
cmpl $0x4e, %eax
sete %sil
orb %dl, %sil
je 0x5af2
cmpw $0x1, %cx
setne %dl
cmpl $0x56, %eax
sete %sil
orb %dl, %sil
je 0x5b11
leal -0x5(%rcx), %edx
cmpw $-0x3, %dx
setb %dl
cmpl $0x60, %eax
sete %al
orb %dl, %al
je 0x5b30
cmpw $0x4, %cx
ja 0x5a77
movzwl 0x4(%rdi), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, %edx
imull %edx, %edx
imulq $0x7c178275, %rdx, %rdx # imm = 0x7C178275
shrq $0x2b, %rdx
addl $0x32, %edx
movl %edx, 0x2c(%rbx)
cmpl $0x0, 0x50(%rbx)
js 0x589a
leaq 0x54(%rbx), %rdx
movq %rdx, 0x38(%rbx)
movq 0x1e(%rdi), %rdx
movq %rdx, 0x54(%rbx)
movl 0x26(%rdi), %edx
movl %edx, 0x5c(%rbx)
movzbl 0x3f(%rdi), %edx
testb $0x1, %dl
je 0x58a7
orb $0x40, 0x8(%rbx)
testb $0x10, %dl
je 0x58b7
cmpl $0x259, %eax            # imm = 0x259
jb 0x58b7
orb $0x4, 0x9(%rbx)
movzbl 0x1e(%rdi), %eax
cmpl $0x8, %eax
je 0x58d6
cmpl $0xa, %eax
jne 0x58cd
movl 0x8(%rbx), %eax
orl $0x8, %eax
jmp 0x58d0
movl 0x8(%rbx), %eax
orl $0x2, %eax
movl %eax, 0x8(%rbx)
movzbl 0x3(%rdi), %eax
movzbl 0x2(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %esi
cltd
idivl %esi
movl %eax, 0x34(%rbx)
movzbl 0x45(%rdi), %eax
movzbl 0x44(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x20(%rbx)
movzbl 0x47(%rdi), %eax
movzbl 0x46(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x24(%rbx)
cmpw $0x2, %cx
jb 0x596d
movzbl 0x57(%rdi), %eax
movzbl 0x56(%rdi), %ecx
shll $0x8, %ecx
movswl %cx, %ecx
orl %eax, %ecx
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x30(%rbx)
movzbl 0x59(%rdi), %eax
movzbl 0x58(%rdi), %ecx
shll $0x8, %ecx
movswl %cx, %ecx
orl %eax, %ecx
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x28(%rbx)
callq 0x21a0
cmpl $0x0, 0x20(%rbx)
je 0x597e
cmpl $0x0, 0x24(%rbx)
jne 0x59da
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x68686561, %esi       # imm = 0x68686561
callq 0x423c
movq %rax, %rdi
testq %rax, %rax
je 0x59d5
movzbl 0x5(%rdi), %eax
movzbl 0x4(%rdi), %ecx
shll $0x8, %ecx
movswl %cx, %ecx
orl %eax, %ecx
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %ecx
cltd
idivl %ecx
movl %eax, 0x20(%rbx)
movzbl 0x7(%rdi), %eax
movzbl 0x6(%rdi), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x24(%rbx)
callq 0x21a0
cmpl $0x0, 0x2c(%rbx)
jne 0x5a55
movq %r14, %rdi
movl $0x2e, %esi
callq 0x4c28
testw %ax, %ax
je 0x5a41
movzwl %ax, %esi
movq %r14, %rdi
callq 0x4ad0
movl %eax, 0x4(%rsp)
cmpl $0x9, %eax
jle 0x5b8d
movq 0x80(%r14), %rax
movzbl 0x7(%rax), %ecx
movzbl 0x6(%rax), %edx
shll $0x8, %edx
movswl %dx, %edx
orl %ecx, %edx
movzbl 0x3(%rax), %ecx
movzbl 0x2(%rax), %eax
shll $0x8, %eax
cwtl
orl %ecx, %eax
subl %eax, %edx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %ecx
cltd
idivl %ecx
jmp 0x5a52
testb $0x1, %bpl
movl $0x6d, %ecx
movl $0xa5, %eax
cmovel %ecx, %eax
movl %eax, 0x2c(%rbx)
cmpl $0x0, 0x28(%rbx)
jne 0x5a61
movl 0x20(%rbx), %eax
movl %eax, 0x28(%rbx)
orb $0x4, 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x2af8
jmp 0x5a7c
callq 0x21a0
movl %ebp, %edi
movq %rbx, %rsi
callq 0x2b1a
jmp 0x5972
callq 0x2ad6
jmp 0x57dd
leaq 0x5060(%rip), %rdi      # 0xaafc
leaq 0x4fa6(%rip), %rsi      # 0xaa49
leaq 0x5057(%rip), %rcx      # 0xab01
movl $0x58, %edx
callq 0x20e0
leaq 0x5086(%rip), %rdi      # 0xab41
leaq 0x4f87(%rip), %rsi      # 0xaa49
leaq 0x5038(%rip), %rcx      # 0xab01
movl $0x61, %edx
callq 0x20e0
leaq 0x506c(%rip), %rdi      # 0xab46
leaq 0x4f68(%rip), %rsi      # 0xaa49
leaq 0x5019(%rip), %rcx      # 0xab01
movl $0x64, %edx
callq 0x20e0
leaq 0x4f2f(%rip), %rdi      # 0xaa28
leaq 0x4f49(%rip), %rsi      # 0xaa49
leaq 0x4ffa(%rip), %rcx      # 0xab01
movl $0x7d, %edx
callq 0x20e0
leaq 0x4f6c(%rip), %rdi      # 0xaa84
leaq 0x4f2a(%rip), %rsi      # 0xaa49
leaq 0x4fdb(%rip), %rcx      # 0xab01
movl $0x7e, %edx
callq 0x20e0
leaq 0x4f6e(%rip), %rdi      # 0xaaa5
leaq 0x4f0b(%rip), %rsi      # 0xaa49
leaq 0x4fbc(%rip), %rcx      # 0xab01
movl $0x7f, %edx
callq 0x20e0
leaq 0x5016(%rip), %rdi      # 0xab6c
leaq 0x4eec(%rip), %rsi      # 0xaa49
leaq 0x4f9d(%rip), %rcx      # 0xab01
movl $0x65, %edx
callq 0x20e0
leaq 0x5063(%rip), %rdi      # 0xabd8
leaq 0x4ecd(%rip), %rsi      # 0xaa49
leaq 0x4f7e(%rip), %rcx      # 0xab01
movl $0x67, %edx
callq 0x20e0
leaq 0x5126(%rip), %rdi      # 0xacba
leaq 0x4eae(%rip), %rsi      # 0xaa49
leaq 0x4f5f(%rip), %rcx      # 0xab01
movl $0xc4, %edx
callq 0x20e0