summaryrefslogtreecommitdiff
path: root/fonts/utilities/mathkit/mathkit
blob: e9b63d27b73e4be9d29ea55f20b427096452a152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
#!/usr/local/bin/perl
### ==================================================================== 
###  @Perl-file{ 
###	author          = "Alan Hoenig", 
###	version         = "0.7", 
###	date            = "January 1998", 
###	filename        = "mathkit", 
###	address         = "Department of Mathematics
###			   John Jay College
###			   445 West 59th Street
###			   New York, NY 10019
###			   USA", 
###	telephone       = "(516) 385-0736", 
###	email           = "ajhjj@cunyvm.cuny.edu", 
###	codetable       = "ISO/ASCII", 
###	supported       = "yes", 
###	abstract        = "This script constructs the scripts for installing
###			   math fonts combining outline letters and numerals 
###			   matching Metafont bitmaps for special symbols.",
###	package         = "MathKit", 
###	dependencies    = "perl," 
###  } 
### ==================================================================== 

## USAGE: perl mathkit <template> <font-fam> <encoding>
## Examples of use (assumes presence of tm.mkr, tm.mkb):
##   perl ../mathkit tm mnt OT1    
## or simply (if the script is made executable)
##   ../mathkit tm mnt OT1; 

## Some system parameters...

$version = "0.7";
$noslant = 0;
$slanted = 1;

$true =    1;
$false =   0;

## Routines to make conversion between DOS and Unix simple..
##   $mv is the form of the `move' command.
##   $sep is the path separator for the system.
##   $q is the quote character to surround command line parameters and
##     prevent expansion by the operating system.
##   $comment is the string that begins a comment in a system file.

sub UNIX{$mv="mv"; $sep="/"; $q="\'"; $comment="##";}
sub DOS{$mv="domove"; $sep="\\"; $q=""; $comment="REM";}

require("../mathkit.par");	# get parameters (system dependent)

if (not $sans_font) {
    $sans_font = "cmss";
    $sans_supplier = "public";
    $sans_typeface = "cm";
    $sans_installer = "base";
}

$rm_tfm = $tfm;			# where are roman tfm's?
$cm_tfm = $tfm;			# where are CM tfm's?
$sf_tfm = $tfm;			# where are sans serif tfm's?
$plain_inputs = $inputs;	# where do plain styles go?
$rm_inputs = $inputs;

$pkext = "pk"; $gfext = "gf";	# DOS defaults
if ($sep ne "\\") {
    $pkext = "${dpi}pk";
    $gfext = "${dpi}gf";
}

if ($texmf) {			# for TDS systems only...
    $pk = "$texmf${sep}fonts${sep}pk${sep}$mode${sep}mathkit";
    $pk = "$pk${sep}z$ARGV[0]$ARGV[1]${sep}dpi$dpi";
    $vf = "$texmf${sep}fonts${sep}vf${sep}mathkit${sep}z$ARGV[0]$ARGV[1]";
    $tfm = "$texmf${sep}fonts${sep}tfm${sep}mathkit${sep}z$ARGV[0]$ARGV[1]";
    $mfinputs = "$texmf${sep}fonts${sep}source";
    $rm_inputs = "$texmf${sep}tex${latex}${sep}$installer";
    $inputs = "$texmf${sep}tex${sep}latex${sep}mathkit";
    $plain_inputs = "$texmf${sep}tex${sep}plain${sep}mathkit";
    $afm = "$texmf${sep}fonts${sep}afm${sep}$supplier$sep$typeface";
    $rm_tfm = "$texmf${sep}fonts${sep}tfm${sep}$supplier$sep$typeface";
    $cm_tfm = "$texmf${sep}fonts${sep}tfm${sep}public${sep}cm";
    $sf_tfm = 
	"$texmf${sep}fonts${sep}tfm${sep}$sans_supplier$sep$sans_typeface";
}

open (MKDIR, ">mkdirs.bat");
&banner("MKDIR", "mkdirs.bat");
$mkdir = "mkdir";		# system dependent `mkdir' command
sub check_dir{			# does a directory exist?
    local($thisdir) = $_[0];
    if (not -d $thisdir) {
	push(@missingdirs, $thisdir);
    }
}

sub check_dirs{			# does a list of directories exist?
    foreach $dir (@_) {
	&check_dir($dir);
    }
    foreach $miss (@missingdirs) {
	$misss = "$miss$sep";
	$Sep = "$sep";
	$Sep = "\\\\" if $sep eq "\\"; # special case for DOS
	while ($misss =~ m/$Sep/g) {
	    if (($know_about{$`} == $false) and (not -d $`)) {
		print MKDIR "$mkdir $`\n" unless $` eq "";
		$know_about{$`} = $true;
	    }
	}
    }
    close MKDIR; undef %know_about;
}
&check_dirs(
    $tfm,
    $rm_tfm,
    $cm_tfm,
    $sf_tfm,
    $afm,
    $pk,
    $vf,
    $psfonts,
    $mfinputs,
    $inputs,
    $plain_inputs,
    $rm_inputs,
	    );
    
## Some bookkeeping with respect to date and time...

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$thisday=(Sun,Mon,Tues,Wednes,Thurs,Fri,Satur)[$wday];
$thisday="${thisday}day";
$month=(January,February,March,April,May,June,July,August,
  September,October,November,December)[$mon];
