// File: /workspace/github_c_makefile_star2_O1/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 0x4249
testq %rax, %rax
je 0x5a9f
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 0x21a0
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x706f7374, %esi       # imm = 0x706F7374
callq 0x4249
testq %rax, %rax
je 0x5abe
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 0x5add
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x574b
movzwl 0x4c(%r14), %edx
leal 0x22(,%rdx,2), %edx
cmpl %edx, %ecx
jl 0x5b78
cmpl $0x25000, %eax          # imm = 0x25000
jne 0x5762
movzwl 0x4c(%r14), %edx
addl $0x23, %edx
cmpl %edx, %ecx
jne 0x5b97
cmpl $0x20, %ecx
sete %cl
cmpl $0x30000, %eax          # imm = 0x30000
setne %dl
orb %cl, %dl
je 0x5afc
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x5793
movzwl 0x20(%r15), %ecx
rolw $0x8, %cx
cmpw 0x4c(%r14), %cx
jne 0x5bb6
cmpl $0x24fff, %eax          # imm = 0x24FFF
jle 0x57b4
cmpl $0x25000, %eax          # imm = 0x25000
je 0x57c6
cmpl $0x40000, %eax          # imm = 0x40000
je 0x57c6
cmpl $0x30000, %eax          # imm = 0x30000
je 0x57c6
jmp 0x5a95
cmpl $0x10000, %eax          # imm = 0x10000
je 0x57c6
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x5a95
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 0x57ec
orb $0x1, 0x8(%rbx)
movq %r15, %rdi
callq 0x21a0
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x4f532f32, %esi       # imm = 0x4F532F32
callq 0x4249
testq %rax, %rax
je 0x5a7c
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 0x5b1b
cmpw $0x1, %cx
setne %dl
cmpl $0x56, %eax
sete %sil
orb %dl, %sil
je 0x5b3a
leal -0x5(%rcx), %edx
cmpw $-0x3, %dx
setb %dl
cmpl $0x60, %eax
sete %al
orb %dl, %al
je 0x5b59
cmpw $0x4, %cx
ja 0x596b
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 0x58a9
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 0x58b6
orb $0x40, 0x8(%rbx)
testb $0x10, %dl
je 0x58c6
cmpl $0x259, %eax            # imm = 0x259
jb 0x58c6
orb $0x4, 0x9(%rbx)
movzbl 0x1e(%rdi), %eax
cmpl $0x8, %eax
je 0x58dc
cmpl $0xa, %eax
jne 0x58d8
orb $0x8, 0x8(%rbx)
orb $0x2, 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 0x5972
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)
jmp 0x5972
callq 0x21a0
xorl %edi, %edi
testq %rdi, %rdi
je 0x5a86
callq 0x21a0
cmpl $0x0, 0x20(%rbx)
je 0x598c
cmpl $0x0, 0x24(%rbx)
jne 0x59e4
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movl $0x68686561, %esi       # imm = 0x68686561
callq 0x4249
movq %rax, %rdi
testq %rax, %rax
je 0x59df
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 0x21a0
cmpl $0x0, 0x2c(%rbx)
jne 0x5a61
movq %r14, %rdi
movl $0x2e, %esi
callq 0x4c58
testw %ax, %ax
je 0x5a4d
movzwl %ax, %esi
movq %r14, %rdi
callq 0x4b02
movl %eax, 0x4(%rsp)
cmpl $0x9, %eax
jle 0x5bd5
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 0x5a5e
testb $0x1, %bpl
movl $0x6d, %ecx
movl $0xa5, %eax
cmovel %ecx, %eax
movl %eax, 0x2c(%rbx)
cmpl $0x0, 0x28(%rbx)
jne 0x5a6d
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 0x2af0
jmp 0x5970
movl %ebp, %edi
movq %rbx, %rsi
callq 0x2b12
jmp 0x5980
callq 0x2ace
jmp 0x57ec
leaq 0x5056(%rip), %rdi      # 0xaafc
leaq 0x4f9c(%rip), %rsi      # 0xaa49
leaq 0x504d(%rip), %rcx      # 0xab01
movl $0x58, %edx
callq 0x20e0
leaq 0x507c(%rip), %rdi      # 0xab41
leaq 0x4f7d(%rip), %rsi      # 0xaa49
leaq 0x502e(%rip), %rcx      # 0xab01
movl $0x61, %edx
callq 0x20e0
leaq 0x5062(%rip), %rdi      # 0xab46
leaq 0x4f5e(%rip), %rsi      # 0xaa49
leaq 0x500f(%rip), %rcx      # 0xab01
movl $0x64, %edx
callq 0x20e0
leaq 0x50d5(%rip), %rdi      # 0xabd8
leaq 0x4f3f(%rip), %rsi      # 0xaa49
leaq 0x4ff0(%rip), %rcx      # 0xab01
movl $0x67, %edx
callq 0x20e0
leaq 0x4f06(%rip), %rdi      # 0xaa28
leaq 0x4f20(%rip), %rsi      # 0xaa49
leaq 0x4fd1(%rip), %rcx      # 0xab01
movl $0x7d, %edx
callq 0x20e0
leaq 0x4f43(%rip), %rdi      # 0xaa84
leaq 0x4f01(%rip), %rsi      # 0xaa49
leaq 0x4fb2(%rip), %rcx      # 0xab01
movl $0x7e, %edx
callq 0x20e0
leaq 0x4f45(%rip), %rdi      # 0xaaa5
leaq 0x4ee2(%rip), %rsi      # 0xaa49
leaq 0x4f93(%rip), %rcx      # 0xab01
movl $0x7f, %edx
callq 0x20e0
leaq 0x4fed(%rip), %rdi      # 0xab6c
leaq 0x4ec3(%rip), %rsi      # 0xaa49
leaq 0x4f74(%rip), %rcx      # 0xab01
movl $0x65, %edx
callq 0x20e0
leaq 0x5005(%rip), %rdi      # 0xaba3
leaq 0x4ea4(%rip), %rsi      # 0xaa49
leaq 0x4f55(%rip), %rcx      # 0xab01
movl $0x66, %edx
callq 0x20e0
leaq 0x5041(%rip), %rdi      # 0xabfe
leaq 0x4e85(%rip), %rsi      # 0xaa49
leaq 0x4f36(%rip), %rcx      # 0xab01
movl $0x68, %edx
callq 0x20e0
leaq 0x50de(%rip), %rdi      # 0xacba
leaq 0x4e66(%rip), %rsi      # 0xaa49
leaq 0x4f17(%rip), %rcx      # 0xab01
movl $0xc4, %edx
callq 0x20e0