summaryrefslogtreecommitdiff
path: root/systems/generic/mltex/mltex.ch
blob: 7efc6ca7a9e325eefd787bd01c638e41933a1221 (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
% This is MLTEX.CH (Version 2.2) in text format, as of Dec 17, 1995.
% WEB change file containing code for various features extending TeX; to
% be applied to TEX.WEB (Version 3.14159) in order to generate MLTEX.WEB.
 
% MLTeX is copyright (C) 1990-92 by Michael J. Ferguson; all rights are reserved.
% MLTeX Version 2.2 is copyright (C) 1995 by B. Raichle; all rights are reserved.
%
%
% The MLTeX changes are copyrighted so that we have some chance to
% forbid unauthorized copies; we explicitly authorize copying of
% correct MLTeX implementations, and not of incorrect ones!
%
% (This means that you can use the MLTeX changes as free as you can
% use TeX and its algorithm.)
%
% Copying of this file is authorized only if either
% (1) you make absolutely no changes to your copy, including name, or
% (2) if you do make changes, you name it to something other than
%     "mltex.ch", "char_sub.ch", or "charsub.ch".


% The TeX program is copyright (C) 1982 by D. E. Knuth.
% TeX is a trademark of the American Mathematical Society.

% MLTeX Version 2.2 was created by Bernd Raichle
% All line numbers refer to TEX.WEB 3.14159 as of March, 1995.


%============================================================
% INSTALLATION NOTICES:
%
% === Creating MLTeX (general information) ===
%
% 1) Merging the change files:
%
% In order to generate an implementation of MLTeX your implementation
% dependent change file and MLTeX's change file "mltex.ch" have to be
% applied to "tex.web", one after the other.
%   The process of merging several change files into "tex.web" should
% certainly not be performed by hand.  There are programs such as TIE
% and PATCHWEB that perform this process automatically.  If no such
% program is available, a TeX program "webmerge.tex" can be used.
%   Examples: To merge the two changes files "mltex.ch" and "tex.ch"
% using tie:
%
%      tie -c newtex.ch tex.web mltex.ch tex.ch
%
% To do the same using "webmerge.tex" you have to apply TeX to the
% following driver input file:
%
%      \input webmerge       % needs `webmerge.tex'
%      \webfile{tex.web}
%      \changefile{mltex.ch}
%      \changefile{tex.ch}
%      \outfile{newtex.ch}   % output file, start processing
%      \bye
%
%   Before merging the change files it might be necessary to make
% small changes in one of the two change files or, as a more elegant
% way, to add an additional change file for theses some changes which
% interfere between the change files.  One of these interferences is
% caused by the change of the banner line ([1] m.2 l.187).
%
% 2) Tangling TeX:
%
% Apply TANGLE on "tex.web" and the combined change file "newtex.ch":
%
%      tangle tex.web newtex.ch
%
% 3) Post-processing the Pascal source "tex.p":
%
% Because of a restriction in TANGLE's macros, you have to replace
% the strings "XLPAREN" and "XRPAREN" (resp. "xlparen"/"xrparen")
% with a left parenthesis "(" and a right parenthesis ")" by hand.
%   If the Unix tool "sed" is available you can use
%
%      sed  -e 's/xlparen/(/g' -e 's/xrparen/)/g' < tex.p > mltex.p
%      mv mltex.p tex.p
%
% 4) Compile TeX and apply the TRIP test:
%
% The final step will be the compilation of (ML)TeX.  You should
% apply the TRIP test to the resulting program to make sure that your
% MLTeX will be compatible to TeX unless one of the new feature is
% used.
%
%
% === Creating MLTeX using web2c v7.0 ===
%
% MLTeX v2.2 is part of the TeX in web2c v7.0, you can use the
% "-mltex" option to enable MLTeX.  Please read the documentation
% of web2c for more information.
%
%
% === Creating MLTeX using web2c prior to v7.0 ===
%
% To create a new change file for web2c v6.1, adapt the change of the
% banner line ([1] m.2 l.187) to the used TeX version.  Then use
% `tie' to merge the two change files:
%
%      tie -c newtex.ch tex.web mltex.ch tex.ch
%
% Make sure that the combined change file "newtex.ch" will identify
% the resulting TeX as a MLTeX, i.e. make sure that the banner line
% is changed accordingly.
%
% Use tangle:
%
%      tangle tex.web newtex.ch
%
% then replace the string "xlparen" with "(" and "xrparen" with `)'
% by using "sed":
%
%      sed  -e 's/xlparen/(/g' -e 's/xrparen/)/g' < tex.p > mltex.p 
%      mv mltex.p tex.p
%
% Finally convert the new Pascal source `tex.p' to a C Source using
% `web2c' and compile it.
%
%
%============================================================
% Differences between MLTeX version 2.1 and 2.2:
%
% * \charsubdefmax has an initial version of -1
% * All non-existing characters of a font are substituted if a
%   \charsubdef is given  (the former version doesn't substitute
%   characters with codes < highest font character code)
% * Changed log output texts.
% * There will be no |push| and |pop| operations in the dvi file
%   for substituted accent constructions
% * With \tracingoutput enabled missing characters are reported
%   _before_ the box contents is shown and the box will not contain
%   additional character nodes showing the substitutions
%
% Bugs fixed:
% * Error message ("Bad metric (TFM) file.") while loading a font
%   was shown in versions 2.01 and older in some cases.
% * No character substitution for large math delimiter character
% * If a character substitution was used and then removed (e.g. by
%   \charsubdef <char>=0 0 or \charsubdefmax=-1), all accesses
%   to |font_info| for this non-existing character are invalid and
%   undefined (=> runtime errors, access violations!).  These
%   accesses are always done in |char_info| and they occur during
%   the |line_break| pass and in |hpack|.
%   Fixed: MLTeX will now access the character information for the
%   first existing character in the font (normally \char0) instead.
%   This means that the character/box dimensions will be incorrect!
%
% ...and in versions before 2.2 (done by Michael J. Ferguson):
% * Errors during shipout to dvi file if \*leaders box contains
%   substituted characters
% * Incorrect spacing of |hpack| or |rebox| if two fonts with
%   different character metrics are used, e.g. use ArabTeX with
%   mixed arabic/latin text.  ArabTeX contains a fix on the macro
%   level, but it doesn't really fix this bug!
%
% Restrictions:
% * Do not change character substitutions!  Because the current
%   character substitutions are only read while creating a character
%   node in the horizontal list, but are _not_ saved in this node,
%   the same substitutions have to be valid during |shipout| of
%   this character node to the dvi file.
%   (Bugs if \charsubdefs are changed: 1) during |hpack| or
%   |shipout| missing character warnings are reported,
%   2) \unhbox will change the width of a box)
% * If an accentee in a \charsubdef has height <= 0 it will
%   overlap the base character (for \c c), a real "\underaccent"
%   primitive is still missing.
% * Accent placement routine is taken from TeX's \accent primitive,
%   adjustments of accent characters (cf. `german.sty' lowers them)
%   are not possible.
%
%
%============================================================
% ChangeLog:
%
% Dec 17, 1995 (br) -- MLTeX v2.2
% -- add flag |mltex_p| and |mltex_enabled_p|,
%    add code to dump and undump the flag and a magic constant
%    simplifying the implementation of a command line option `-mltex'
% -- print additional banner line "MLTeX v2.2 enabled" after the fmt
%    file is loaded; original banner line is left unmodified
%
% Dec 07, 1995 (br) -- MLTeX v2.2 (betatest)
% -- |effective_char| can return non-existing characters.
%    This is not valid after character substitutions are used and
%    then are disabled because a node for a non-existing character
%    can remain in a horizontal list.  (TeX assumes that all char
%    nodes are valid, no checks are done after the character node
%    is created.)  Introduced third argument for |effective_char|,
%    introduced function |effective_char_info| as shortcut.
% -- Do not try to substitute characters in math mode
%    (for compatibility with MLTeX v2.01)
% -- removed old change of [32] m.638 l.12638; with the changed dvi
%     character substitution routines |show_box| can be called in
%    |ship_out| before shipping the box to the dvi file---without
%    getting incompatible with TRIP
%
% Dec 05, 1995 (br) -- MLTeX v2.2 (alphatest)
% -- typo in test version: |..._slant| are |real|, not |scaled|
%    (Thanks, em!)
% -- bug while loading fonts which contains less than 256 characters
%    and a character substitution was defined for an existing
%    character but a non-existing base character; the analysis of
%    bug showed another one w.r.t. a non-existing boundary character
%    (fix: define macro |orig_char_info| which doesn't use
%    |effective_char| and use it while loading a new font).
%
% Oct 28, 1995 (br) -- MLTeX v2.1+ (not released)
% -- Subsumed "\charsubdef" under |shorthand_def| to simplify
%    the merge of multiple change files.
% -- Moved character substitution from |eqtb| region 1 to region 4
%    which will allow \subcharcode<charnum>=<integer> -- a TeX
%    construct analogous to \catcode et.al.  (This change is already
%    included in a prototype of MLTeX version 3 which will use
%    different data structures to allow \charsubdef changes on the fly
%    without the anomalies in version 2.)
% -- When substituting a character, test if c>font_ec and additionally
%    if c<font_bc.
% -- Removed the changes in part 2 ("The character set"), module 24 to
%    allow non-ASCII characters in the input files.  These changes
%    should be included in the implementation specific change files!
% -- addded \charsubdefmin parameter (which can not be accessed by
%    the user!)
% -- there was a bug using a \*leaders box with charsubdef'ed
%    characters as a \*leaders box; it will try to access characters
%    in the |null_font| (probably resulting in access violations)!
%    (Fix: additional test for |if f<>null_font ...| in [32] m.620)
%    Better fix: don't construct a box construction like \accent,
%    write the dvi code directly; this will also simplify other
%    changes for TeX--XeT or e-TeX which need a |prevp| pointer.
%
% Sep 9 + Sep 16, 1992 (br) -- MLTeX v2.1+ (test, not released)
% -- initialize char_sub_def_max with -1 (instead of 0)
% -- changed effective_char to blend with the rest of TeX.web
%    parameter `c' is an `eight_bit'
% -- removed change of \S708 because it is silly to substitute
%    (character parts of) a large math delimiter character
% -- added proposals from E. Mattes
% -- deleted `font_chs'  (unnecessary and inconsistent use)
%    replaced chs_accent, chs_char, chs_replace
% -- make changes as short as possible
% -- make sure that xchr[@'177] is initialized
%
%============================================================
% Comments from `char_sub.ch', the former MLTeX change file:
%
% Oct 29, 1993 (MJF)
% In hlist_out change, prevent nullfont from being sent out to .dvi
% file (proposed by B.Raichle).
%   ........ version 2.01
%
% Feb 3, 1992 (MJF)
% effective_char ... modified to include explicit font information. 
% This change ensures that the effective_char returns the character
% in the font when that character actually exists ... and not the
% base character in the substitution list.
% char_info and char_info-end have been modified to overcome a Web
% limitation on two parameters and a Web2c parsing problem. 
% The result is a tex.p(as) file that will NOT compile WITHOUT the
% following change ... (ugh!):
% Globally replace XIIIX with ( and XTTTX with ) in the tex.p(as)
% file.
%
% bug corrections ... made May 8, 1991 ... errors found by B. Raichle
%
% Trip Switch added by PCTeX ... May 1990 -- Section [*638] 
%
% \tracingcharsubdef added June 1990---to aid in debugging---non zero
% values will leave a record of a charsub definition in the log file.
%
%
% Charsub modification of TeX 3.+ to allow for the hyphenation of 
% words with current (May 1990) fonts and explicitly built accents.
%
%============================================================

