1::
2::  This is dvidrv.btm, a batch file for 4DOS/4OS2 written by
3::  Werner Lemberg <wl@gnu.org> partially based on the
4::  dvidrv.btm file of the 4allTeX package written by Phons Bloemen.
5::
6::  It is a replacement for dvidrv.exe of the emTeX package.
7::
8::  Additional features: support of ps2pk
9::                       support of ttf2pk
10::                       support of hbf2gf for HBFs (Hanzi bitmap fonts)
11::
12::      Only quadratic printer modes are supported for ttf2pk!
13::
14::  All needed binaries will be searched in the path.
15
16iff %# lt 2 then
17  echo ``
18  echo Usage: %0 dvi-driver dvi-file [parameters]
19  quit
20endiff
21
22setlocal
23  :: we set a default value only if the corresponding environment variable
24  :: is empty. The `=' must follow the variable name immediately.
25  alias set_def `iff "%[%@word["=",0,%1]]" eq "" then %+ set %& %+ endiff`
26
27  :: =========================================
28  :: ======== User defined variables. ========
29  :: =========================================
30  ::
31  :: Can be overridden in the environment (except %ps2pk, %ttf2pk, %hbf2gf,
32  :: %pre_dpi, and %post_dpi).
33
34  :: Set these values to `no' for  the programs you don't want to use.
35  set ps2pk=yes
36  set ttf2pk=yes
37  set hbf2gf=yes
38
39  :: where the PK files will be installed created by ps2pk, ttf2pk, and
40  :: hbf2gf.
41  :: dvidrv.btm adds `\modeless\XXXdpi' to this string (see below for the
42  :: exact string).
43  set_def pkdir=%emtexdir\pixel\tmp\pk
44
45  :: where the TrueType fonts reside. Supports trailing `!' and `!!'.
46  set_def ttfonts=%emtexdir\fonts\truetype
47  :: where auxiliary files of ttf2pk are located. Supports trailing `!'
48  :: and `!!'.
49  set_def ttfcfg=%emtexdir\ttf2pk
50
51  :: where the HBF files reside. Supports trailing `!' and `!!'.
52  set_def hbfonts=%emtexdir\fonts\hbf
53  :: where the hbf2gf config files are. Supports trailing `!' and `!!'.
54  set_def hbfcfg=%emtexdir\hbf2gf
55
56  :: the dpi-subdirectory has various forms; the most common are XXXdpi and
57  :: dpiXXX, e.g. 300dpi and dpi300. Here we define two variables which
58  :: control this behaviour.
59  :: [dpiXXX is used for TDS compatible TeX trees.]
60  set pre_dpi=dpi
61  set post_dpi=
62
63  :: ================================================
64  :: ======== End of user defined variables. ========
65  :: ================================================
66
67  :: the base name for log files etc.
68  set basename=%@name[%1]
69
70  :: do we run dvips?
71  iff "%basename" ne "dvips" then
72    goto nodvips1
73  endiff
74
75  :: we delete dvips.mfj if it exists.
76  del dvips.mfj >& nul
77
78  :: now we run dvips with all supplied parameters.
79  %&
80
81  :: do we have to generate fonts?
82  iff exist dvips.mfj then
83    goto generate_fonts
84  endiff
85
86  :: else all is done.
87  goto end
88
89
90:nodvips1
91  :: here we call the dvi driver with all supplied parameters.
92  %& -pj:%basename.mfj
93
94  :: do we have to generate fonts?
95  iff errorlevel 8 then
96    goto generate_fonts
97  endiff
98
99  :: else all is done.
100  goto end
101
102
103:generate_fonts
104  gosub call_mfjob
105
106  :: do we run dvips?
107  iff "%basename" ne "dvips" then
108    goto nodvips2
109  endiff
110
111  :: we now call dvips a second time
112  %&
113
114  :: since no fonts will be generated in the second run we remove dvips.mfj
115  del dvips.mfj >& nul
116
117  goto end
118
119
120:nodvips2
121  :: we call the dvi driver a second time (without font generation).
122  %& -pj -fm
123
124  del %basename.mfj >& nul
125
126  goto end
127
128
129:: this is the main subroutine which calls mfjob and then parses the mfjob
130:: file for fonts which can be handled by ps2pk, ttf2pk, or hbf2gf.
131:call_mfjob
132  :: this alias prints a message on the screen and writes it into the logfile.
133  alias echolog=`echo %& | tee /a %basename.mlg`
134
135  echo ======== DVIDRV.BTM logfile ========  > %basename.mlg
136  echo Commandline: dvidrv.btm %&           >> %basename.mlg
137  echo ``                                   >> %basename.mlg
138  echo basename=%basename                   >> %basename.mlg
139  echo emtexdir=%emtexdir                   >> %basename.mlg
140  echo dvidrvfonts=%dvidrvfonts             >> %basename.mlg
141  echo mfinput=%mfinput                     >> %basename.mlg
142  echo mfjobopt=%mfjobopt                   >> %basename.mlg
143  echo textfm=%textfm                       >> %basename.mlg
144  echo psfonts=%psfonts                     >> %basename.mlg
145  echo ttfonts=%ttfonts                     >> %basename.mlg
146  echo ttfcfg=%ttfcfg                       >> %basename.mlg
147  echo hbfonts=%hbfonts                     >> %basename.mlg
148  echo hbfcfg=%hbfcfg                       >> %basename.mlg
149  echo ``                                   >> %basename.mlg
150
151  iff "%@search[mfjob]" eq "" then
152    echolog MFJOB not found in the path.
153    echolog Can't generate PK fonts from METAFONT automatically.
154    echo ``
155    quit
156  endiff
157
158  :: now we call mfjob.
159  echolog mfjob %mfjobopt -g%basename.mfl %basename.mfj
160  mfjob %mfjobopt -g%basename.mfl %basename.mfj
161  iff %? gt 0 then
162    echolog ``
163    echolog Something went wrong while running METAFONT.
164    echolog Look into the log files...
165    echolog ``
166  endiff
167
168  :: after running mfjob we scan the mfjob input file for fonts which can
169  :: be handled by ps2pk, ttf2pk, or hbf2gf.
170  set lnnr=0
171  set totlines=%@lines[%basename.mfj]
172
173  do while %lnnr le %totlines
174    :: input a line.
175    set scratch=%@line[%basename.mfj,%lnnr]
176    set lnnr=%@inc[%lnnr]
177
178    :: get resolutions (METAFONT mode will be ignored).
179    :: example:
180    ::   mode=lqlores[180 180];
181    iff %@index[%scratch,mode] ge 0 then
182      set temp=%@word["[]",1,%scratch]
183      set xdpi=%@word[0,%temp]
184      set ydpi=%@word[1,%temp]
185    endiff
186
187    :: get fontname and magnification; we then compute the font resolutions
188    :: (rounded to the nearest integer---there is no necessity to adjust
189    :: these values further because of possible rounding errors since both
190    :: emTeX's dvi drivers and dvips check neighbored values too).
191    :: example:
192    ::   {font=cmr10; mag=0.5;}
193    iff %@index[%scratch,{font] ge 0 then
194      set testfont=%@word["=;",1,%scratch]
195      set temp=%@word["=;",3,%scratch]
196      :: we use highest arithmetic precision for these calculations.
197      :: Additionally we write 1/2 instead of 0.5 to avoid problems with
198      :: countries which use a comma instead of a colon as the decimal
199      :: separator.
200      set fontresx=%@int[%@eval[%xdpi*%temp+1/2=8]]
201      set fontresy=%@int[%@eval[%ydpi*%temp+1/2=8]]
202      set pksubdir=modeless\%[pre_dpi]%[fontresx]%post_dpi
203
204      set success=0
205
206      :: check whether %testfont leads to a PS font (we call ps2pkmfj).
207      iff %success == 0 .and. "%ps2pk" eq "yes" then
208        gosub call_ps2pk
209      endiff
210
211      :: check whether %testfont leads to a TrueType font.
212      iff %success == 0 .and. "%ttf2pk" eq "yes" then
213        gosub call_ttf2pk
214      endiff
215
216      :: check whether %testfont leads to a HBF file.
217      iff %success == 0 .and. "%hbf2gf" eq "yes" then
218        gosub call_hbf2gf
219      endiff
220    endiff
221  enddo
222
223  iff exist %basename.mfp then
224    echo ``                              >> %basename.mlg
225    echo ``                              >> %basename.mlg
226    echo ======== PS2PK logfile ======== >> %basename.mlg
227    echo ``                              >> %basename.mlg
228    type %basename.mfp                   >> %basename.mlg
229    echo ``                              >> %basename.mlg
230    del %basename.mfp >& nul
231  endiff
232
233  iff exist %basename.mfl then
234    echo ``                              >> %basename.mlg
235    type %basename.mfl                   >> %basename.mlg
236    echo ``                              >> %basename.mlg
237    del %basename.mfl >& nul
238  endiff
239
240  return
241
242
243:call_ttf2pk
244  iff "%@search[ttf2pk]" eq "" then
245    echolog TTF2PK not found in the path.
246    echolog Can't generate PK fonts from TrueType fonts automatically.
247    echo ``
248    quit
249  endiff
250
251  echolog ttf2pk -q -n %testfont %fontresx
252  ttf2pk -q -n %testfont %fontresx
253
254  iff %? == 0 then
255    echolog Font %pkdir\%pksubdir\%testfont.pk generated.
256    mkdir /s %pkdir\%pksubdir >& nul
257    move %testfont.pk %pkdir\%pksubdir >& nul
258    set success=1
259  elseiff %? == 2 then
260    echolog (%lnnr/%totlines): Font %testfont is no TrueType font.
261    set success=0
262  else
263    echolog Error running TTF2PK for font %testfont!
264  endiff
265
266  return
267
268
269:call_hbf2gf
270  iff "%@search[hbf2gf]" eq "" then
271    echolog HBF2GF not found in the path.
272    echolog Can't generate PK fonts from HBF files automatically.
273    echo ``
274    quit
275  endiff
276
277  echolog hbf2gf -q -p -n %testfont %fontresx %fontresy
278  hbf2gf -q -p -n %testfont %fontresx %fontresy
279
280  iff %? == 0 then
281    echolog Font %pkdir\%pksubdir\%testfont.pk generated.
282    mkdir /s %pkdir\%pksubdir >& nul
283    gftopk %testfont.gf %testfont.pk
284    move %testfont.pk %pkdir\%pksubdir >& nul
285    del %testfont.gf >& nul
286    set success=1
287  elseiff %? == 2 then
288    echolog (%lnnr/%totlines): Font %testfont is no HBF.
289    set success=0
290  else
291    echolog Error running HBF2GF for font %testfont!
292  endiff
293
294  return
295
296
297:call_ps2pk
298  iff not exist %pkdir\%pksubdir\%testfont.pk then
299    iff "%@search[ps2pkmfj]" eq "" .or. "%@search[ps2pk]" eq "" then
300      echolog PS2PKMFJ and/or PS2PK not found in the path.
301      echolog Can't generate PK fonts from PostScript fonts automatically.
302      echo ``
303      quit
304    endiff
305
306    echolog ps2pkmfj -X%fontresx -Y%fontresy %testfont %testfont.pk
307    ps2pkmfj -X%fontresx -Y%fontresy %testfont %testfont.pk | input %%pspkline
308
309    iff errorlevel != 1 then
310      %pspkline >> %basename.mfp
311      iff %? == 0 then
312        echolog Font %pkdir\%pksubdir\%testfont.pk generated.
313        mkdir /s %pkdir\%pksubdir >& nul
314        move %testfont.pk %pkdir\%pksubdir >& nul
315        success=1
316      else
317        echolog Error running PS2PK for font %testfont!
318      endiff
319    else
320      echolog (%lnnr/%totlines): Font %testfont is no PS font.
321      success=0
322    endiff
323  else
324    echolog Font %pkdir\%pksubdir\%testfont.pk already exists.
325  endiff
326
327  return
328
329
330:end
331
332endlocal
333
334:: ==== end of dvidrv.btm ====
335