// File: /workspace/github_c_makefile_star2_O2/smilingthax[P]libfontembed/embed_sfnt.c
// Function: emb_otf_ps
int emb_otf_ps(OTF_FILE *otf,unsigned short *encoding,int len,unsigned short *to_unicode,OUTPUT_FN output,void *context) // {{{
{
  const int binary=0; // binary format? // TODO
  if (len>256) {
    fprintf(stderr,"Encoding too big(%d) for Type42\n",len);
    return -1;
  }
  if (len<1) {
    fprintf(stderr,"At least .notdef required in Type42\n");
    return -1;
  }
  if (!encoding) {
    to_unicode=NULL; // does not make sense
  }
  int iA,ret=0;

  DYN_STRING ds;
  if (dyn_init(&ds,1024)==-1) {
    return -1;
  }

  int rlen=0;
  char *head=otf_get_table(otf,OTF_TAG('h','e','a','d'),&rlen);
  if (!head) {
    free(ds.buf);
    return -1;
  }
  dyn_printf(&ds,"%%!PS-TrueTypeFont-%d-%d\n",
                 otf->version,get_ULONG(head+4));
  const int bbxmin=get_SHORT(head+36)*1000/otf->unitsPerEm,
            bbymin=get_SHORT(head+38)*1000/otf->unitsPerEm,
            bbxmax=get_SHORT(head+40)*1000/otf->unitsPerEm,
            bbymax=get_SHORT(head+42)*1000/otf->unitsPerEm;
  free(head);

  char *post=otf_get_table(otf,OTF_TAG('p','o','s','t'),&rlen);
  if ( (!post)&&(rlen!=-1) ) { // other error than "not found"
    free(ds.buf);
    return -1;
  }
  if (post) {
    const unsigned int minMem=get_ULONG(post+16),maxMem=get_ULONG(post+20);
    if (minMem) {
      dyn_printf(&ds,"%%VMusage: %d %d\n",minMem,maxMem);
    }
  }

  // don't forget the coordinate scaling...
  dyn_printf(&ds,"11 dict begin\n"
                 "/FontName /%s def\n"
                 "/FontType 42 def\n"
                 "/FontMatrix [1 0 0 1 0 0] def\n"
                 "/FontBBox [%f %f %f %f] def\n"
                 "/PaintType 0 def\n",
//                 "/XUID [42 16#%X 16#%X 16#%X 16#%X] def\n"  // TODO?!? (md5 of font data)  (16# means base16)
                 emb_otf_get_fontname(otf),
                 bbxmin/1000.0,bbymin/1000.0,bbxmax/1000.0,bbymax/1000.0);
  if (post) {
    dyn_printf(&ds,"/FontInfo 4 dict dup begin\n"
// TODO? [even non-post]: /version|/Notice|/Copyright|/FullName|/FamilyName|/Weight  () readonly def\n   from name table: 5 7 0 4 1 2
// using: otf_get_name(otf,3,1,0x409,?,&len) / otf_get_name(otf,1,0,0,?,&len)   + encoding
                   "  /ItalicAngle %d def\n"
                   "  /isFixedPitch %s def\n"
                   "  /UnderlinePosition %f def\n"
                   "  /UnderlineThickness %f def\n"
                   "end readonly def\n",
                   get_LONG(post+4)>>16,
                   (get_ULONG(post+12)?"true":"false"),
                   (get_SHORT(post+8)-get_SHORT(post+10)/2)/(float)otf->unitsPerEm,
                   get_SHORT(post+10)/(float)otf->unitsPerEm);
  }
  dyn_printf(&ds,"/Encoding 256 array\n"
                 "0 1 255 { 1 index exch /.notdef put } for\n");
  for (iA=0;iA<len;iA++) { // encoding data: 0...255 -> /glyphname
    const int gid=(encoding)?encoding[iA]:otf_from_unicode(otf,iA);
    if (gid!=0) {
      dyn_printf(&ds,"dup %d /%s put\n",
                     iA,get_glyphname(post,to_unicode,iA,gid));
    }
  }
  dyn_printf(&ds,"readonly def\n");

  if (binary) {
    dyn_printf(&ds,"/RD { string currentfile exch readstring pop } executeonly def\n");
  }
  dyn_printf(&ds,"/sfnts[\n");

  if (ds.len<0) {
    free(post);
    free(ds.buf);
    return -1;
  }
  (*output)(ds.buf,ds.len,context);
  ret+=ds.len;
  ds.len=0;

// TODO: only tables as in otf_subset
// TODO:  somehow communicate table boundaries:
  //   otf_action_copy  does exactly one output call (per table)
  //   only otf_action_replace might do two (padding)
  // {{{ copy tables verbatim (does not affect ds .len)
  struct _OTF_WRITE_TABLE *tablesfree=NULL;
#if 0
  struct _OTF_WRITE_TABLE *tables;
  tablesfree=tables=malloc(sizeof(struct _OTF_WRITE_TABLE)*otf->numTables);
  if (!tables) {
    fprintf(stderr,"Bad alloc: %m\n");
    free(post);
    free(ds.buf);
    return -1;
  }
  // just copy everything
  for (iA=0;iA<otf->numTables;iA++) {
    tables[iA].tag=otf->tables[iA].tag;
    tables[iA].action=otf_action_copy;
    tables[iA].copy.otf=otf;
    tables[iA].copy.table_no=iA;
  }
  const int numTables=otf->numTables;
#else
  struct _OTF_WRITE_TABLE tables[]={ // sorted
      {OTF_TAG('c','m','a','p'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('c','v','t',' '),otf_action_copy,.copy={otf,}},
      {OTF_TAG('f','p','g','m'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('g','l','y','f'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('h','e','a','d'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('h','h','e','a'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('h','m','t','x'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('l','o','c','a'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('m','a','x','p'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('n','a','m','e'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('p','r','e','p'),otf_action_copy,.copy={otf,}},
      // vhea vmtx (never used in PDF, but possible in PS>=3011)
      {0,0,}};
  const int numTables=otf_intersect_tables(otf,tables);
#endif

  struct OUTFILTER_PS of;
  of.out=output;
  of.ctx=context;
  of.len=0;
  struct _OTF_WRITE_INFO otw={
    .version=otf->version,
    .numTables=numTables,
    .tables=tables,
    .order=otf_tagorder_win_sort(tables,numTables)
  };
  if (binary) {
    iA=otf_write_sfnt(&otw,NULL,outfilter_binary_ps,&of);
  } else {
    iA=otf_write_sfnt(&otw,NULL,outfilter_ascii_ps,&of);
  }
  free(tablesfree);
  if (iA==-1) {
    free(post);
    free(ds.buf);
    return -1;
  }
  ret+=of.len;
  // }}} done copying

  dyn_printf(&ds,"] def\n");

  dyn_printf(&ds,"/CharStrings %d dict dup begin\n"
                 "/.notdef 0 def\n",len);
  for (iA=0;iA<len;iA++) { // charstrings data: /glyphname -> gid
    const int gid=(encoding)?encoding[iA]:otf_from_unicode(otf,iA);
    if (gid) {
      dyn_printf(&ds,"/%s %d def\n",get_glyphname(post,to_unicode,iA,gid),gid);
    }
    // (respecting subsetting...)
  }
  dyn_printf(&ds,"end readonly def\n");
  dyn_printf(&ds,"FontName currentdict end definefont pop\n");
  free(post);

  if (ds.len<0) {
    free(ds.buf);
    return -1;
  }
  (*output)(ds.buf,ds.len,context);
  ret+=ds.len;
  ds.len=0;

  free(ds.buf);
  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rcx, 0x28(%rsp)
movl %edx, %ebx
cmpl $0x101, %edx            # imm = 0x101
jge 0x5cf5
testl %ebx, %ebx
jle 0x5d11
movq %r9, %r14
movq %r8, %r15
movq %rsi, %r12
movq %rdi, %r13
testq %rsi, %rsi
movq 0x28(%rsp), %rax
cmoveq %rsi, %rax
movq %rax, 0x28(%rsp)
movq %rsp, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x71e0
cmpl $-0x1, %eax
je 0x5cdc
leaq 0x1c(%rsp), %rdx
andl $0x0, (%rdx)
movq %r13, %rdi
movl $0x68656164, %esi       # imm = 0x68656164
callq 0x371c
testq %rax, %rax
je 0x5cd2
movq %rax, %rbp
movq %r14, 0x38(%rsp)
movl 0x14(%r13), %r14d
movq %rax, %rdi
addq $0x4, %rdi
callq 0x528a
leaq 0x57bf(%rip), %rsi      # 0xaddf
movq %rsp, %rdi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x745d
movzbl 0x24(%rbp), %eax
shll $0x8, %eax
movzbl 0x25(%rbp), %ecx
cwtl
orl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movzwl 0x48(%r13), %ecx
cltd
idivl %ecx
movl %eax, 0x10(%rsp)
movzbl 0x26(%rbp), %eax
shll $0x8, %eax
movzbl 0x27(%rbp), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x18(%rsp)
movzbl 0x28(%rbp), %eax
shll $0x8, %eax
movzbl 0x29(%rbp), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x14(%rsp)
movzbl 0x2a(%rbp), %eax
shll $0x8, %eax
movzbl 0x2b(%rbp), %edx
cwtl
orl %edx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltd
idivl %ecx
movl %eax, %r14d
movq %rbp, %rdi
callq 0x21b0
leaq 0x1c(%rsp), %rdx
movq %r13, %rdi
movl $0x706f7374, %esi       # imm = 0x706F7374
callq 0x371c
movq %rax, %rbp
testq %rax, %rax
jne 0x56cc
cmpl $-0x1, 0x1c(%rsp)
jne 0x5cd2
movq %r15, 0x30(%rsp)
movl %ebx, %ebx
testq %rbp, %rbp
movq %r13, 0x20(%rsp)
je 0x57f4
leaq 0x10(%rbp), %rdi
callq 0x528a
testl %eax, %eax
je 0x5710
movl %eax, %r15d
leaq 0x14(%rbp), %rdi
callq 0x528a
leaq 0x56f8(%rip), %rsi      # 0xadf9
movq %rsp, %rdi
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x745d
movq %r13, %rdi
callq 0x4b9a
cvtsi2sdl 0x10(%rsp), %xmm0
movsd 0x52fa(%rip), %xmm4     # 0xaa20
cvtsi2sdl 0x18(%rsp), %xmm1
divsd %xmm4, %xmm0
divsd %xmm4, %xmm1
cvtsi2sdl 0x14(%rsp), %xmm2
divsd %xmm4, %xmm2
cvtsi2sd %r14d, %xmm3
divsd %xmm4, %xmm3
leaq 0x56bd(%rip), %rsi      # 0xae0b
leaq 0x9a1b(%rip), %rdx      # 0xf170
movq %rsp, %r15
movq %r15, %rdi
movb $0x4, %al
callq 0x745d
leaq 0x4(%rbp), %rdi
callq 0x528f
movl %eax, %r14d
sarl $0x10, %r14d
movq %rbp, %rdi
addq $0xc, %rdi
callq 0x528a
testl %eax, %eax
leaq 0x5799(%rip), %rax      # 0xaf20
leaq 0x578d(%rip), %rcx      # 0xaf1b
cmoveq %rax, %rcx
movzbl 0x8(%rbp), %eax
shll $0x8, %eax
movzbl 0x9(%rbp), %edx
movswl %ax, %edi
movzwl 0xa(%rbp), %esi
rolw $0x8, %si
orl %edx, %edi
movl %esi, %eax
cwtd
movw $0x2, %r8w
idivw %r8w
cwtl
subl %eax, %edi
cvtsi2ss %edi, %xmm0
movzwl 0x48(%r13), %eax
cvtsi2ss %eax, %xmm1
movswl %si, %eax
cvtsi2ss %eax, %xmm2
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
divss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm1
leaq 0x56a3(%rip), %rsi      # 0xae88
movq %r15, %rdi
movl %r14d, %edx
movb $0x2, %al
callq 0x745d
jmp 0x5843
movq %r13, %rdi
callq 0x4b9a
cvtsi2sdl 0x10(%rsp), %xmm0
movsd 0x5216(%rip), %xmm4     # 0xaa20
cvtsi2sdl 0x18(%rsp), %xmm1
divsd %xmm4, %xmm0
divsd %xmm4, %xmm1
cvtsi2sdl 0x14(%rsp), %xmm2
divsd %xmm4, %xmm2
cvtsi2sd %r14d, %xmm3
divsd %xmm4, %xmm3
leaq 0x55d9(%rip), %rsi      # 0xae0b
leaq 0x9937(%rip), %rdx      # 0xf170
movq %rsp, %rdi
movb $0x4, %al
callq 0x745d
leaq 0x56dc(%rip), %rsi      # 0xaf26
movq %rsp, %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x745d
leaq 0x5707(%rip), %r13      # 0xaf65
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x58af
testq %r12, %r12
je 0x5872
movzwl (%r12,%r15,2), %eax
jmp 0x587f
movq 0x20(%rsp), %rdi
movl %r15d, %esi
callq 0x42a4
testw %ax, %ax
je 0x58aa
movzwl %ax, %ecx
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
movl %r15d, %edx
callq 0x5d47
movq %r14, %rdi
movq %r13, %rsi
movl %r15d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x745d
incq %r15
jmp 0x5861
leaq 0x5657(%rip), %rsi      # 0xaf0d
movq %rsp, %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x745d
leaq 0x56ab(%rip), %rsi      # 0xaf75
movq %r14, %rdi
xorl %eax, %eax
callq 0x745d
movl (%r14), %esi
testl %esi, %esi
js 0x5cca
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rdx
callq *0x30(%rsp)
movl (%rsp), %eax
movl %eax, 0x10(%rsp)
andl $0x0, (%rsp)
leaq 0x70(%rsp), %r14
movq $0x636d6170, (%r14)     # imm = 0x636D6170
leaq 0x2ec2(%rip), %rax      # 0x87cd
movq %rax, 0x8(%r14)
movq 0x20(%rsp), %r13
movq %r13, 0x10(%r14)
andl $0x0, 0x18(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x40(%r14)
movq $0x63767420, 0x50(%r14) # imm = 0x63767420
movq %rax, 0x58(%r14)
movq %r13, 0x60(%r14)
andl $0x0, 0x68(%r14)
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x80(%r14)
movaps %xmm0, 0x90(%r14)
movq $0x6670676d, 0xa0(%r14) # imm = 0x6670676D
movq %rax, 0xa8(%r14)
movq %r13, 0xb0(%r14)
andl $0x0, 0xb8(%r14)
movaps %xmm0, 0xc0(%r14)
movaps %xmm0, 0xd0(%r14)
movaps %xmm0, 0xe0(%r14)
movq $0x676c7966, 0xf0(%r14) # imm = 0x676C7966
movq %rax, 0xf8(%r14)
movq %r13, 0x100(%r14)
andl $0x0, 0x108(%r14)
movaps %xmm0, 0x110(%r14)
movaps %xmm0, 0x120(%r14)
movaps %xmm0, 0x130(%r14)
movq $0x68656164, 0x140(%r14) # imm = 0x68656164
movq %rax, 0x148(%r14)
movq %r13, 0x150(%r14)
andl $0x0, 0x158(%r14)
movaps %xmm0, 0x160(%r14)
movaps %xmm0, 0x170(%r14)
movaps %xmm0, 0x180(%r14)
movq $0x68686561, 0x190(%r14) # imm = 0x68686561
movq %rax, 0x198(%r14)
movq %r13, 0x1a0(%r14)
andl $0x0, 0x1a8(%r14)
movaps %xmm0, 0x1d0(%r14)
movaps %xmm0, 0x1c0(%r14)
movaps %xmm0, 0x1b0(%r14)
movq $0x686d7478, 0x1e0(%r14) # imm = 0x686D7478
movq %rax, 0x1e8(%r14)
movq %r13, 0x1f0(%r14)
andl $0x0, 0x1f8(%r14)
movaps %xmm0, 0x200(%r14)
movaps %xmm0, 0x210(%r14)
movaps %xmm0, 0x220(%r14)
movq $0x6c6f6361, 0x230(%r14) # imm = 0x6C6F6361
movq %rax, 0x238(%r14)
movq %r13, 0x240(%r14)
andl $0x0, 0x248(%r14)
movaps %xmm0, 0x250(%r14)
movaps %xmm0, 0x260(%r14)
movaps %xmm0, 0x270(%r14)
movq $0x6d617870, 0x280(%r14) # imm = 0x6D617870
movq %rax, 0x288(%r14)
movq %r13, 0x290(%r14)
andl $0x0, 0x298(%r14)
movaps %xmm0, 0x2c0(%r14)
movaps %xmm0, 0x2b0(%r14)
movaps %xmm0, 0x2a0(%r14)
movq $0x6e616d65, 0x2d0(%r14) # imm = 0x6E616D65
movq %rax, 0x2d8(%r14)
movq %r13, 0x2e0(%r14)
andl $0x0, 0x2e8(%r14)
movaps %xmm0, 0x310(%r14)
movaps %xmm0, 0x300(%r14)
movaps %xmm0, 0x2f0(%r14)
movq $0x70726570, 0x320(%r14) # imm = 0x70726570
movq %rax, 0x328(%r14)
movq %r13, 0x330(%r14)
andl $0x0, 0x338(%r14)
movaps %xmm0, 0x3b0(%r14)
movaps %xmm0, 0x3a0(%r14)
movaps %xmm0, 0x390(%r14)
movaps %xmm0, 0x380(%r14)
movaps %xmm0, 0x370(%r14)
movaps %xmm0, 0x360(%r14)
movaps %xmm0, 0x350(%r14)
movaps %xmm0, 0x340(%r14)
movq %r13, %rdi
movq %r14, %rsi
callq 0x7860
leaq 0x40(%rsp), %r15
movq 0x30(%rsp), %rcx
movq %rcx, (%r15)
movq 0x38(%rsp), %rcx
movq %rcx, 0x8(%r15)
andl $0x0, 0x10(%r15)
movl 0x14(%r13), %ecx
leaq 0x58(%rsp), %r13
movl %ecx, (%r13)
movw %ax, 0x4(%r13)
movq %r14, 0x8(%r13)
movq %r14, %rdi
movl %eax, %esi
callq 0x85e8
movq %rax, 0x10(%r13)
leaq 0x270(%rip), %rdx       # 0x5e43
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rcx
callq 0x894c
cmpl $-0x1, %eax
je 0x5d2f
movl 0x50(%rsp), %r14d
leaq 0x5389(%rip), %rsi      # 0xaf7e
movq %rsp, %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x745d
leaq 0x537c(%rip), %rsi      # 0xaf85
movq %r15, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x745d
xorl %r15d, %r15d
movq 0x20(%rsp), %r13
cmpq %r15, %rbx
je 0x5c76
testq %r12, %r12
je 0x5c2e
movzwl (%r12,%r15,2), %eax
jmp 0x5c39
movq %r13, %rdi
movl %r15d, %esi
callq 0x42a4
testw %ax, %ax
je 0x5c71
movzwl %ax, %r13d
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
movl %r15d, %edx
movl %r13d, %ecx
callq 0x5d47
movq %rsp, %rdi
leaq 0x5355(%rip), %rsi      # 0xafb4
movq %rax, %rdx
movl %r13d, %ecx
movq 0x20(%rsp), %r13
xorl %eax, %eax
callq 0x745d
incq %r15
jmp 0x5c1d
leaq 0x528c(%rip), %rsi      # 0xaf09
movq %rsp, %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x745d
leaq 0x532f(%rip), %rsi      # 0xafc0
movq %rbx, %rdi
xorl %eax, %eax
callq 0x745d
movq %rbp, %rdi
callq 0x21b0
movl (%rbx), %esi
testl %esi, %esi
js 0x5d37
addl 0x10(%rsp), %r14d
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rdx
callq *0x30(%rsp)
addl (%rsp), %r14d
andl $0x0, (%rsp)
jmp 0x5d3b
movq %rbp, %rdi
callq 0x21b0
movq 0x8(%rsp), %rdi
callq 0x21b0
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x92e4(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x5093(%rip), %rsi      # 0xad99
movl %ebx, %edx
xorl %eax, %eax
callq 0x21e0
jmp 0x5cdc
movq 0x92c8(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x5098(%rip), %rdi      # 0xadba
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x5cdc
movq %rbp, %rdi
callq 0x21b0
pushq $-0x1
popq %r14
movq 0x8(%rsp), %rdi
callq 0x21b0
jmp 0x5ce0