$fullyear="19$year";
if ($year<50) {$fullyear="20$year";}
sub timestamp{
  if ($min<10) {$min="0$min";}
  $currtime="$hour:$min";
  $currdate="$thisday, $month $mday, $fullyear";
}
&timestamp;

## Some utility routines...

sub display{			# prints its argument on screen
    foreach $thingy (@_) {
	print $thingy;
    }
}

sub log{			# prints arguments in log file
    foreach $thingy (@_) {
	print LOG $thingy;
    }
}

sub record{			# logs and prints arguments
    foreach $thingy (@_) {
	print $thingy;
	print LOG $thingy;
    }
}

$FH_ = "STDOUT";			# default value
sub out{			# prints to a given file.
    foreach $str (@_){
	print $FH_ $str;
    }
}
sub copy{
  die "`copy' can't find file $_[0]" unless -e $_[0];
  open(COPYIN, $_[0]); # the source
  open(COPYOUT, ">$_[1]"); # the target
  while (<COPYIN>) {print COPYOUT $_;}
  close COPYOUT;
}

sub safecopy{# copy source to target ONLY if target doesn't exist either in
    ## current dir or in parent dir
    if ((not -e $_[1]) and (not -e "..$sep$_[1]")) { # target doesn't exist
	&copy(@_);
    } else {
	&record("Copy operation skipped; file $_[1] already exists.\n");
    }
}

## We need a routine to print a banner on files and scripts
## that MathKit makes.  Arguments are: filehandle, comment char,
## file name.  Usage: &printbanner("TEX", "%", "testmath.tex")

sub printbanner{
    local($fh_,$comment_,$filename)=($_[0],$_[1],$_[2]);
    print $fh_ "$comment_ This is file $filename,";
    print $fh_ " created by MathKit$version + Perl$].\n";
    print $fh_ "$comment_ It was created at $currtime on $currdate.\n";
    print $fh_ "$comment_ \n";
    print $fh_ "$comment_ YOU MAY DELETE THIS FILE AFTER USE.\n";
    print $fh_ "$comment_ \n";
}

sub banner{# prints banner in auxiliary files.
    &printbanner($_[0], $comment, $_[1]);
}

## Get command line params...

  $start_up =<<"EndStart";
MathKit needs three arguments to work.  Firstly, I need to know the name
of the template I should use.  At the moment, there are three templates
to choose from:

    1. bv (Baskerville-like);
    2. tm (Times Roman-like); and
    3. pl (Palatino-like).

I also need to know the name of the Roman font to use.  This should be the
three-character abbreviation under which this family was installed---some-
thing like 'mbv' (Baskerville), 'pad' (Adobe Garamond), 'ppl' (Palatino),
or 'ptm' (Times) is what I am looking for.  Finally, I need an encoding
scheme, either 'OT1' or 'ot1'.  As a result, the command line to get me 
going should look something like this:

    ../mathkit bv mbv OT1

Please consider reading the file 'mathkit.tex' (a LaTeX file) for further
important information.  (These comments are stored in the file 'todo.1st'.)

EndStart

if ($#ARGV != 2) {
    open (TODO, ">todo.1st");  
    print TODO $start_up;
    print $start_up;
    close TODO;
    die "Fix me and start again.";
}
$tseries=$ARGV[0]; # the template series
$tser=substr($tseries,0,1); # 1st char becomes $tseries abbreviation
$fontfam=$ARGV[1]; # font family (using Berry name)
open (LOG, ">z$tseries$fontfam.klg"); # open log file
$encoding=$ARGV[2];# which encoding? (T1 or OT1 only)

## Print banner, first off...

&record("This is MathKit$version + Perl$] at $currtime on $currdate.\n\n");
&record("We are using MathKit template $tseries.\n");
&record("We are using font family $fontfam.\n");
&record("The encoding is $encoding.\n");


$enc = 8;            # set the encoding `digit'
$enc = 7 if $encoding =~ /o/i;

## Checking and retrieving font template information...

if (-e "..${sep}${tseries}.mkr") {# get template for Roman fonts
  $template[0]="..${sep}${tseries}.mkr";
} else {# template doesn't exist!
  &record("Where's the template file $tseries?  Dying...\n");
  die "At least, I need to see $tseries.mkr in the parent directory.";
}

## We need only one bold template (if there is at least one).  We try
## bold first, but prefer the semibold...

if (-e "..${sep}${tseries}.mkb") {# get bold template
  $template[1]="..${sep}${tseries}.mkb";
##   $bld = "b";
##   $nfssbld = "b";
} 
if (-e "..${sep}${tseries}.mks") {# get semibold template
  $template[1]="..${sep}${tseries}.mks";
##   $bld = "s";
##   $nfssbld = "sb";
} 

## We need to determine the proper boldweight to use...
## The default:
$bld = "b";
$nfssbld = "b";
## Now to test for the semibold (we prefer to use the semibold if the
## Roman semibold fonts are present.)
if (-e "$afm${sep}${fontfam}s8a.afm") {
    $bld = "s";
    $nfssbld = "sb";
    &record("Text fonts: We are using semibold weights in this run.\n");
} else {
    &record("Text fonts: We are using bold weights in this run.\n");
}

## Now to check font info...

$fullafm = "$afm${sep}${fontfam}r8a.afm";
$expafm = "$afm${sep}${fontfam}r8x.afm";
if (not -e $fullafm) {
    &record("I'm missing font info in $fullafm. Help...\n");
    die "I need font info to continue...";
}
$exp = 1 if -e $expafm;		# Do real experts exist?

