#!/bin/sh
#
#dco v1.3: A package for using oldstyle numerals with the dc fonts
#
#For use with v1.3pl2 of the dc fonts (dc fonts by J"org Knappen
#<knappen@vkpmzd.kph.uni-mainz.de>, CTAN:/fonts/dc)
#
#Copyright (C) 1996 Sebastian Marius Kirsch <sebastian_kirsch@kl.maus.de>
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#This Bourne shell script requires the following programs:
# - tftopl, vptovf
# - ed, sed
# - TeX, fontinst >= 1.335
# - a shell which supports here-documents (ie. bash)
#

size="0500 0600 0700 0800 0900 1000 1095 1200 1440 1728 2074 2488 2986 3583"
family="r b bx sl bl ti bi u ss si sx so tt st it vt vi dh"
tfmpath="/usr/lib/texmf/fonts/dc-1.3/tfm"
logfile=./dco_fonts.log

rofamily="r b bx sl bl ti bi u"
rfamily="m/sc bx/sc"
# which indentifiers give to this one? sc=m/slc, oc=bx/slc?

ttofamily="tt st it"
ttfamily="m/sc"

ssofamily="ss si sx so"

vtofamily="vt vi"

dhofamily="dh"

function getpl () {
curfam=$1

echo "# Converting .tfm to .pl" >> $logfile

for f in ${curfam}; do 
    for s in ${size}; do 
 	if [ -f	${tfmpath}/dc${f}${s}.tfm ] ; then 
 	   tftopl ${tfmpath}/dc${f}${s}.tfm ./dc${f}${s}.pl;
 	   echo "dc${f}${s}.tfm -> dc${f}${s}.pl" >> ${logfile}
	   echo -n "dc${f}${s}.pl "
 	else
 	   echo "! tfm file dc${f}${s}.tfm not found." >> ${logfile} 
	   echo "! dc${f}${s}.tfm not found."
 	fi
 	if [ -f	${tfmpath}/tc${f}${s}.tfm ] ; then 
 	   tftopl ${tfmpath}/tc${f}${s}.tfm ./tc${f}${s}.pl;
 	   echo "tc${f}${s}.tfm -> tc${f}${s}.pl" >> ${logfile}
	   echo -n "tc${f}${s}.pl "
 	else
 	   echo "! tfm file tc${f}${s}.tfm not found" >> ${logfile}
	   echo "! tc${f}${s}.tfm not found."
 	fi
    done
done

echo
}

function getvf () {
curfam=$1

echo "# Converting .vpl to .vf and .tfm" >> $logfile

for f in $curfam; do 
    for s in $size; do 
	if [ -f	dco${f}${s}.vpl ]; then 
	   vptovf dco${f}${s}.vpl ../vf/dco${f}${s}.vf ../tfm/dco${f}${s}.tfm;
	   echo "dco${f}${s}.vpl -> dco${f}${s}.vf" >> ${logfile}
	   echo -n "dco${f}${s}.vf "
	else
	   echo "vpl file dco${f}${s}.vpl not found" >> ${logfile}
	   echo "! dco${f}${s}.vpl not found."
	fi
    done
done

echo
}

function cleanup () {
curfam=$1

for f in $curfam; do 
    for s in $size; do 
	rm -f dco${f}${s}.vpl dc${f}${s}.pl dc${f}${s}.mtx tc${f}${s}.pl tc${f}${s}.mtx
    done
done
}

function intro () {
cat << EOM
\input fontinst.sty

\declareencoding{TEX TEXT COMPANION SYMBOLS 1---TS1}{TS1}

\installfonts
EOM
}

function extro () {
cat << EOM
\endinstallfonts

\bye
EOM
}

function echoinstalls () {
enc=$1; f=$2; se=$3; sh=$4

echo
for dc in ${size}; do
      if [ ${dc} = "0500" ]; then fs="5";
             elif [ ${dc} = "0600" ]; then fs="6";
	     elif [ ${dc} = "0700" ]; then fs="7";
	     elif [ ${dc} = "0800" ]; then fs="8";
	     elif [ ${dc} = "0900" ]; then fs="9";
	     elif [ ${dc} = "1000" ]; then fs="10";
	     elif [ ${dc} = "1095" ]; then fs="10.95";
	     elif [ ${dc} = "1200" ]; then fs="12";
	     elif [ ${dc} = "1440" ]; then fs="14.4";
	     elif [ ${dc} = "1728" ]; then fs="17.28";
	     elif [ ${dc} = "2074" ]; then fs="20.74";
	     elif [ ${dc} = "2488" ]; then fs="24.88";
	     elif [ ${dc} = "2986" ]; then fs="29.86";
	     elif [ ${dc} = "3583" ]; then fs="35.83";
      fi
	if [ -f dc${f}${dc}.pl -a -f tc${f}${dc}.pl ]; then
	  echo "\installfont{dco${f}${dc}}{dc${f}${dc},tc${f}${dc}}{T19}{T1}{cmo${enc}}{${se}}{${sh}}{<${fs}>}"
	fi
  done;
}

