#!/usr/bin/env fontforge

/* process arguments */

output = ""
source = "taphac.ttf"
base = ""
map = ""
fontname = ""
family = ""
fullname = ""
weight = ""
copyrightnotice = ""
fontversion = ""
verbose = 1

i = 1
while(i < $argc)
  if($argv[i] == "--output" || $argv[i] == "-o")
    i++
    output = $argv[i]
  elseif($argv[i] == "--source" || $argv[i] == "-s")
    i++
    source = $argv[i]
  elseif($argv[i] == "--base" || $argv[i] == "-b")
    i++
    base = $argv[i]
  elseif($argv[i] == "--map" || $argv[i] == "-m")
    i++
    map = $argv[i]
  elseif($argv[i] == "--fontname" || $argv[i] == "-n")
    i++
    fontname = $argv[i]
  elseif($argv[i] == "--family" || $argv[i] == "-f")
    i++
    family = $argv[i]
  elseif($argv[i] == "--fullname")
    i++
    fullname = $argv[i]
  elseif($argv[i] == "--weight")
    i++
    weight = $argv[i]
  elseif($argv[i] == "--copyright")
    i++
    copyrightnotice = $argv[i]
  elseif($argv[i] == "--fontver")
    i++
    fontversion = $argv[i]
  elseif($argv[i] == "--quiet" || $argv[i] == "-q")
    verbose = 0
  elseif($argv[i] == "--help" || $argv[i] == "-h")
    Print("usage: ", $argv[0], " -m MAPFILE ...")
    Print("options:")
    Print("  -m, --map MAPFILE    specify mapping file to use")
    Print("  -s, --source SOURCE  TTF file containing hacm glyphs (default taphac.ttf)")
    Print("  -b, --base BASEFONT  TTF font to which hacm charcters are added (optional)")
    Print("  -o, --output OUTPUT  specify name of output file (default BASEFONT-hacm.ttf or out.ttf)")
    Print("  -n, --fontname TEXT  set TTF font name")
    Print("  -f, --family TEXT    set TTF font family")
    Print("      --fullname TEXT  set TTF font fullname")
    Print("      --weight TEXT    set TTF font weight")
    Print("      --copyright TEXT set TTF copyright notice")
    Print("      --fontver TEXT   set TTF font version")
    Print("  -q, --quiet          suppress progress display")
    Print("  -h, --help           display this help and exit")
    return
  else
    Print("Error: bad argument: ", $argv[i])
    return
  endif
  i++
endloop

if(output == "")
  if(base == "")
    output = "out.ttf"
  else
    output = base:r + "-hacm.ttf"
  endif
endif

if(map == "")
  Print("Error: mapfile not specified")
  Print("use --help for usage information")
  return
endif

if(verbose)
  Print("")
endif

/* load mapping table */

if(FileAccess(map) < 0)
  Print("Error: cannot access ", map)
  return
endif

mapstr = Ord(LoadStringFromFile(map))
table = Array(1000)
i = 0
index = 0
hash = Ord('#', 0)
newline = 0x0a
tab = 0x09


while(i < SizeOf(mapstr))
  /* skip comment and empty line */
  if(mapstr[i] == hash || mapstr[i] == newline)
    while(i < SizeOf(mapstr) && mapstr[i] != newline)
      i++
    endloop
    if(i < SizeOf(mapstr) && mapstr[i] == newline)
      i++
    endif
  else
    field = 0
    table[index] = Array(4)
    while(mapstr[i] != newline)
      table[index][field] = ""
      while(mapstr[i] != tab && mapstr[i] != newline)
        table[index][field] += Chr(mapstr[i])
        i++
      endloop
      if(mapstr[i] == tab)
        i++
      endif
      field++
    endloop
    index++
    i++
  endif
endloop

table_size = index

/* prepare for copying */

if(verbose)
  Print("opening fonts...")
endif


if(base == "")
  Open(source)
  bitmap = $bitmaps
  
  /* workaround a bug in fontforge */
  workaround_escape = 0u0060
  Select(workaround_escape)
  Copy()

  New()

  Select(workaround_escape)
  Paste()

  BitmapsAvail(bitmap)
  ScaleToEm(1024)
  Generate(output, "ttf")
  dest = output
else
  dest = base
endif

Open(source)
sourceem = $em

Open(dest)
Reencode("unicodefull")
destem = $em

ratio = (0.0 + destem) / sourceem

/* copy glyphs */

i = 0
while(i < table_size)
  from = table[i][2]
  to = table[i][0]
  if(from != "" && to != "")
    if(verbose)
      progress = 10 * i / table_size
      prev_progress = 10 * (i-1) / table_size
      if(progress != prev_progress)
        Print(Chr(0x0d), "copying glyphs ...", 10 * progress, "%")
      endif
    endif

    fromucp = UCodePoint(Strtol(from, 16))
    toucp = UCodePoint(Strtol(to, 16))

    Open(source)
    Select(fromucp)
    srcwidth = GlyphInfo("Width")
    Copy()
    Open(dest)
    Select(toucp)
    Paste()
    Scale(ratio * 100, 0, 0)
    SetWidth(Floor(srcwidth * ratio), 0)
  endif
  i++
endloop

/* save output */

if(verbose)
  Print("saving to ", output, "...")
endif

Open(dest)
if(base == "")
  Select(workaround_escape)
  Cut()
endif
SetFontNames(fontname, family, fullname, weight, copyrightnotice, fontversion)
Generate(output, "ttf")

if(verbose)
  Print("done!")
endif
