// File: /workspace/github_c_makefile_star2_O2/smilingthax[P]libfontembed/embed.c
// Function: emb_new
EMB_PARAMS *emb_new(FONTFILE *font,EMB_DESTINATION dest,EMB_CONSTRAINTS mode) // {{{
{
  assert(font);

  EMB_PARAMS *ret=calloc(1,sizeof(EMB_PARAMS));
  if (!ret) {
    fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
    if (mode&EMB_C_TAKE_FONTFILE) {
      fontfile_close(font);
    }
    return NULL;
  }
  ret->dest=dest;
  ret->font=font;
  if (mode&EMB_C_TAKE_FONTFILE) {
    ret->plan|=EMB_A_CLOSE_FONTFILE;
  }

  // check parameters
  if ( (mode&EMB_C_KEEP_T1)&&(mode&EMB_C_FORCE_MULTIBYTE) ) {
    fprintf(stderr,"Incompatible mode: KEEP_T1 and FORCE_MULTIBYTE\n");
    emb_close(ret);
    return NULL;
  }
  if ((mode&0x07)>5) {
    fprintf(stderr,"Bad subset specification\n");
    emb_close(ret);
    return NULL;
  }

  // determine intype
  int numGlyphs=0;
  if (font->sfnt) {
    if (font->sfnt->flags&OTF_F_FMT_CFF) {
      ret->intype=EMB_FMT_OTF;
    } else {
      ret->intype=EMB_FMT_TTF;
    }
    ret->rights=emb_otf_get_rights(ret->font->sfnt);
    numGlyphs=ret->font->sfnt->numGlyphs; // TODO
  } else if (font->stdname) {
    ret->intype=EMB_FMT_STDFONT;
    ret->rights=EMB_RIGHT_NONE;
  } else {
    assert(0);
  }
/*
  if ( (ret->intype==EMB_FMT_CFF)&&
       (ret->cffFont.is_cid()) ) {
     ?= || ( (ret->intype==EMB_FMT_OTF)&&(ret->sfnt->cffFont.is_cid()) ) // TODO?
    ret->plan|=EMB_A_MULTIBYTE;
  }
*/

  // determine outtype
  if (ret->intype==EMB_FMT_STDFONT) {
    ret->outtype=ret->intype;
    if (mode&EMB_C_FORCE_MULTIBYTE) {
      fprintf(stderr,"Multibyte stdfonts are not possible\n");
      emb_close(ret);
      return NULL;
    }
    return ret; // never subset, no multibyte
  } else if (ret->intype==EMB_FMT_T1) {
    if (mode&EMB_C_KEEP_T1) {
      ret->outtype=EMB_FMT_T1;
    } else {
      ret->plan|=EMB_A_T1_TO_CFF;
      ret->outtype=EMB_FMT_CFF;
    }
  } else {
    ret->outtype=ret->intype;
  }
  if (ret->outtype==EMB_FMT_CFF) {
    if (mode&EMB_C_PDF_OT) {
      ret->outtype=EMB_FMT_OTF;
      ret->plan|=EMB_A_CFF_TO_OTF;
    }
  } else if (ret->outtype==EMB_FMT_OTF) {
    // TODO: no support yet;  but we want to get the FontDescriptor/Name right
    mode|=EMB_C_NEVER_SUBSET;
    if (!(mode&EMB_C_PDF_OT)) { // TODO!?!
      ret->outtype=EMB_FMT_CFF;
      ret->plan|=EMB_A_OTF_TO_CFF;
    }
  }

  if (mode&EMB_C_FORCE_MULTIBYTE) {
    ret->plan|=EMB_A_MULTIBYTE;
  }

  // check rights (for subsetting)
  if (  (ret->rights&EMB_RIGHT_NONE)||
        (ret->rights&EMB_RIGHT_BITMAPONLY)||
        ( (ret->rights&EMB_RIGHT_READONLY)&&(mode&EMB_C_EDITABLE_SUBSET) )||
        ( (ret->rights&EMB_RIGHT_NO_SUBSET)&&(mode&EMB_C_MUST_SUBSET) )  ) {
    fprintf(stderr,"The font does not permit the requested embedding\n");
    emb_close(ret);
    return NULL;
  } else if ( (!(ret->rights&EMB_RIGHT_NO_SUBSET))&&
              (!(mode&EMB_C_NEVER_SUBSET)) ) {
    ret->plan|=EMB_A_SUBSET;
  }

  // alloc subset
  if (ret->plan&EMB_A_SUBSET) {
    ret->subset=bitset_new(numGlyphs);
    if (!ret->subset) {
      fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
      emb_close(ret);
      return NULL;
    }
  }

  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x46e8
movl %edx, %ebp
movl %esi, %r15d
movq %rdi, %r14
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x2100
testq %rax, %rax
je 0x459a
movq %rax, %rbx
movl %r15d, 0x8(%rax)
movq %r14, 0x10(%rax)
testw %bp, %bp
jns 0x44d3
movl $0x8000, 0xc(%rbx)      # imm = 0x8000
movl %ebp, %eax
notl %eax
testb $0x28, %al
je 0x469f
testb $0x6, %al
je 0x46b4
movq (%r14), %rdi
testq %rdi, %rdi
je 0x452f
xorl %eax, %eax
btl $0x10, 0x8(%rdi)
adcl $0x1, %eax
movl %eax, (%rbx)
callq 0x4aa4
movl %eax, 0x18(%rbx)
movq 0x10(%rbx), %rcx
movq (%rcx), %rcx
movzwl 0x4c(%rcx), %esi
movl (%rbx), %ecx
testl %ecx, %ecx
je 0x4570
cmpl $0x4, %ecx
je 0x4547
movl %ecx, 0x4(%rbx)
cmpl $0x2, %ecx
je 0x45dc
cmpl $0x3, %ecx
je 0x4581
jmp 0x45f4
cmpq $0x0, 0x8(%r14)
je 0x4701
movl $0x4, (%rbx)
movl $0x2, 0x18(%rbx)
movl $0x4, 0x4(%rbx)
testb $0x8, %bpl
je 0x46da
movq 0xaa81(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x63bf(%rip), %rdi      # 0xa928
pushq $0x24
jmp 0x46c7
testb $0x20, %bpl
jne 0x4594
orb $0x4, 0xc(%rbx)
movl $0x3, 0x4(%rbx)
testb $0x10, %bpl
je 0x45f4
movl $0x2, 0x4(%rbx)
orb $0x8, 0xc(%rbx)
jmp 0x45f4
andl $0x0, 0x4(%rbx)
jmp 0x45f4
movq 0xaa3f(%rip), %rax      # 0xefe0
movq (%rax), %r15
callq 0x2040
movl (%rax), %edi
callq 0x2070
leaq 0x5da2(%rip), %rsi      # 0xa359
xorl %ebx, %ebx
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21e0
testw %bp, %bp
jns 0x46da
movq %r14, %rdi
callq 0x7568
jmp 0x46d8
movl %ebp, %ecx
orl $0x4, %ecx
testb $0x10, %bpl
jne 0x45f2
movl $0x3, 0x4(%rbx)
orb $0x10, 0xc(%rbx)
movl %ecx, %ebp
testb $0x8, %bpl
je 0x45fe
orb $0x1, 0xc(%rbx)
testw $0x202, %ax             # imm = 0x202
jne 0x468a
movl %eax, %ecx
andl $0x4, %ecx
shrl $0x2, %ecx
movl %ebp, %edx
shrb %dl
testb %cl, %dl
jne 0x468a
andl $0x100, %eax            # imm = 0x100
sete %cl
testb $0x1, %bpl
sete %dl
orb %cl, %dl
je 0x468a
andl $0x4, %ebp
orl %ebp, %eax
movl 0xc(%rbx), %eax
je 0x463e
testb $0x2, %al
jne 0x4644
jmp 0x46da
orl $0x2, %eax
movl %eax, 0xc(%rbx)
addl $0x1f, %esi
shrl $0x3, %esi
andl $-0x4, %esi
pushq $0x1
popq %rdi
callq 0x2100
movq %rax, 0x20(%rbx)
testq %rax, %rax
jne 0x46da
movq 0xa97b(%rip), %rax      # 0xefe0
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
callq 0x2070
leaq 0x5cde(%rip), %rsi      # 0xa359
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x46d0
movq 0xa94f(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x62b2(%rip), %rdi      # 0xa94d
pushq $0x31
jmp 0x46c7
movq 0xa93a(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x622e(%rip), %rdi      # 0xa8de
pushq $0x2f
jmp 0x46c7
movq 0xa925(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x6249(%rip), %rdi      # 0xa90e
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq %rbx, %rdi
callq 0x471e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x61a0(%rip), %rdi      # 0xa88f
leaq 0x619e(%rip), %rsi      # 0xa894
leaq 0x619f(%rip), %rcx      # 0xa89c
pushq $0x48
jmp 0x4718
leaq 0x65b8(%rip), %rdi      # 0xacc0
leaq 0x6185(%rip), %rsi      # 0xa894
leaq 0x6186(%rip), %rcx      # 0xa89c
pushq $0x72
popq %rdx
callq 0x20f0