#!/bin/sh
#
# This file is part of the CJK macro package ver. 4.1.3 (20-Jun-1997).
#
# MakeTTFPK, originally written by Y. R. Li
#
# $1: output name of tfm font (ttf2pk adds ".tfm" if extension is missing)
# $2: output name of pk font (ttf2pk adds ".pk" if extension is missing)
# $3: subfont number
# $4: parameter from ttf2pk.cfg (usually encoding and TrueType font with
#     full path)
# $5: resolution
#

if test $# -ne 5; then
  echo \
  "Usage: $0 tfm-name pk-name subfont-number ttf2pk-parameter resolution." >&2
  exit 1
fi


# we filter out the first letter of the -e parameter's value.
enc=`echo "$4" | sed -e 's/^.*[ 	]-e[ 	]*\(.\).*$/\1/'`

# Big 5 encoding (the default)
if test -z "$enc" || test "$enc" = "B" || test "$enc" = "b"; then
  sb1=`expr \( $3 - 1 \) \* 256 / 157 + 161`
  sb2=`expr \( $3 - 1 \) \* 256 % 157`

  if test $sb2 -lt 63; then
    sb2=`expr $sb2 + 64`
  else
    sb2=`expr $sb2 + 98`
  fi

# EUC encoding
elif test "$enc" = "E" || test "$enc" = "X" || \
     test "$enc" = "e" || test "$enc" = "x"; then
  sb1=`expr \( $3 - 1 \) \* 256 / 94 + 161`
  sb2=`expr \( $3 - 1 \) \* 256 % 94 + 161`

# Unicode encoding
elif test "$enc" = "U" || test "$enc" = "u"; then
  # we let the shell read in the subfile number as a hexadecimal digit.
  # if your shell does not support this, you need an external program.
  sb1=$[0x$3]
  sb2=0

# SJIS encoding
else
  sb1=`expr \( $3 - 1 \) \* 256 / 189`
  sb2=`expr \( $3 - 1 \) \* 256 % 189`

  if test $sb1 -lt 31; then
    sb1=`expr $sb1 + 129`
  else
    sb1=`expr $sb1 + 193`
  fi
  if test $sb2 -lt 63; then
    sb2=`expr $sb2 + 64`
  else
    sb2=`expr $sb2 + 65`
  fi
fi

bb=`expr $sb1 \* 256 + $sb2`

cmd="ttf2pk $2 $1 $5 1.0 $bb 256 $4"
echo $cmd
$cmd

if test $? -eq 0; then
  exit 0
else
  exit 1
fi