## End of preliminary processing...

## Subroutines follow... 

sub afm_metric_info{
    local($target_char, $afm_file) = @_;
    open (TMP, "$afm$sep$afm_file.afm") or
	die "Can't find $afm$sep$afm_file.afm.  Dying...";
    local($h, $d) = (-999,999);	# default values
    while (<TMP>) {
	chomp $_;
	last if /EndCharMetrics/;
	next unless /^C\s+\d+/;
	/C\s+\d+\s+\; WX\s+\d+ \; N ([A-Za-z]+) \; B -?\d+ (-?\d+) \d+ (\d+) \;/;
	if ($1 eq $target_char) {
	    $h = sprintf "%5.1f", 36*$3/100;
	    $d = sprintf "%5.1f", 36*$2/100;
	    $h =~ s/\.0$//;	# strip off final `.0'
	    $d =~ s/\.0$//;
	    $h = "$h/36pt\#";	# add Metafont grammar
	    $d = "$d/36pt\#";
	}
    }
    close TMP;
    ($h, $d);
}

## The routine get_fontfam_parameters reads the afm file to get 
## values for seven properties of the font that enter into a
## Metafont font description.  (The only 2 we can't extract are
## the bar height and math axis.)  We only use this information, however,
## if the parameter $should_try_to_match is set to a non-null, non-zero
## value in the file `mathkit.par'.  
##    The parameter for this routine is a the Berry weight---usually
## `r', or `b', or `s'---of the scalable fonts we are matching.

sub get_fontfam_parameters{
    local($tmp);
    local($afmfile) = "${fontfam}$_[0]8a";
    ($body_height, $tmp) = &afm_metric_info("parenleft", $afmfile);
    ($asc_height, $tmp) = &afm_metric_info("h", $afmfile);
    ($cap_height, $tmp) = &afm_metric_info("B", $afmfile);
    ($fig_height, $tmp) = &afm_metric_info("5", $afmfile);
    ($x_height, $tmp) = &afm_metric_info("x", $afmfile);
    ($tmp, $comma_depth) = &afm_metric_info(",", $afmfile);
    ($tmp, $desc_depth) = &afm_metric_info("g", $afmfile);
}

sub adjust_font_parameters{
    local(@tmp) = ("body_height", "asc_height", "cap_height", "fig_height",
	       "x_height", "comma_depth", "desc_depth");
    local($temp);
    foreach $strng (@tmp) {
	$temp = "$str\#";
	$param{$temp} = $$temp;
    }
}


