#!/bin/sh
#
# Converts from a pair of Postscript font files (file.pfb and
# file.afm) 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.
#
# Note that the pfb file is a Postscript font file, not to be confused
# with a Performer scene graph file (which also used the .pfb
# extension).  This script has nothing to do with Performer.
#
# Both a pfb file and an afm file ("Adobe Font Metrics") must be
# supplied.  Typically, Postscript fonts are shipped to Windows with a
# pfm file ("Printer Font Metrics") instead of an afm file.  If this
# is the case, you must use a utility like CrossFont to convert the
# pfm to afm format.  (Look to http://www.asy.com/ for CrossFont, or
# see http://www.sketchpad.net/fonts1.htm for a treatise on the
# subject.)
#
# This script takes advantage of afm2tfm and ps2pk, which I believe
# are typically shipped with TeX.
#
#
# Usage:
# 
#   pfb2egg [opts] file.pfb 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.
#
# The afm file is assumed to be the same name as the pfb file, with
# pfb replaced by afm.
#
# 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.
#
#   -p pointsize
#       Specify the pointsize at which to convert the font.  This doesn't
#       make a whole lot of difference to its appearance other than
#       scaling the dpi value, above, unless the font has a different
#       appearance at smaller point sizes (some fonts include hints to
#       change the way they are rendered at small point sizes).  The
#       default is 10.
#
# See egg-mkfont -h for a description of the additional options.
#
#ENDCOMMENT

showhelp=
dpi=720
pointsize=10
while getopts "d:p:h" flag; do
  case $flag in
    d) dpi="$OPTARG";;
    p) pointsize="$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`

pfb="$1"
afm=`basename "$1" .pfb`.afm
egg=$2

# Perform some sanity checks on input parameters.

if [ -f "$pfb" ]; then
  basepfb=`basename "$pfb" .pfb`
  basePFB=`basename "$pfb" .PFB`
  base=`basename "$pfb"`
  if [ "$basepfb" = "$base" -a "$basePFB" = "$base" ]; then
    echo "$pfb should end in .pfb or .PFB"
    exit 1
  fi
else
  echo "$pfb does not exist."
  exit 1
fi

if [ ! -f "$afm" ]; then
  echo "$afm 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 PFB and AFM filename in case the source filename
# has bad characters (like spaces).
temppfb=$fontname.pfb
tempafm=$fontname.afm
if [ $temppfb != "$pfb" ]; then
  echo ln -s \"$pfb\" $temppfb
  if ln -s "$pfb" $temppfb; then
    status=0
  else
    echo Cannot create $temppfb
    exit 1
  fi
  echo ln -s \"$afm\" $tempafm
  if ln -s "$afm" $tempafm; then
    status=0
  else
    echo Cannot create $tempafm
    exit 1
  fi
fi

status=0
echo afm2tfm $tempafm
if afm2tfm $tempafm; then
  echo ps2pk -X$dpi -P$pointsize \"$fontname\"
  if ps2pk -X$dpi -P$pointsize "$fontname" >/dev/null; then
    echo egg-mkfont $* -o \"$egg\" \"$fontname$pointsize.${dpi}pk\"
    egg-mkfont $* -o "$egg" "$fontname$pointsize.${dpi}pk"
    status=$?
  else
    echo Error running ps2pk.
    status=1
  fi
else
  echo Error running afm2tfm.
  status=1
fi

rm -f "$fontname.tfm" "$fontname$pointsize.${dpi}pk"
if [ $temppfb != "$pfb" ]; then
  rm -f $temppfb
fi
if [ $tempafm != "$afm" ]; then
  rm -f $tempafm
fi

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

exit $status
