#!/bin/sh
#
# Converts from a TTF font file to an egg file and associated texture map
# that is suitable for using with the TextNode class in Panda to render
# text in the world.
#
# This script takes advantage of ttf2tfm and ttf2pk, two programs that
# can be found in the freetype project--see www.freetype.org.  Look
# in the contrib subdirectory of freetype-1.3.1; it doesn't seem to exist
# yet in freetype-2.0.
#
#
# Usage:
# 
#   ttf2egg [opts] file.ttf file.egg -- [mkfont-opts]
#
# Options that control the behavior of this script must appear first.
# The extra options options following the first two parameters are passed
# unchanged to egg-mkfont.
#
# Local options:
#
#   -d dpi
#       Specify the resolution at which to rasterize the font before
#       converting it.  This affects the size of the generated texture
#       image and, in turn, the clarity of the resulting font.  The
#       default is 720, which generally seems to be a good ratio.
#
#   -T file.enc
#       Specify the name and/or location of the encoding file to use
#       to convert the glyphs from the TTF font.  A couple of suitable
#       files are provided with ttf2tfm; most of the time, you will
#       want to use T1-WGL4.enc.
#
# See egg-mkfont -h for a description of the additional options.
#
#ENDCOMMENT

showhelp=
dpi=720
enc=$PANDATOOL/shared/T1-WGL4.enc
while getopts "d:T:h" flag; do
  case $flag in
    d) dpi="$OPTARG";;
    T) enc="$OPTARG";;
    h) showhelp=y;;
    \?) exit 1;
  esac
done

if [ -z "$1" -o -z "$2" -o "$showhelp" ]; then
  sed '/#ENDCOMMENT/,$d' <$0 >&2
  exit 1
fi

shift `expr $OPTIND - 1`

ttf="$1"
egg=$2

# Perform some sanity checks on input parameters.

if [ -f "$ttf" ]; then
  basettf=`basename "$ttf" .ttf`
  baseTTF=`basename "$ttf" .TTF`
  base=`basename "$ttf"`
  if [ "$basettf" = "$base" -a "$baseTTF" = "$base" ]; then
    echo "$ttf should end in .ttf or .TTF"
    exit 1
  fi
else
  echo "$ttf does not exist."
  exit 1
fi

if [ `basename $egg .egg` = `basename $egg` ]; then
  echo "$egg should end in .egg"
  exit 1
fi

fontname=`basename $egg .egg`
shift 2

if [ "x$1" = "x--" ]; then
  shift
fi

# Create a temporary TTF filename in case the source filename has bad
# characters (like spaces).
tempttf=$fontname.ttf
if [ $tempttf != "$ttf" ]; then
  echo ln -s \"$ttf\" $tempttf
  if ln -s "$ttf" $tempttf; then
    status=0
  else
    echo Cannot create $tempttf
    exit 1
  fi
fi

status=0
rm -f ttfonts1.map
echo ttf2tfm $tempttf -q -u -T \"$enc\" \"$fontname.tfm\"
if ttf2tfm $tempttf -q -u -T "$enc" "$fontname.tfm" > ttfonts1.map 2>/dev/null; then
  rm -f ttfonts.map
  tail -1 ttfonts1.map >ttfonts.map
  echo ttf2pk -q \"$fontname\" $dpi
  if ttf2pk -q "$fontname" $dpi >/dev/null 2>/dev/null; then
    echo egg-mkfont $* -o \"$egg\" \"$fontname.${dpi}pk\"
    egg-mkfont $* -o "$egg" "$fontname.${dpi}pk"
    status=$?
  else
    echo Error running ttf2pk.
    status=1
  fi
else
  echo Error running ttf2tfm.
  status=1
fi

rm -f ttfonts1.map ttfonts.map "$fontname.tfm" "$fontname.${dpi}pk"
if [ $tempttf != "$ttf" ]; then
  rm -f $tempttf
fi

if [ $status != 0 ]; then
  echo ""
  echo "***************************"
  echo "*** Cannot convert $ttf ***"
  echo "***************************"
  echo ""
fi

exit $status