sub get_template_info{
    open (IN, "$_[0]"); 
    undef %param; # just in case
    while ($_ = <IN>) {
	if ($_ =~ /^([a-zA-Z_#]+):=([0-9a-z\/\.#]+);/) {
		      $param{$1} = "$2"; # 
	    
	}
    }
    close IN;
}

## Some comments about the special fonts (cal, tt, frak, bb, sans serif):
## Calligraphic is easiest---we pluck them from the cmsy font, and they
## already have been properly scaled.  Typewriter tt fonts come from
## cmtt10 which we assume to exist in a scalable version. 
## Fraktur comes from the scalable version of eufm10 (BaKoMa font).
## Blackboard bold will be generated only if the mf source exists;
## bb comes from Alan Jeffrey's bbold10 font.
## 
## Sans serif fonts are a very special case, as they are almost always
## part of a family of sans serif fonts.  MathKit examines the $ss_font 
## variable in the .par file.  If it doesn't exist, no sans serif is taken.  

$cm_x_height = "155/36pt\#"; 
$cm_xht_num = "155";
$cmss_x_height = "160/36pt\#"; 
$cmss_xht_num = "160";
$frak_x_height = "169.6/36pt\#";
$frak_xht_num = "169.6";
	$newbbold =<<"endnewbbold";
\% Font ${tseries}bb10 is modified Blackboard bold 10pt.
mode_setup;
input bbbase;
% Hacks to make sure cmr doesn't generate the font, and doesn't load cmbase.
cmbase := 1;
def generate suffix t = enddef;
% Input the cmr parameters.
input ${tseries}r10;
font_identifier := "${tseries}-BBOLD";
font_coding_scheme := "Blackboard bold";
% Then generate blackboard bold.
input bbold;
bye;
endnewbbold

sub get_font_fam{
    local($fontfam, $fontenc) = ("cmss", "7t");	# default
    local($font) = $_[0];
    if (not $font =~ /^cmss/) {
	$fontfam = substr $font, 0, 3;
	$fontenc = substr $font, 4, 99;
    }
    ($fontfam, $fontenc);
}
    
## First, define some fudge factors for better visual compatibility
## during the scaling process...
$fudge_frak  = 1.03;
$fudge_tt    = 1.0;
$fudge_bb    = 1.0;
$fudge_sans  = 1.03;

sub do_special_fonts{
    ($xht_num, $tmp) = split /\//, $x_height;
    # Nothing special for calligraphic...
    # Now for tt...
    $ttplace = $psfonts;		# traditional
    $ttplace = "$texmf$sep$fonts${sep}type1${sep}public${sep}cm"
	if $texmf;		# TDS
    $ttfont = "$ttplace${sep}cmtt10";
    if ((-e "$ttfont.pfb") or (-e "$ttfont.pfa")) {
	$tt = $true;
	$tt_sf = $xht_num / $cm_xht_num;
	$tt_sf *= $fudge_tt;
	$tt_sf = sprintf "%4.2f", $tt_sf;
	$ttsize = 10*$tt_sf;
	$ttsize = sprintf "%4.1f", $ttsize;
	$replace{"<ttsize>"} = $ttsize;
	&record("Source file $ttfont found:\n  ",
		"scaling cmtt10 by a factor of $tt_sf.\n");	
    } else {
	&record("No tt font source found; no tt font will be provided.\n"); 
    }
    # Now for fraktur...
    $frakplace = $psfonts;		# traditional
    $frakplace = "$texmf$sep$fonts${sep}type1${sep}ams${sep}euler"
	if $texmf;		# TDS
    $frakfont = "$frakplace${sep}eufm10";
    if ((-e "$frakfont.pfb") or (-e "$frakfont.pfa")) {
	$fr = $true;
	$frak_sf = $xht_num / $frak_xht_num;
	$frak_sf *= $fudge_frak;  
	$frak_sf = sprintf "%4.2f", $frak_sf;
	$frsize = 10*$frak_sf;
	$frsize = sprintf "%4.1f", $frsize;
	$replace{"<frsize>"} = $frsize;
	$replace{"<frsf>"} = $frak_sf;
	&record("Source file $frakfont found:\n  ",
		"scaling eufm10 by a factor of $frak_sf.\n");	
    } else {
	&record("No fraktur font found; and none will be provided.\n"); 
    }
    # Now for blackboard bold...
    $bbplace = $mfinputs;		# traditional
    $bbplace = "$texmf$sep$fonts${sep}source${sep}public${sep}bbold"
	if $texmf;		# TDS
    $bbsource = "$bbplace${sep}bbold10.mf";
    if (-e $bbsource) {
	$bb = $true;
	$bb_sf = $tt_sf;
	$bb_sf *= $fudge_bb;
	$bb_sf = sprintf "%4.2f", $bb_sf;
	&record("Source file $bbsource found:\n  ",
		"scaling bbold10 by a factor of $bb_sf.\n");	
	open (OUT, ">${tseries}bb10.mf");
	&printbanner("OUT", "%", "${tseries}bb10.mf");
	print OUT $newbbold;
	close OUT;
	print MF "$mf ${q}\\mode=$mode; scrollmode; input ${tseries}bb10$q\n";
	print GFTOPK "gftopk ${tseries}bb10.$gfext ${tseries}bb10.$pkext\n";
    } else {
	&record("No bbold font found; and none will be provided.\n"); 
    }
    # Now for sans serif fonts...
    if ($sans_font) {
	$sa = $true;
	($sansfam, $sansencoding) = &get_font_fam($sans_font);
	if ($sansfam eq "cmss") {
	    $sans_sf = $xht_num / $cmss_xht_num;
	    $sans_sf = sprintf "%4.2f", $sans_sf;
	    &record("CMSS sans serif scaled by $sans_sf will be used.\n");
	} else {		# compute sf for non-cm sans serif
	    $replace{"<ssr>"} = $sans_font;
	    $replace{"<ssri>"} = "${sansfam}ri$sansencoding";
	    $sansbold = "b";
	    $sansbold = "s" if -e "$sf_tfm$sep${sansfam}s$sansencoding.tfm";
	    $replace{"<ssb>"} = "$sansfam${sansbold}$sansencoding";
	    $replace{"<ssbi>"} = "$sansfam${sansbold}i$sansencoding";
	    ($sans_x_height, $tmp) = &afm_metric_info("x", "${sansfam}r8a"); 
	    ($sans_xht_num, $tmp) = split /\//, $sans_x_height;
	    $sans_sf = $xht_num / $sans_xht_num;
	    $sans_sf *= $fudge_sans;
	    $sans_sf = sprintf "%4.2f", $sans_sf;
	    $sasize = 10*$sans_sf;
	    $sasize = sprintf "%4.1f", $sasize;
	    $replace{"<sasize>"} = $sasize;
	    &record("Sans serif family $sansfam (contining font $sans_font) ",
		"scaled by $sans_sf will be used.\n");
	}
    } else {
	&record("No sans serif font found; and none will be provided.\n"); 
    }
}

## Making Metafont source files...

## When we make Metafont source files, the easiest case occurs 
## when there is a comparable cm file we can copy.  For example, 
## in a times-like (tm) setting, we create tmmi7.mf by copying cmmi7.mf
## and simply making suitable changes to the Metafont parameters.
## In this case the Perl subroutine `makemffiles' calls upon the 
## routine `normalcopy' to perform this creation.

sub normalcopy{
  local($sf, $source) = @_;
  open (IN, "$source") || die "Where is $source?";
  while ($_=<IN>) {# read and digest template values
    last if /font_size/; # discard 1st few lines
  }
  print OUT "SF_=$sf;  % scale factor\n";
  while ($_=<IN>) {# read rest of .mf file
    if ( /^([a-zA-Z_#]+):=([0-9\.\/a-z#]+);/ ) {
	 $curparam=$1; $oldvalue=$2; # save these!
	 if (defined $param{$curparam}) {
	   $newvalue=$param{$curparam};
	   $newvalue="SF_*$newvalue" if /pt#/;
	   s/$oldvalue/$newvalue/;
	 }
    }
    if ($slantind==$noslant) {
	 if (/^slant:=([\.0-9]+);/) {# zero slant in roman files
	   s/$1/0/;
	 }
    }
    if (/generate/) { # have we reached the `generate' statement?
	s/generate\s+/generate mk/;
    }
    print OUT $_;
  }
}

## In case the source file (eg cmsy12.mf or cmmib7.mf) does NOT exist,
## some handwaving is necessary.  We'll read from the corresponding roman
## file (eg cmr12 or cmbx7) which HAD better exist up through the
## `generate' command, but then finish off with the corresponding 
## roman file.  Thus, to create tmbsy7.mf, we will read cmbx7 up through
## the `generate' command towards the end of that file, and then pick
## up cmsy7.mf, using only the material following the `math_fitting'
## parameter (this is the last parameter in the standard .mf files).
## Final concern: the sy fonts need the parameter `rth#' defined as
## either .4pt# or .6pt# (roman or bold), so we have to take 
## care to create this variable if need be.

sub fudgecopy{
  local($sf, $source) = @_;
  ## redefine the source...
  if ($doingbold == $true) {
    $source="$mfinputs${sep}cmbx$size.mf";
  } else {
    $source="$mfinputs${sep}cmr$size.mf";
  }
  $nextsize=$size; 
  $nextsize=10 if $size>10; # unfortunate default
  $nextmid=$midfix;
  $nextmid =~ s/b//;
  $nextsource="$mfinputs${sep}cm$nextmid$nextsize.mf";
  open (IN, "$source") || die "Where is $source?";
  while ($_=<IN>) {# read and digest template values
    last if /font_size/; # discard 1st few lines
  }
  print OUT "SF_=$sf;  % scale factor\n";
  while ($_=<IN>) {# read rest of .mf file until `generate'
    if ( /^([a-zA-Z_#]+):=([0-9\.\/a-z#]+);/ ) {
	 last if /^generate/;
	 $curparam=$1; $oldvalue=$2; # save these!
	 if (defined $param{$curparam}) {
	   $newvalue=$param{$curparam};
	   $newvalue="SF_*$newvalue" if /pt#/;
	   s/$oldvalue/$newvalue/;
	 }
    }
    if ($slantind==$noslant) {
	 if (/^slant:=([\.0-9]+);/) {# zero slant in roman files
	   s/$1/0/;
	 }
    }
    print OUT $_ unless /^generate/;
  }
  close IN;
  open (IN, $nextsource) ||
    die "Where is $nextsource?";
  while (<IN>) {last if /^math_fitting/;} # discard front of file
  $myrth=".4pt#";
  if ($doingbold==$true) {$myrth=".6pt#";}
  print OUT "\nrth#:=$myrth; % assume rules come from extension font\n";
  while (<IN>) {
    next if /^rth#/; # we generated this line already
    s/rule_thickness#/rth#/g; # ensure references are to rth#
    s/generate\s+/generate mk/;
    print OUT $_;
  }
}

## The actual mf files are created by the following routine.

sub mf_file_header{
    local($target, $midfix, $size) = @_;
    print OUT "%% $tseries-like font, file $target $size point\n";
    print OUT "%% Created by MathKit$version at $currtime on $currdate.\n";
    print OUT "if unknown cmbase: input cmbase fi\n\n";
    $fontid="$tseries$midfix";
    $fontid =~ tr/a-z/A-Z/; # make everything uppercase
    print OUT "font_identifier:=\"$fontid\"; font_size ${size}pt#;\n\n";
}

## &makemffiles(src prefix, target midfix, slant ind, list of sizes);
sub makemffiles{# create and process Roman fonts
  local($prefix,$midfix,$slantind)=($_[0],$_[1],$_[2]);
  local(*sizes)=$_[3];
  foreach $size (@sizes) {
    $source="$mfinputs${sep}${prefix}${midfix}${size}.mf";
    $targetfirst="${tseries}${midfix}${size}";
    $target="$targetfirst.mf";
    $tfmsource="$cm_tfm$sep${prefix}${midfix}${size}.tfm";
    ## All gf and pk files will have been created, and we need pl files.
    print PL "echo $targetfirst\n"; # system dependent
    print PL "tftopl $targetfirst.tfm $targetfirst.pl\n";
    print MF "$mf ${q}&cm \\mode=$mode; scrollmode; input $targetfirst$q\n";
    print GFTOPK "gftopk $targetfirst.$gfext $targetfirst.$pkext\n";
    open (OUT, ">$target") || die "Cannot open $target.  Help!";
    $sf=$size/10;
    &mf_file_header($target, $midfix, $size);
    if (-e $source) {
	 &normalcopy($sf, $source); # source exists--o joy, o rapture!
    } else {
	 &fudgecopy($sf, $source); # no source---finagling follows
    }
    close IN; close OUT;
  }
}

sub do_roman{
  $doingbold=$false;
  &makemffiles("cm","r",$noslant,*romans);
}

sub do_bold{
  $doingbold=$true;
  &makemffiles("cm","bx",$noslant,*bolds);
}

sub do_mathital{
  $doingbold=$false;
  &makemffiles("cm","mi",$slanted,*romans);
}

sub do_mathitalb{
  $doingbold=$true;
  &makemffiles("cm","mib",$slanted,*bolds);
}

sub do_mathsy{
  $doingbold=$false;
  &makemffiles("cm","sy",$slanted,*romans);
}

sub do_mathbsy{
  $doingbold=$true;
  &makemffiles("cm","bsy",$slanted,*bolds);
}

sub do_tenex{
  $doingbold=$false;
  &makemffiles("cm","ex",$noslant,*ex);
}

sub do_tenbex{
  $doingbold=$true;
  &makemffiles("cm","bex",$noslant,*ex);
}

sub get_encoding_sequence{
    local($sequence) = "${enc}t";
    if ($exp) {			# expertised fonts?
	$sequence = "9e";	# Cork (8t) + experts
	$sequence = "9t" if $enc == 7; # 
    }
    $replace{"<enct>"} = $sequence;
    $sequence;
}
 
sub startpl{
  local($e) = &get_encoding_sequence($enc);
  print PL "tftopl $rm_tfm$sep${fontfam}r$e.tfm ${fontfam}r$e.pl\n";
  print PL "tftopl $rm_tfm$sep${fontfam}ri$e.tfm ${fontfam}ri$e.pl\n";
  if ($#template==1) {
	print PL "tftopl $rm_tfm$sep$fontfam$bld$e.tfm $fontfam$bld$e.pl\n";
	print PL 
	    "tftopl $rm_tfm$sep$fontfam${bld}i$e.tfm $fontfam${bld}i$e.pl\n";
  }
}

## This routine reads an fd file and discards comments, the `\endinput' 
## statement, and the `\ProvidesPackage' statement.  It adds font scaling
## info specified as an argument to the routine into all 
## `\DeclareFontShape' statements.  One catch: scaling info may have
## been placed in the fd file by fontinst, so we want to catch this and
## replace it by our current scaling factor.

sub get_fd_info_and_scale_it{
    local($fdfile, $mysize, $inputs_) = @_;
    local($pathandfdfile) = "$inputs_${sep}$fdfile.fd";
    open(FD, $pathandfdfile) or die "Can't find $pathandfdfile.  Help";
    while (<FD>) {		# skips comments and ProvidesPackage
	last if /\\DeclareFontFamily/;
    }
    &out("\n%% Extracting font info from file $fdfile.fd...\n");
    FDLINE: for (;;) {
	if ( $_ =~ /\[\d*\.\d+\]/ ) { # scaling factor found!
	    $_ =~ s/\[\d*\.\d+\]/\[$mysize\]/g;
	} else {		# no scaling factor found
	    # we search for right angle, followed by 0 or more space,
	    # followed by fontname (which begins with alpha)
	    s/(>\s*)([^<\s])/\1 \[$mysize\] \2/g;
	}
	&out("$_") unless /^%/;	# ignore comments
	last FDLINE if eof(FD);
	$_ = <FD>;
	last FDLINE if /\\endinput/;
    }
    &out("%% End of extraction from file $fdfile.fd.\n\n");
    close FD;
}

sub makestylefile{
  $source="z$_[0].mk";
  $pathsource="..$sep$source";
  $target="z$tseries$fontfam.$_[0]";
  close IN; close OUT; # just in case
  open (IN, $pathsource) 
    || die "We are missing file $pathsource.  HELP!";
  open (OUT, ">$target")
    || die "Cannot open file $target.  Help!";
  $FH_ = "OUT";
  print OUT "%% This is file $target, containing ";
  print OUT "TeX macros to typeset mock-MF fonts.\n";
  print OUT "%% Created by MathKit$version at $currtime on $currdate.\n";
  while ($_=<IN>) {
    next if /^%%/; # strip out comment lines
    while ( /(<[a-z]+>)/ ) {  # parameters for replacement?
	 $a_ = $1; $b_ = $replace{$a_};
	 $_ =~ s/$a_/$b_/g;
    }
    if ( /^\[([a-z]+)\]/ ) {# do we strip off special font indicators?
	 $ind = $1;
	 $_ =~ s/\[b\]// if defined $template[1];
	 $_ =~ s/\[$ind\]// if $$ind;
    }
    print OUT $_ unless /^\[/; # some special lines are not needed
  }
  if ($_[0] eq "tex") {		# hook for the plain .tex file
  }
  if ($_[0] eq "sty") {		# hook for the LaTeX (NFSS) .sty file
      if ($template[1]) {	# is there a bold series?
	  print OUT "\\DeclareMathAlphabet{\\mathbf}{$encoding}";
	  print OUT "{$fontfamx}{$nfss_bold}{n}\n";
      }
      if ($tt) {		# for tt
	  $tt_inputs = $inputs;
	  $tt_inputs = "$texmf${sep}tex${sep}latex${sep}$tt_installer"
	      if $texmf;
	  &get_fd_info_and_scale_it("${encoding}cmtt", $tt_sf, $tt_inputs);
	  print OUT "\\DeclareMathAlphabet{\\mathtt}{$encoding}";
	  print OUT "{\\ttdefault}{m}{n}\n";
      }
      if ($sa) {		# for sans serif
	  $sans_inputs = $inputs;
	  $sans_inputs = "$texmf${sep}tex${sep}latex${sep}$sans_installer"
	      if $texmf;
	  &get_fd_info_and_scale_it("${encoding}${sansfam}", $sans_sf, 
				    $sans_inputs);
	  print OUT "\\newcommand{\\sansdefault}{$sansfam}\n";
	  print OUT "\\DeclareMathAlphabet{\\mathsf}{$encoding}";
	  print OUT "{\\sansdefault}{m}{n}\n";
      }
  }
  print OUT "\\endinput\n"; 
  close IN; close OUT;
}

sub create_hax_files{
    $hackprefix = "$tseries$fontfam";
    &safecopy("..${sep}rhax.mtx", "${hackprefix}rrx.mtx");
    &safecopy("..${sep}ihax.mtx", "${hackprefix}rix.mtx");
    &safecopy("..${sep}iskew.mtx", "${hackprefix}riw.mtx");
    $replace{"<hax>"} = $hackprefix;
    if (defined $template[1]) {	# A bold?  If so, we need bold versions...
        &safecopy("..${sep}rhax.mtx", "${hackprefix}brx.mtx");
        &safecopy("..${sep}ihax.mtx", "${hackprefix}bix.mtx");
        &safecopy("..${sep}iskew.mtx", "${hackprefix}biw.mtx");
    }
}

sub makemakevp{ # makes the file makevp.tex, the fontinst install file.
  &create_hax_files;		# files for fixing metric info
  $source="makevp.mk";
  $pathsource="..$sep$source";
  $target="makevp.tex";
  close IN; close OUT; # just in case
  open (IN, $pathsource) 
    || die "We are missing file $pathsource.  HELP!";
  open (OUT, ">$target")
    || die "Cannot open file $target.  Help!";
  print OUT "%&plain\n";
  print OUT "%% This is file $target, containing ";
  print OUT "fontinst install commands to create virtual fonts.\n";
  print OUT "%% Created by MathKit$version at $currtime on $currdate.\n";
  while ($_=<IN>) {
    next if /^%%/; # strip off comment lines
    while ( /(<[a-z]+>)/ ) {# parameters for replacement?
	 $a_ = $1;
	 $b_ = $replace{$a_};
	 $_ =~ s/$a_/$b_/;
    }
    if ( /^\[b\]/ ) {# do we strip off bold indicator?
	 $_ =~ s/\[b\]// if defined $template[1];
    }
    print OUT $_ unless /^\[b\]/; # bold lines are not needed
  } 
  close IN; close OUT;
  open (IN, "makevp.tex");
  open (OUT, ">makevf.bat");
  &banner("OUT", "makevf.bat");
  while ($_ = <IN>) {
      next unless $_ =~ /\\installfont\{/;
      $_ =~ /\s+\\installfont\{([a-z0-9]+)\}\{/;
      print OUT "vptovf $1.vpl $1.vf $1.tfm\n";
  }
  close IN; close OUT;
}

## This routine prints $_ to output filehandle TEX provided there
## are no bracketed qualifiers at the line beginning.  It tests 
## for 2-char qualifiers; "b" is handled separately.

sub printTEXlines {
    if ($_ =~ /^\[([a-z]+)\]/) {
	local($ind_)=$1;
	$_=~s/^\[$ind_\]// if $$ind_;
    }
    print TEX $_ unless /^\[/;
}

sub makeLaTeXtest{
    open(TEX, ">testmath.tex");
    &printbanner("TEX", "%", "testmath.tex");
    local($infile) = "..${sep}testmath.mk";
    open(TEMPLATE, $infile) or
	die "Where is the template $infile?";
    while ($_ = <TEMPLATE>) {
	next if /^%/;		# strip comments away
        while ( /(<[a-z]+>)/ ) {# parameters for replacement?
             $a_ = $1;
             $b_ = $replace{$a_};
             $_ =~ s/$a_/$b_/;
        }
        if ( /^\[b\]/ ) { # do we strip off bold indicator?
	    $_ =~ s/\[b\]// if defined $template[1];
	}
	&printTEXlines;
    }
    close TEX; close TEMPLATE;
}
  
sub makeplaintest{
    open(TEX, ">testmatp.tex");
    &printbanner("TEX", "%", "testmatp.tex");
    local($infile) = "..${sep}testmatp.mk";
    open(TEMPLATE, $infile) or
	die "Where is the template $infile?";
    while ($_ = <TEMPLATE>) {
	next if /^%/;		# strip comments away
        while ( /(<[a-z]+>)/ ) {# parameters for replacement?
             $a_ = $1;
             $b_ = $replace{$a_};
             $_ =~ s/$a_/$b_/;
        }
        if ( /^\[b\]/ ) { # do we strip off bold indicator?
	    $_ =~ s/\[b\]// if defined $template[1];
	}
	&printTEXlines;
    }
    close TEX; close TEMPLATE;
}
  
    $finish_up =<<"EndFinish";

Now to finish up.  Perform the following tasks:
0.  Execute 'mkdirs.bat' to create any missing directories.
1.  Execute the file 'makegf.bat'.  (This may take some time...)
2.  Create the packed pixel .pk files.  On some (Unix) systems, you do this  
    via commands like:
localhost>>foreach X (*.300gf)
localhost>>gftopk \$X \$X:r.300pk
localhost>>end
    but many systems do not permit this.  Therefore, you will need to examine
    the accompanying file 'makepk.bat' and EDIT it accordingly to conform
    to your system standards and syntax.  Thereafter, execute this file;
    make sure you EDIT it first!
3.  Execute the file 'makepl.bat'.
4.  Execute the command 'tex makevp'.  (This may take some time...)
5.  Execute the file 'makevf.bat'.
6.  Run the files 'testmath.tex' (LaTeX) or 'testmatp.tex' (plain TeX) 
    through TeX to test the files.  [Consult the documentation 'mathkit.tex'
    for advice on adjusting these new math fonts.]
7.  When you are satisfied with the results, execute 'putfonts.bat' to store
    the fonts and files where they belong.
(This advice appears in the Ascii file 'todo.nxt' and in the log file
'z$tseries$fontfam.klg' which contains other information about this run.)
EndFinish

## Now let's record the remaining tasks...
sub finish{
   ## first, make bat files executable...
    @batch_files = 
      ("putfonts.bat", "mkdirs.bat", "makegf.bat", 
       "makepk.bat", "makepl.bat", "makevf.bat");
    chmod 0755, @batch_files;
    ## Putting files where they belong...
    print PUT "$mv *.mf $mfinputs\n";
    print PUT "$mv *.vf $vf\n";
    print PUT "$mv *.tfm $tfm\n";
    ## $pkext = "pk";		# DOS default
    ## if ($sep ne "\\") {
    ## 	   print "I need the extension for the pixel files you created.\n";
    ## 	   print "This extension is something like '300pk' or '600pk'.\n";
    ## 	   print "Please enter it now: ";
    ## 	   $pkext = <STDIN>; chomp $pkext;
    ## }
    ## $pkext =~ s/^\.//;		# just in case user added a `dot'
    &record("I am using the extension `.$pkext' for pixel files.\n");
    print PUT "$mv *.$pkext $pk\n";
    print PUT "$mv *.fd $inputs\n";
    print PUT "$mv z$tseries$fontfam.sty $inputs\n";
    print PUT "$mv z$tseries$fontfam.tex $plain_inputs\n";
    ## User instructions...
    &record($finish_up);
    open (TODO, ">todo.nxt");
    &banner("TODO", "todo.nxt");
    print TODO $finish_up;
    close TODO;
}

## For each point size <n> we create roman, mi, sy

@romans=(12,10,9,7,5); # point sizes for Roman fonts
@bolds=(12,10,7,5);  # point sizes for bold fonts
@ex=(10);                  # point sizes for extension fonts

## The following replacements will be used to personalize the templates.

$replace{"<currdate>"} = $currdate;
$replace{"<fam>"} = $fontfam;
$replace{"<famx>"} = $fontfam;	# expert family?
$replace{"<famx>"} = "${fontfam}x" 
    if -e "$rm_inputs$sep$encoding${fontfam}x.fd";
$fontfamx = $replace{"<famx>"};
$replace{"<mock>"} = $tseries;
$replace{"<m>"} = $tser;
$replace{"<enc>"} = $enc;
$replace{"<enx>"} = $replace{"<enc>"};
$replace{"<enx>"} = "9"
    if -e "$rm_inputs$sep$encoding${fontfam}x.fd";
$replace{"<encoding>"} = $encoding;
$replace{"<b>"} = $bld;
$replace{"<nfssb>"} = $nfssbld;
$replace{"<bseries>"} = "b"; ## change to "bx" if appropriate
## but...
$replace{"<bseries>"} = "sb" if $bld eq "s";
$nfss_bold = $replace{"<bseries>"};

## Some defaults for the special fonts...

$replace{"<ssr>"} = "cmss10";		# sans serif stuff
$replace{"<ssb>"} = "cmssbx10";
$replace{"<ssri>"} = "cmssi10";
$replace{"<ssbi>"} = "cmssbx10";
$replace{"<sasize>"} = "10";
$replace{"<ttsize>"} = "10";		# tt
$replace{"<cal>"} = "${tseries}sy10";		# calligraphic
$replace{"<calb>"} = "${tseries}bsy10";		# calligraphic
$replace{"<casize>"} = "10";
$replace{"<frsize>"} = "10";		# fraktur
$replace{"<frsf>"} = "1";

sub do_it_all{
  open (PL, ">makepl.bat"); # script to create pl files fontinst needs
  &banner("PL", "makepl.bat");
  open (LTX, ">z${tseries}${fontfam}.sty"); # contains NFSS (LaTeX) macros
  open (PLN, ">z${tseries}${fontfam}.tex"); # contains plain TeX macros
  open (PUT, ">putfonts.bat");	# places completed fonts where they belong
  &banner("PUT", "putfonts.bat");
  open (MF, ">makegf.bat"); # script to execute Metafont
  &banner("MF", "makegf.bat");
  open (GFTOPK, ">makepk.bat"); # script to pack all pixels
  &banner("GFTOPK", "makepk.bat");
  &get_fontfam_parameters("r");
  &get_template_info($template[0]);
  &adjust_font_parameters if $I_should_try_to_match;
  &startpl;
  ## Do all roman fonts...
  &do_roman;
 &do_mathital;
 &do_mathsy;
 &do_tenex;
 &do_special_fonts;		# tt, frak, b'board bold, cal, sans serif
 if (defined $template[1]) { # do bold fonts...
   &get_template_info($template[1]);
   &do_bold;
   &do_mathitalb;
   &do_mathbsy;
   &do_tenbex;
 }
  &makestylefile("tex"); # make plain TeX macro file
  &makestylefile("sty"); # make LaTeX NFSS package file
  &makemakevp;           # make the fontinst install file
  close PL; close LTX; close PLN; close MF; close GFTOPK;
  &makeLaTeXtest;
  &makeplaintest;
  &finish;
}

## Let 'er rip...

&do_it_all;