@x limbo l.64 - bug fix (print only changed modules)
\def\pct!{{\char`\%}} % percent sign in ordinary text
@y
\def\pct!{{\char`\%}} % percent sign in ordinary text
\def\grp{\.{\char'173...\char'175}}
\let\maybe=\iffalse % print only changed modules
@z

@x [1] m.2 l.187 - MLTeX: change banner line
@d banner=='This is TeX, Version 3.14159' {printed when \TeX\ starts}
@y

ML\TeX{} will add new primitives changing the behaviour of \TeX.  The
|banner| string has to be changed.  We do not change the |banner|
string, but will output an additional line to make clear that this is
a modified \TeX{} version.

If you plan to add a command line option ``mltex'' to your \TeX{}
implementation, use the boolean variable |mltex_p| to reflect this
option.  The boolean |mltex_p| should be set {\it before\/} or in the
initialisation routine |initialize| in the Ini\TeX{} run.  (You can
not use a ``mltex'' option in Vir\TeX{}.)  To add this option, remove
the definition of |mltex_p| below and add some code to set it.

The boolean value of |mltex_p| is dumped to the \.{FMT} file and
undumped from this file to the boolean |mltex_enabled_p|.

@d mltex_p==true            {enable ML\TeX{} primitives}
@d banner=='This is TeX, Version 3.14159' {printed when \TeX\ starts}
@z