# Roman family
#
# I had to split up the roman family into two TeX-files because my TeX
# isn't big enough for this. That's also why I have to save T1cmor.fd after
# the first run and paste it together after the second. :-(

echo >> $logfile
echo "Roman Family" >> $logfile
echo "------------" >> $logfile
echo >> $logfile

getpl "$rofamily"

(
intro

echo; echo "\installfamily{T1}{cmor}{\hyphenchar\font='177 }"

echoinstalls r r m n
echoinstalls r b b n
echoinstalls r bx bx n
echoinstalls r bl bx sl

extro
) > dcor.tex

tex dcor.tex

mv T1cmor.fd _T1cmor.fd

(
intro

echo; echo "\installfamily{T1}{cmor}{\hyphenchar\font='177 }"

echoinstalls r sl m sl
echoinstalls r ti m it
echoinstalls r bi bx it
echoinstalls r u m ui

extro
) > dcori.tex

tex dcori.tex

getvf "$rofamily"

cleanup "$rofamily"

ed _T1cmor.fd <<EOS
\$-1,\$d
w
q
EOS

ed T1cmor.fd <<EOS
1,10d
\$-1,\$d
1r _T1cmor.fd
w
q
EOS

for f in $rfamily; do
	se=`echo $f | cut -f 1 -d "/"`
	sh=`echo $f | cut -f 2 -d "/"`
	echo "\DeclareFontShape{T1}{cmor}{$se}{$sh}{<->ssub * cmr/$se/$sh}{}" >> T1cmor.fd
done

echo '\endinput' >> T1cmor.fd

mv T1cmor.fd ../fd

rm _T1cmor.fd

# Sans Serif family

echo >> $logfile
echo "Sans Serif family" >> $logfile
echo "-----------------" >> $logfile
echo >> $logfile

getpl "$ssofamily"

(
intro

echo; echo "\installfamily{T1}{cmoss}{\hyphenchar\font='177 }"

echoinstalls ss ss m n
echoinstalls ss si m sl
echoinstalls ss sx bx n
echoinstalls ss so bx sl

extro
) > dcoss.tex

tex dcoss.tex

getvf "$ssofamily"

cleanup "$ssofamily"

ed T1cmoss.fd <<EOS
\$-1,\$d
w
q
EOS

for f in $ssfamily; do
	se=`echo $f | cut -f 1 -d "/"`
	sh=`echo $f | cut -f 2 -d "/"`
	echo "\DeclareFontShape{T1}{cmor}{$se}{$sh}{<->ssub * cmr/$se/$sh}{}" >> T1cmoss.fd
done
echo '\endinput' >> T1cmoss.fd

mv T1cmoss.fd ../fd

# Typewriter family

echo >> $logfile
echo "Typewriter family" >> $logfile
echo "-----------------" >> $logfile
echo >> $logfile

getpl "$ttofamily"

(
intro

echo; echo "\installfamily{T1}{cmott}{\hyphenchar\font=\m@ne }"

echoinstalls tt tt m n
echoinstalls tt it m it
echoinstalls tt st m sl

extro
) > dcott.tex

tex dcott.tex

getvf "$ttofamily"

cleanup "$ttofamily"

ed T1cmott.fd <<EOS
,s/\\\hyphenchar \\\font =\\\m @ne /\\\hyphenchar\\\font=\\\m@ne/
w
q
EOS

mv T1cmott.fd ../fd

# Dunhill family

echo >> $logfile
echo "Dunhill family" >> $logfile
echo "--------------" >> $logfile
echo >> $logfile

getpl "$dhofamily"

(
intro

echo; echo "\installfamily{T1}{cmodh}{\hyphenchar\font='177 }"

echoinstalls dh dh m n

extro

) > dcodh.tex

tex dcodh.tex

getvf "$dhofamily"

cleanup "$dhofamily"

mv T1cmodh.fd ../fd

# Variable width Typewriter family

echo >> $logfile
echo "Variable width Typewriter family" >> $logfile
echo "--------------------------------" >> $logfile
echo >> $logfile

getpl "$vtofamily"

(
intro

echo; echo "\installfamily{T1}{cmovt}{\hyphenchar\font=\m@ne }"

echoinstalls vt vt m n
echoinstalls vt vi m it

extro
) > dcovt.tex

tex dcovt.tex

getvf "$vtofamily"

cleanup "$vtofamily"

ed T1cmovt.fd <<EOS
,s/\\\hyphenchar \\\font =\\\m @ne /\\\hyphenchar\\\font=\\\m@ne/
w
q
EOS

mv T1cmovt.fd ../fd

exit 0
