// File: /workspace/github_c_makefile_star2_O2/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 0x371c
testq %rax, %rax
je 0x5199
movq %rax, %rdi
movzbl 0x24(%rax), %eax
shll $0x8, %eax
movzbl 0x25(%rdi), %ecx
cwtl
orl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %ecx
cltd
idivl %ecx
movl %eax, 0xc(%rbx)
movzbl 0x26(%rdi), %eax
shll $0x8, %eax
movzbl 0x27(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x10(%rbx)
movzbl 0x28(%rdi), %eax
shll $0x8, %eax
movzbl 0x29(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x14(%rbx)
movzbl 0x2a(%rdi), %eax
shll $0x8, %eax
movzbl 0x2b(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x18(%rbx)
movzbl 0x2d(%rdi), %ebp
callq 0x21b0
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x706f7374, %esi       # imm = 0x706F7374
callq 0x371c
testq %rax, %rax
je 0x51b5
movq %rax, %r15
movq %rax, %rdi
callq 0x528a
cmpl $0x10000, %eax          # imm = 0x10000
setne %dl
movl 0x4(%rsp), %ecx
cmpl $0x20, %ecx
sete %sil
orb %dl, %sil
je 0x51d1
cmpl $0x25000, %eax          # imm = 0x25000
je 0x4e23
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x4e4b
movzwl 0x4c(%r14), %eax
leal 0x22(,%rax,2), %edx
cmpl %edx, %ecx
jl 0x5235
movzwl 0x20(%r15), %ecx
rolw $0x8, %cx
cmpw %ax, %cx
je 0x4e7a
leaq 0x5df0(%rip), %rdi      # 0xabfe
leaq 0x5c34(%rip), %rsi      # 0xaa49
leaq 0x5ce5(%rip), %rcx      # 0xab01
pushq $0x68
jmp 0x5265
movzwl 0x4c(%r14), %eax
addl $0x23, %eax
cmpl %eax, %ecx
je 0x4e7a
leaq 0x5d6d(%rip), %rdi      # 0xaba3
leaq 0x5c0c(%rip), %rsi      # 0xaa49
leaq 0x5cbd(%rip), %rcx      # 0xab01
pushq $0x66
jmp 0x5265
cmpl $0x20, %ecx
sete %cl
cmpl $0x30000, %eax          # imm = 0x30000
setne %dl
orb %cl, %dl
je 0x524e
cmpl $0x10000, %eax          # imm = 0x10000
je 0x4e7a
cmpl $0x40000, %eax          # imm = 0x40000
je 0x4e7a
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x5178
leaq 0x4(%r15), %rdi
callq 0x528f
sarl $0x10, %eax
movl %eax, 0x1c(%rbx)
movq %r15, %rdi
addq $0xc, %rdi
callq 0x528a
testl %eax, %eax
je 0x4e9d
orb $0x1, 0x8(%rbx)
movq %r15, %rdi
callq 0x21b0
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x4f532f32, %esi       # imm = 0x4F532F32
callq 0x371c
testq %rax, %rax
je 0x511c
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 0x51ea
cmpw $0x1, %cx
setne %dl
cmpl $0x56, %eax
sete %sil
orb %dl, %sil
je 0x5203
leal -0x5(%rcx), %edx
cmpw $-0x3, %dx
setb %dl
cmpl $0x60, %eax
sete %al
orb %dl, %al
je 0x521c
cmpw $0x4, %cx
ja 0x513a
movzwl 0x4(%rdi), %esi
rolw $0x8, %si
movzwl %si, %eax
imull %eax, %eax
movl $0x1081, %r8d           # imm = 0x1081
xorl %edx, %edx
divl %r8d
addl $0x32, %eax
movl %eax, 0x2c(%rbx)
cmpl $0x0, 0x50(%rbx)
js 0x4f58
leaq 0x54(%rbx), %rax
movq %rax, 0x38(%rbx)
movq 0x1e(%rdi), %rax
movq %rax, 0x54(%rbx)
movl 0x26(%rdi), %eax
movl %eax, 0x5c(%rbx)
movzbl 0x3f(%rdi), %eax
testb $0x1, %al
je 0x4f64
orb $0x40, 0x8(%rbx)
testb $0x10, %al
je 0x4f73
cmpw $0x259, %si             # imm = 0x259
jb 0x4f73
orb $0x4, 0x9(%rbx)
movzbl 0x1e(%rdi), %eax
cmpl $0x8, %eax
je 0x4f92
cmpl $0xa, %eax
jne 0x4f89
movl 0x8(%rbx), %eax
orl $0x8, %eax
jmp 0x4f8c
movl 0x8(%rbx), %eax
orl $0x2, %eax
movl %eax, 0x8(%rbx)
movzbl 0x2(%rdi), %eax
shll $0x8, %eax
movzbl 0x3(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %esi
cltd
idivl %esi
movl %eax, 0x34(%rbx)
movzbl 0x44(%rdi), %eax
shll $0x8, %eax
movzbl 0x45(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x20(%rbx)
movzbl 0x46(%rdi), %eax
shll $0x8, %eax
movzbl 0x47(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x24(%rbx)
cmpw $0x2, %cx
jb 0x501f
movzbl 0x56(%rdi), %eax
shll $0x8, %eax
movzbl 0x57(%rdi), %ecx
cwtl
orl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x30(%rbx)
movzbl 0x58(%rdi), %eax
shll $0x8, %eax
movzbl 0x59(%rdi), %ecx
cwtl
orl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %esi
movl %eax, 0x28(%rbx)
callq 0x21b0
cmpl $0x0, 0x20(%rbx)
je 0x5030
cmpl $0x0, 0x24(%rbx)
jne 0x5088
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x68686561, %esi       # imm = 0x68686561
callq 0x371c
movq %rax, %rdi
testq %rax, %rax
je 0x5083
movzbl 0x4(%rdi), %eax
shll $0x8, %eax
movzbl 0x5(%rdi), %ecx
cwtl
orl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %ecx
cltd
idivl %ecx
movl %eax, 0x20(%rbx)
movzbl 0x6(%rdi), %eax
shll $0x8, %eax
movzbl 0x7(%rdi), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x24(%rbx)
callq 0x21b0
cmpl $0x0, 0x2c(%rbx)
jne 0x5101
pushq $0x2e
popq %rsi
movq %r14, %rdi
callq 0x42a4
testw %ax, %ax
je 0x50ef
movzwl %ax, %esi
movq %r14, %rdi
callq 0x4137
movl %eax, 0x4(%rsp)
cmpl $0x9, %eax
jle 0x526b
movq 0x80(%r14), %rax
movzbl 0x6(%rax), %ecx
shll $0x8, %ecx
movzbl 0x7(%rax), %edx
movswl %cx, %ecx
orl %edx, %ecx
movzbl 0x2(%rax), %edx
shll $0x8, %edx
movzbl 0x3(%rax), %eax
movswl %dx, %edx
orl %eax, %edx
subl %edx, %ecx
imull $0x3e8, %ecx, %eax      # imm = 0x3E8
movzwl 0x48(%r14), %ecx
cltd
idivl %ecx
jmp 0x50fe
testb $0x1, %bpl
pushq $0x6d
popq %rcx
movl $0xa5, %eax
cmovel %ecx, %eax
movl %eax, 0x2c(%rbx)
cmpl $0x0, 0x28(%rbx)
jne 0x510d
movl 0x20(%rbx), %eax
movl %eax, 0x28(%rbx)
orb $0x4, 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x9ebd(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x5b40(%rip), %rdi      # 0xac6d
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x513f
callq 0x21b0
movq 0x9e9a(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x5b35(%rip), %rdi      # 0xac85
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
testb $0x1, %bpl
je 0x5165
orb $0x4, 0x9(%rbx)
testb $0x2, %bpl
je 0x5024
orb $0x4, 0x8(%rbx)
jmp 0x5024
movq 0x9e61(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x5ab7(%rip), %rdi      # 0xac40
pushq $0x2c
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x4e9d
leaq 0x595c(%rip), %rdi      # 0xaafc
leaq 0x58a2(%rip), %rsi      # 0xaa49
leaq 0x5953(%rip), %rcx      # 0xab01
pushq $0x58
jmp 0x5265
leaq 0x5985(%rip), %rdi      # 0xab41
leaq 0x5886(%rip), %rsi      # 0xaa49
leaq 0x5937(%rip), %rcx      # 0xab01
pushq $0x61
jmp 0x5265
leaq 0x596e(%rip), %rdi      # 0xab46
leaq 0x586a(%rip), %rsi      # 0xaa49
leaq 0x591b(%rip), %rcx      # 0xab01
pushq $0x64
jmp 0x5265
leaq 0x5837(%rip), %rdi      # 0xaa28
leaq 0x5851(%rip), %rsi      # 0xaa49
leaq 0x5902(%rip), %rcx      # 0xab01
pushq $0x7d
jmp 0x5265
leaq 0x587a(%rip), %rdi      # 0xaa84
leaq 0x5838(%rip), %rsi      # 0xaa49
leaq 0x58e9(%rip), %rcx      # 0xab01
pushq $0x7e
jmp 0x5265
leaq 0x5882(%rip), %rdi      # 0xaaa5
leaq 0x581f(%rip), %rsi      # 0xaa49
leaq 0x58d0(%rip), %rcx      # 0xab01
pushq $0x7f
jmp 0x5265
leaq 0x5930(%rip), %rdi      # 0xab6c
leaq 0x5806(%rip), %rsi      # 0xaa49
leaq 0x58b7(%rip), %rcx      # 0xab01
pushq $0x65
jmp 0x5265
leaq 0x5983(%rip), %rdi      # 0xabd8
leaq 0x57ed(%rip), %rsi      # 0xaa49
leaq 0x589e(%rip), %rcx      # 0xab01
pushq $0x67
popq %rdx
callq 0x20f0
leaq 0x5a48(%rip), %rdi      # 0xacba
leaq 0x57d0(%rip), %rsi      # 0xaa49
leaq 0x5881(%rip), %rcx      # 0xab01
movl $0xc4, %edx
callq 0x20f0