%---------------------------------------

@x [15] m.209 l.4165 - MLTeX: \charsubdef primitive
@d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
@y
@d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)}
  {or \.{\\charsubdef}}
@z


%---------------------------------------

@x [17] m.220 l.4448 - MLTeX: char_sub_code_base
paragraph shape.
@y
paragraph shape.
Additionally region~4 contains the table with ML\TeX's character
substitution definitions.
@z

@x [17] m.230 l.4731 - MLTeX: char_sub_code_base
@d int_base=math_code_base+256 {beginning of region 5}
@y
@d char_sub_code_base=math_code_base+256 {table of character substitutions}
@d int_base=char_sub_code_base+256 {beginning of region 5}
@z

@x [17] m.230 l.4752 - MLTeX: char_sub_code_base
  {Note: |math_code(c)| is the true math code plus |min_halfword|}
@y
  {Note: |math_code(c)| is the true math code plus |min_halfword|}
@d char_sub_code(#)==equiv(char_sub_code_base+#)
  {Note: |char_sub_code(c)| is the true substitution info plus |min_halfword|}
@z


%---------------------------------------

@x [17] m.236 l.4954 - MLTeX: \charsubdefmax and \tracingcharsubdef
@d int_pars=55 {total number of integer parameters}
@y
@d char_sub_def_min_code=55 {smallest value in the charsubdef list}
@d char_sub_def_max_code=56 {largest value in the charsubdef list}
@d tracing_char_sub_def_code=57 {traces changes to a charsubdef def}
@d int_pars=58 {total number of integer parameters}
@z

@x [17] m.236 l.5016
@d error_context_lines==int_par(error_context_lines_code)
@y
@d error_context_lines==int_par(error_context_lines_code)
@d char_sub_def_min==int_par(char_sub_def_min_code)
@d char_sub_def_max==int_par(char_sub_def_max_code)
@d tracing_char_sub_def==int_par(tracing_char_sub_def_code)
@z

@x [17] m.237 l.5080
error_context_lines_code:print_esc("errorcontextlines");
@y
error_context_lines_code:print_esc("errorcontextlines");
char_sub_def_min_code:print_esc("charsubdefmin");
char_sub_def_max_code:print_esc("charsubdefmax");
tracing_char_sub_def_code:print_esc("tracingcharsubdef");
@z

@x [17] m.238 l.5200
@!@:error_context_lines_}{\.{\\errorcontextlines} primitive@>
@y
@!@:error_context_lines_}{\.{\\errorcontextlines} primitive@>
if mltex_p then
  begin mltex_enabled_p:=true;  {enable character substitution}
  if false then {remove the if-clause to enable \.{\\charsubdefmin}}
  primitive("charsubdefmin",assign_int,int_base+char_sub_def_min_code);@/
@!@:char_sub_def_min_}{\.{\\charsubdefmin} primitive@>
  primitive("charsubdefmax",assign_int,int_base+char_sub_def_max_code);@/
@!@:char_sub_def_max_}{\.{\\charsubdefmax} primitive@>
  primitive("tracingcharsubdef",assign_int,int_base+tracing_char_sub_def_code);@/
@!@:tracing_char_sub_def_}{\.{\\tracingcharsubdef} primitive@>
  end;
@z

@x [17] m.240 l.5213
for k:=int_base to del_code_base-1 do eqtb[k].int:=0;
@y
for k:=int_base to del_code_base-1 do eqtb[k].int:=0;
char_sub_def_min:=256; char_sub_def_max:=-1;
{allow \.{\\charsubdef} for char 0}@/
{|tracing_char_sub_def:=0| is already done}@/
@z


%---------------------------------------

@x [29] m.534 l.10293 - MLTeX: add. MLTeX banner after loading fmt file
@<Print the banner line, including the date and time@>;
@y
@<Print the banner line, including the date and time@>;
if mltex_enabled_p then
  begin wlog_cr; wlog('MLTeX v2.2 enabled');
  end;
@z


%---------------------------------------

@x [30] m.554 l.10795 - MLTeX: |effective_char| in |char_info|
as fast as possible under the circumstances.
@^inner loop@>

@d char_info_end(#)==#].qqqq
@d char_info(#)==font_info[char_base[#]+char_info_end
@y
as fast as possible under the circumstances.

ML\TeX{} will assume that a character |c| exists iff either exists in
the current font or a character substitution definition for this
character was defined using \.{\\charsubdef}.  To avoid the
distinction between these two cases, ML\TeX{} introduces the notion
``effective character'' of an input character |c|.  If |c| exists in
the current font, the effective character of |c| is the character |c|
itself.  If it doesn't exist but a character substitution is defined,
the effective character of |c| is the base character defined in the
character substitution.  If there is an effective character for a
non-existing character |c|, the ``virtual character'' |c| will get
appended to the horizontal lists.

The effective character is used within |char_info| to access
appropriate character descriptions in the font.  For example, when
calculating the width of a box, ML\TeX{} will use the metrics of the
effective characters.  For the case of a substitution, ML\TeX{} uses
the metrics of the base character, ignoring the metrics of the accent
character.

If character substitutions are changed, it will be possible that a
character |c| neither exists in a font nor there is a valid character
substitution for |c|.  To handle these cases |effective_char| should
be called with its first argument set to |true| to ensure that it
will still return an existing character in the font.  If neither |c|
nor the substituted base character in the current character
substitution exists, |effective_char| will output a warning and
return the character |font_bc[f]| (which is incorrect, but can not be
changed within the current framework).

Sometimes character substitutions are unwanted, therefore the
original definition of |char_info| can be used using the macro
|orig_char_info|.  Operations in which character substitutions should
be avoided are, for example, loading a new font and checking the font
metric information in this font, and character accesses in math mode.

(Because of retrictions in \.{TANGLE}'s macro capabilities you have
to replace \.{XLPAREN} resp.\ \.{xlparen} with an opening brace and
\.{XRPAREN} resp.\ \.{xrparen} with a closing brace after tangling
\TeX!)
@^inner loop@>

@d char_list_exists(#)==(char_sub_code(#)>hi(0))
@d char_list_accent(#)==(ho(char_sub_code(#)) div 256)
@d char_list_char(#)==(ho(char_sub_code(#)) mod 256)
@#
@d char_info_end(#)== #xrparen].qqqq
@d char_info(#)==
  font_info[char_base[#]+effective_char xlparen true,#,char_info_end
@#
@d orig_char_info_end(#)==#].qqqq
@d orig_char_info(#)==font_info[char_base[#]+orig_char_info_end
@#
@z

@x [30] m.560 l.10876 - MLTeX: |effective_char| in |char_info|
@p function read_font_info(@!u:pointer;@!nom,@!aire:str_number;
@y
@p @t\4@>@<Declare additional functions for ML\TeX@>@/

function read_font_info(@!u:pointer;@!nom,@!aire:str_number;
@z


% Bug report:  (Nov/Dec 95)
% When
%  1) character substitution \charsubdef x1=x2 x3 is active and
%  2) font Y with character x1 but the base char x3 doesn't exist
% the error message 
%    ! Font \test=test not loadable: Bad metric (TFM) file.
% will appear when loading Y.
%
% Bug analysis:
% !! |font_bc[f]| and |font_ec[f]| are set at the end of the loading
% !! process in module 576.  |effective_char| uses these two
% !! values, therefore never use |effective_char| or |char_info|
% !! until the end of |read_font_info|.
%
% There are three places in which |char_info| is called:
%  a) @<Check for charlist cycle@>
%     no box dimensions are read at this time, i.e.
%     width index is 0 for each character and
%     font_ec/bc have undefined values
%     => |effective_char| assumes that char x1 doesn't exist
%     ==> x1 gets replaced by the "effective" character x3
%         which doesn't exist!  ==> error message
%  b) @<Read ligature/kern program@>, macro |check_existence|
%     box dimensions are already read, i.e. width index<>0 and
%     ligature/kerning program contains "real" characters,
%     nonetheless font_bc/ec have undefined values!
%     => |effective_char| assumes that char x1 doesn't exist
%        in most cases (because font_bc/ec are normally zeroed)
%     ==> error message
%  c) @<Make final adjustments...@>
%     code decides if boundary character exists in the font,
%     it will be ok---normally, but font_bc/ec... :-(
%     => |effective_char| assumes that char x1 doesn't exist
%        in most cases (because font_bc/ec are normally zeroed)
%     ==> wrong _if_ effective character exits but not the
%         boundary character and vice versa
%     This is another bug, because the value of the flag
%     |font_false_bchar| depends on current substitution.
%
% The cause of the error message was the undefined values of
% |font_bc[f]| and |font_ec[f]|, and even if they will have
% defined values there would be error because of a) and, if a
% font contains a boundary character, c).
%
% Bug fix:
% Don't use |effective_char|, use the original definition of
% |char_info| named |orig_char_info|.
%

@x [30] m.570 l.11064 - MLTeX: fix for bug while loading font
  begin qw:=char_info(f)(d);
@y
  begin qw:=orig_char_info(f)(d);
@z
@x [30] m.573 l.11116 - MLTeX: fix for bug while loading font
  qw:=char_info(f)(#); {N.B.: not |qi(#)|}
@y
  qw:=orig_char_info(f)(#); {N.B.: not |qi(#)|}
@z
@x [30] m.576 l.11180 - MLTeX: fix for bug while loading font
  begin qw:=char_info(f)(bchar); {N.B.: not |qi(bchar)|}
@y
  begin qw:=orig_char_info(f)(bchar); {N.B.: not |qi(bchar)|}
@z


%---------------------------------------

@x [30] m.582 l.11276 - MLTeX: call |effective_char| in |new_character|
@p function new_character(@!f:internal_font_number;@!c:eight_bits):pointer;
label exit;
var p:pointer; {newly allocated node}
begin if font_bc[f]<=c then if font_ec[f]>=c then
  if char_exists(char_info(f)(qi(c))) then
@y

This allows a character node to be used if there is an equivalent
in the |char_sub_code| list.

@p function new_character(@!f:internal_font_number;@!c:eight_bits):pointer;
label exit;
var p:pointer; {newly allocated node}
@!ec:quarterword;  {effective character of |c|}
begin ec:=effective_char(false,f,qi(c));
if font_bc[f]<=qo(ec) then if font_ec[f]>=qo(ec) then
  if char_exists(orig_char_info(f)(ec)) then  {N.B.: not |char_info|}
@z


%---------------------------------------

@x [32] m.619 l.12294 - MLTeX: substitute character in |hlist_out|
procedure hlist_out; {output an |hlist_node| box}
label reswitch, move_past, fin_rule, next_p;
@y
procedure hlist_out; {output an |hlist_node| box}
label reswitch, move_past, fin_rule, next_p, continue, found;
@z

@x [32] m.620 l.12326 - MLTeX: replace virtual character in |hlist_out|
reaching a non-|char_node|. The program uses the fact that |set_char_0=0|.
@^inner loop@>
@y
reaching a non-|char_node|. The program uses the fact that |set_char_0=0|.

In ML\TeX{} this part looks for the existence of a substitution
definition for a character |c|, if |c| does not exist in the font,
and create appropriate \.{DVI} commands.  Former versions of ML\TeX{}
have spliced appropriate character, kern, and box nodes into the
horizontal list.
%
% 91/05/08 \charsubdefmax bug detected by Bernd Raichle
Because the user can change character substitions or
\.{\\charsubdefmax} on the fly, we have to test a again
for valid substitutions.
%
% 93/10/29 \leaders bug detected by Eberhard Mattes
%      ==> prevent nullfont from being sent to .dvi file (B.R.)
(Additional it is necessary to be careful---if leaders are used
the current hlist is normally traversed more than once!)
@^inner loop@>
@z

@x [32] m.620 l.12334 -- MLTeX: substitute character during |shipout|
  if c>=qi(128) then dvi_out(set1);
  dvi_out(qo(c));@/
  cur_h:=cur_h+char_width(f)(char_info(f)(c));
@y
  if font_ec[f]>=qo(c) then if font_bc[f]<=qo(c) then
    if char_exists(orig_char_info(f)(c)) then  {N.B.: not |char_info|}
      begin if c>=qi(128) then dvi_out(set1);
      dvi_out(qo(c));@/
      cur_h:=cur_h+char_width(f)(orig_char_info(f)(c));
      goto continue;
      end;
  if mltex_enabled_p then
    @<Output a substitution, |goto continue| if not possible@>;
continue:
@z


%---------------------------------------

% The MLTeX changes never dealt with the problems of character 
% substitutions in math mode.  With the new additions in v2.2,
% non-existing characters between |font_bc[f]| and |font_ec[f]|
% can be substituted => we have to avoid this in math mode
% (for compatibility reasons and to avoid other problems).

@x [35] m.708 l.13903 -- MLTeX: avoid substitution in |var_delimiter|
if (qo(y)>=font_bc[g])and(qo(y)<=font_ec[g]) then
  begin continue: q:=char_info(g)(y);
@y
if (qo(y)>=font_bc[g])and(qo(y)<=font_ec[g]) then
  begin continue: q:=orig_char_info(g)(y);
@z

@x [36] m.722 l.14172 -- MLTeX: avoid substitution in |fetch|
else  begin if (qo(cur_c)>=font_bc[cur_f])and(qo(cur_c)<=font_ec[cur_f]) then
    cur_i:=char_info(cur_f)(cur_c)
@y
else  begin if (qo(cur_c)>=font_bc[cur_f])and(qo(cur_c)<=font_ec[cur_f]) then
    cur_i:=orig_char_info(cur_f)(cur_c)
@z

@x [36] m.740 l.14486 -- MLTeX: avoid substitution in |make_math_accent|
  i:=char_info(f)(y);
@y
  i:=orig_char_info(f)(y);
@z

@x [36] m.749 l.14638 -- MLTeX: avoid substitution in |make_op|
    begin c:=rem_byte(cur_i); i:=char_info(cur_f)(c);
@y
    begin c:=rem_byte(cur_i); i:=orig_char_info(cur_f)(c);
@z

%---------------------------------------

%%
%% We can rewrite the original code after "main_loop_move+2" upto the
%% "tail_append(lig_stack)" in module 1036 as
%%
%
% main_loop_move+2:
% if font_bc[main_f]<=cur_chr then
%  if cur_chr<=font_ec[main_f] then
%    begin  main_i:=char_info(main_f)(cur_l);
%    if char_exists(main_i) goto main_loop_move+3;
%    end;
% char_warning(main_f,cur_chr); free_avail(lig_stack); goto big_switch;
% main_loop_move+3:
% tail_append(lig_stack) {|main_loop_lookahead| is next}
%
%%
%% We can use the rewritten code above to include additional MLTeX
%% specific parts in the future.  Additionally it can be used when
%% optimizing |main_control| to minimize the call of the function
%% |effective_char|.
%%
%
%@x [46] m.1030 l.19977 - MLTeX: substitution in |main_control|
%  main_loop_move,main_loop_move+1,main_loop_move+2,main_loop_move_lig,
%@y
%  main_loop_move,main_loop_move+1,main_loop_move+2,main_loop_move+3,
%  main_loop_move_lig,
%@z

@x [46] m.1036 l.20138 - MLTeX: substitution in |main_control|
main_loop_move+2:if(cur_chr<font_bc[main_f])or(cur_chr>font_ec[main_f]) then
@y
main_loop_move+2:
if(qo(effective_char(false,main_f,qi(cur_chr)))>font_ec[main_f])or
  (qo(effective_char(false,main_f,qi(cur_chr)))<font_bc[main_f]) then
@z

@x [46] m.1036 l.20141 - MLTeX: substitution in |main_control|
main_i:=char_info(main_f)(cur_l);
@y
main_i:=effective_char_info(main_f,cur_l);
@z


%---------------------------------------

@x [49] m.1222 l.22794 - MLTeX: \charsubdef primitive
@d toks_def_code=6 {|shorthand_def| for \.{\\toksdef}}
@y
@d toks_def_code=6 {|shorthand_def| for \.{\\toksdef}}
@d char_sub_def_code=7 {|shorthand_def| for \.{\\charsubdef}}
@z

@x [49] m.1222 l.22810 - MLTeX: \charsubdef primitive
@!@:toks_def_}{\.{\\toksdef} primitive@>
@y
@!@:toks_def_}{\.{\\toksdef} primitive@>
if mltex_p then
  begin
  primitive("charsubdef",shorthand_def,char_sub_def_code);@/
@!@:char_sub_def_}{\.{\\charsubdef} primitive@>
  end;
@z

@x [49] m.1222 l.22820 - MLTeX: \charsubdef primitive
  othercases print_esc("toksdef")
@y
  char_sub_def_code: print_esc("charsubdef");
  othercases print_esc("toksdef")
@z

@x [49] m.1222 l.22833 - MLTeX: \charsubdef primitive
shorthand_def: begin n:=cur_chr; get_r_token; p:=cur_cs; define(p,relax,256);
@y
shorthand_def: if cur_chr=char_sub_def_code then
 begin scan_char_num; p:=char_sub_code_base+cur_val; scan_optional_equals;
  scan_char_num; n:=cur_val; {accent character in substitution}
  scan_char_num;
  if (tracing_char_sub_def>0) then
    begin begin_diagnostic; print_nl("New character substitution: ");
    print_ASCII(p-char_sub_code_base); print(" = ");
    print_ASCII(n); print_char(" ");
    print_ASCII(cur_val); end_diagnostic(false);
    end;
  n:=n*256+cur_val;
  define(p,data,hi(n));
  if (p-char_sub_code_base)<char_sub_def_min then
    word_define(int_base+char_sub_def_min_code,p-char_sub_code_base);
  if (p-char_sub_code_base)>char_sub_def_max then
    word_define(int_base+char_sub_def_max_code,p-char_sub_code_base);
 end
else begin n:=cur_chr; get_r_token; p:=cur_cs; define(p,relax,256);
@z


%---------------------------------------

@x [50] m.1302 l.23694 - MLTeX: dump |mltex_p| to fmt file
@<Dump constants for consistency check@>;
@y
@<Dump constants for consistency check@>;
dump_int(@"4D4C5458);  {ML\TeX's magic constant: "MLTX"}
if mltex_p then dump_int(1)
else dump_int(0);
@z

@x [50] m.1303 l.23694 - MLTeX: undump |mltex_enabled_p| from fmt file
begin @<Undump constants for consistency check@>;
@y
begin @<Undump constants for consistency check@>;
undump_int(x);   {check magic constant of ML\TeX}
if x<>@"4D4C5458 then goto bad_fmt;
undump_int(x);   {undump |mltex_p| flag into |mltex_enabled_p|}
if x=1 then mltex_enabled_p:=true
else if x<>0 then goto bad_fmt;
@z

@x [51] m.1337 l.24371 - MLTeX: add. MLTeX banner after loading fmt file
fix_date_and_time;@/
@y
if mltex_enabled_p then
  begin wterm_ln('MLTeX v2.2 enabled');
  end;
fix_date_and_time;@/
@z


%---------------------------------------

@x [54] m.1376 l.24903 - MLTeX: additional routines
@* \[54] System-dependent changes.
@y
@* \[54/ML\TeX] System-dependent changes for ML\TeX.

The boolean variable |mltex_enabled_p| is used to enable ML\TeX's
character substitution.  It is initialised to |false|.  When loading
a \.{FMT} it is set to the value of the boolean |mltex_p| saved in
the \.{FMT} file.  Additionally it is set to the value of |mltex_p|
in Ini\TeX.

@<Glob...@>=
@!mltex_enabled_p:boolean;  {enable character substitution}


@ @<Set init...@>=
mltex_enabled_p:=false;


@ The function |effective_char| computes the effective character with
respect to font information.  The effective character is either the
base character part of a character substitution definition, if the
character does not exist in the font or the character itself.

Inside |effective_char| we can not use |char_info| because the macro
|char_info| uses |effective_char| calling this function a second time
with the same arguments.

If neither the character |c| exists in font |f| nor a character
substitution for |c| was defined, you can not use the function value
as a character offset in |char_info| because it will access an
undefined or invalid |font_info| entry!  Therefore inside |char_info|
and in other places, |effective_char|'s boolean parameter |err_p| is
set to |true| to issue a warning and return the incorrect
replacement, but always existing character |font_bc[f]|.
@^inner loop@>

@<Declare additional functions for ML\TeX@>=
function effective_char(@!err_p:boolean;
                        @!f:internal_font_number;@!c:quarterword):integer;
label found;
var base_c: integer; {or |eightbits|: replacement base character}
@!result: integer; {or |quarterword|}
begin result:=c;  {return |c| unless it does not exist in the font}
if not mltex_enabled_p then goto found;
if font_ec[f]>=qo(c) then if font_bc[f]<=qo(c) then
  if char_exists(orig_char_info(f)(c)) then  {N.B.: not |char_info|(f)(c)}
    goto found;
if qo(c)>=char_sub_def_min then if qo(c)<=char_sub_def_max then
  if char_list_exists(qo(c)) then
    begin base_c:=char_list_char(qo(c));
    result:=qi(base_c);  {return |base_c|}
    if not err_p then goto found;
    if font_ec[f]>=base_c then if font_bc[f]<=base_c then
      if char_exists(orig_char_info(f)(qi(base_c))) then goto found;
    end;
if err_p then  {print error and return existing character?}
  begin begin_diagnostic;
  print_nl("Missing character: There is no "); print("substitution for ");
@.Missing character@>
  print_ASCII(qo(c)); print(" in font ");
  slow_print(font_name[f]); print_char("!"); end_diagnostic(false);
  result:=qi(font_bc[f]); {N.B.: not non-existing character |c|!}
  end;
found: effective_char:=result;
end;


@ The function |effective_char_info| is equivalent to |char_info|,
except it will return |null_character| if neither the character |c|
exists in font |f| nor is there a substitution definition for |c|.
(For these cases |char_info| using |effective_char| will access an
undefined or invalid |font_info| entry.  See the documentation of
|effective_char| for more information.)
@^inner loop@>

@<Declare additional functions for ML\TeX@>=
function effective_char_info(@!f:internal_font_number;
                             @!c:quarterword):four_quarters;
label exit;
var ci:four_quarters; {character information bytes for |c|}
@!base_c:integer; {or |eightbits|: replacement base character}
begin if not mltex_enabled_p then
  begin effective_char_info:=orig_char_info(f)(c); return;
  end;
if font_ec[f]>=qo(c) then if font_bc[f]<=qo(c) then
  begin ci:=orig_char_info(f)(c);  {N.B.: not |char_info|(f)(c)}
  if char_exists(ci) then
    begin effective_char_info:=ci; return;
    end;
  end;
if qo(c)>=char_sub_def_min then if qo(c)<=char_sub_def_max then
  if char_list_exists(qo(c)) then
    begin {|effective_char_info:=char_info(f)(qi(char_list_char(qo(c))));|}
    base_c:=char_list_char(qo(c));
    if font_ec[f]>=base_c then if font_bc[f]<=base_c then
      begin ci:=orig_char_info(f)(qi(base_c));  {N.B.: not |char_info|(f)(c)}
      if char_exists(ci) then
        begin effective_char_info:=ci; return;
        end;
      end;
    end;
effective_char_info:=null_character;
exit:end;


@ This code is called for a virtual character |c| in |hlist_out|
during |ship_out|.  It tries to built a character substitution
construct for |c| generating appropriate \.{DVI} code using the
character substitution definition for this character.  If a valid
character substitution exists \.{DVI} code is created as if
|make_accent| was used.  In all other cases the status of the
substituion for this character has been changed between the creation
of the character node in the hlist and the output of the page---the
created \.{DVI} code will be correct but the visual result will be
undefined.

Former ML\TeX\ versions have replaced the character node by a
sequence of character, box, and accent kern nodes splicing them into
the original horizontal list.  This version does not do this to avoid
a)~a memory overflow at this processing stage, b)~additional code to
add a pointer to the previous node needed for the replacement, and
c)~to avoid wrong code resulting in anomalies because of the use
within a \.{\\leaders} box.

@<Output a substitution, |goto continue| if not possible@>=
  begin
  @<Get substitution information, check it, goto |found|
  if all is ok, otherwise goto |continue|@>;
found: @<Print character substition tracing log@>;
  @<Rebuild character using substitution information@>;
  end


@ The global variables for the code to substitute a virtual character
can be declared as local.  Nonetheless we declare them as global to
avoid stack overflows because |hlist_out| can be called recursivly.
 
@<Glob...@>=
@!accent_c,@!base_c,@!replace_c:integer;
@!ia_c,@!ib_c:four_quarters; {accent and base character information}
@!base_slant,@!accent_slant:real; {amount of slant}
@!base_x_height:scaled; {accent is designed for characters of this height}
@!base_width,@!base_height:scaled; {height and width for base character}
@!accent_width,@!accent_height:scaled; {height and width for accent}
@!delta:scaled; {amount of right shift}


@ Get the character substitution information in |char_sub_code| for
the character |c|.  The current code checks that the substition
exists and is valid and all substitution characters exist in the
font, so we can {\it not\/} substitute a character used in a
substitution.  This simplifies the code because we have not to check
for cycles in all character substitution definitions.

@<Get substitution information, check it...@>=
  if qo(c)>=char_sub_def_min then if qo(c)<=char_sub_def_max then
    if char_list_exists(qo(c)) then
      begin  base_c:=char_list_char(qo(c));
      accent_c:=char_list_accent(qo(c));
      if (font_ec[f]>=base_c) then if (font_bc[f]<=base_c) then
        if (font_ec[f]>=accent_c) then if (font_bc[f]<=accent_c) then
          begin ia_c:=char_info(f)(qi(accent_c));
          ib_c:=char_info(f)(qi(base_c));
          if char_exists(ib_c) then
            if char_exists(ia_c) then goto found;
          end;
      begin_diagnostic;
      print_nl("Missing character: Incomplete substitution ");
@.Missing character@>
      print_ASCII(qo(c)); print(" = "); print_ASCII(accent_c);
      print(" "); print_ASCII(base_c); print(" in font ");
      slow_print(font_name[f]); print_char("!"); end_diagnostic(false);
      goto continue;
      end;
  begin_diagnostic;
  print_nl("Missing character: There is no "); print("substitution for ");
@.Missing character@>
  print_ASCII(qo(c)); print(" in font ");
  slow_print(font_name[f]); print_char("!"); end_diagnostic(false);
  goto continue


@ For |tracinglostchars>99| the substitution is shown in the log file.

@<Print character substition tracing log@>=
 if tracing_lost_chars>99 then
   begin begin_diagnostic;
   print_nl("Using character substitution: ");
   print_ASCII(qo(c)); print(" = ");
   print_ASCII(accent_c); print(" "); print_ASCII(base_c);
   print(" in font "); slow_print(font_name[f]); print_char(".");
   end_diagnostic(false);
   end


@ This outputs the accent and the base character given in the
substitution.  It uses code virtually identical to the |make_accent|
procedure, but without the node creation steps.
 
Additionally if the accent character has to be shifted vertically it
does {\it not\/} create the same code.  The original routine in
|make_accent| and former versions of ML\TeX{} creates a box node
resulting in |push| and |pop| operations, whereas this code simply
produces vertical positioning operations.  This can influence the
pixel rounding algorithm in some \.{DVI} drivers---and therefore will
probably be changed in one of the next ML\TeX{} versions.

@<Rebuild character using substitution information@>=
  base_x_height:=x_height(f);
  base_slant:=slant(f)/float_constant(65536);
@^real division@>
  accent_slant:=base_slant; {slant of accent character font}
  base_width:=char_width(f)(ib_c);
  base_height:=char_height(f)(height_depth(ib_c));
  accent_width:=char_width(f)(ia_c);
  accent_height:=char_height(f)(height_depth(ia_c));@/
  {compute necessary horizontal shift (don't forget slant)}
  delta:=round((base_width-accent_width)/float_constant(2)+
            base_height*base_slant-base_x_height*accent_slant);
@^real multiplication@>
@^real addition@>
  dvi_h:=cur_h;  {update |dvi_h|, similar to the last statement in module 620}@/
  {1. For centering/horizontal shifting insert a kern node.}
  cur_h:=cur_h+delta; synch_h;@/
  {2. Then insert the accent character possibly shifted up or down.}
  if ((base_height<>base_x_height) and (accent_height>0)) then
    begin {the accent must be shifted up or down}
    cur_v:=base_line+(base_x_height-base_height); synch_v;
    if accent_c>=128 then dvi_out(set1);
    dvi_out(accent_c);
    cur_v:=base_line;
    end
  else begin synch_v;
    if accent_c>=128 then dvi_out(set1);
    dvi_out(accent_c);
    end;
  cur_h:=cur_h+accent_width; dvi_h:=cur_h;@/
  {3. For centering/horizontal shifting insert another kern node.}
  cur_h:=cur_h+(-accent_width-delta);@/
  {4. Output the base character.}
  synch_h; synch_v;
  if base_c>=128 then dvi_out(set1);
  dvi_out(base_c);@/
  cur_h:=cur_h+base_width;
  dvi_h:=cur_h {update of |dvi_h| is unnecessary, will be set in module 620}

 
@* \[54] System-dependent changes.
@z
%
%%% --- end of MLTEX.CH ---