dvitomp fix from Akira
[mplib] / src / texk / kpathsea / mktexpk
1 #!/bin/sh
2 # original mktexpk -- make a new PK font, because one wasn't found.
3
4 # (If you change or delete the word `original' on the previous line,
5 # installation won't write this script over yours.)
6
7 # Originally written by Thomas Esser, Karl Berry, and Olaf Weber.
8 # Report bugs to tex-k@tug.org.
9 # Public domain.
10
11 version='$Id: mktexpk,v 1.34 2005/06/21 10:49:10 olaf Exp $'
12 progname=`echo $0 | sed 's%.*/%%'`
13 usage="Usage: $progname [OPTIONS] NAME [REDIRECT],
14   Create a PK font.
15
16 --dpi DPI           use resolution DPI.
17 --bdpi BDPI         use base resolution BDPI.
18 --mag MAG           use magnificiation MAG.
19 --mfmode MODE       use MODE as the METAFONT mode.
20 --destdir DESTDIR   write fonts in DESTDIR.
21
22 Try to create a PK file for NAME at resolution DPI, with an assumed
23 device base resolution of BDPI, and a Metafont \`mag' of MAG. Use MODE
24 for the METAFONT mode.  Use DESTDIR for the root of where to install
25 into, either the absolute directory name to use (if it starts with a
26 /) or relative to the default DESTDIR (if not). REDIRECT, if supplied,
27 is a string of the form '>&n', where n is the number of the file
28 descriptor which is to receive, instead of stdout, the name of the
29 newly created pk file."
30
31 # Handle non-positional options, except for --version/--help
32 while test $# -gt 0; do
33   case "$1" in
34     --destdir) shift; DEST="$1"; shift ;;
35     --destdir=*) DEST=`echo "$1" | sed 's/--destdir=//'`; shift ;;
36     --dpi) shift; DPI="$1"; shift ;;
37     --dpi=*) DPI=`echo "$1" | sed 's/--dpi=//'`; shift ;;
38     --bdpi) shift; BDPI="$1"; shift ;;
39     --bdpi=*) BDPI=`echo "$1" | sed 's/--bdpi=//'`; shift ;;
40     --mfmode) shift; test "x$1" != "x/" && MODE="$1"; shift ;;
41     --mfmode=*) test "x$1" != "x--mfmode/" \
42                 && MODE=`echo "$1" | sed 's/--mfmode=//'`; shift ;;
43     --mag) shift; MAG="$1"; shift;;
44     --mag=*) MAG=`echo "$1" | sed '/s--mag=//'`; shift;;
45     --version|-version) break ;;
46     --help|-help) break ;;
47     *) break ;;
48   esac
49 done
50
51 if test "x$2" != x; then
52   tail="`echo \"x$2\" | sed 's/^x>&//' | grep '^[0-9]*$'`"
53   if test -z "$tail"; then
54     echo "$progname: argument '$2' ignored - bad file number" >&2
55   elif test "$tail" != 1; then
56       eval 'exec 1>&$tail'
57   fi
58 fi
59
60 mt_max_args=2
61
62 # Common code for all scripts.
63 : ${MT_TEXMFMAIN=`kpsewhich --expand-path='$TEXMFMAIN'`}
64 : ${MT_MKTEX_OPT=`kpsewhich --format='web2c files' mktex.opt`}
65 test -n "$MT_MKTEX_OPT" || MT_MKTEX_OPT="$MT_TEXMFMAIN/web2c/mktex.opt"
66 if test ! -f "$MT_MKTEX_OPT"; then
67   echo "$progname: Cannot find mktex.opt; check your installation." >&2
68   exit 1
69 fi
70
71 . "$MT_MKTEX_OPT"
72
73 # Where do potential driver files belong?
74 case "$MT_FEATURES" in
75   *nomfdrivers*)
76      : ${MT_MFDESTDIR=`pwd`}
77      export MT_MFDESTDIR;;
78 esac
79
80 # Since we want to pass the generated filename and only that filename
81 # back to the caller on standard output, we do some redirections so
82 # regular echo's will end up on stderr, and do an echo >$STDOUT at the end.
83 # Then the contents of $STDOUT will be echoed to stdout by a trap.
84
85 # start of redirection stdout -> stderr, stdin <- /dev/null
86 (
87
88 NAME=$1
89
90 if kpsewhich $NAME.mf >/dev/null 2>&1 || mktexmf $NAME >/dev/null 2>&1; then
91   # determine the progname of metafont to use; prefer mf-nowin.
92   if (mf-nowin --version) >/dev/null 2>&1; then
93     MF="mf-nowin -progname=mf"
94   else
95     MF=mf
96   fi
97
98   # Check that $BDPI and $MODE are consistent; if not, ignore the mode and
99   # hope we can correctly guess it from bdpi.  (People like to specify the
100   # resolution on the command line, not the mode so much.)
101   if test -n "$MODE"; then
102     mf_bdpi=`$MF \
103 '\mode:='$MODE';mode_setup;message"BDPI= "&decimal round pixels_per_inch;end.'\
104              </dev/null \
105              | awk '/DPI=/ {print $2}'`
106     if test "x$mf_bdpi" != x$BDPI; then
107       echo "$progname: Mismatched mode $MODE and resolution $BDPI; ignoring mode." >&2
108       MODE=
109     fi
110   fi
111
112   # If an explicit mode is not supplied, try to guess. You can get a
113   # list of extant modes from ftp://ftp.tug.org/tex/modes.mf.
114   if test -z "$MODE" || test "x$MODE" = xdefault; then
115     case "$BDPI" in
116       85) MODE=sun;;
117      100) MODE=nextscrn;;
118      180) MODE=toshiba;;
119      300) MODE=cx;;
120      360) MODE=epstylus;;
121      400) MODE=nexthi;;
122      600) MODE=ljfour;;
123      720) MODE=epscszz;;
124     1200) MODE=ultre;;
125     1270) MODE=linoone;;
126     8000) MODE=dpdfezzz;;
127        *) echo "$progname: Can't guess mode for $BDPI dpi devices." >&2
128           echo "$progname: Use a config file or option to specify the mode; see:" >&2
129           echo "$progname: http://tug.org/texinfohtml/kpathsea.html#Unable-to-generate-fonts" >&2
130           exit 1
131     esac
132   fi
133
134   # Run Metafont. Always use plain Metafont, since reading cmbase.mf
135   # does not noticeably slow things down.
136   cmd="$MF \mode:=$MODE; mag:=$MAG; nonstopmode; input $NAME"
137 else
138   MODE=modeless
139   # ps_to_pk is set in mktex.opt
140   case $ps_to_pk in
141     gsftopk)
142       if $ps_to_pk -t "$NAME"; then
143         cmd="$ps_to_pk $NAME $DPI"
144       fi;;
145     ps2pk)
146       # grep for the font in $PSMAPFILE.  These are base font names, such as
147       # rpplr (the original) or pplr0 (an interim step) or pplr8r (current).
148       : ${PSMAPFILE=`kpsewhich --format=map psfonts.map ps2pk.map`}
149       pattern="^$NAME"'([       ]|$)' 
150       psline=`egrep "$pattern" $PSMAPFILE | tail -1`
151       if test -n "$psline"; then
152         set x `echo "$psline" | sed 's%[<["]%%g'`
153         shift; shift; shift;
154         encoding=; psname=; slant=; extend=
155         while test ! -z "$1"; do
156           case "$1" in
157             *.enc)       encoding="-e $1";;
158             *.pf[ab])    psname="$1";;
159             *SlantFont)  slant="-S $lastopt";;
160             *ExtendFont) extend="-E $lastopt";;
161           esac
162           lastopt="$1"
163           shift
164         done
165         # Guessing the name of the type1 font file as fallback:
166         ANAME=`echo $NAME | sed 's/8r$/8a/'`
167         for i in $NAME.pfa $NAME.pfb $ANAME.pfa $ANAME.pfb; do
168           if kpsewhich $i >/dev/null 2>&1; then
169             psname=$i
170             break
171           fi
172         done
173
174         if [ -n "$psname" ]; then
175           cmd="ps2pk -v -X$DPI -R$BDPI $slant $extend $encoding $psname $NAME.${DPI}pk"
176         else
177           if gsftopk -t "$NAME"; then
178             echo "$progname: cannot find $NAME.pfa or $NAME.pfb. Trying gsftopk." >&2
179             cmd="gsftopk $NAME $DPI"
180           fi
181         fi
182       fi;;
183   esac
184
185   # unsupported by $ps_to_pk, try other conversions:
186   if test -z "$cmd"; then
187     if (ttf2pk -t -q $NAME) >/dev/null 2>&1; then
188       cmd="ttf2pk -q $NAME $DPI"
189     elif (hbf2gf -t -q $NAME) >/dev/null 2>&1; then
190       cmd="hbf2gf -q -p $NAME $DPI"
191     else
192       echo "$progname: don't know how to create bitmap font for $NAME." >&2
193       exit 1
194     fi
195   fi
196 fi
197
198 OIFS=$IFS; IFS=$SEP
199 set x `"$MT_MKTEXNAM" $NAME $DPI $MODE $DEST`; shift
200 IFS=$OIFS
201
202 PKDEST="$1"
203 PKDESTDIR=`echo "$PKDEST" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
204 PKNAME=`basename "$PKDEST"`
205 GFNAME=$NAME.${DPI}gf
206
207 if test -r "$PKDESTDIR/$PKNAME"; then
208   echo "$progname: $PKDESTDIR/$PKNAME already exists." >&2
209   echo "$PKDESTDIR/$PKNAME" >$STDOUT
210   "$MT_MKTEXUPD" "$PKDESTDIR" $PKNAME
211   exit 0
212 fi
213
214 "$MT_MKTEXDIR" "$PKDESTDIR"
215 if test ! -d "$PKDESTDIR"; then
216   echo "$progname: $MT_MKTEXDIR $PKDESTDIR failed." >&2
217   exit 1
218 fi
219
220 echo "$progname: Running $cmd"
221 $cmd </dev/null || {
222   # Don't abort if only "Strange path", "bad pos" or "angle(0,0)"
223   # errors occurr.
224   grep '^!' $NAME.log | sort >$$.errs 2>/dev/null
225   grep '^! Strange path' $$.errs >$$.strange 2>/dev/null
226   grep '^! bad pos.' $$.errs >$$.badpos 2>/dev/null
227   grep '^! angle(0,0) is taken as zero.' $$.errs >$$.angle 2>/dev/null
228   cat $$.badpos $$.strange $$.angle | sort > $$.errs_accept
229   if cmp $$.errs $$.errs_accept >/dev/null 2>&1; then
230     test -s $$.strange >/dev/null 2>&1 \
231       && echo "$progname: warning: \`$cmd' caused strange path errors." >&2
232     test -s $$.badpos >/dev/null 2>&1 \
233       && echo "$progname: warning: \`$cmd' caused bad pos errors." >&2
234     test -s $$.angle >/dev/null 2>&1 \
235       && echo "$progname: warning: \`$cmd' caused angle(0,0) errors." >&2
236   else
237     echo "$progname: \`$cmd' failed." >&2
238     test -s $NAME.log && mv -f $NAME.log "$KPSE_DOT"
239     exit 1;
240   fi
241 }
242
243 test -r $GFNAME && { gftopk ./$GFNAME $PKNAME || exit 1; }
244 test ! -f $PKNAME && test -f $NAME.${DPI}pk && mv $NAME.${DPI}pk $PKNAME
245 if test ! -s $PKNAME; then
246   echo "$progname: \`$cmd' failed to make $PKNAME." >&2
247   exit 1
248 fi
249
250 # Install the PK file carefully, since others may be working simultaneously.
251 # Use cp when mv fails, since DOS will fail mv for deeply-nested directories.
252 mv $PKNAME "$PKDESTDIR/pk$$.tmp" 2>/dev/null \
253   || cp $PKNAME "$PKDESTDIR/pk$$.tmp" || exit 1
254 cd "$PKDESTDIR" || exit 1
255 chmod `kpsestat -xst,go-w .` pk$$.tmp
256 test -r $PKNAME || mv pk$$.tmp $PKNAME || exit 1
257
258 # Update ls-R if necessary.
259 "$MT_MKTEXUPD" "$PKDESTDIR" $PKNAME
260
261 # If this line (or an equivalent) is not present, dvipsk/xdvik/dviljk
262 # will think mktexpk failed.  Any other output to stdout will also lose.
263 echo "$PKDESTDIR/$PKNAME" >$STDOUT
264 echo "$progname: $PKDESTDIR/$PKNAME: successfully generated." >&2
265 ) 1>&2 </dev/null