@x \def\?##1]{\hbox to 1in{\hfil##1.\ }} } @y \def\?##1]{\hbox{Changes to \hbox to 1em{\hfil##1}.\ }} } \let\maybe=\iffalse @z @x November 1984]. @y November 1984]. 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. @z @x @d banner=='This is TeX, Version 3.14159' {printed when \TeX\ starts} @y @d banner=='This is PDFTeX, Version 0.11 (based on TeX Version 3.14159)' {printed when \TeX\ starts} @z @x Actually the heading shown here is not quite normal: The |program| line does not mention any |output| file, because \ph\ would ask the \TeX\ user to specify a file name if |output| were specified here. @^system dependencies@> @y @z @x program TEX; {all file names are defined dynamically} label @@/ @y program TEX; {all file names are defined dynamically} @z @x @ Three labels must be declared in the main program, so we give them symbolic names. @d start_of_TEX=1 {go here when \TeX's variables are initialized} @d end_of_TEX=9998 {go here to close files and terminate gracefully} @d final_end=9999 {this label marks the ending of the program} @= start_of_TEX@t\hskip-2pt@>, end_of_TEX@t\hskip-2pt@>,@,final_end; {key control points} @y @ For Web2c, labels are not declared in the main program, but we still have to declare the symbolic names. @d start_of_TEX=1 {go here when \TeX's variables are initialized} @d final_end=9999 {this label marks the ending of the program} @z @x @d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging} @d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging} @y @d debug==ifdef('TEXMF_DEBUG') @d gubed==endif('TEXMF_DEBUG') @z @x @d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering usage statistics} @d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering usage statistics} @y @d stat==ifdef('STAT') @d tats==endif('STAT') @z @x @d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version} @d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version} @y @d init==ifdef('INITEX') @d tini==endif('INITEX') @z @x @!init @@;@+tini @y @!init if ini_version then begin @@; end; @+tini @z @x @= @!mem_max=30000; {greatest index in \TeX's internal |mem| array; must be strictly less than |max_halfword|; must be equal to |mem_top| in \.{INITEX}, otherwise |>=mem_top|} @!mem_min=0; {smallest index in \TeX's internal |mem| array; must be |min_halfword| or more; must be equal to |mem_bot| in \.{INITEX}, otherwise |<=mem_bot|} @!buf_size=500; {maximum number of characters simultaneously present in current lines of open files and in control sequences between \.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|} @!error_line=72; {width of context lines on terminal error messages} @!half_error_line=42; {width of first lines of contexts in terminal error messages; should be between 30 and |error_line-15|} @!max_print_line=79; {width of longest text lines output; should be at least 60} @!stack_size=200; {maximum number of simultaneous input sources} @!max_in_open=6; {maximum number of input files and error insertions that can be going on simultaneously} @!font_max=75; {maximum internal font number; must not exceed |max_quarterword| and must be at most |font_base+256|} @!font_mem_size=20000; {number of words of |font_info| for all fonts} @!param_size=60; {maximum number of simultaneous macro parameters} @!nest_size=40; {maximum number of semantic levels simultaneously active} @!max_strings=3000; {maximum number of strings; must not exceed |max_halfword|} @!string_vacancies=8000; {the minimum number of characters that should be available for the user's control sequences and font names, after \TeX's own error messages are stored} @!pool_size=32000; {maximum number of characters in strings, including all error messages and help texts, and the names of all fonts and control sequences; must exceed |string_vacancies| by the total length of \TeX's own strings, which is currently about 23000} @!save_size=600; {space for saving values outside of current group; must be at most |max_halfword|} @!trie_size=8000; {space for hyphenation patterns; should be larger for \.{INITEX} than it is in production versions of \TeX} @!trie_op_size=500; {space for ``opcodes'' in the hyphenation patterns} @!dvi_buf_size=800; {size of the output buffer; must be a multiple of 8} @!file_name_size=40; {file names shouldn't be longer than this} @!pool_name='TeXformats:TEX.POOL '; {string of length |file_name_size|; tells where the string pool appears} @y @d file_name_size == maxint @d ssup_error_line = 255 @d ssup_max_strings ==65535 {Larger values may be used, but then the arrays consume much more memory.} @d ssup_trie_opcode == 65535 @d ssup_trie_size == 65535 @d ssup_hyph_size == 65535 {Changing this requires changing (un)dumping!} @d iinf_hyphen_size == 610 {Must be not less than |hyph_prime|!} @d max_font_max=2000 {maximum number of internal fonts; this can be increased, but |hash_size+max_font_max| should not exceed 29000.} @d font_base=0 {smallest internal font number; must be |>= min_quarterword|; do not change this without modifying the dynamic definition of the font arrays.} @= @!mem_bot=@"8000000; {smallest index in the |mem| array dumped by \.{INITEX}; must not be less than |mem_min|} {Use |mem_bot=0| for compilers which cannot decrement pointers.} @!hash_offset=514; {smallest index in hash array, i.e., |hash_base| } {Use |hash_offset=0| for compilers which cannot decrement pointers.} @!trie_op_size=1501; {space for ``opcodes'' in the hyphenation patterns; best if relatively prime to 313, 361, and 1009.} @!neg_trie_op_size=-1501; {for lower |trie_op_hash| array bound; must be equal to |-trie_op_size|.} @!min_trie_op=0; {first possible trie op code for any language} @!max_trie_op=ssup_trie_opcode; {largest possible trie opcode for any language} @!pool_name=PDFTEX_POOL_NAME; {this is configurable, for the sake of ML-\TeX} {string of length |file_name_size|; tells where the string pool appears} @# @!inf_main_memory = 2999; @!sup_main_memory = 8000000; @!inf_trie_size = 8000; @!sup_trie_size = ssup_trie_size; @!inf_max_strings = 3000; @!sup_max_strings = ssup_max_strings; @!inf_buf_size = 500; @!sup_buf_size = 30000; @!inf_nest_size = 40; @!sup_nest_size = 400; @!inf_max_in_open = 6; @!sup_max_in_open = 127; @!inf_param_size = 60; @!sup_param_size = 600; @!inf_save_size = 600; @!sup_save_size = 40000; @!inf_stack_size = 200; @!sup_stack_size = 3000; @!inf_dvi_buf_size = 800; @!sup_dvi_buf_size = 65536; @!inf_font_mem_size = 20000; @!sup_font_mem_size = 1000000; @!sup_font_max = max_font_max; @!inf_font_max = 50; {could be smaller, but why?} @!inf_pool_size = 32000; @!sup_pool_size = 10000000; @!inf_pool_free = 1000; @!sup_pool_free = sup_pool_size; @!inf_string_vacancies = 8000; @!sup_string_vacancies = sup_pool_size - 23000; @!sup_hash_extra = sup_max_strings; @!inf_hash_extra = 0; @!sup_hyph_size = ssup_hyph_size; @!inf_hyph_size = iinf_hyphen_size; {Must be not less than |hyph_prime|!} @z @x @d mem_bot=0 {smallest index in the |mem| array dumped by \.{INITEX}; must not be less than |mem_min|} @d mem_top==30000 {largest index in the |mem| array dumped by \.{INITEX}; must be substantially larger than |mem_bot| and not greater than |mem_max|} @d font_base=0 {smallest internal font number; must not be less than |min_quarterword|} @d hash_size=2100 {maximum number of control sequences; it should be at most about |(mem_max-mem_min)/10|} @d hash_prime=1777 {a prime number equal to about 85\pct! of |hash_size|} @d hyph_size=307 {another prime; the number of \.{\\hyphenation} exceptions} @y @d hash_size=10000 {maximum number of control sequences; it should be at most about |(mem_max-mem_min)/10|} @d hash_prime=8501 {a prime number equal to about 85\pct! of |hash_size|} @d hyph_prime=607 {another prime for hashing \.{\\hyphenation} exceptions; if you change this, you should also change |iinf_hyphen_size|.} @z @x @d incr(#) == #:=#+1 {increase a variable by unity} @d decr(#) == #:=#-1 {decrease a variable by unity} @y @z @x @d text_char == char {the data type of characters in text files} @y @d text_char == ASCII_code {the data type of characters in text files} @z @x for i:=0 to @'37 do xchr[i]:=' '; for i:=@'177 to @'377 do xchr[i]:=' '; @y for i:=0 to @'37 do xchr[i]:=chr(i); for i:=@'177 to @'377 do xchr[i]:=chr(i); @z @x @!name_of_file:packed array[1..file_name_size] of char;@;@/ {on some systems this may be a \&{record} variable} @y @!name_of_file:^text_char; @z @x @ The \ph\ compiler with which the present version of \TeX\ was prepared has extended the rules of \PASCAL\ in a very convenient way. To open file~|f|, we can write $$\vbox{\halign{#\hfil\qquad&#\hfil\cr |reset(f,@t\\{name}@>,'/O')|&for input;\cr |rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$ The `\\{name}' parameter, which is of type `{\bf packed array $[\langle\\{any}\rangle]$ of \\{char}}', stands for the name of the external file that is being opened for input or output. Blank spaces that might appear in \\{name} are ignored. The `\.{/O}' parameter tells the operating system not to issue its own error messages if something goes wrong. If a file of the specified name cannot be found, or if such a file cannot be opened for some other reason (e.g., someone may already be trying to write the same file), we will have |@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|. This allows \TeX\ to undertake appropriate corrective action. @:PASCAL H}{\ph@> @^system dependencies@> \TeX's file-opening procedures return |false| if no file identified by |name_of_file| could be opened. @d reset_OK(#)==erstat(#)=0 @d rewrite_OK(#)==erstat(#)=0 @p function a_open_in(var f:alpha_file):boolean; {open a text file for input} begin reset(f,name_of_file,'/O'); a_open_in:=reset_OK(f); end; @# function a_open_out(var f:alpha_file):boolean; {open a text file for output} begin rewrite(f,name_of_file,'/O'); a_open_out:=rewrite_OK(f); end; @# function b_open_in(var f:byte_file):boolean; {open a binary file for input} begin reset(f,name_of_file,'/O'); b_open_in:=reset_OK(f); end; @# function b_open_out(var f:byte_file):boolean; {open a binary file for output} begin rewrite(f,name_of_file,'/O'); b_open_out:=rewrite_OK(f); end; @# function w_open_in(var f:word_file):boolean; {open a word file for input} begin reset(f,name_of_file,'/O'); w_open_in:=reset_OK(f); end; @# function w_open_out(var f:word_file):boolean; {open a word file for output} begin rewrite(f,name_of_file,'/O'); w_open_out:=rewrite_OK(f); end; @y @ All of the file opening functions are defined in C. @z @x @ Files can be closed with the \ph\ routine `|close(f)|', which @^system dependencies@> should be used when all input or output with respect to |f| has been completed. This makes |f| available to be opened again, if desired; and if |f| was used for output, the |close| operation makes the corresponding external file appear on the user's area, ready to be read. These procedures should not generate error messages if a file is being closed before it has been successfully opened. @p procedure a_close(var f:alpha_file); {close a text file} begin close(f); end; @# procedure b_close(var f:byte_file); {close a binary file} begin close(f); end; @# procedure w_close(var f:word_file); {close a word file} begin close(f); end; @y @ And all the file closing routines as well. @z @x @!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read} @y @!buffer:^ASCII_code; {lines of characters being read} @z @x @p function input_ln(var f:alpha_file;@!bypass_eoln:boolean):boolean; {inputs the next line or returns |false|} var last_nonblank:0..buf_size; {|last| with trailing blanks removed} begin if bypass_eoln then if not eof(f) then get(f); {input the first character of the line into |f^|} last:=first; {cf.\ Matthew 19\thinspace:\thinspace30} if eof(f) then input_ln:=false else begin last_nonblank:=first; while not eoln(f) do begin if last>=max_buf_stack then begin max_buf_stack:=last+1; if max_buf_stack=buf_size then @; end; buffer[last]:=xord[f^]; get(f); incr(last); if buffer[last-1]<>" " then last_nonblank:=last; end; last:=last_nonblank; input_ln:=true; end; end; @y We define |input_ln| in C, for efficiency. @z @x @= @!term_in:alpha_file; {the terminal as an input file} @!term_out:alpha_file; {the terminal as an output file} @y @d term_in==stdin {the terminal as an input file} @d term_out==stdout {the terminal as an output file} @= @!init @!ini_version:boolean; {are we \.{INITEX}?} tini@/ @# @!bound_default:integer; {temporary for setup} @!bound_name:^char; {temporary for setup} @# @!main_memory:integer; {total memory words allocated in initex} @!extra_mem_bot:integer; {|mem_min:=mem_bot-extra_mem_bot| except in \.{INITEX}} @!mem_min:integer; {smallest index in \TeX's internal |mem| array; must be |min_halfword| or more; must be equal to |mem_bot| in \.{INITEX}, otherwise |<=mem_bot|} @!mem_top:integer; {largest index in the |mem| array dumped by \.{INITEX}; must be substantially larger than |mem_bot|, equal to |mem_max| in \.{INITEX}, else not greater than |mem_max|} @!extra_mem_top:integer; {|mem_max:=mem_top+extra_mem_top| except in \.{INITEX}} @!mem_max:integer; {greatest index in \TeX's internal |mem| array; must be strictly less than |max_halfword|; must be equal to |mem_top| in \.{INITEX}, otherwise |>=mem_top|} @!error_line:integer; {width of context lines on terminal error messages} @!half_error_line:integer; {width of first lines of contexts in terminal error messages; should be between 30 and |error_line-15|} @!max_print_line:integer; {width of longest text lines output; should be at least 60} @!max_strings:integer; {maximum number of strings; must not exceed |max_halfword|} @!string_vacancies:integer; {the minimum number of characters that should be available for the user's control sequences and font names, after \TeX's own error messages are stored} @!pool_size:integer; {maximum number of characters in strings, including all error messages and help texts, and the names of all fonts and control sequences; must exceed |string_vacancies| by the total length of \TeX's own strings, which is currently about 23000} @!pool_free:integer;{pool space free after format loaded} @!font_mem_size:integer; {number of words of |font_info| for all fonts} @!font_max:integer; {maximum internal font number; ok to exceed |max_quarterword| and must be at most |font_base|+|max_font_max|} @!font_k:integer; {loop variable for initialization} @!hyph_size:integer; {maximun number of hyphen exceptions} @!trie_size:integer; {space for hyphenation patterns; should be larger for \.{INITEX} than it is in production versions of \TeX. 50000 is needed for English, German, and Portuguese.} @!buf_size:integer; {maximum number of characters simultaneously present in current lines of open files and in control sequences between \.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|} @!stack_size:integer; {maximum number of simultaneous input sources} @!max_in_open:integer; {maximum number of input files and error insertions that can be going on simultaneously} @!param_size:integer; {maximum number of simultaneous macro parameters} @!nest_size:integer; {maximum number of semantic levels simultaneously active} @!save_size:integer; {space for saving values outside of current group; must be at most |max_halfword|} @!dvi_buf_size:integer; {size of the output buffer; must be a multiple of 8} @z @x @ Here is how to open the terminal files in \ph. The `\.{/I}' switch suppresses the first |get|. @^system dependencies@> @d t_open_in==reset(term_in,'TTY:','/O/I') {open the terminal for text input} @d t_open_out==rewrite(term_out,'TTY:','/O') {open the terminal for text output} @y @ Here is how to open the terminal files. |t_open_out| does nothing. |t_open_in|, on the other hand, does the work of ``rescanning,'' or getting any command line arguments the user has provided. It's defined in C. @d t_open_out == {output already open for text output} @z @x these operations can be specified in \ph: @^system dependencies@> @d update_terminal == break(term_out) {empty the terminal output buffer} @d clear_terminal == break_in(term_in,true) {clear the terminal input buffer} @y these operations can be specified with {\mc UNIX}. |update_terminal| does an |fflush|. |clear_terminal| is redefined to do nothing, since the user should control the terminal. @^system dependencies@> @d update_terminal == fflush (term_out) @d clear_terminal == do_nothing @z @x @= if format_ident=0 then begin write_ln(term_out,'Buffer size exceeded!'); goto final_end; @.Buffer size exceeded@> end else begin cur_input.loc_field:=first; cur_input.limit_field:=last-1; overflow("buffer size",buf_size); @:TeX capacity exceeded buffer size}{\quad buffer size@> end @y Routine is implemented in C, module is not needed. @z @x @ The following program does the required initialization without retrieving a possible command line. It should be clear how to modify this routine to deal with command lines, if the system permits them. @^system dependencies@> @p function init_terminal:boolean; {gets the terminal input started} label exit; begin t_open_in; @y @ The following program does the required initialization. Iff anything has been specified on the command line, then |t_open_in| will return with |last > first|. @^system dependencies@> @p function init_terminal:boolean; {gets the terminal input started} label exit; begin t_open_in; if last > first then begin loc := first; while (loc < last) and (buffer[loc]=' ') do incr(loc); if loc < last then begin init_terminal := true; goto exit; end; end; @z @x write(term_out,'! End of file on the terminal... why?'); @y write_ln(term_out,'! End of file on the terminal... why?'); @z @x @!pool_pointer = 0..pool_size; {for variables that point into |str_pool|} @!str_number = 0..max_strings; {for variables that point into |str_start|} @y @!pool_pointer = integer; {for variables that point into |str_pool|} @!str_number = 0..ssup_max_strings; {for variables that point into |str_start|} @z @x @!str_pool:packed array[pool_pointer] of packed_ASCII_code; {the characters} @!str_start : array[str_number] of pool_pointer; {the starting pointers} @y @!str_pool: ^packed_ASCII_code; {the characters} @!str_start : ^pool_pointer; {the starting pointers} @z @x @p @!init function get_strings_started:boolean; {initializes the string pool, @y @p @t\4@>@@/ @!init function get_strings_started:boolean; {initializes the string pool, @z @x name_of_file:=pool_name; {we needn't set |name_length|} if a_open_in(pool_file) then @y name_length := strlen (pool_name); name_of_file := xmalloc (1 + name_length + 1); strcpy (name_of_file+1, pool_name); {copy the string} if a_open_in (pool_file, kpse_texpool_format) then @z @x else bad_pool('! I can''t read TEX.POOL.') @y else bad_pool('! I can''t read tex.pool; bad path?') @z @x begin if eof(pool_file) then bad_pool('! TEX.POOL has no check sum.'); @.TEX.POOL has no check sum@> read(pool_file,m,n); {read two digits of string length} @y begin if eof(pool_file) then bad_pool('! tex.pool has no check sum.'); @.TEX.POOL has no check sum@> read(pool_file,m); read(pool_file,n); {read two digits of string length} @z @x bad_pool('! TEX.POOL line doesn''t begin with two digits.'); @y bad_pool('! tex.pool line doesn''t begin with two digits.'); @z @x bad_pool('! TEX.POOL check sum doesn''t have nine digits.'); @y bad_pool('! tex.pool check sum doesn''t have nine digits.'); @z @x done: if a<>@$ then bad_pool('! TEX.POOL doesn''t match; TANGLE me again.'); @y done: if a<>@$ then bad_pool('! tex.pool doesn''t match; tangle me again (or fix the path).'); @z @x @!trick_buf:array[0..error_line] of ASCII_code; {circular buffer for @y @!trick_buf:array[0..ssup_error_line] of ASCII_code; {circular buffer for @z @x wterm(banner); if format_ident=0 then wterm_ln(' (no format preloaded)') else begin slow_print(format_ident); print_ln; end; @y wterm(banner); wterm(version_string); if format_ident>0 then slow_print(format_ident); print_ln; @z @x @= procedure jump_out; begin goto end_of_TEX; end; @y @d do_final_end==begin update_terminal; ready_already:=0; if (history <> spotless) and (history <> warning_issued) then uexit(1) else uexit(0); end @= procedure jump_out; begin close_files_and_terminate; do_final_end; end; @z @x line ready to be edited. But such an extension requires some system wizardry, so the present implementation simply types out the name of the file that should be edited and the relevant line number. @^system dependencies@> There is a secret `\.D' option available when the debugging routines haven't been commented~out. @^debugging@> @y line ready to be edited. We do this by calling the external procedure |call_edit| with a pointer to the filename, its length, and the line number. However, here we just set up the variables that will be used as arguments, since we don't want to do the switch-to-editor until after TeX has closed its files. @^system dependencies@> There is a secret `\.D' option available when the debugging routines haven't been commented~out. @^debugging@> @d edit_file==input_stack[base_ptr] @z @x "E": if base_ptr>0 then begin print_nl("You want to edit file "); @.You want to edit file x@> slow_print(input_stack[base_ptr].name_field); print(" at line "); print_int(line); interaction:=scroll_mode; jump_out; @y "E": if base_ptr>0 then begin edit_name_start:=str_start[edit_file.name_field]; edit_name_length:=str_start[edit_file.name_field+1] - str_start[edit_file.name_field]; edit_line:=line; jump_out; @z @x |remainder|, holds the remainder after a division. @= @y |remainder|, holds the remainder after a division. @d remainder==tex_remainder @= @z @x @!glue_ratio=real; {one-word representation of a glue expansion factor} @y @z @x @d min_quarterword=0 {smallest allowable value in a |quarterword|} @d max_quarterword=255 {largest allowable value in a |quarterword|} @d min_halfword==0 {smallest allowable value in a |halfword|} @d max_halfword==65535 {largest allowable value in a |halfword|} @y @d min_quarterword=0 {smallest allowable value in a |quarterword|} @d max_quarterword=255 {largest allowable value in a |quarterword|} @d min_halfword==0 {smallest allowable value in a |halfword|} @d max_halfword==@"FFFFFFF {largest allowable value in a |halfword|} @z @x if (font_basemax_quarterword) then bad:=15; if font_max>font_base+256 then bad:=16; @y if (max_font_maxmax_halfword) then bad:=15; if font_max>font_base+max_font_max then bad:=16; @z @x macros are simplified in the obvious way when |min_quarterword=0|. @^inner loop@>@^system dependencies@> @d qi(#)==#+min_quarterword {to put an |eight_bits| item into a quarterword} @d qo(#)==#-min_quarterword {to take an |eight_bits| item out of a quarterword} @d hi(#)==#+min_halfword {to put a sixteen-bit item into a halfword} @d ho(#)==#-min_halfword {to take a sixteen-bit item from a halfword} @y macros are simplified in the obvious way when |min_quarterword=0|. So they have been simplified here in the obvious way. @^inner loop@>@^system dependencies@> @d qi(#)==# {to put an |eight_bits| item into a quarterword} @d qo(#)==# {to take an |eight_bits| item from a quarterword} @d hi(#)==# {to put a sixteen-bit item into a halfword} @d ho(#)==# {to take a sixteen-bit item from a halfword} @z @x @!quarterword = min_quarterword..max_quarterword; {1/4 of a word} @!halfword=min_halfword..max_halfword; {1/2 of a word} @!two_choices = 1..2; {used when there are two variants in a record} @!four_choices = 1..4; {used when there are four variants in a record} @!two_halves = packed record@;@/ @!rh:halfword; case two_choices of 1: (@!lh:halfword); 2: (@!b0:quarterword; @!b1:quarterword); end; @!four_quarters = packed record@;@/ @!b0:quarterword; @!b1:quarterword; @!b2:quarterword; @!b3:quarterword; end; @!memory_word = record@;@/ case four_choices of 1: (@!int:integer); 2: (@!gr:glue_ratio); 3: (@!hh:two_halves); 4: (@!qqqq:four_quarters); end; @y @!quarterword = min_quarterword..max_quarterword; @!halfword = min_halfword..max_halfword; @!two_choices = 1..2; {used when there are two variants in a record} @!four_choices = 1..4; {used when there are four variants in a record} @=#include "texmfmem.h";@> @z @x @!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area} @y @!yzmem : ^memory_word; {the big dynamic storage area} @!zmem : ^memory_word; {the big dynamic storage area} @z @x if r>p+1 then @; @y if r>toint(p+1) then @; @z @x @p function new_ligature(@!f,@!c:quarterword; @!q:pointer):pointer; @y @p function new_ligature(@!f:internal_font_number; @!c:quarterword; @!q:pointer):pointer; @z @x are debugging.) @y are debugging.) @d free==free_arr @z @x @!debug @!free: packed array [mem_min..mem_max] of boolean; {free cells} @t\hskip10pt@>@!was_free: packed array [mem_min..mem_max] of boolean; @y {The debug memory arrays have not been mallocated yet.} @!debug @!free: packed array [0..9] of boolean; {free cells} @t\hskip10pt@>@!was_free: packed array [0..9] of boolean; @z @x begin if (font(p)font_max) then @y begin if (font(p)>font_max) then @z @x @p procedure print_font_and_char(@!p:integer); {prints |char_node| data} begin if p>mem_end then print_esc("CLOBBERED.") else begin if (font(p)font_max) then print_char("*") @y @p procedure print_font_and_char(@!p:integer); {prints |char_node| data} begin if p>mem_end then print_esc("CLOBBERED.") else begin if (font(p)>font_max) then print_char("*") @z @x if abs(mem[p+glue_offset].int)<@'4000000 then print("?.?") else if abs(g)>float_constant(20000) then @y { The Unix |pc| folks removed this restriction with a remark that invalid bit patterns were vanishingly improbable, so we follow their example without really understanding it. |if abs(mem[p+glue_offset].int)<@'4000000 then print('?.?')| |else| } if fabs(g)>float_constant(20000) then @z @x @ Now we are ready to delete any node list, recursively. @y @ For \.{pdftex} we may have whatsit node for link annotation. This node may contain a pointer to next nodes appended when the corresponding link is multi-line. @p procedure free_annot_link_node(p: pointer); {free whatsit node for link annotation} var n: pointer; begin while p <> null do begin n := pdf_annot_link_next(p); free_node(p, pdf_annot_link_node_size); p := n; end; end; procedure free_action_struct(p: pointer); begin if pdf_action_type(p) = pdf_action_user_defined then delete_token_ref(pdf_action_user_defined_str(p)) else begin if pdf_action_file(p) <> null then delete_token_ref(pdf_action_file(p)); if pdf_action_id_type(p) = pdf_id_name then delete_token_ref(pdf_action_name(p)); end; end; @ Now we are ready to delete any node list, recursively. @z @x @ The copying procedure copies words en masse without bothering @y @ The following procedures are required for copying whatsit node for link annotation. This node may contain a pointer to next nodes appended when the corresponding link is multi-line or multi-page. @p procedure copy_action_struct(p, r: pointer); var i: integer; begin for i := 0 to pdf_action_struct_size - 1 do mem[r + i] := mem[p + i]; {copy action struct} if pdf_action_type(p) = pdf_action_user_defined then add_token_ref(pdf_action_user_defined_str(p)) else begin if pdf_action_file(p) <> null then add_token_ref(pdf_action_file(p)); if pdf_action_id_type(p) = pdf_id_name then add_token_ref(pdf_action_name(p)); end; end; procedure copy_annot_link_node(p, r: pointer); {copy list for next links which represent multi-line link annotation from node |p| to |r|} begin pdf_width(r) := pdf_width(p); pdf_height(r) := pdf_height(p); pdf_depth(r) := pdf_depth(p); pdf_annot_attr(r) := pdf_annot_attr(p); if pdf_annot_attr(p) <> null then add_token_ref(pdf_annot_attr(p)); pdf_annot_link_action(r) := get_node(pdf_action_struct_size); copy_action_struct(pdf_annot_link_action(p), pdf_annot_link_action(r)); pdf_annot_link_next(r) := null; end; @ The copying procedure copies words en masse without bothering @z @x @d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)} @y @d shorthand_def=95 {code definition ( \.{\\chardef}, \.{\\countdef}, etc.~)} {or \.{\\charsubdef}} @z @x @!nest:array[0..nest_size] of list_state_record; @y @!nest:^list_state_record; @z @x prev_graf:=0; shown_mode:=0; @; @y prev_graf:=0; shown_mode:=0; @/{The following piece of code is a copy of module 991:} page_contents:=empty; page_tail:=page_head; {|link(page_head):=null;|}@/ last_glue:=max_halfword; last_penalty:=0; last_kern:=0; page_depth:=0; page_max_depth:=0; @z @x paragraph shape. @y paragraph shape. Additionally region~4 contains the table with ML\TeX's character substitution definitions. @z @x @d undefined_control_sequence=frozen_null_font+257 {dummy location} @y @d undefined_control_sequence=frozen_null_font+max_font_max+1 {dummy location} @z @x for k:=active_base to undefined_control_sequence-1 do eqtb[k]:=eqtb[undefined_control_sequence]; @y for k:=active_base to eqtb_top do eqtb[k]:=eqtb[undefined_control_sequence]; @z @x @d err_help_loc=local_base+9 {points to token list for \.{\\errhelp}} @d toks_base=local_base+10 {table of 256 token list registers} @y @d pdf_page_attr_loc=local_base+9 {points to token list for \.{\\pdfpageattr}} @d pdf_pages_attr_loc=local_base+10 {points to token list for \.{\\pdfpagesattr}} @d err_help_loc=local_base+11 {points to token list for \.{\\errhelp}} @d toks_base=local_base+12 {table of 256 token list registers} @z @x @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 @d err_help==equiv(err_help_loc) @y @d pdf_page_attr==equiv(pdf_page_attr_loc) @d pdf_pages_attr==equiv(pdf_pages_attr_loc) @d err_help==equiv(err_help_loc) @z @x {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 primitive("errhelp",assign_toks,err_help_loc); @!@:err_help_}{\.{\\errhelp} primitive@> @y primitive("pdfpageattr",assign_toks,pdf_page_attr_loc); @!@:pdf_page_attr_}{\.{\\pdfpageattr} primitive@> primitive("pdfpagesattr",assign_toks,pdf_pages_attr_loc); @!@:pdf_pages_attr_}{\.{\\pdfpagesattr} primitive@> primitive("errhelp",assign_toks,err_help_loc); @!@:err_help_}{\.{\\errhelp} primitive@> @z @x othercases print_esc("errhelp") @y pdf_page_attr_loc: print_esc("pdfpageattr"); pdf_pages_attr_loc: print_esc("pdfpagesattr"); othercases print_esc("errhelp") @z @x @d error_context_lines_code=54 {maximum intermediate line pairs shown} @d int_pars=55 {total number of integer parameters} @y @d error_context_lines_code=54 {maximum intermediate line pairs shown} @d pdf_output_code=55 {switch on \.{PDF} output if positive} @d pdf_compress_level_code=56 {compress level of text contents} @d pdf_last_form_code=57 {object number of the most recently output form} @d char_sub_def_min_code=58 {smallest value in the charsubdef list} @d char_sub_def_max_code=59 {largest value in the charsubdef list} @d tracing_char_sub_def_code=60 {traces changes to a charsubdef def} @d int_pars=61 {total number of integer parameters} @z @x @d error_context_lines==int_par(error_context_lines_code) @y @d error_context_lines==int_par(error_context_lines_code) @d pdf_output==int_par(pdf_output_code) @d pdf_compress_level==int_par(pdf_compress_level_code) @d pdf_last_form==int_par(pdf_last_form_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 error_context_lines_code:print_esc("errorcontextlines"); @y error_context_lines_code:print_esc("errorcontextlines"); pdf_output_code:print_esc("pdfoutput"); pdf_compress_level_code:print_esc("pdfcompresslevel"); pdf_last_form_code:print_esc("pdflastform"); 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 primitive("errorcontextlines",assign_int,int_base+error_context_lines_code);@/ @!@:error_context_lines_}{\.{\\errorcontextlines} primitive@> @y primitive("errorcontextlines",assign_int,int_base+error_context_lines_code);@/ @!@:error_context_lines_}{\.{\\errorcontextlines} primitive@> primitive("pdfoutput",assign_int,int_base+pdf_output_code);@/ @!@:pdf_output_}{\.{\\pdfoutput} primitive@> primitive("pdfcompresslevel",assign_int,int_base+pdf_compress_level_code);@/ @!@:pdf_compress_level_}{\.{\\pdfcompresslevel} primitive@> primitive("pdflastform",assign_int,int_base+pdf_last_form_code);@/ @!@:pdf_last_form_}{\.{\\pdflastform} 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 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 @ The following procedure, which is called just before \TeX\ initializes its input and output, establishes the initial values of the date and time. @^system dependencies@> Since standard \PASCAL\ cannot provide such information, something special is needed. The program here simply specifies July 4, 1776, at noon; but users probably want a better approximation to the truth. @p procedure fix_date_and_time; begin time:=12*60; {minutes since midnight} day:=4; {fourth day of the month} month:=7; {seventh month of the year} year:=1776; {Anno Domini} end; @y @ The following procedure, which is called just before \TeX\ initializes its input and output, establishes the initial values of the date and time. It calls a macro-defined |date_and_time| routine. |date_and_time| in turn is a C macro, which calls |get_date_and_time|, passing it the addresses of the day, month, etc., so they can be set by the routine. |get_date_and_time| also sets up interrupt catching if that is conditionally compiled in the C code. @^system dependencies@> @d fix_date_and_time==date_and_time(time,day,month,year) @z @x @d emergency_stretch_code=20 {reduces badnesses on final pass of line-breaking} @d dimen_pars=21 {total number of dimension parameters} @y @d emergency_stretch_code=20 {reduces badnesses on final pass of line-breaking} @d pdf_page_width_code=21 {page width of \.{PDF} output} @d pdf_page_height_code=22 {page height of \.{PDF} output} @d pdf_thread_hoffset_code=23 {thread hoffset of \.{PDF} output} @d pdf_thread_voffset_code=24 {thread voffset of \.{PDF} output} @d dimen_pars=25 {total number of dimension parameters} @z @x @d emergency_stretch==dimen_par(emergency_stretch_code) @y @d emergency_stretch==dimen_par(emergency_stretch_code) @d pdf_page_width==dimen_par(pdf_page_width_code) @d pdf_page_height==dimen_par(pdf_page_height_code) @d pdf_thread_hoffset==dimen_par(pdf_thread_hoffset_code) @d pdf_thread_voffset==dimen_par(pdf_thread_voffset_code) @z @x emergency_stretch_code:print_esc("emergencystretch"); @y emergency_stretch_code:print_esc("emergencystretch"); pdf_page_width_code: print_esc("pdfpagewidth"); pdf_page_height_code: print_esc("pdfpageheight"); pdf_thread_hoffset_code: print_esc("pdfthreadhoffset"); pdf_thread_voffset_code: print_esc("pdfthreadvoffset"); @z @x primitive("emergencystretch",assign_dimen,dimen_base+emergency_stretch_code);@/ @!@:emergency_stretch_}{\.{\\emergencystretch} primitive@> @y primitive("emergencystretch",assign_dimen,dimen_base+emergency_stretch_code);@/ @!@:emergency_stretch_}{\.{\\emergencystretch} primitive@> primitive("pdfpagewidth",assign_dimen,dimen_base+pdf_page_width_code);@/ @!@:pdf_page_width_}{\.{\\pdfpagewidth} primitive@> primitive("pdfpageheight",assign_dimen,dimen_base+pdf_page_height_code);@/ @!@:pdf_page_height_}{\.{\\pdfpageheight} primitive@> primitive("pdfthreadhoffset",assign_dimen,dimen_base+pdf_thread_hoffset_code);@/ @!@:pdf_thread_hoffset_}{\.{\\pdfthreadhoffset} primitive@> primitive("pdfthreadvoffset",assign_dimen,dimen_base+pdf_thread_voffset_code);@/ @!@:pdf_thread_voffset_}{\.{\\pdfthreadvoffset} primitive@> @z @x else if n @y else if (neqtb_size)and(n<=eqtb_top)) then @ @z @x @!eqtb:array[active_base..eqtb_size] of memory_word; @y @!zeqtb:^memory_word; @z @x @!hash: array[hash_base..undefined_control_sequence-1] of two_halves; {the hash table} @!hash_used:pointer; {allocation pointer for |hash|} @y @!hash: ^two_halves; {the hash table} @!yhash: ^two_halves; {auxiliary pointer for freeing hash} @!hash_used:pointer; {allocation pointer for |hash|} @!hash_extra:pointer; {|hash_extra=hash| above |eqtb_size|} @!hash_top:pointer; {maximum of the hash array} @!eqtb_top:pointer; {maximum of the |eqtb|} @!hash_high:pointer; {pointer to next high hash location} @z @x next(hash_base):=0; text(hash_base):=0; for k:=hash_base+1 to undefined_control_sequence-1 do hash[k]:=hash[hash_base]; @y @z @x hash_used:=frozen_control_sequence; {nothing is used} @y hash_used:=frozen_control_sequence; {nothing is used} hash_high:=0; @z @x @ @= begin if text(p)>0 then begin repeat if hash_is_full then overflow("hash size",hash_size); @:TeX capacity exceeded hash size}{\quad hash size@> decr(hash_used); until text(hash_used)=0; {search for an empty location in |hash|} next(p):=hash_used; p:=hash_used; end; @y @ @= begin if text(p)>0 then begin if hash_high decr(hash_used); until text(hash_used)=0; {search for an empty location in |hash|} next(p):=hash_used; p:=hash_used; end; end; @z @x else if p>=undefined_control_sequence then print_esc("IMPOSSIBLE.") @y else if ((p>=undefined_control_sequence)and(p<=eqtb_size))or(p>eqtb_top) then print_esc("IMPOSSIBLE.") @z @x else if (text(p)<0)or(text(p)>=str_ptr) then print_esc("NONEXISTENT.") @y else if (text(p)>=str_ptr) then print_esc("NONEXISTENT.") @z @x @!save_stack : array[0..save_size] of memory_word; @y @!save_stack : ^memory_word; @z @x if peqtb_size) then @z @x if cs_token_flag+undefined_control_sequence>max_halfword then bad:=21; @y if cs_token_flag+eqtb_size+hash_extra>max_halfword then bad:=21; if (hash_offset<0)or(hash_offset>hash_base) then bad:=42; @z @x @!input_stack : array[0..stack_size] of in_state_record; @y @!input_stack : ^in_state_record; @z @x @!input_file : array[1..max_in_open] of alpha_file; @!line : integer; {current line number in the current source file} @!line_stack : array[1..max_in_open] of integer; @y @!input_file : ^alpha_file; @!line : integer; {current line number in the current source file} @!line_stack : ^integer; @z @x @d write_text=15 {|token_type| code for \.{\\write}} @y @d write_text=15 {|token_type| code for \.{\\write}} @d pdf_page_attr_text=16 {|token_type| code for \.{\\pdfpageattr}} @d pdf_pages_attr_text=17 {|token_type| code for \.{\\pdfpagesattr}} @z @x @!param_stack:array [0..param_size] of pointer; {token list pointers for parameters} @y @!param_stack: ^pointer; {token list pointers for parameters} @z @x write_text: print_nl(" "); othercases print_nl("?") {this should never happen} @y write_text: print_nl(" "); pdf_page_attr_text: print_nl(" "); pdf_pages_attr_text: print_nl(" "); othercases print_nl("?") {this should never happen} @z @x @ The file names we shall deal with for illustrative purposes have the following structure: If the name contains `\.>' or `\.:', the file area consists of all characters up to and including the final such character; otherwise the file area is null. If the remaining file name contains `\..', the file extension consists of all such characters from the first remaining `\..' to the end, otherwise the file extension is null. @y @ The file names we shall deal with have the following structure: If the name contains `\./' or `\.:' (for Amiga only), the file area consists of all characters up to and including the final such character; otherwise the file area is null. If the remaining file name contains `\..', the file extension consists of all such characters from the last `\..' to the end, otherwise the file extension is null. @z @x @!area_delimiter:pool_pointer; {the most recent `\.>' or `\.:', if any} @!ext_delimiter:pool_pointer; {the relevant `\..', if any} @y @!area_delimiter:pool_pointer; {the most recent `\./', if any} @!ext_delimiter:pool_pointer; {the most recent `\..', if any} @z @x @d TEX_area=="TeXinputs:" @.TeXinputs@> @d TEX_font_area=="TeXfonts:" @.TeXfonts@> @y In C, the default paths are specified separately. But let's take this opportunity to declare some pseudo-enumerated constants. These constants are also in \.{lib/texmfmp.c} (in two places). @d extend_jobname_never = 0 @d extend_jobname_maybe = 1 @d extend_jobname_always = 2 @z @x if (c=">")or(c=":") then @y if IS_DIR_SEP(c) then @z @x else if (c=".")and(ext_delimiter=0) then ext_delimiter:=cur_length; @y else if c="." then ext_delimiter:=cur_length; @z @x @ The third. @^system dependencies@> @p procedure end_name; @y @ The third. @^system dependencies@> If a string is already in the string pool, the function |slow_make_string| does not create a new string but returns this string number, thus saving string space. Because of this new property of the returned string number it is not possible to apply |flush_string| to these strings. While the |job_name| is scanned in the procedure |start_input|, all non-trivial file name extensions should not be used as |cur_ext| but as a part of |cur_name|. Thus we end up with \.{one.two.dvi} for the input file \.{one.two}, not \.{one.dvi}. But this does not apply not for the usual extensions ``.tex'', ``.ltx'', ``.texi'', etc., because output files named \.{latex.ltx.fmt} or \.{gcc.texi.dvi} are highly undesirable. This changed behavior of |start_input| is triggered using the flag |scan_jobname_p|. @p procedure end_name; var temp_str: str_number; {result of file name cache lookups} @!j: pool_pointer; {running index} @z @x str_start[str_ptr+1]:=str_start[str_ptr]+area_delimiter; incr(str_ptr); end; if ext_delimiter=0 then begin cur_ext:=""; cur_name:=make_string; @y str_start[str_ptr+1]:=str_start[str_ptr]+area_delimiter; incr(str_ptr); temp_str:=search_string(cur_area); if temp_str>0 then begin cur_area:=temp_str; decr(str_ptr); {no |flush_string|, |pool_ptr| will be wrong!} for j:=str_start[str_ptr+1] to pool_ptr-1 do begin str_pool[j-area_delimiter]:=str_pool[j]; end; pool_ptr:=pool_ptr-area_delimiter; {update |pool_ptr|} end; end; if ext_delimiter=0 then begin cur_ext:=""; cur_name:=slow_make_string; @z @x else begin cur_name:=str_ptr; str_start[str_ptr+1]:=str_start[str_ptr]+ext_delimiter-area_delimiter-1; incr(str_ptr); cur_ext:=make_string; @y else begin cur_name:=str_ptr; str_start[str_ptr+1]:=str_start[str_ptr]+ext_delimiter-area_delimiter-1; incr(str_ptr); cur_ext:=make_string; {avoid combination of |cur_name| and |cur_ext| for standard extensions} temp_str:=0; if extend_jobname <> extend_jobname_always then if extend_jobname = extend_jobname_never then temp_str:=cur_ext else if str_eq_str(cur_ext,".tex") then temp_str:=".tex" {If the following list changes, also change \.{kpathsea/tex-file.c}.} else if str_eq_str(cur_ext,".texi") then temp_str:=".texi" else if str_eq_str(cur_ext,".texinfo") then temp_str:=".texinfo" else if str_eq_str(cur_ext,".txi") then temp_str:=".txi" else if str_eq_str(cur_ext,".dtx") then temp_str:=".dtx" else if str_eq_str(cur_ext,".drv") then temp_str:=".drv" else if str_eq_str(cur_ext,".ltx") then temp_str:=".ltx" else if str_eq_str(cur_ext,".ins") then temp_str:=".ins" ; if temp_str>0 then {save string space for these extensions} begin flush_string; cur_ext:=temp_str; temp_str:=search_string(cur_name); if temp_str>0 then begin cur_name:=temp_str; flush_string; end; end else if scan_jobname_p then {in |start_input| combine |cur_name| and |cur_ext|} begin decr(str_ptr); decr(str_ptr); {undo |cur_ext| and |cur_name|} cur_name:=slow_make_string; {combine them; |job_name:=cur_name|, later} cur_ext:=""; end else begin decr(str_ptr); {undo extension string to look at name part} temp_str:=search_string(cur_name); if temp_str>0 then begin cur_name:=temp_str; decr(str_ptr); {no |flush_string|, |pool_ptr| will be wrong!} for j:=str_start[str_ptr+1] to pool_ptr-1 do begin str_pool[j-ext_delimiter+area_delimiter+1]:=str_pool[j]; end; pool_ptr:=pool_ptr-ext_delimiter+area_delimiter+1; {update |pool_ptr|} end; cur_ext:=slow_make_string; {remake extension string} end; @z @x for j:=str_start[a] to str_start[a+1]-1 do append_to_name(so(str_pool[j])); @y if name_of_file then libc_free (name_of_file); name_of_file:= xmalloc(1 + length(a) + length(n) + length(e) + 1); for j:=str_start[a] to str_start[a+1]-1 do append_to_name(so(str_pool[j])); @z @x for k:=name_length+1 to file_name_size do name_of_file[k]:=' '; @y name_of_file[name_length+1]:=0; @z @x @d format_default_length=20 {length of the |TEX_format_default| string} @d format_area_length=11 {length of its area part} @d format_ext_length=4 {length of its `\.{.fmt}' part} @y Under {\mc UNIX} we don't give the area part, instead depending on the path searching that will happen during file opening. Also, the length will be set in the main program. @d format_area_length=0 {length of its area part} @d format_ext_length=4 {length of its `\.{.fmt}' part} @z @x @!TEX_format_default:packed array[1..format_default_length] of char; @ @= TEX_format_default:='TeXformats:plain.fmt'; @y @!format_default_length: integer; @!TEX_format_default: ^char; @ We set the name of the default format file and the length of that name in C, instead of Pascal, since we want them to depend on the name of the program. @z @x for j:=1 to n do append_to_name(xord[TEX_format_default[j]]); @y if name_of_file then libc_free (name_of_file); name_of_file := xmalloc (1 + n + (b - a + 1) + format_ext_length + 1); for j:=1 to n do append_to_name(xord[TEX_format_default[j]]); @z @x for k:=name_length+1 to file_name_size do name_of_file[k]:=' '; @y name_of_file[name_length+1]:=0; @z @x pack_buffered_name(0,loc,j-1); {try first without the system file area} if w_open_in(fmt_file) then goto found; pack_buffered_name(format_area_length,loc,j-1); {now try the system format file area} if w_open_in(fmt_file) then goto found; @y pack_buffered_name(0,loc,j-1); {Kpathsea does everything} if w_open_in(fmt_file) then goto found; @z @x wterm_ln('Sorry, I can''t find that format;',' will try PLAIN.'); @y wterm ('Sorry, I can''t find the format `'); fputs (name_of_file + 1, stdout); wterm ('''; will try `'); fputs (TEX_format_default + 1, stdout); wterm_ln ('''.'); @z @x wterm_ln('I can''t find the PLAIN format file!'); @.I can't find PLAIN...@> @y wterm ('I can''t find the format file `'); fputs (TEX_format_default + 1, stdout); wterm_ln ('''!'); @.I can't find the format...@> @z @x @!name_in_progress:boolean; {is a file name being scanned?} @y @!name_in_progress:boolean; {is a file name being scanned?} @!scan_jobname_p:boolean; {scanning file name for |job_name|?} @z @x job_name:=0; name_in_progress:=false; log_opened:=false; @y job_name:=0; name_in_progress:=false; log_opened:=false; scan_jobname_p:=false; @z @x @d ensure_dvi_open==if output_file_name=0 then @y @d log_name == texmf_log_name @d ensure_dvi_open==if output_file_name=0 then @z @x @!months:packed array [1..36] of char; {abbreviations of month names} @y @!months:^char; @z @x @; @y @; if mltex_enabled_p then begin wlog_cr; wlog('MLTeX v2.2 enabled'); end; @z @x begin wlog(banner); @y begin wlog(banner); wlog(version_string); @z @x months:='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'; @y months := ' JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'; @z @x begin scan_file_name; {set |cur_name| to desired file name} if cur_ext="" then cur_ext:=".tex"; pack_cur_name; loop@+ begin begin_file_reading; {set up |cur_file| and new level of input} if a_open_in(cur_file) then goto done; if cur_area="" then begin pack_file_name(cur_name,TEX_area,cur_ext); if a_open_in(cur_file) then goto done; end; @y var temp_str: str_number; begin scan_jobname_p:=true; scan_file_name; {set |cur_name| to desired file name} pack_cur_name; loop@+ begin begin_file_reading; {set up |cur_file| and new level of input} tex_input_type := 1; {Tell |open_input| we are \.{\\input}.} if cur_ext = ".tex" then begin {|prompt_file_name| packed the \.{.tex} in, so if the user says \.{foo.bar} in response to `type another input file name:', we'd end up looking for \.{foo.bar.tex} only, and never find \.{foo.bar}.} cur_ext := ""; pack_cur_name; end; {Kpathsea tries all the various ways to get the file.} if a_open_in(cur_file, kpse_tex_format) then begin scan_jobname_p:=false; goto done; end; @z @x done: name:=a_make_name_string(cur_file); @y done: name:=a_make_name_string(cur_file); if name=str_ptr-1 then {we can try to conserve string pool space now} begin temp_str:=search_string(name); if temp_str>0 then begin name:=temp_str; flush_string; end; end; @z @x if name=str_ptr-1 then {we can conserve string pool space now} begin flush_string; name:=cur_name; end; @y @z @x @!internal_font_number=font_base..font_max; {|font| in a |char_node|} @!font_index=0..font_mem_size; {index into |font_info|} @y @!internal_font_number=integer; {|font| in a |char_node|} @!font_index=integer; {index into |font_info|} @!nine_bits=min_quarterword..non_char; @z @x @!font_info:array[font_index] of memory_word; {the big collection of font data} @!fmem_ptr:font_index; {first unused word of |font_info|} @!font_ptr:internal_font_number; {largest internal font number in use} @!font_check:array[internal_font_number] of four_quarters; {check sum} @!font_size:array[internal_font_number] of scaled; {``at'' size} @!font_dsize:array[internal_font_number] of scaled; {``design'' size} @!font_params:array[internal_font_number] of font_index; {how many font parameters are present} @!font_name:array[internal_font_number] of str_number; {name of the font} @!font_area:array[internal_font_number] of str_number; {area of the font} @!font_bc:array[internal_font_number] of eight_bits; {beginning (smallest) character code} @!font_ec:array[internal_font_number] of eight_bits; {ending (largest) character code} @!font_glue:array[internal_font_number] of pointer; {glue specification for interword space, |null| if not allocated} @!font_used:array[internal_font_number] of boolean; {has a character from this font actually appeared in the output?} @!hyphen_char:array[internal_font_number] of integer; {current \.{\\hyphenchar} values} @!skew_char:array[internal_font_number] of integer; {current \.{\\skewchar} values} @!bchar_label:array[internal_font_number] of font_index; {start of |lig_kern| program for left boundary character, |non_address| if there is none} @!font_bchar:array[internal_font_number] of min_quarterword..non_char; {right boundary character, |non_char| if there is none} @!font_false_bchar:array[internal_font_number] of min_quarterword..non_char; {|font_bchar| if it doesn't exist in the font, otherwise |non_char|} @y @!font_info: ^fmemory_word; {the big collection of font data} @!fmem_ptr:font_index; {first unused word of |font_info|} @!font_ptr:internal_font_number; {largest internal font number in use} @!font_check: ^four_quarters; {check sum} @!font_size: ^scaled; {``at'' size} @!font_dsize: ^scaled; {``design'' size} @!font_params: ^font_index; {how many font parameters are present} @!font_name: ^str_number; {name of the font} @!font_area: ^str_number; {area of the font} @!font_bc: ^eight_bits; {beginning (smallest) character code} @!font_ec: ^eight_bits; {ending (largest) character code} @!font_glue: ^pointer; {glue specification for interword space, |null| if not allocated} @!font_used: ^boolean; {has a character from this font actually appeared in the output?} @!hyphen_char: ^integer; {current \.{\\hyphenchar} values} @!skew_char: ^integer; {current \.{\\skewchar} values} @!bchar_label: ^font_index; {start of |lig_kern| program for left boundary character, |non_address| if there is none} @!font_bchar: ^nine_bits; {right boundary character, |non_char| if there is none} @!font_false_bchar: ^nine_bits; {|font_bchar| if it doesn't exist in the font, otherwise |non_char|} @z @x @!char_base:array[internal_font_number] of integer; {base addresses for |char_info|} @!width_base:array[internal_font_number] of integer; {base addresses for widths} @!height_base:array[internal_font_number] of integer; {base addresses for heights} @!depth_base:array[internal_font_number] of integer; {base addresses for depths} @!italic_base:array[internal_font_number] of integer; {base addresses for italic corrections} @!lig_kern_base:array[internal_font_number] of integer; {base addresses for ligature/kerning programs} @!kern_base:array[internal_font_number] of integer; {base addresses for kerns} @!exten_base:array[internal_font_number] of integer; {base addresses for extensible recipes} @!param_base:array[internal_font_number] of integer; {base addresses for font parameters} @y @!char_base: ^integer; {base addresses for |char_info|} @!width_base: ^integer; {base addresses for widths} @!height_base: ^integer; {base addresses for heights} @!depth_base: ^integer; {base addresses for depths} @!italic_base: ^integer; {base addresses for italic corrections} @!lig_kern_base: ^integer; {base addresses for ligature/kerning programs} @!kern_base: ^integer; {base addresses for kerns} @!exten_base: ^integer; {base addresses for extensible recipes} @!param_base: ^integer; {base addresses for font parameters} @z @x for k:=font_base to font_max do font_used[k]:=false; @y @z @x font_ptr:=null_font; fmem_ptr:=7; font_name[null_font]:="nullfont"; font_area[null_font]:=""; hyphen_char[null_font]:="-"; skew_char[null_font]:=-1; bchar_label[null_font]:=non_address; font_bchar[null_font]:=non_char; font_false_bchar[null_font]:=non_char; font_bc[null_font]:=1; font_ec[null_font]:=0; font_size[null_font]:=0; font_dsize[null_font]:=0; char_base[null_font]:=0; width_base[null_font]:=0; height_base[null_font]:=0; depth_base[null_font]:=0; italic_base[null_font]:=0; lig_kern_base[null_font]:=0; kern_base[null_font]:=0; exten_base[null_font]:=0; font_glue[null_font]:=null; font_params[null_font]:=7; param_base[null_font]:=-1; for k:=0 to 6 do font_info[k].sc:=0; @y @z @x 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 restrictions in \.{TANGLE}'s macro capabilities you have to replace \.{XLPAREN} resp.\ \.{xlparen} with an opening parenthesis and \.{XRPAREN} resp.\ \.{xrparen} with a closing parenthesis 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 @p function read_font_info(@!u:pointer;@!nom,@!aire:str_number; @y @p @t\4@>@@/ function read_font_info(@!u:pointer;@!nom,@!aire:str_number; @z @x if aire="" then pack_file_name(nom,TEX_font_area,".tfm") else pack_file_name(nom,aire,".tfm"); @y {|kpse_find_file| will append the |".tfm"|, and avoid searching the disk before the font alias files as well.} pack_file_name(nom,aire,""); @z @x @d fget==get(tfm_file) @d fbyte==tfm_file^ @y @d fget==tfm_temp:=getc(tfm_file) @d fbyte==tfm_temp @z @x begin qw:=char_info(f)(d); @y begin qw:=orig_char_info(f)(d); @z @x qw:=char_info(f)(#); {N.B.: not |qi(#)|} @y qw:=orig_char_info(f)(#); {N.B.: not |qi(#)|} @z @x if eof(tfm_file) then abort; @y if feof(tfm_file) then abort; @z @x 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 @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 @!c,@!f:quarterword; {character and font in current |char_node|} @y {character and font in current |char_node|} @!c:quarterword; @!f:internal_font_number; @z @x @!dvi_buf:array[dvi_index] of eight_bits; {buffer for \.{DVI} output} @!half_buf:dvi_index; {half of |dvi_buf_size|} @!dvi_limit:dvi_index; {end of the current half buffer} @!dvi_ptr:dvi_index; {the next available buffer address} @y @!dvi_buf:^eight_bits; {buffer for \.{DVI} output} @!half_buf:integer; {half of |dvi_buf_size|} @!dvi_limit:integer; {end of the current half buffer} @!dvi_ptr:integer; {the next available buffer address} @z @x @p procedure write_dvi(@!a,@!b:dvi_index); var k:dvi_index; begin for k:=a to b do write(dvi_file,dvi_buf[k]); end; @y In C, we use a macro to call |fwrite| or |write| directly, writing all the bytes in one shot. Much better even than writing four bytes at a time. @z @x begin dvi_out(fnt_def1); dvi_out(f-font_base-1);@/ @y begin if f<=256+font_base then begin dvi_out(fnt_def1); dvi_out(f-font_base-1); end else begin dvi_out(fnt_def1+1); dvi_out((f-font_base-1) div @'400); dvi_out((f-font_base-1) mod @'400); end; @z @x old_setting:=selector; selector:=new_string; @y if output_comment then begin l:=strlen(output_comment); dvi_out(l); for s:=0 to l-1 do dvi_out(output_comment[s]); end else begin {the default code is unchanged} old_setting:=selector; selector:=new_string; @z @x pool_ptr:=str_start[str_ptr]; {flush the current string} @y pool_ptr:=str_start[str_ptr]; {flush the current string} end; @z @x 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 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 (Additional it is necessary to be careful---if leaders are used the current hlist is normally traversed more than once!) @^inner loop@> @z @x 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 @; continue: @z @x else begin dvi_out(fnt1); dvi_out(f-font_base-1); end; @y else if f<=256+font_base then begin dvi_out(fnt1); dvi_out(f-font_base-1); end else begin dvi_out(fnt1+1); dvi_out((f-font_base-1) div @'400); dvi_out((f-font_base-1) mod @'400); end; @z @x @ The |hlist_out| and |vlist_out| procedures are now complete, so we are ready for the |ship_out| routine that gets them started in the first place. @p procedure ship_out(@!p:pointer); {output the box |p|} @y @ The |hlist_out| and |vlist_out| procedures are now complete, so we are ready for the |ship_out| routine that gets them started in the first place. But first we insert here \.{PDF} output section. @= @!pdf_ptr: integer; {pointer to the first unused byte in the \.{PDF} buffer} @!zip_write_state: integer; {which state of compression we are in} @ @= pdf_buf[0] := "%"; pdf_buf[1] := "P"; pdf_buf[2] := "D"; pdf_buf[3] := "F"; pdf_buf[4] := "-"; pdf_buf[5] := "1"; pdf_buf[6] := "."; pdf_buf[7] := "2"; pdf_buf[8] := pdf_new_line_char; pdf_ptr := 9; zip_write_state := no_zip; @ We will share some variables and subroutines with \.{DVI} output. @d pdf_buf_size == dvi_buf_size {size of the \.{PDF} buffer} @d pdf_file == dvi_file {the \.{PDF} output file} @d pdf_buf == dvi_buf {the \.{PDF} buffer} @d pdf_gone == dvi_gone {number of bytes that were flushed to output} @d pdf_offset == (pdf_gone + pdf_ptr) {the file offset of last byte in \.{PDF} buffer} @d pdf_save_offset == dvi_offset {to save |pdf_offset|} @d no_zip == 0 {no \.{ZIP} compression} @d zip_writting == 1 {writting by \.{ZIP} compression} @d zip_finish == 2 {finish \.{ZIP} compression} @d ensure_pdf_open == {open \.{PDF} output file if necessary} if output_file_name = 0 then begin if job_name = 0 then open_log_file; pack_job_name(".pdf"); while not b_open_out(pdf_file) do prompt_file_name("file name for output",".pdf"); output_file_name := b_make_name_string(pdf_file); if pdf_page_width = 0 then pdf_page_width := hsize + 2*(pdf_1in + h_offset); if pdf_page_height = 0 then pdf_page_height := vsize + 2*(pdf_1in + v_offset); if get_pdf_page_height + get_pdf_page_width > 0 then do_nothing; {only make sure that \.{pdfTeX} dimension parameters must be set before opening output file} end @d pdf_quick_out(#) == {output a byte to \.{PDF} buffer without checking of overflow} begin pdf_buf[pdf_ptr] := #; incr(pdf_ptr); end @d pdf_out(#) == {do the same as |pdf_quick_out| and flush the \.{PDF} buffer if necessary} begin pdf_quick_out(#); if pdf_ptr = pdf_buf_size then pdf_flush; end @d pdf_remove_last_char == if pdf_ptr > 0 then decr(pdf_ptr) @d pdf_room(#) == {make sure that there are |n| bytes free in \.{PDF} buffer} begin if # + pdf_ptr >= pdf_buf_size then pdf_flush; end @p procedure start_compress; {start compression} begin pdf_flush; pdf_stream_length := 0; zip_write_state := zip_writting; end; procedure finish_compress; {finish compression} begin zip_write_state := zip_finish; pdf_flush; end; procedure prepare_pdf_par(s: str_number; p: pointer; a, b, v: integer; is_int_par: boolean); begin if (v >= 0) and ((is_int_par and (int_par(p) <> v)) or ((not is_int_par) and (dimen_par(p) <> v))) then begin print_err("Incompatible value of "); print_esc(s); print(" ("); if is_int_par then print_int(int_par(p)) else begin print_scaled(dimen_par(p)); print("pt"); end; print(")"); print_nl("The previous value ("); print_int(v); print(") will be retained"); @.Incompatible value of pdftex parameter@> help3 ("I can handle only one value of the above parameter")@/ ("per job, so I've reverted it to the value ")@/ ("you used earlier on this run.");@/ error; if is_int_par then int_par(p) := v else dimen_par(p) := v; end; if (is_int_par and ((int_par(p) < a) or (int_par(p) > b))) or ((not is_int_par) and ((dimen_par(p) < a) or (dimen_par(p) > b))) then begin print_err("Illegal value of "); print_esc(s); print(" ("); if is_int_par then print_int(int_par(p)) else begin print_scaled(dimen_par(p)); print("pt"); end; print(")"); @.Illegal value of pdftex parameter@> help2 ("The value of of the above parameter is out of allowed range,")@/ ("so I've set it to the most closed allowed value");@/ error; if is_int_par then begin if int_par(p) < a then int_par(p) := a else int_par(p) := b; end else begin if dimen_par(p) < a then dimen_par(p) := a else dimen_par(p) := b; end; end; end; function get_pdf_output: integer; begin prepare_pdf_par("pdfoutput", pdf_output_code, 0, 1, set_pdf_output, true); set_pdf_output := pdf_output; get_pdf_output := pdf_output; end; function get_pdf_compress_level: integer; begin prepare_pdf_par("pdfcompresslevel", pdf_compress_level_code, 0, 9, -1, true); get_pdf_compress_level := pdf_compress_level; end; function get_pdf_page_width: integer; begin prepare_pdf_par("pdfpagewidth", pdf_page_width_code, pdf_1in, max_dimen, set_pdf_page_width, false); set_pdf_page_width := pdf_page_width; get_pdf_page_width := pdf_page_width; end; function get_pdf_page_height: integer; begin prepare_pdf_par("pdfpageheight", pdf_page_height_code, pdf_1in, max_dimen, set_pdf_page_height, false); set_pdf_page_height := pdf_page_height; get_pdf_page_height := pdf_page_height; end; procedure pdf_flush; {flush out the |pdf_buf|} begin if pdf_ptr = 0 then return; ensure_pdf_open; case zip_write_state of no_zip: begin write_pdf(0, pdf_ptr - 1); pdf_gone := pdf_offset; end; zip_writting: write_zip(false); zip_finish: begin write_zip(true); pdf_gone := pdf_gone + pdf_stream_length; zip_write_state := no_zip; end; end; pdf_ptr := 0; end; @ Basic printing subroutines for \.{PDF} output. Basic printing procedures for \.{PDF} output are very similiar to \TeX\ basic printing ones but the output is \.{PDF} buffer instead of \.{DVI} buffer; they have all prefix |pdf_|. Subroutines with sufix |_nl| do then same and append a new-line character to the \.{PDF} output. @d pdf_new_line_char == 10 {new-line character for UNIX platforms} @d pdf_print_ln == pdf_out(pdf_new_line_char) {output a new-line character to \.{PDF} buffer} @p procedure pdf_print(s: str_number); {print out a string to \.{PDF} buffer} var j: pool_pointer; {current character code position} begin pdf_room(length(s)); j:=str_start[s]; while j-100000000 then negate(n) else begin m:=-1-n; n:=m div 10; m:=(m mod 10)+1; k:=1; if m<10 then dig[0]:=m else begin dig[0]:=0; incr(n); end; end; end; repeat dig[k]:=n mod 10; n:=n div 10; incr(k); until n=0; pdf_room(k); while k>0 do begin decr(k); pdf_quick_out("0"+dig[k]); end; end; procedure pdf_print_int_nl(n:integer); {print out an integer to \.{PDF} buffer followed by new-line character} begin pdf_print_int(n); pdf_print_ln; end; procedure pdf_print_two(n:integer); {prints two least significant digits in decimal form to \.{PDF} buffer} begin n:=abs(n) mod 100; pdf_out("0"+(n div 10)); pdf_out("0"+(n mod 10)); end; procedure pdf_print_scaled(s:scaled); {prints scaled real, rounded to two digits to \.{PDF} buffer} var delta:scaled; {amount of allowable inaccuracy} begin if s > 0 then s := xn_over_d(s, 7200, 7227) else s := -xn_over_d(-s, 7200, 7227); if s<0 then begin pdf_out("-"); negate(s); {print the sign, if negative} end; s := s + 328; {328 represents 0.005pt -- to round the last cifer} pdf_print_int(s div unity); {print the integer part} if (s mod unity) < 655 then {655 represents 0.01pt} return; pdf_out("."); s:=10*(s mod unity); pdf_out("0" + (s div unity)); s:=(10*(s mod unity)) div unity; if s > 0 then pdf_out("0"+s); end; procedure pdf_print_scaled_nl(s:scaled); {print out a scaled real to \.{PDF} buffer followed by new-line character} begin pdf_print_int(s); pdf_print_ln; end; procedure pdf_print_octal(n:integer); {prints an integer in octal form to \.{PDF} buffer} var k:0..23; {index to current digit; we assume that $|n|<10^{23}$} begin k:=0; repeat dig[k]:=n mod 8; n:=n div 8; incr(k); until n=0; if k = 1 then pdf_print("00"); if k = 2 then pdf_out("0"); while k>0 do begin decr(k); pdf_out("0"+dig[k]); end; end; procedure pdf_print_mag; {print a real number representing the magnification ratio} var m: integer; begin m := mag; pdf_print_int(m div 1000); m := m mod 1000; while m > 0 do begin pdf_out(" "); m := m*10; pdf_print_int(m div 1000); m := m mod 1000; end; end; @ Some macros and procedures to output entries of \.{PDF} dictionaries @d pdf_begin_obj(#) == begin {begin a \.{PDF} object} obj_offset(#) := pdf_offset; pdf_print_int(#); pdf_print_nl(" 0 obj"); end @# @d pdf_end_obj == pdf_print_nl("endobj") {end a \.{PDF} object} @# @d pdf_begin_dict(#) == begin {begin a \.{PDF} dictionary object} obj_offset(#) := pdf_offset; pdf_print_int(#); pdf_print_nl(" 0 obj <<"); end @# @d pdf_end_dict == pdf_print_nl(">> endobj") {end a \.{PDF} object of type dictionary} @p procedure pdf_print_offset(n: integer); {print out an entry of cross-reference table to \.{PDF} buffer} var k:0..23; {index to current digit; we assume that $|n|<10^{23}$} m:integer; {used to negate |n| in possibly dangerous cases} begin k:=0; repeat dig[k]:=n mod 10; n:=n div 10; incr(k); until k = 10; pdf_room(k); while k>0 do begin decr(k); pdf_quick_out("0"+dig[k]); end; end; procedure pdf_int_entry(s: str_number; v: integer); {print out an entry in dictionary with integer value to \.{PDF} buffer} begin pdf_out("/"); pdf_print(s); pdf_out(" "); pdf_print_int(v); end; procedure pdf_int_entry_nl(s: str_number; v: integer); begin pdf_int_entry(s, v); pdf_print_ln; end; procedure pdf_indirect(s: str_number; o: pointer); {print out an indirect entry in dictionary} begin pdf_out("/"); pdf_print(s); pdf_out(" "); pdf_print_int(o); pdf_print(" 0 R"); end; procedure pdf_indirect_nl(s: str_number; o: pointer); begin pdf_indirect(s, o); pdf_print_ln; end; procedure pdf_print_str(s: str_number); {print out |s| as string in \.{PDF} output} begin pdf_out("("); pdf_print(s); pdf_out(")"); end; procedure pdf_print_str_nl(s: str_number); {print out |s| as string in \.{PDF} output} begin pdf_print_str(s); pdf_print_ln; end; procedure pdf_str_entry(s, v: str_number); {print out an entry in dictionary with string value to \.{PDF} buffer} begin if v = 0 then return; pdf_out("/"); pdf_print(s); pdf_out(" "); pdf_print_str(v); end; procedure pdf_str_entry_nl(s, v: str_number); begin if v = 0 then return; pdf_str_entry(s, v); pdf_print_ln; end; @ Coordinate system and moving in \.{PDF} page description. @d pdf_h == dvi_h {horizontal coordinate in \.{PDF} output page} @d pdf_v == dvi_v {vertical coordinate in \.{PDF} output page} @d pdf_f == dvi_f {the current font in \.{PDF} output page} @= @!pdf_last_h: scaled; @!pdf_last_v: scaled; @!pdf_origin_h: scaled; @!pdf_origin_v: scaled; @!pdf_word_spacing: integer; {space amount to add to the beginning of the string being written} @!pdf_last_leading: scaled; {spave amount (leading) between last two lines} @!pdf_doing_string: boolean; {we are writting string to \.{PDF} file?} @!pdf_doing_text: boolean; {we are writting text section to \.{PDF} file?} @!pdf_font_changed: boolean; {current font was changed?} @ Following procedures take care of moving and drawing text string in \.{PDF} output page. @d pdf_1in == 4736287 {scaled value coresponds to 1in = unity*72.27 = 4736286.62} @d pdf_x(#) == ((#) - pdf_origin_h) @d pdf_y(#) == (pdf_origin_v - (#)) @p function em_thousandth(s, m: scaled): scaled; {convert a |scaled| value |s| in to thousandths of |m|, which should be a positive |scaled|.} var k, a, b: integer; neg: boolean; begin if s < 0 then begin s := -s; neg := true; end else neg := false; if m <= 0 then begin arith_error := true; return; end; if m*10 < 0 then begin {m is too big, we must round the last digit} if m mod 10 >= 5 then m := 1 + m div 10 else m := m div 10; a := 100*(s div m); end else a := 1000*(s div m); s := 10*(s mod m); b := 0; for k := 1 to 3 do begin b := 10*b + s div m; s := 10*(s mod m); end; if s div m >= 5 then incr(b); if neg then em_thousandth := -a - b else em_thousandth := a + b; end; procedure pdf_set_origin; {set the origin to |cur_h|, |cur_v|} var x, y: scaled; begin pdf_h := cur_h; pdf_v := cur_v; pdf_last_h := cur_h; pdf_last_v := cur_v; x := pdf_x(cur_h); y := pdf_y(cur_v); pdf_origin_h := cur_h; pdf_origin_v := cur_v; if (x = 0) and (y = 0) then return; pdf_print("1 0 0 1 "); pdf_print_scaled(x); pdf_out(" "); pdf_print_scaled(y); pdf_print_nl(" cm"); end; procedure pdf_end_string; {end the current string} begin if pdf_doing_string then begin pdf_print(")]TJ"); if pdf_word_spacing <> 0 then pdf_h := pdf_h - pdf_word_spacing; pdf_doing_string := false; end; end; procedure pdf_moveto; {move the starting point for drawing the next string} begin pdf_end_string; if (pdf_ptr > 0) then if (pdf_buf[pdf_ptr - 1] <> " ") and (pdf_buf[pdf_ptr - 1] <> pdf_new_line_char) then pdf_out(" "); if (pdf_h = pdf_last_h) and (pdf_v - pdf_last_v = pdf_last_leading) then begin pdf_print_nl("T*"); pdf_last_v := pdf_v; end else begin pdf_print_scaled(pdf_h - pdf_last_h); if pdf_v = pdf_last_v then pdf_print(" 0 Td") else begin pdf_out(" "); pdf_last_leading := pdf_v - pdf_last_v; pdf_print_scaled(-pdf_last_leading); pdf_print(" TD"); end; pdf_last_h := pdf_h; pdf_last_v := pdf_v; end; end; procedure pdf_begin_text; {begin a text section} begin if not pdf_doing_text then begin pdf_set_origin; pdf_print_nl("BT"); pdf_doing_text := true; end; end; procedure pdf_begin_string; {begin to draw a string} var b: boolean; {|b| is true only when we must adjust word spacing at the beginning of string} s: integer; begin pdf_begin_text; b := false; s := em_thousandth(cur_h - pdf_h, font_size[f]); if pdf_font_changed or (cur_v <> pdf_v) or (abs(s) >= @'100000) then begin pdf_word_spacing := 0; pdf_h := cur_h; pdf_v := cur_v; pdf_moveto; pdf_font_changed := false; end; if pdf_doing_string then begin if cur_h <> pdf_h then pdf_out(")"); end else begin pdf_out("["); if cur_h = pdf_h then pdf_out("(") else b := true; pdf_doing_string := true; end; if cur_h <> pdf_h then begin pdf_print_int(-em_thousandth(cur_h - pdf_h, font_size[f])); if b then pdf_word_spacing := cur_h - pdf_h; pdf_out("("); pdf_h := cur_h; end; end; procedure pdf_end_text; {end a text section} begin if pdf_doing_text then begin pdf_end_string; pdf_print_ln; pdf_print_nl("ET"); {pdf_print_nl("Q");} pdf_doing_text := false; {pdf_f := null_font;} end; end; procedure pdf_set_rule(h, v, wd, ht: scaled); {output a rule to \.{PDF} file} begin pdf_end_text; pdf_print_scaled(pdf_x(h)); pdf_out(" "); pdf_print_scaled(pdf_y(v)); pdf_out(" "); pdf_print_scaled(ht); pdf_out(" "); pdf_print_scaled(wd); pdf_print_nl(" re f"); end; procedure pdf_rectangle(left, top, right, bottom: scaled); {output a rectangle specification to \.{PDF} file} begin pdf_print("/Rect ["); pdf_print_scaled(pdf_x(left)); pdf_out(" "); pdf_print_scaled(pdf_y(bottom)); pdf_out(" "); pdf_print_scaled(pdf_x(right)); pdf_out(" "); pdf_print_scaled(pdf_y(top)); pdf_print_nl("]"); end; @ The cross-reference table and related subroutines This is the most important part of \.{PDF} file generating. The cross-reference table |obj_tab| is an array of |obj_tab_size| of |tab_entry|. Each entry contains two memory word and represents an object in \.{PDF} file whose object number is the index of this entry in |obj_tab|. Objects in |obj_tab| maybe linked into linked list; objects in a list always have the same type. The first memory word of |obj_entry| contains (1) link to the next object in |obj_tab| if this object in linked in a list; (2) the most important information of this object in linked list. The second memory word may contain various types of data; their interpretations are dependent on the type of object represented by this entry. The array |head_tab| contains pointers representing head of each linked list of entries in |obj_tab|. Some entries have a auxiliary pointer which point to next data of this object. It's typically for annotations, outlines, XObject etc. @d obj_w0(#) == obj_tab[#].obj_entry_w0 {the first memory word of |obj_entry|} @d obj_w1(#) == obj_tab[#].obj_entry_w1 {the second memory word of |obj_entry|} @# @d obj_info(#) == obj_w0(#).hh.lh {the most important information of this entry; it's usually an object' identifier} @d obj_link(#) == obj_w0(#).hh.rh {link to the next entry in linked list} @d obj_lh(#) == obj_w1(#).hh.lh {first halfword in second memory word} @d obj_rh(#) == obj_w1(#).hh.rh {first halfword in second memory word} @d obj_int(#) == obj_w1(#).int {accumulator for saving integer data} @d obj_scaled(#) == obj_w1(#).sc {accumulator for saving scaled data} @d obj_offset == obj_int {byte offset of this object in \.{PDF} output file} @d obj_ptr == obj_lh @d obj_aux == obj_rh @# @d pdf_others == 0 {objects which are not linked in any list} @d pdf_page == 1 {index for linked list of Page objects} @d pdf_pages == 2 {index for linked list of Pages objects} @d pdf_font == 3 {index for linked list of Fonts objects} @d pdf_outline == 4 {index for linked list of outline objects} @d pdf_dest == 5 {index for linked list of destination objects} @d pdf_form == 6 {index for linked list of XObject forms} @d pdf_thread == 7 {index for linked list of article threads} @d head_tab_max == 7 {max index of |head_tab|} @# @d pages_tree_kids_max == 6 {max number of kids of Pages tree node} @d name_tree_kids_max == 6 {max number of kids of name tree node for name destinations} @# @d pdf_id_num == 0 {num identifier} @d pdf_id_name == 1 {name identifier} @d pdf_id_page == 2 {page number} @# @d pdf_left(#) == mem[# + 1].sc @d pdf_top(#) == mem[# + 2].sc @d pdf_right(#) == mem[# + 3].sc @d pdf_bottom(#) == mem[# + 4].sc @# @d pdf_width(#) == mem[# + 1].sc @d pdf_height(#) == mem[# + 2].sc @d pdf_depth(#) == mem[# + 3].sc @# {when a node for annotation is created, words |1..3| are width, height and depth of this annotation; after shipping out words |1..4| are rectangle specification of annotation} @d pdf_annot_attr(#) == info(# + 5) {attributes of annotation} @# @d obj_annot_text_ptr == obj_ptr {pointer to |pdf_annot_text_node|} @d pdf_annot_text_node_size== 6 {size of whatsit node for text annotation} @d pdf_annot_text_title(#) == link(# + 5) @# @d obj_annot_link_ptr == obj_ptr {pointer to |pdf_annot_link_node|} @d pdf_annot_link_node_size== 7 {size of whatsit node for link annotation} @d pdf_annot_link_next(#) == link(# + 5) {next node for multi-line link} @d pdf_annot_link_action(#)== info(# + 6) {pointer to action structure} @d pdf_action_struct_size == 3 {structure to hold action information} @d pdf_action_id_type == type {identifier type} @d pdf_action_type == subtype {action type} @d pdf_action_goto == 0 {Goto action} @d pdf_action_thread == 1 {Thread action} @d pdf_action_user_defined == 2 {user-defined action} @d pdf_action_name == link {destination/thread name identifier} @d pdf_action_num == link {destination/thread num identifier; for link page action this is the page number} @d pdf_action_user_defined_str == link {user-defined action string} @d pdf_action_file(#) == info(# + 1) {file name for GoToR/Thread action} @d pdf_action_objnum(#) == link(# + 1) {object number of destination/page for GoTo action} @d pdf_action_page_dest(#) == info(# + 2) {specification of goto page action} @# {the following constants defines the context of |find_identifier|} @d find_goto_dest == 0 @d find_thread_dest == 1 @d find_page == 2 @d find_goto_action == 3 @d find_thread_action == 4 @# {it's not able to write out outline entries before all outline entries are defined, so memory allocated for outline entries can't not be deallocated and will stay in memory. For this reason we will store data of outline entries in |pdf_mem| instead of |mem|} @# @d pdf_lh(#) == pdf_mem[#].hh.lh @d pdf_rh(#) == pdf_mem[#].hh.rh @# @d pdf_outline_struct_size == 3 {size of memory in |pdf_mem| which |obj_ptr| points to} @d obj_outline_action_objnum== obj_info {object number of action} @d obj_outline_ptr == obj_ptr @d obj_outline_count == obj_aux {count of all opened children} @d obj_outline_title(#) == pdf_lh(obj_outline_ptr(#)) @d obj_outline_parent(#) == pdf_rh(obj_outline_ptr(#)) @d obj_outline_prev(#) == pdf_lh(obj_outline_ptr(#) + 1) @d obj_outline_next(#) == pdf_rh(obj_outline_ptr(#) + 1) @d obj_outline_first(#) == pdf_lh(obj_outline_ptr(#) + 2) @d obj_outline_last(#) == pdf_rh(obj_outline_ptr(#) + 2) @# @d obj_dest_num == obj_info {num identifier of destination; destination with name identifier will have this entry non-positive; its absolute value is string number of identifier name} @d obj_dest_name == -obj_info @d obj_dest_ptr == obj_ptr {pointer to |pdf_dest_node|} @d pdf_dest_node_size == 5 {size of whatsit node for destination; words |1..2| are current position, word |3| is identifier type, subtype and identifier of destination} @d pdf_dest_id_type(#) == type(# + 3) {type of destination identifier} @d pdf_dest_type(#) == subtype(# + 3) {type of destination} @d pdf_dest_xyz == 0 @d pdf_dest_fit == 1 @d pdf_dest_fit_h == 2 @d pdf_dest_fit_v == 3 @d pdf_dest_fitb == 4 @d pdf_dest_fitb_h == 5 @d pdf_dest_fitb_v == 6 @d pdf_dest_num(#) == link(# + 3) {num identifier of destination} @d pdf_dest_name(#) == link(# + 3) @d pdf_dest_objnum(#) == info(# + 4) {zoom factor for |destxyz| destination} @d pdf_dest_xyz_zoom(#) == link(# + 4) {zoom factor for |destxyz| destination} @# @d obj_thread_num == obj_info @d obj_thread_name == -obj_info {this similiar as destination} @d obj_thread_first == obj_ptr {pointer to the first bead of thread} @d pdf_thread_node_size == 2 @d pdf_thread_id_type(#) == type(# + 1) {type of thread identifier} @d pdf_thread_num(#) == link(# + 1) {thread num identifier} @d pdf_thread_name(#) == link(# + 1) {thread name identifier} @d pdf_bead_struct_size == 3 {this is similiar as |pdf_outline_struct_size|} @d obj_bead_rect(#) == pdf_lh(obj_ptr(#)) @d obj_bead_page(#) == pdf_rh(obj_ptr(#)) @d obj_bead_next(#) == pdf_lh(obj_ptr(#) + 1) @d obj_bead_prev(#) == pdf_rh(obj_ptr(#) + 1) @# {for |pdf_bead_node| word |1..4| are rectangle specification; word 0 is unused} @d pdf_bead_rect_node_size == 5 {bead node size} @# @d pdf_image_node_size == 2 {size of whatsit node for image} @d pdf_image_info(#) == info(# + 1) {pointer to hold data structures in external \.{libpng} library} @d pdf_image_box(#) == link(# + 1) {pointer to box containing image} @# {for |pdf_image_node| and |pdf_ref_form_node| the identifier of XObject will be store in |obj_info| of coresponding object in |obj_tab|} @# @d pdf_ref_form_node_size == 2 {size of whatsit node for \.{\\pdfrefform} primitive} @d pdf_form_objnum(#) == info(# + 1) {index of coresponding XObject in |obj_tab|} @d pdf_form_box(#) == link(# + 1) {pointer to box containing form} @# @d pdf_form_struct_size == 4 {size of memory in |pdf_mem| which |obj_ptr| points to} @d obj_form_offset(#) == pdf_mem[obj_ptr(#)].int {to store offset of coresponding form in \.{PDF} file, because |obj_ptr| will be used in whole run-time} @d obj_form_width(#) == pdf_mem[obj_ptr(#) + 1].sc @d obj_form_height(#) == pdf_mem[obj_ptr(#) + 2].sc @d obj_form_depth(#) == pdf_mem[obj_ptr(#) + 3].sc @= @!obj_tab_size=65535; {size of the cross-reference table for \.{PDF} output} @ @= @!obj_entry = record@;@/ obj_entry_w0, obj_entry_w1: memory_word; end; @ @= @!obj_tab: array [0..obj_tab_size] of obj_entry; @!head_tab: array[1..head_tab_max] of pointer; @!obj_count: pointer; {objects counter} @!pdf_last_pages: pointer; {pointer to most recently generated pages object} @!pdf_last_page: pointer; {pointer to most recently generated page object} @!pdf_last_stream: pointer; {pointer to most recently generated stream} @!pdf_last_resources: pointer; {pointer to most recently generated Resources object} @!pdf_stream_length: integer; {length of most recently generated stream} @!pdf_image_b: boolean; {is black-white image used in most recently output page/form?} @!pdf_image_c: boolean; {is color image used in most recently output page/form?} @ @= obj_count := 0; for k := 1 to head_tab_max do head_tab[k] := null; @ Subroutines for working with objects Procedure |pdf_create_obj| creats a new object with identifier |i|. If type |t| of object is other than |pdf_others| then it also inserts this object to the coresponding linked list in |head_tab|. Page objects are sorted. @p procedure pdf_create_obj(t, i: halfword); {creat a object with type |t| and identifier |id|} label done; var p, q: integer; begin if obj_count = obj_tab_size then overflow("indirect objects table size", obj_tab_size); incr(obj_count); obj_info(obj_count) := i; if t = pdf_page then begin obj_int(obj_count) := 0; {mark that this Page object is newly created} p := head_tab[t]; {find the right poition to insert newly created object}@/ if (p = null) or (obj_info(p) < i) then begin obj_link(obj_count) := p; head_tab[t] := obj_count; end else begin while p <> null do begin if obj_info(p) < i then goto done; q := p; p := obj_link(p); end; done: obj_link(q) := obj_count; obj_link(obj_count) := p; end; end else if t <> pdf_others then begin obj_link(obj_count) := head_tab[t]; head_tab[t] := obj_count; end; end; function pdf_find_objnum(t, i: halfword): pointer; {find out a object with info |i| in a linked list of type |t|} var p: pointer; begin pdf_find_objnum := null; p := head_tab[t]; while p <> null do begin if obj_info(p) = i then begin pdf_find_objnum := p; return; end; p := obj_link(p); end; end; function pdf_find_objname(t: halfword; s: str_number): pointer; {find out a object with |-obj_info=s|} var p: pointer; begin pdf_find_objname := null; p := head_tab[t]; while p <> null do begin if (obj_info(p) <= 0) and str_eq_str(-obj_info(p), s) then begin pdf_find_objname := p; return; end; p := obj_link(p); end; end; procedure pdf_new_obj(t, i: halfword); {begin a new object} begin pdf_create_obj(t, i); pdf_begin_obj(obj_count); end; procedure pdf_new_dict(t, i: halfword); {begin a new object with type dictionary} begin pdf_create_obj(t, i); pdf_begin_dict(obj_count); end; function push_ptr(p: pointer; i: halfword): pointer; {push a pointer with info |i| into a linked list with head |p|} var q: pointer; begin fast_get_avail(q); link(q) := p; info(q) := i; push_ptr := q; end; procedure pdf_setfont(f: internal_font_number); {change font |pdf_f| to |f|; generate font object for font |f| if necessary} label done, done1; var p: pointer; i, k: integer; begin p := pdf_font_list; {first look-up for font |f| in |pdf_font_list|} while p <> null do begin if info(p) = f then goto done; p := link(p); end; done: if p = null then pdf_font_list := push_ptr(pdf_font_list, f); if p = null then begin {|f| not found in |pdf_font_list|} if not font_used[f] then begin {test if |f| was already generated} k := font_base; while k <= font_ptr do begin if font_used[k] and str_eq_str(font_name[k], font_name[f]) and str_eq_str(font_area[k], font_area[f]) then goto done1; {found font with same name} incr(k); end; done1: if k <= font_ptr then begin pdf_font_num[f] := pdf_font_num[k]; end else begin pdf_create_obj(pdf_font, f); pdf_font_num[f] := obj_count; font_used[f] := true; end; end; end; pdf_begin_text; pdf_print("/F"); pdf_print_int(f); pdf_out(" "); pdf_print_scaled(font_size[f]); pdf_print(" Tf"); pdf_f := f; pdf_font_changed := true; pdf_last_leading := max_dimen; end; function pdf_get_mem(s: small_number): pointer; {allocate |s| words in |pdf_mem|} begin if pdf_mem_ptr + s > pdf_mem_size then overflow("PDF memory size", pdf_mem_size); pdf_get_mem := pdf_mem_ptr; pdf_mem_ptr := pdf_mem_ptr + s; end; @ Processing fonts in \.{PDF}. Here we declare the global variables, related macros, constants for processing fonts in \.{PDF} file. We will use the same way in \TeX\ to handle the \.{PDF} font parametrs, the big array |pdf_font_info| is the same as |font_info|, but for the \.{PDF} output using. @d pdf_ascent_code = 0 @d pdf_capheight_code = 1 @d pdf_descent_code = 2 @d pdf_fontname_code = 3 @d pdf_italic_angle_code = 4 @d pdf_stemv_code = 5 @d pdf_avgwidth_code = 6 @d pdf_leading_code = 7 @d pdf_maxwidth_code = 8 @d pdf_missingwidth_code = 9 @d pdf_stemh_code = 10 @d pdf_xheight_code = 11 @d pdf_fontbbox_code = 12 @d pdf_fp_max = 15 @# @d pdf_fp_end(#) == pdf_fp_base[#]].int @d pdf_fp(#) == pdf_font_info[#+pdf_fp_end @# @d pdf_ascent == pdf_fp(pdf_ascent_code) @d pdf_capheight == pdf_fp(pdf_capheight_code) @d pdf_descent == pdf_fp(pdf_descent_code) @d pdf_fontname == pdf_fp(pdf_fontname_code) @d pdf_italic_angle == pdf_fp(pdf_italic_angle_code) @d pdf_stemv == pdf_fp(pdf_stemv_code) @d pdf_avgwidth == pdf_fp(pdf_avgwidth_code) @d pdf_leading == pdf_fp(pdf_leading_code) @d pdf_maxwidth == pdf_fp(pdf_maxwidth_code) @d pdf_missingwidth == pdf_fp(pdf_missingwidth_code) @d pdf_stemh == pdf_fp(pdf_stemh_code) @d pdf_xheight == pdf_fp(pdf_xheight_code) @d pdf_fontbbox == pdf_fp(pdf_fontbbox_code) @# @d new_font_type = 0 {flag for font that is not used yet} @d real_font_type = 1 {flag for real font} @d virtual_font_type = 2 {flag for virtual font} @= @!pdf_fmem_size=20000; {number of word of |pdf_font_info|} @ @= @!pdf_font_index=0..pdf_fmem_size; {index into |pdf_font_info|} @ @= @!pdf_font_num:^pointer; {mapping between internal font number in \TeX\ and font name defined in resources in \.{PDF} file} @!pdf_font_info: array[pdf_font_index] of memory_word; @!pdf_fp_names: array[0..pdf_fp_max] of str_number; {the font parametr names table to look up parametr in type1 pfb file} @!pdf_fp_base: ^integer; {base addresses for \.{PDF} font parametrs in |pdf_font_info|} @!pdf_font_included: ^boolean; {the font file will be included to \.{PDF} output?} @!pdf_fmem_ptr: pdf_font_index; {first unused word of |pdf_font_info|} @!pdf_font_list: pointer; {list of used fonts in current page} @!pdf_char_used: array[nine_bits,0..31] of eight_bits; {is character of font used?} @!pdf_font_type: ^eight_bits; {the type of font} @!pdf_ff_length1: integer; {length of ASCII portion in \.{T1} font} @!pdf_ff_length2: integer; {length of encrypted portion in \.{T1} font} @!pdf_ff_length3: integer; {length of last portion in \.{T1} font} @ @= pdf_fmem_ptr := 0; pdf_font_list := null; for k := 0 to pdf_fp_max do case k of pdf_ascent_code: pdf_fp_names[k] := "/Ascender"; pdf_capheight_code: pdf_fp_names[k] := "/CapHeight"; pdf_descent_code: pdf_fp_names[k] := "/Descender"; pdf_fontname_code: pdf_fp_names[k] := "/FontName"; pdf_italic_angle_code: pdf_fp_names[k] := "/ItalicAngle"; pdf_stemv_code: pdf_fp_names[k] := "/StdVW"; pdf_stemh_code: pdf_fp_names[k] := "/StdHW"; pdf_xheight_code: pdf_fp_names[k] := "/XHeight"; pdf_fontbbox_code: pdf_fp_names[k] := "/FontBBox"; othercases pdf_fp_names[k] := 0; endcases; @ Font reencoding We use a subroutine called |enc_include| to replace built-in encoding of font by new encoding specified in encoding file (see also font mapping). Encoding vectors are store in |enc_tab|. @d enc_byte == getc(enc_file) {get a byte from |enc_file|} @d append_char_to_buf(#) == {append a char to buffer, skip unnecesary spaces and remap tab to space, CR to LF} if # = 9 then # := 32; {remap tabs to spaces} if # = 13 then # := 10; {remap CR to LF} cur_char := #; {for using in |append_char_to_buf_end|} append_char_to_buf_end @d append_char_to_buf_end(#) == if (cur_char <> 32) or ((#@&end > 0) and (#[#@&end - 1] <> 32)) then begin if (#@&end >= line_size) then overflow("enc/fm/pfb line size", line_size); #[#@&end] := cur_char; incr(#@&end); end @d append_eol(#) == if # <> 10 then append_eol_end @d append_eol_end(#) == begin if (#@&end >= line_size) then overflow("enc/fm/pfb line size", line_size); #[#@&end] := 10; incr(#@&end); end @= @!enc_max = 20; {the maximum number of encoding table } @ @= @!enc_vector = array[0..255] of str_number; {table of glyph names} @!enc_entry = record enc_name: str_number; {name of encoding vector} enc_objnum: integer; {object number that refers to this vector in \.{PDF} file} enc_glyph_names: enc_vector; end; @ @= @!enc_file: byte_file; {the encoding file} @!enc_line: array[0..line_size] of eight_bits; {buffer to process lines of encoding file} @!enc_line_end: integer; {the pointer to the end of the line buffer} @!cur_char: eight_bits; {accumulator for using of macro |append_char_to_buf|} @!enc_tab: array[1..enc_max] of enc_entry; @!enc_ptr: integer; {first unused entry in |enc_tab|} @ @= enc_ptr := 1; {|enc_tab| begins from 1} @ Now we declare procedure |enc_include|, which should include the encoding vector to \.{DPF} output. Subroutines |enc_prefix| is used to test if a string is prefix of |enc_line|. @p function enc_prefix(i: integer; s: str_number): boolean; {test if |s| is |enc_prefix| of |enc_line| from position |i|} var k: integer; begin enc_prefix := false; if s = 0 then return; k := str_start[s]; while (i < enc_line_end) and (k < str_start[s + 1]) do begin if (enc_line[i] <> str_pool[k]) then return; incr(i); incr(k); end; if k < str_start[s + 1] then return; enc_prefix := true; end; procedure enc_include(e: integer); {include the new encoding to Type1 font file; |e| is the index of the encoding vector in |enc_tab|} label done, done1, continue; var c, k, names_count: integer; s: str_number; is_notdef: boolean; begin pack_file_name(enc_tab[e].enc_name, "", ""); if not texpsheader_b_open_in(enc_file) then begin print_nl("Error in reencoding font("); print(enc_tab[e].enc_name); print("): Can not open encoding file for reading!"); print_ln; return; end; print("<"); slow_print(b_make_name_string(cur_file)); flush_string; update_terminal; enc_line_end := 0; is_notdef := false; while true do begin repeat c := enc_byte; append_char_to_buf(c)(enc_line); until eof(enc_file) or (c = 10); if (enc_line[0] <> "%") and (enc_line_end > 1) then goto done1; {we assume that this line contains only the encoding name followed by the character \.{`['}} enc_line_end := 0; end; done1: pdf_new_dict(pdf_others, e); enc_tab[e].enc_objnum := obj_count; names_count := 0; for k := 0 to 255 do enc_tab[e].enc_glyph_names[k] := 0; pdf_print_nl("/Type /Encoding"); pdf_print("/Differences ["); enc_line_end := 0; while true do begin repeat c := enc_byte; if c = "%" then begin repeat c := enc_byte; until eof(enc_file) or (c = 10) or (c = 13); c := 10; end; append_char_to_buf(c)(enc_line); until eof(enc_file) or (c = 10); append_eol(c)(enc_line); if enc_line[enc_line_end - 2] = 32 then begin decr(enc_line_end); enc_line[enc_line_end - 1] := 10; end; if (enc_line[0] = "%") or (enc_line_end = 1) then goto continue; if enc_prefix(0, "] def") then goto done; k := 0; repeat str_room(enc_line_end); while (enc_line[k] <> 32) and (enc_line[k] <> 10) do begin append_char(enc_line[k]); incr(k); end; s := make_string; if str_eq_str(s, "/.notdef") then begin if not is_notdef then begin pdf_out(" "); pdf_print_int(names_count); pdf_print(s); is_notdef := true; end; flush_string; end else begin if is_notdef or (names_count = 0) then begin pdf_out(" "); pdf_print_int(names_count); is_notdef := false; end; pdf_print(s); enc_tab[e].enc_glyph_names[names_count] := s; end; incr(names_count); incr(k); until k = enc_line_end; continue: enc_line_end := 0; end; done: pdf_print_nl("]"); pdf_end_dict; print_char(">"); update_terminal; b_close(enc_file); end; @ Procesing font mapping We use the similiar way of font maping for downloading in \.{dvips}. See source file \.{resident.c} for more detail. @d mapfile == "pdftex.map" {the font mapping file name} @d fm_byte == getc(fm_file) {get a byte from |fm_file|} @d is_base_font == (fm_tab[fm_cur].flags = 0) @d is_font_file == (fm_tab[fm_cur].fname > 0) @d is_included_font == fm_tab[fm_cur].include @d is_subsetted_font == fm_tab[fm_cur].subset @d is_truetype_font == fm_tab[fm_cur].truetype @d is_reencoded_font == (fm_tab[fm_cur].encoding > 0) @d skip_field == {skip a field in |fm_line|} while (k < fm_line_end) and (fm_line[k] <> 32) and (fm_line[k] <> 10) do incr(k); if k >= fm_line_end - 1 then goto done; incr(k) @d read_field(#) == {read a field from |fm_line| to |fm_entry|} str_room(fm_line_end - k); while (k < fm_line_end) and (fm_line[k] <> 32) and (fm_line[k] <> 10) do begin append_char(fm_line[k]); incr(k); end; s := make_string; if length(s) > 0 then fm_tab[fm_ptr].# := s else flush_string; if k >= fm_line_end - 1 then goto done; incr(k) @= @!fm_entry = record tex_name, {name of \.{TFM} metric without suffix \.{.tfm}} fontbase_name, {font base name} flags, {font flags} fname: str_number; {font file name} encoding: eight_bits; {index to |enc_tab|} include: boolean; {this font will be embbed?} subset: boolean; {this font will be subsetted?} truetype: boolean; {is the TrueType font?} end; @ @= @!fm_file: byte_file; {the mapping font file} @!fm_line: array[0..line_size] of eight_bits; {buffer to process lines of map file} @!fm_line_end: integer; {the pointer to the end of the line buffer} @!fm_tab: ^fm_entry; {table of font mappings} @!fm_ptr: integer; {first unused entry in |fm_tab|} @!fm_cur: integer; {index of current font with internal font number |f|} @!base_font_names: array[0..13] of str_number; {names of 14 base fonts} @ @= fm_ptr := 0; base_font_names[0] := "Courier"; base_font_names[1] := "Courier-Bold"; base_font_names[2] := "Courier-Oblique"; base_font_names[3] := "Courier-BoldOblique"; base_font_names[4] := "Helvetica"; base_font_names[5] := "Helvetica-Bold"; base_font_names[6] := "Helvetica-Oblique"; base_font_names[7] := "Helvetica-BoldOblique"; base_font_names[8] := "Symbol"; base_font_names[9] := "Times-Roman"; base_font_names[10] := "Times-Bold"; base_font_names[11] := "Times-Italic"; base_font_names[12] := "Times-BoldItalic"; base_font_names[13] := "ZapfDingbats"; @ Each line in font mapping file should contain these fields in fixed order: |tex_name|, |fontbase_name|, |flags|, |fname|, |encoding_file|. We store these information in |fm_tab| by procedure |fm_get_info|, and to lookup an entry in this table we use function |fm_lookup|. If an encoding file is specified we replace the built-in font encoding by this encoding file. @p function fm_lookup(s: str_number): integer; {lookup for a name in |fm_tab|} var k: integer; begin fm_lookup := -1; k := 0; while k < fm_ptr do begin if str_eq_str(s, fm_tab[k].tex_name) then begin fm_lookup := k; return; end; incr(k); end; end; procedure fm_get_info; {read font mapping from |mapfile|} label done, continue; var c, k, i: integer; s: str_number; begin pack_file_name(mapfile, "", ""); if not texpsheader_b_open_in(fm_file) then begin print_nl("Can not open font mapping file ("); print(mapfile); print(") for reading"); print_ln; fatal_error("map file not found"); end; fm_line_end := 0; while true do begin fm_tab[fm_ptr].tex_name := 0; fm_tab[fm_ptr].fontbase_name := 0; fm_tab[fm_ptr].flags := 0; fm_tab[fm_ptr].fname := 0; fm_tab[fm_ptr].encoding := 0; fm_tab[fm_ptr].include := false; fm_tab[fm_ptr].subset := false; fm_tab[fm_ptr].truetype := false; repeat c := fm_byte; append_char_to_buf(c)(fm_line); until eof(fm_file) or (c = 10); append_eol(c)(fm_line); c := fm_line[0]; if (fm_line_end = 0) or (c = "*") or (c = "#") or (c = ";") or (c = "%") then goto continue; k := 0; read_field(tex_name); i := 0; repeat incr(i); until (i = 256) or (str_eq_str(s, font_name[i]) and font_used[i]); if i = 256 then goto continue; {we read only informations about used fonts} read_field(fontbase_name); i := 0; while (i < 14) and not str_eq_str(s, base_font_names[i]) do incr(i); if i < 14 then begin flush_string; fm_tab[fm_ptr].fontbase_name := base_font_names[i]; skip_field; {skip the font flag} skip_field; {skip the font file} end else begin read_field(flags); if fm_line[k] = "<" then begin fm_tab[fm_ptr].include := true; incr(k); end; if fm_line[k] = "<" then incr(k) else fm_tab[fm_ptr].subset := true; read_field(fname); end; if fm_line[k] = "<" then incr(k); str_room(fm_line_end - k); while (k < fm_line_end) and (fm_line[k] <> 32) and (fm_line[k] <> 10) do begin append_char(fm_line[k]); incr(k); end; s := make_string; if length(s) > 0 then begin i := 0; while (not str_eq_str(s, enc_tab[i].enc_name)) and (i < enc_ptr) do incr(i); if i = enc_ptr then begin enc_tab[i].enc_name := s; enc_include(i); incr(enc_ptr); if enc_ptr > enc_max then overflow("Encoding table size", enc_max); end; fm_tab[fm_ptr].encoding := i; end else flush_string; done: if fm_tab[fm_ptr].fname <> 0 then begin i := str_start[fm_tab[fm_ptr].fname + 1] - 1; if ((str_pool[i-2]="T") and (str_pool[i-1]="T") and (str_pool[i]="F")) or ((str_pool[i-2]="t") and (str_pool[i-1]="t") and (str_pool[i]="f")) then fm_tab[fm_ptr].truetype := true; end; incr(fm_ptr); if fm_ptr > font_max then overflow("Font map table size", font_max); continue: fm_line_end := 0; if eof(fm_file) then return; end; end; @ Processing \.{T1} font file @d t1_error = 60 {label to goto when an error ocurrs} @d is_newline(#) == ((# = 10) or (# = 13)) {is character a CR or LF?} @d is_space(#) == ((# = 9) or (# = 32)) {is character a space or tab?} @d is_digit(#) == ((# >= "0") and (# <= "9")) {is character a digit} @d t1_byte == getc(t1_file) {get a byte from \.{T1} file} @d bad_t1(#) == begin t1_err_str := #; goto t1_error; end {go out from processing \.{T1} font file with an error message} @d t1_check_EOF == if eof(t1_file) then bad_t1("unexpected end of file") {check if there is an unexpected end of file} @= @!t1_c1=52845; @!t1_c2=22719; {constants to decrypt the encrypted data in \.{T1} font file} @!line_size=1023; {size of buffer to process data in \.{T1} font file} @ @= @!t1_file:byte_file; {\.{T1} font file} @!t1_line:array[0..line_size] of eight_bits; {buffer to process lines of \.{T1} font file} @!t1_line_end:integer; {pointer to the end of \.{T1} buffer} @!t1_dr:integer; @!t1_er:integer; {variables to decrypt the encrypted data in \.{T1} font file} @!t1_in_eexec:boolean; {we are in |eexec|\/?} @!t1_is_pfa:boolean; {\.{T1} font is \.{PFA}?} @!t1_err_str:str_number; {error message} @ Subroutines for processing \.{T1} font file @p function xor(a, b: integer): integer; {XOR operator} var i, k: integer; begin i := 1; k := 0; while (a > 0) or (b > 0) do begin if (a mod 2) <> (b mod 2) then k := k + i; i := i*2; a := a div 2; b := b div 2; end; xor := k; end; function char2hex(c: eight_bits): eight_bits; begin if c <= "9" then c := c - "0" else if c <= "F" then c := c - "A" + 10 else if c <= "f" then c := c - "a" + 10; char2hex := c; end; function t1_decrypt(cipher: eight_bits): eight_bits; {decrypt a byte} var plain, b: eight_bits; begin if t1_is_pfa then begin while is_newline(cipher) do cipher := t1_byte; cipher := char2hex(cipher)*16 + char2hex(t1_byte); end; if t1_dr >= 0 then b := (t1_dr mod 65536) div 256 else b := (65536 + t1_dr mod 65536) div 256; plain := xor(cipher, b); t1_dr := (cipher + t1_dr)*t1_c1 + t1_c2; t1_decrypt := plain; end; function t1_encrypt(plain: eight_bits): eight_bits; {encrypt a byte} var cipher, b: eight_bits; begin if t1_er >= 0 then b := (t1_er mod 65536) div 256 else b := (65536 + t1_er mod 65536) div 256; cipher := xor(plain, b); t1_er := (cipher + t1_er)*t1_c1 + t1_c2; t1_encrypt := cipher; end; function t1_marker(b, m: eight_bits): boolean; {reads the marker of ASCII or encrypted portion in \.{T1} font file, returns |true| if OK} label t1_error; var k: integer; begin t1_marker := true; while is_newline(b) do begin b := t1_byte; t1_check_EOF; end; if b <> 128 then bad_t1("invalid marker"); if t1_byte <> m then bad_t1("invalid block type"); for k := 1 to 4 do begin b := t1_byte; t1_check_EOF; end; return; t1_error: t1_marker := false; end; function t1_prefix(s: str_number): boolean; {test if |s| is |t1_prefix| of |t1_line|} var i, k: integer; begin t1_prefix := false; if s = 0 then return; i := 0; k := str_start[s]; while (i < t1_line_end) and (k < str_start[s + 1]) do begin if (t1_line[i] <> str_pool[k]) then return; incr(i); incr(k); end; if k < str_start[s + 1] then return; t1_prefix := true; end; function t1_suffix(s: str_number): boolean; {test if |s| is suffix of |t1_line|} var i, k: integer; begin t1_suffix := false; if s = 0 then return; i := t1_line_end - 1; if t1_line[i] = 10 then decr(i); k := str_start[s + 1] - 1; while (i >= 0) and (k >= str_start[s]) do begin if (t1_line[i] <> str_pool[k]) then return; decr(i); decr(k); end; if k >= str_start[s] then return; t1_suffix := true; end; function subset_prefix(f: internal_font_number): str_number; {generate prefix for subsetted font name} var l, k: integer; begin l := 0; for k := 0 to 255 do if pdf_is_char_used(f, k) then l := l*2 + k; if l < 0 then l := -l - 1; for k := 1 to 6 do begin append_char("A" + l mod("Z" - "A")); l := l div("Z" - "A"); end; subset_prefix := make_string; end; function t1_scan_int(k: integer): integer; {scan an integer parametr from |k| position in |t1_line|} var i: integer; is_neg: boolean; begin i := 0; is_neg := false; if t1_line[k] = " " then incr(k); if k >= t1_line_end then return; if t1_line[k] = "-" then begin incr(k); is_neg := true; end else if t1_line[k] = "+" then incr(k); while (k < t1_line_end) and is_digit(t1_line[k]) do begin i := i*10 + t1_line[k] - "0"; incr(k); end; if is_neg then t1_scan_int := -i else t1_scan_int := i; end; procedure t1_getline; {get a line terminated by a new-line character from \.{T1} font file, decrypt it if necessary} label continue, reswitch; var i, k: integer; b: eight_bits; charstring : boolean; begin reswitch: t1_line_end := 0; charstring := false; b := t1_byte; while not eof(t1_file) do begin if t1_in_eexec then b := t1_decrypt(b); append_char_to_buf(b)(t1_line); if b = 10 then if t1_line_end = 1 then goto reswitch else return; if t1_in_eexec and (t1_line[0] <> "%") and not charstring then if t1_suffix(" RD ") or t1_suffix(" -| ") then begin k := t1_line_end - 5; while t1_line[k] <> " " do decr(k); i := t1_scan_int(k); for k := 1 to i do begin t1_line[t1_line_end] := t1_decrypt(t1_byte); incr(t1_line_end); end; charstring := true; end; continue: b := t1_byte; end; append_eol(t1_line[t1_line_end - 1])(t1_line); end; procedure t1_putline; {put a line to \.{PDF} output file, encrypt it if necessary} var k: integer; begin if (not is_included_font) or (t1_line_end <= 1) then return; pdf_room(t1_line_end); if t1_in_eexec then for k := 0 to t1_line_end - 1 do pdf_quick_out(t1_encrypt(t1_line[k])) else for k := 0 to t1_line_end - 1 do pdf_quick_out(t1_line[k]); end; procedure t1_scan_param; {scan parametrs with names in |pdf_fp_names| from |t1_line|, store its value if find} var i, j, k, l, m: integer; s, p, r: str_number; begin k := 0; while not t1_prefix(pdf_fp_names[k]) do begin incr(k); if k > pdf_fp_max then return; end; j := length(pdf_fp_names[k]); if t1_line[j] = " " then incr(j); {skip space if any} if k = pdf_fontname_code then begin incr(j); {skip the slash at the beginning of name} str_room(t1_line_end); m := j; repeat append_char(t1_line[j]); incr(j); until (t1_line[j] = " ") or is_newline(t1_line[j]); pdf_fontname(f) := make_string; while j < t1_line_end do begin append_char(t1_line[j]); incr(j); end; r := make_string; if is_subsetted_font then begin p := subset_prefix(f); for i := 0 to length(p) - 1 do t1_line[m + i] := strpool[str_start[p] + i]; m := m + length(p) + 1; t1_line[m - 1] := "+"; end; if str_eq_str(pdf_fontname(f), fm_tab[fm_cur].fontbase_name) then s := pdf_fontname(f) else s := fm_tab[fm_cur].fontbase_name; for i := 0 to length(s) - 1 do t1_line[m + i] := strpool[str_start[s] + i]; m := m + length(s); for i := 0 to length(r) - 1 do t1_line[m + i] := strpool[str_start[r] + i]; t1_line_end := m + length(r); if is_subsetted_font then flush_string; flush_string; end else begin if ((t1_line[j] = "[") or (t1_line[j] = "{")) and ((k = pdf_stemv_code) or (k = pdf_stemh_code) or (k = pdf_fontbbox_code)) then incr(j); if k = pdf_fontbbox_code then for i := 0 to 3 do begin pdf_fp(i + pdf_fontbbox_code)(f) := t1_scan_int(j); incr(j); while t1_line[j] <> " " do incr(j); end else pdf_fp(k)(f) := t1_scan_int(j); end; end; procedure t1_include; {the main procedure; include \.{T1} font file with internal font number |f| to \.{PDF} output file and scan all available parametrs of this font} label t1_error, done; var i, k: integer; s: str_number; t1_opened, pred_encoding: boolean; begin @; @; @; @; @; @; @; t1_error: if t1_err_str <> 0 then begin if t1_opened then b_close(t1_file); print_nl("Error in processing Type1 font file ("); print(fm_tab[fm_cur].fname); print("): "); print(t1_err_str); update_terminal; fatal_error("Can't handle Type1 font file"); end; end; @ @= t1_dr := 55665; t1_er := 55665; t1_in_eexec := false; t1_err_str := 0; pdf_fp_base[f] := pdf_fmem_ptr; pdf_fmem_ptr := pdf_fmem_ptr + pdf_fp_max + 1; if pdf_fmem_ptr > pdf_fmem_size then overflow("PDF memory size", pdf_fmem_size); for k := 0 to pdf_fp_max do pdf_fp(k)(f) := 0 @ @= t1_opened := false; pack_file_name(fm_tab[fm_cur].fname, "", ""); if not typeone_b_open_in(t1_file) then bad_t1("cannot open Type1 font file for reading"); t1_opened := true @ @= i := t1_byte; t1_is_pfa := false; if i <> 128 then t1_is_pfa := true else if not t1_marker(i, 1) then goto t1_error; pdf_save_offset := pdf_offset; t1_check_EOF; t1_getline; while not t1_prefix("/Encoding ") do begin t1_scan_param; t1_putline; t1_check_EOF; t1_getline; end; pred_encoding := t1_suffix("def"); {test if encoding is predefined} if not is_reencoded_font then begin t1_putline; if not pred_encoding then repeat t1_check_EOF; t1_getline; t1_putline; until t1_suffix("readonly def"); end else begin if is_included_font then begin pdf_print_nl("/Encoding 256 array"); pdf_print_nl("0 1 255 {1 index exch /.notdef put} for"); pdf_print_nl("readonly def"); end; if not pred_encoding then repeat t1_check_EOF; t1_getline; until t1_suffix("readonly def"); end; repeat {t1_check_EOF;} t1_getline; t1_scan_param; t1_putline; until t1_prefix("currentfile eexec"); pdf_ff_length1 := pdf_offset - pdf_save_offset @ @= if not t1_is_pfa then if not t1_marker(t1_byte, 2) then goto t1_error; pdf_save_offset := pdf_offset; t1_in_eexec := true; for k := 1 to 4 do begin i := t1_decrypt(t1_byte); if is_included_font then pdf_out(t1_encrypt(0)); end; repeat t1_check_EOF; t1_getline; t1_putline; t1_scan_param; until t1_prefix("/Subrs") or t1_prefix("2 index /CharStrings") or t1_prefix("dup /CharStrings"); if t1_prefix("/Subrs") then repeat t1_check_EOF; t1_getline; if t1_prefix("readonly def") then begin t1_line[0] := "|"; t1_line[1] := "-"; t1_line[2] := t1_line[t1_line_end - 1]; t1_line_end := 3; end; t1_putline; until t1_prefix("2 index /CharStrings") or t1_prefix("dup /CharStrings"); i := fm_tab[fm_cur].encoding; repeat t1_check_EOF; t1_getline; if (i = 0) or (not is_subsetted_font) or (t1_line[0] <> "/") then t1_put_line else begin k := 0; str_room(t1_line_end); while t1_line[k] <> " " do begin append_char(t1_line[k]); incr(k); end; s := make_string; if str_eq_str(s, "/.notdef") then t1_putline else begin k := 0; while (k < 256) and not str_eq_str(s, enc_tab[i].enc_glyph_names[k]) do incr(k); if (k < 256) and pdf_is_char_used(f, k) then t1_putline; end; flush_string; end; until t1_prefix("mark currentfile closefile"); pdf_ff_length2 := pdf_offset - pdf_save_offset @ @= if not t1_is_pfa then if not t1_marker(t1_byte, 1) then goto t1_error; pdf_save_offset := pdf_offset; t1_in_eexec := false; repeat t1_check_EOF; t1_getline; t1_putline; until t1_prefix("cleartomark"); pdf_ff_length3 := pdf_offset - pdf_save_offset @ @= print_char("<"); if not is_included_font then print_char("("); slow_print(b_make_name_string(cur_file)); flush_string; update_terminal @ @= b_close(t1_file); if not is_included_font then print_char(")"); print_char(">"); update_terminal @ Here are the stuffs to include TrueType font to \.{PDF} output file. @d ttf_file == t1_file @d ttf_byte == t1_byte @ In procedure |ttf_include| we simple copy the \.{TTF} font file to \.{PDF} output. Parameters are not extracted from font file at the moment. If we reencode the \.{TTF} font, we should call procedure |write_ttf|, which is an external subroutine written in C to reencode (and subset) font file. @p procedure ttf_include; {include TrueType font file with internal font number |f| to \.{PDF} output file} var k: integer; begin pack_file_name(fm_tab[fm_cur].fname, "", ""); if not typeone_b_open_in(ttf_file) then begin print_nl("Error in processing TrueType font file ("); print(fm_tab[fm_cur].fname); print("): "); print("cannot open TypeType font file for reading"); update_terminal; fatal_error("Can't handle TrueType font file"); return; end; print_char("<"); slow_print(b_make_name_string(cur_file)); flush_string; pdf_fp_base[f] := pdf_fmem_ptr; pdf_fmem_ptr := pdf_fmem_ptr + pdf_fp_max + 1; if pdf_fmem_ptr > pdf_fmem_size then overflow("PDF memory size", pdf_fmem_size); for k := 0 to pdf_fp_max do pdf_fp(k)(f) := 0; pdf_save_offset := pdf_offset; if is_reencoded_font then writettf else repeat pdf_out(ttf_byte); until eof(ttf_file); b_close(ttf_file); pdf_ff_length1 := pdf_offset - pdf_save_offset; print_char(">"); update_terminal; end; @ Virtual font processing. The charater packets will be stored in |str_pool| array, and the pointer to packets are stored in |pdf_font_info|. We write |vf_packet(f)(c)| to get the address of packet of charracter |c| in font |f| in |str_pool|. We will replace the local font number in \.{VF} file by the correspond internal font number while reading the \.{VF} file. @d vf_max_packet_length = 255 {max length of character packet in \.{VF} file} @# @d vf_error = 61 {label to go to when an error occur} @d do_char = 70 {label to go to typesetting a character of virtual font} @# @d long_char = 242 {\.{VF} command for general character packet} @d vf_id = 202 {identifies \.{VF} files} @d put1=133 {typeset a character} @# @d vf_byte == getc(vf_file) {get a byte from\.{VF} file} @d vf_packet_end(#) == #].int @d vf_packet(#) == pdf_font_info[vf_packet_base[#] + vf_packet_end {to access character packet of character |c| in font |f|} @d bad_vf(#) == begin vf_err_str := #; goto vf_error; end {go out \.{VF} processing with an error message} @d four_cases(#) == #,#+1,#+2,#+3 @# @d tmp_b0 == tmp_w.qqqq.b0 @d tmp_b1 == tmp_w.qqqq.b1 @d tmp_b2 == tmp_w.qqqq.b2 @d tmp_b3 == tmp_w.qqqq.b3 @d tmp_int == tmp_w.int @# @d scaled3u == {convert |tmp_b1..tmp_b3| to an unsigned scaled dimension} (((((tmp_b3*vf_z)div@'400)+(tmp_b2*vf_z))div@'400)+(tmp_b1*vf_z))div vf_beta @d scaled4(#) == {convert |tmp_b0..tmp_b3| to a scaled dimension} #:=scaled3u; if tmp_b0>0 then if tmp_b0=255 then # := # - vf_alpha @d scaled3(#) == {convert |tmp_b1..tmp_b3| to a scaled dimension} #:=scaled3u; @+ if tmp_b1>127 then # := # - vf_alpha @d scaled2 == {convert |tmp_b2..tmp_b3| to a scaled dimension} if tmp_b2>127 then tmp_b1:=255 else tmp_b1:=0; scaled3 @d scaled1 == {convert |tmp_b3| to a scaled dimension} if tmp_b3>127 then tmp_b1:=255 else tmp_b1:=0; tmp_b2:=tmp_b1; scaled3 @= @!vf_packet_base: ^integer; {base addresses for start of character packets from virtual fonts} @!vf_default_font: ^internal_font_number; {default font in \.{VF} file} @!vf_packet_length: integer; {length of the current packet} @!vf_file: byte_file; @!vf_nf: internal_font_number; {the local fonts counter} @!vf_e_fnts:^integer; {external font numbers} @!vf_i_fnts:^internal_font_number; {corresponding internal font numbers} @!tmp_w: memory_word; {acumulator} @!vf_z:integer; {multiplier} @!vf_alpha:integer; {correction for negative values} @!vf_beta:1..16; {divisor} @ The |do_vf| procedure attempts to read the \.{VF} file for a font, and sets |pdf_font_type[f]| to |real_font_type| if the \.{VF} file could not be found or loaded, otherwise sets |pdf_font_type[f]| to |virtual_font_type|. At this time, |f| is the internal font number of the current \.{TFM} font. To process font definitions in virtual font we call |vf_def_font|. @p procedure vf_replace_z; begin vf_alpha:=16; while vf_z>=@'40000000 do begin vf_z:=vf_z div 2; vf_alpha:=vf_alpha+vf_alpha; end; vf_beta:=256 div vf_alpha; vf_alpha:=vf_alpha*vf_z; end; function vf_read(k: integer): integer; {read |k| bytes as an integer from \.{VF} file} var i: integer; begin i := 0; while k > 0 do begin i := i*256 + vf_byte; decr(k); end; vf_read := i; end; procedure vf_local_font_warning(k: internal_font_number; s: str_number); {print a warning message if an error ocurrs during processing local fonts in \.{VF} file} begin print_nl(s); print(" in local font "); print(font_name[k]); print(" in virtual font "); print(font_name[f]); print(".vf was ignored"); print_ln; end; function vf_def_font: internal_font_number; {process a local font in \.{VF} file} label found; var k: internal_font_number; lf_name, lf_area: str_number; ds, s: scaled; cs: four_quarters; begin tmp_int := vf_read(4); cs := tmp_w.qqqq; tmp_int := vf_read(4); scaled4(s); ds := vf_read(4) div @'20; tmp_b0 := vf_byte; tmp_b1 := vf_byte; str_room(tmp_b0 + tmp_b1); while tmp_b0 > 0 do begin decr(tmp_b0); append_char(vf_byte); end; lf_area := make_string; if length(lf_area) = 0 then begin lf_area := ""; flush_string; end; while tmp_b1 > 0 do begin decr(tmp_b1); append_char(vf_byte); end; lf_name := make_string; for k:=font_base+1 to font_ptr do if str_eq_str(font_name[k], lf_name) and ((lf_area = "") or str_eq_str(font_area[k], lf_area)) then begin if lf_name = str_ptr - 1 then begin flush_string; lf_name := font_name[k]; if lf_area <> "" then begin flush_string; lf_area := font_area[k]; end; end; if font_size[k] = s then goto found; end; k := read_font_info(vf_nf, lf_name, lf_area, s); found: if k <> null_font then begin if (cs.b0*cs.b1*cs.b2*cs.b3 <> 0) and (font_check[k].b0*font_check[k].b1*font_check[k].b2*font_check[k].b3 <> 0) and ((cs.b0 <> font_check[k].b0) or (cs.b1 <> font_check[k].b1) or (cs.b2 <> font_check[k].b2) or (cs.b3 <> font_check[k].b3)) then vf_local_font_warning(k, "checksum missmatch"); if ds <> font_dsize[k] then vf_local_font_warning(k, "design size missmatch"); end; vf_def_font := k; end; procedure do_vf; {process \.{VF} file with font internal number |f|} label vf_error; var cmd: integer; k: internal_font_number; cc, cmd_length: integer; tfm_width: scaled; vf_err_str: str_number; stack_level: vf_stack_index; begin stack_level := 0; pdf_font_type[f] := real_font_type; @; @;@/ @;@/ if pdf_fmem_ptr + font_ec[f] - font_bc[f] + 1 > pdf_fmem_size then overflow("PDF memory size", pdf_fmem_size); vf_packet_base[f] := pdf_fmem_ptr - font_bc[f];@/ while cmd <= long_char do begin@/ @;@/ end; if cmd <> post then bad_vf("POST command expected"); b_close(vf_file); pdf_font_type[f] := virtual_font_type; pdf_fmem_ptr := pdf_fmem_ptr + font_ec[f] - font_bc[f] + 1; return; vf_error: print_nl("Error in processing VF font ("); print(font_name[f]); print(".vf): "); print(vf_err_str); print(", virtual font will be ignored"); print_ln; b_close(vf_file); update_terminal; end; @ @= pack_file_name(font_name[f], "", ".vf"); if not vf_b_open_in(vf_file) then return @ @= if vf_byte <> pre then bad_vf("PRE command expected"); if vf_byte <> vf_id then bad_vf("wrong id byte"); cmd_length := vf_byte; for k := 1 to cmd_length do tmp_int := vf_byte; tmp_int := vf_read(4); if (tmp_b0 <> font_check[f].b0) or (tmp_b1 <> font_check[f].b1) or (tmp_b2 <> font_check[f].b2) or (tmp_b3 <> font_check[f].b3) then begin print_nl("checksum missmatch in font "); print(font_name[f]); print(".vf was ignored"); end; if vf_read(4) div @'20 <> font_dsize[f] then begin print_nl("design size missmatch in font "); print(font_name[f]); print(".vf was ignored"); end; update_terminal; vf_z := font_size[f]; vf_replace_z @ @= vf_nf := 0; cmd := vf_byte; while (cmd >= fnt_def1) and (cmd <= fnt_def1 + 3) do begin vf_e_fnts[vf_nf] := vf_read(cmd - fnt_def1 + 1); vf_i_fnts[vf_nf] := vf_def_font; incr(vf_nf); cmd := vf_byte; end; vf_default_font[f] := vf_i_fnts[0] @ @= if cmd = long_char then begin vf_packet_length := vf_read(4); cc := vf_read(4); tmp_int := vf_read(4); scaled4(tfm_width); end else begin vf_packet_length := cmd; cc := vf_byte; tmp_int := vf_read(3); scaled3(tfm_width); end; if vf_packet_length < 0 then bad_vf("negative packet lenght"); if vf_packet_length > vf_max_packet_length then bad_vf("packet length too long"); if (cc < 0) or (cc > 255) then bad_vf("character code out of range"); if (tfm_width <> char_width(f)(char_info(f)(cc))) then begin print_nl("character width missmatch in font "); print(font_name[f]); print(".vf was ignored"); end; str_room(vf_packet_length); while vf_packet_length > 0 do begin cmd := vf_byte; decr(vf_packet_length); @; if cmd_length > 0 then begin vf_packet_length := vf_packet_length - cmd_length + 1; append_char(cmd); end; while cmd_length > 1 do begin decr(cmd_length); append_char(vf_byte); end; end; if stack_level <> 0 then bad_vf("more PUSHs than POPs in character packet"); if vf_packet_length <> 0 then bad_vf("invalid packet length or DVI command in packet"); vf_packet(f)(cc) := make_string; cmd := vf_byte @ @= if (cmd >= set_char_0) and (cmd <= set_char_0 + 127) then cmd_length := 1 else if ((fnt_num_0 <= cmd) and (cmd <= fnt_num_0 + 63)) or ((fnt1 <= cmd) and (cmd <= fnt1 + 3)) then begin if cmd >= fnt1 then begin tmp_int := vf_read(cmd - fnt1 + 1); vf_packet_length := vf_packet_length - (cmd - fnt1 + 1); end else tmp_int := cmd - fnt_num_0; k := 0; while (k < vf_nf) and (vf_e_fnts[k] <> tmp_int) do incr(k); if k = vf_nf then bad_vf("undefined local font"); if vf_i_fnts[k] <= 63 then append_char(fnt_num_0 + vf_i_fnts[k]) else begin append_char(fnt1); append_char(vf_i_fnts[k]); end; cmd_length := 0; end else case cmd of set_rule, put_rule: cmd_length := 9; four_cases(set1): cmd_length := cmd - set1 + 2; four_cases(put1): cmd_length := cmd - put1 + 2; four_cases(right1): cmd_length := cmd - right1 + 2; four_cases(w1): cmd_length := cmd - w1 + 2; four_cases(x1): cmd_length := cmd - x1 + 2; four_cases(down1): cmd_length := cmd - down1 + 2; four_cases(y1): cmd_length := cmd - y1 + 2; four_cases(z1): cmd_length := cmd - z1 + 2; four_cases(xxx1): begin cmd_length := vf_read(cmd - xxx1 + 1); vf_packet_length := vf_packet_length - (cmd - xxx1 + 1); if cmd_length < 0 then bad_vf("string of negative length"); append_char(cmd); case cmd - xxx1 + 1 of 1: append_char(tmp_b3); 2: begin append_char(tmp_b2); append_char(tmp_b3); end; 3: begin append_char(tmp_b1); append_char(tmp_b2); append_char(tmp_b3); end; 4: begin append_char(tmp_b0); append_char(tmp_b1); append_char(tmp_b2); append_char(tmp_b3); end; endcases; end; w0, x0, y0, z0: cmd_length := 1; nop: cmd_length := 0; push, pop: begin cmd_length := 1; if cmd = push then if stack_level = vf_stack_size then overflow("virtual font stack size", vf_stack_size) else incr(stack_level) else if stack_level = 0 then bad_vf("more POPs than PUSHs in character") else decr(stack_level); end; othercases bad_vf("improver DVI command"); endcases @ The |do_vf_packet| procedure is called in order to interpret the character packet for a virtual character. Such a packet may contain the instruction to typeset a character from the same or an other virtual font; in such cases |do_vf_packet| calls itself recursively. The recursion level, i.e., the number of times this has happened, is kept in the global variable |vf_cur_s| and should not exceed |vf_max_recursion|. @= @!vf_max_recursion=10; {\.{VF} files shouldn't recurse beyond this level} @!vf_stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth} @ @= @!vf_stack_index=0..vf_stack_size; {an index into the stack} @!vf_stack_record=record stack_h, stack_v, stack_w, stack_x, stack_y, stack_z: scaled; end; @ @= @!vf_cur_s: 0..vf_max_recursion; {current recursion level} @!vf_stack: array [vf_stack_index] of vf_stack_record; @!vf_stack_ptr: vf_stack_index; {pointer into |vf_stack|} @!vf_packet_ptr: pool_pointer; {pointer into virtual character packet stored in |str_pool|} @ @= vf_cur_s := 0; vf_stack_ptr := 0; @ Some functions for processing character packets. @p function packet_byte: eight_bits; {get a byte from character packet} begin incr(vf_packet_ptr); decr(vf_packet_length); packet_byte := str_pool[vf_packet_ptr-1]; end; function packet_read(k: integer): integer; {read |k| bytes as an integer from character packet} var i: integer; begin i := 0; while k > 0 do begin i := i*256 + packet_byte; decr(k); end; packet_read := i; end; function packet_scaled(k: integer): integer; {get |k| bytes from packet as a scaled} var s: scaled; begin tmp_int := packet_read(k); case k of 1: begin scaled1(s); end; 2: begin scaled2(s); end; 3: begin scaled3(s); end; 4: begin scaled4(s); end; endcases; packet_scaled := s; end; procedure do_vf_packet(c: eight_bits); {typeset the \.{DVI} commands in the character packet for character |c| in current font |f|} label do_char, continue; var save_packet_ptr, save_packet_length: pool_pointer; save_font: internal_font_number; save_h, save_v: scaled; cmd: integer; char_move: boolean; w, x, y, z: scaled; begin incr(vf_cur_s); if vf_cur_s > vf_max_recursion then overflow("max level recursion", vf_max_recursion); save_packet_ptr := vf_packet_ptr; save_packet_length := vf_packet_length; save_font := f; vf_packet_ptr := str_start[vf_packet(f)(c)]; vf_packet_length := length(vf_packet(f)(c)); vf_z := font_size[f]; vf_replace_z; f := vf_default_font[f]; save_v := cur_v; save_h := cur_h; w := 0; x := 0; y := 0; z := 0; while vf_packet_length > 0 do begin cmd := packet_byte; @; continue: end; cur_h := save_h; cur_v := save_v; vf_packet_ptr := save_packet_ptr; vf_packet_length := save_packet_length; f := save_font; vf_z := font_size[f]; vf_replace_z; decr(vf_cur_s); end; @ @= if (cmd >= set_char_0) and (cmd <= set_char_0 + 127) then begin if not ((font_bc[f] <= cmd) and (cmd <= font_ec[f])) then char_warning(f, cmd); c := cmd; char_move := true; goto do_char; end else if (fnt_num_0 <= cmd) and (cmd <= fnt_num_0 + 63) then f := cmd - fnt_num_0 else case cmd of push: begin vf_stack[vf_stack_ptr].stack_h := cur_h; vf_stack[vf_stack_ptr].stack_v := cur_v; vf_stack[vf_stack_ptr].stack_w := w; vf_stack[vf_stack_ptr].stack_x := x; vf_stack[vf_stack_ptr].stack_y := y; vf_stack[vf_stack_ptr].stack_z := z; incr(vf_stack_ptr); end; pop: begin decr(vf_stack_ptr); cur_h := vf_stack[vf_stack_ptr].stack_h; cur_v := vf_stack[vf_stack_ptr].stack_v; w := vf_stack[vf_stack_ptr].stack_w; x := vf_stack[vf_stack_ptr].stack_x; y := vf_stack[vf_stack_ptr].stack_y; z := vf_stack[vf_stack_ptr].stack_z; end; four_cases(set1), four_cases(put1): begin if (set1 <= cmd) and (cmd <= set1 + 3) then begin tmp_int := packet_read(cmd - set1 + 1); char_move := true; end else begin tmp_int := packet_read(cmd - put1 + 1); char_move := false; end; if not ((font_bc[f] <= tmp_int) and (tmp_int <= font_ec[f])) then char_warning(f, tmp_int); c := tmp_int; goto do_char; end; fnt1: f := packet_byte; set_rule, put_rule: begin rule_ht := packet_scaled(4); rule_wd := packet_scaled(4); pdf_set_rule(cur_h, cur_v - rule_ht, rule_ht, rule_wd); if cmd = set_rule then cur_h := cur_h + rule_wd; end; four_cases(right1): cur_h := cur_h + packet_scaled(cmd - right1 + 1); w0, four_cases(w1): begin if cmd > w0 then w := packet_scaled(cmd - w0); cur_h := cur_h + w; end; x0, four_cases(x1): begin if cmd > x0 then x := packet_scaled(cmd - x0); cur_h := cur_h + x; end; four_cases(down1): cur_v := cur_v + packet_scaled(cmd - down1 + 1); y0, four_cases(y1): begin if cmd > y0 then y := packet_scaled(cmd - y0); cur_v := cur_v + y; end; z0, four_cases(z1): begin if cmd > z0 then z := packet_scaled(cmd - z0); cur_v := cur_v + z; end; four_cases(xxx1): begin tmp_int := packet_read(cmd - xxx1 + 1); while tmp_int > 0 do begin decr(tmp_int); if packet_byte > 0 then do_nothing; {skip \.{\\special} commands} end; end; endcases; goto continue; do_char: @; if char_move then cur_h := cur_h + char_width(f)(char_info(f)(c)) @ @= if pdf_font_type[f] = new_font_type then do_vf; if pdf_font_type[f] = virtual_font_type then do_vf_packet(c) else begin if f<>pdf_f then begin pdf_end_string; pdf_setfont(f); end; pdf_begin_string; case c of 32, {space} 10, {linefeed} 13, {carriage return} 9, {horizontal tab} 8, {backspace} 12, {formfeed} 92, {backslash} 40, {left parenthesis} 41: begin {right parenthesis} pdf_out(92); {output a backslash} pdf_print_octal(c); end; othercases pdf_out(c); endcases; pdf_h := pdf_h + char_width(f)(char_info(f)(c)); pdf_set_char_used(f, c); end @ Here we change the \TeX\ output subroutines for \.{PDF} output. @p @t\4@>@@t@>@/ @ The procedures |pdf_hlist_out| and |pdf_vlist_out| are very similiar the \TeX's original |hlist_out| and |vlist_out| but \.{PDF} output. @p procedure@?pdf_vlist_out; forward; procedure pdf_hlist_out; {output an |hlist_node| box} label reswitch, move_past, fin_rule, next_p; var base_line: scaled; {the baseline coordinate for this box} @!left_edge: scaled; {the left coordinate for this box} @!save_h: scaled; {what |cur_h| should pop to} @!this_box: pointer; {pointer to containing box} @!g_order: glue_ord; {applicable order of infinity for glue} @!g_sign: normal..shrinking; {selects type of glue} @!p:pointer; {current position in the hlist} @!leader_box:pointer; {the leader box being replicated} @!leader_wd:scaled; {width of leader box being replicated} @!lx:scaled; {extra space between leader boxes} @!outer_doing_leaders:boolean; {were we doing leaders?} @!edge:scaled; {left edge of sub-box, or right edge of leader space} begin this_box:=temp_ptr; g_order:=glue_order(this_box); g_sign:=glue_sign(this_box); p:=list_ptr(this_box); incr(cur_s); base_line:=cur_v; left_edge:=cur_h; @; while p<>null do @; decr(cur_s); end; @ @= reswitch: if is_char_node(p) then begin repeat f:=font(p); c:=character(p); @; cur_h:=cur_h+char_width(f)(char_info(f)(c)); p:=link(p); until not is_char_node(p); end else @ @ @= begin case type(p) of hlist_node,vlist_node:@<(in |pdf_hlist_out|)Output a box in an hlist@>; rule_node: begin rule_ht:=height(p); rule_dp:=depth(p); rule_wd:=width(p); goto fin_rule; end; whatsit_node: @; glue_node: @<(in |pdf_hlist_out|)Move right or output leaders@>; kern_node,math_node:cur_h:=cur_h+width(p); ligature_node: @; othercases do_nothing endcases;@/ goto next_p; fin_rule: @<(in |pdf_hlist_out|)Output a rule in an hlist@>; move_past: cur_h:=cur_h+rule_wd; next_p:p:=link(p); end @ @<(in |pdf_hlist_out|)Output a box in an hlist@>= if list_ptr(p)=null then cur_h:=cur_h+width(p) else begin cur_v:=base_line+shift_amount(p); {shift the box down} temp_ptr:=p; edge:=cur_h; if type(p)=vlist_node then pdf_vlist_out@+else pdf_hlist_out; cur_h:=edge+width(p); cur_v:=base_line; end @ @<(in |pdf_hlist_out|)Output a rule in an hlist@>= if is_running(rule_ht) then rule_ht:=height(this_box); if is_running(rule_dp) then rule_dp:=depth(this_box); rule_ht:=rule_ht+rule_dp; {this is the rule thickness} if (rule_ht>0)and(rule_wd>0) then {we don't output empty rules} begin cur_v:=base_line+rule_dp; pdf_set_rule(cur_h, cur_v, rule_ht, rule_wd); cur_v:=base_line; end @ @<(in |pdf_hlist_out|)Move right or output leaders@>= begin g:=glue_ptr(p); rule_wd:=width(g); if g_sign<>normal then begin if g_sign=stretching then begin if stretch_order(g)=g_order then rule_wd:=rule_wd+round(float(glue_set(this_box))*stretch(g)); @^real multiplication@> end else begin if shrink_order(g)=g_order then rule_wd:=rule_wd-round(float(glue_set(this_box))*shrink(g)); end; end; if subtype(p)>=a_leaders then @<(in |pdf_hlist_out|)Output leaders in an hlist, |goto fin_rule| if a rule or to |next_p| if done@>; goto move_past; end @ @<(in |pdf_hlist_out|)Output leaders in an hlist...@>= begin leader_box:=leader_ptr(p); if type(leader_box)=rule_node then begin rule_ht:=height(leader_box); rule_dp:=depth(leader_box); goto fin_rule; end; leader_wd:=width(leader_box); if (leader_wd>0)and(rule_wd>0) then begin rule_wd:=rule_wd+10; {compensate for floating-point rounding} edge:=cur_h+rule_wd; lx:=0; @<(in |pdf_hlist_out|)Let |cur_h| be the position of the first box, and set |leader_wd+lx| to the spacing between corresponding parts of boxes@>; while cur_h+leader_wd<=edge do @<(in |pdf_hlist_out|)Output a leader box at |cur_h|, then advance |cur_h| by |leader_wd+lx|@>; cur_h:=edge-10; goto next_p; end; end @ @<(in |pdf_hlist_out|)Let |cur_h| be the position of the first box, ...@>= if subtype(p)=a_leaders then begin save_h:=cur_h; cur_h:=left_edge+leader_wd*((cur_h-left_edge)div leader_wd); if cur_h= begin cur_v:=base_line+shift_amount(leader_box);@/ save_h:=cur_h; temp_ptr:=leader_box; outer_doing_leaders:=doing_leaders; doing_leaders:=true; if type(leader_box)=vlist_node then pdf_vlist_out@+else pdf_hlist_out; doing_leaders:=outer_doing_leaders; cur_v:=base_line; cur_h:=save_h+leader_wd+lx; end @ The |pdf_vlist_out| routine is similar to |pdf_hlist_out|, but a bit simpler. @p procedure pdf_vlist_out; {output a |pdf_vlist_node| box} label move_past, fin_rule, next_p; var left_edge: scaled; {the left coordinate for this box} @!top_edge: scaled; {the top coordinate for this box} @!save_v: scaled; {what |cur_v| should pop to} @!this_box: pointer; {pointer to containing box} @!g_order: glue_ord; {applicable order of infinity for glue} @!g_sign: normal..shrinking; {selects type of glue} @!p:pointer; {current position in the vlist} @!leader_box:pointer; {the leader box being replicated} @!leader_ht:scaled; {height of leader box being replicated} @!lx:scaled; {extra space between leader boxes} @!outer_doing_leaders:boolean; {were we doing leaders?} @!edge:scaled; {bottom boundary of leader space} begin this_box:=temp_ptr; g_order:=glue_order(this_box); g_sign:=glue_sign(this_box); p:=list_ptr(this_box); incr(cur_s); left_edge:=cur_h; cur_v:=cur_v-height(this_box); top_edge:=cur_v; @; while p<>null do @; decr(cur_s); end; @ @= begin if is_char_node(p) then confusion("pdfvlistout") @:this can't happen pdfvlistout}{\quad pdfvlistout@> else @; next_p:p:=link(p); end @ @= begin case type(p) of hlist_node,vlist_node:@<(in |pdf_vlist_out|)Output a box in a vlist@>; rule_node: begin rule_ht:=height(p); rule_dp:=depth(p); rule_wd:=width(p); goto fin_rule; end; whatsit_node: @; glue_node: @<(in |pdf_vlist_out|)Move down or output leaders@>; kern_node:cur_v:=cur_v+width(p); othercases do_nothing endcases;@/ goto next_p; fin_rule: @<(in |pdf_vlist_out|)Output a rule in a vlist, |goto next_p|@>; move_past: cur_v:=cur_v+rule_ht; end @ @<(in |pdf_vlist_out|)Output a box in a vlist@>= if list_ptr(p)=null then cur_v:=cur_v+height(p)+depth(p) else begin cur_v:=cur_v+height(p); save_v:=cur_v; cur_h:=left_edge+shift_amount(p); {shift the box right} temp_ptr:=p; if type(p)=vlist_node then pdf_vlist_out@+else pdf_hlist_out; cur_v:=save_v+depth(p); cur_h:=left_edge; end @ @<(in |pdf_vlist_out|)Output a rule in a vlist...@>= if is_running(rule_wd) then rule_wd:=width(this_box); rule_ht:=rule_ht+rule_dp; {this is the rule thickness} cur_v:=cur_v+rule_ht; if (rule_ht>0)and(rule_wd>0) then {we don't output empty rules} pdf_set_rule(cur_h, cur_v, rule_ht, rule_wd); goto next_p @ @<(in |pdf_vlist_out|)Move down or output leaders@>= begin g:=glue_ptr(p); rule_ht:=width(g); if g_sign<>normal then begin if g_sign=stretching then begin if stretch_order(g)=g_order then rule_ht:=rule_ht+round(float(glue_set(this_box))*stretch(g)); @^real multiplication@> end else begin if shrink_order(g)=g_order then rule_ht:=rule_ht-round(float(glue_set(this_box))*shrink(g)); end; end; if subtype(p)>=a_leaders then @<(in |pdf_vlist_out|)Output leaders in a vlist, |goto fin_rule| if a rule or to |next_p| if done@>; goto move_past; end @ @<(in |pdf_vlist_out|)Output leaders in a vlist...@>= begin leader_box:=leader_ptr(p); if type(leader_box)=rule_node then begin rule_wd:=width(leader_box); rule_dp:=0; goto fin_rule; end; leader_ht:=height(leader_box)+depth(leader_box); if (leader_ht>0)and(rule_ht>0) then begin rule_ht:=rule_ht+10; {compensate for floating-point rounding} edge:=cur_v+rule_ht; lx:=0; @<(in |pdf_vlist_out|)Let |cur_v| be the position of the first box, and set |leader_ht+lx| to the spacing between corresponding parts of boxes@>; while cur_v+leader_ht<=edge do @<(in |pdf_vlist_out|)Output a leader box at |cur_v|, then advance |cur_v| by |leader_ht+lx|@>; cur_v:=edge-10; goto next_p; end; end @ @<(in |pdf_vlist_out|)Let |cur_v| be the position of the first box, ...@>= if subtype(p)=a_leaders then begin save_v:=cur_v; cur_v:=top_edge+leader_ht*((cur_v-top_edge)div leader_ht); if cur_v= begin cur_h:=left_edge+shift_amount(leader_box);@/ cur_v:=cur_v+height(leader_box); save_v:=cur_v; temp_ptr:=leader_box; outer_doing_leaders:=doing_leaders; doing_leaders:=true; if type(leader_box)=vlist_node then pdf_vlist_out@+else pdf_hlist_out; doing_leaders:=outer_doing_leaders; cur_h:=left_edge; cur_v:=save_v-height(leader_box)+leader_ht+lx; end @ |pdf_ship_out| is used instead of |ship_out| to shipout a box to \.{PDF} output. If |pdf_shipping_form| is set then the output will be a page, otherwise it will be a Form object. @p procedure pdf_ship_out(p:pointer); {output the box |p|} label done, done1; var i,j,k:integer; {indices to first ten count registers} @!s:pool_pointer; {index into |str_pool|} @!old_setting:0..max_selector; {saved |selector| setting} begin if tracing_output>0 then begin print_nl(""); print_ln; print("Completed box being shipped out"); @.Completed box...@> end; if not pdf_shipping_form then begin if term_offset>max_print_line-9 then print_ln else if (term_offset>0)or(file_offset>0) then print_char(" "); print_char("["); j:=9; while (count(j)=0)and(j>0) do decr(j); for k:=0 to j do begin print_int(count(k)); if k0 then begin if not pdf_shipping_form then print_char("]"); begin_diagnostic; show_box(p); end_diagnostic(true); end; @<(in |pdf_ship_out|)Ship box |p| out@>; if (tracing_output<=0) and not pdf_shipping_form then print_char("]"); dead_cycles:=0; update_terminal; {progress report} @<(in |pdf_ship_out|)Flush the box from memory, showing statistics if requested@>; end; @ @<(in |pdf_ship_out|)Flush the box from memory, showing statistics if requested@>= @!stat if tracing_stats>1 then begin print_nl("Memory usage before: "); @.Memory usage...@> print_int(var_used); print_char("&"); print_int(dyn_used); print_char(";"); end; tats@/ flush_node_list(p); @!stat if tracing_stats>1 then begin print(" after: "); print_int(var_used); print_char("&"); print_int(dyn_used); print("; still untouched: "); print_int(hi_mem_min-lo_mem_max-1); print_ln; end; tats @ @<(in |pdf_ship_out|)Ship box |p| out@>= @<(in |pdf_ship_out|)Update the values of |max_h| and |max_v|; but if the page is too large, |goto done|@>; @; if type(p)=vlist_node then pdf_vlist_out@+else pdf_hlist_out; if not pdf_shipping_form then incr(total_pages); cur_s:=-1; @; done: @ @= pdf_f := null_font; pdf_last_leading := max_dimen; pdf_word_spacing := 0; pdf_doing_string := false; temp_ptr:=p; zip_write_state := no_zip; pdf_image_b := false; pdf_image_c := false; ensure_pdf_open; pdf_create_obj(pdf_others, 0); pdf_last_resources := obj_count; if pdf_shipping_form then begin pdf_form_width := width(p); pdf_form_height := height(p); pdf_form_depth := depth(p); incr(form_count); pdf_new_dict(pdf_form, form_count); k := obj_offset(obj_count); {save byte offset of Form} obj_ptr(obj_count) := pdf_get_mem(pdf_form_struct_size); obj_form_offset(obj_count) := k; {store byte offset of Form in |pdf_mem|} obj_form_width(obj_count) := pdf_form_width; obj_form_height(obj_count) := pdf_form_height; obj_form_depth(obj_count) := pdf_form_depth; pdf_last_form := obj_count; cur_v := height(p); cur_h := 0; pdf_origin_h := 0; pdf_origin_v := pdf_form_height; end else begin pdf_last_page := find_identifier(find_page, total_pages + 1); obj_int(pdf_last_page) := 1; {mark that this page is valid} pdf_new_dict(pdf_others, 0); cur_h := h_offset; cur_v := height(p) + v_offset; pdf_origin_h := -pdf_1in; pdf_origin_v := pdf_page_height - pdf_1in; end; pdf_last_stream := obj_count; pdf_create_obj(pdf_others, 0); pdf_indirect_nl("Length", pdf_last_stream + 1); if pdf_shipping_form then begin @; end; @ @ Here we write out the header for Form. @d pdf_1bp == 65782 {scaled value corresponds to 1bp |= xn_over_d(unity, 7227, 7200)|} @= pdf_print_nl("/Type /XObject"); pdf_print_nl("/Subtype /Form"); pdf_print("/BBox ["); pdf_print("-1 "); pdf_print_scaled(-pdf_form_depth - pdf_1bp); pdf_out(" "); pdf_print_scaled(pdf_form_width + pdf_1bp); pdf_out(" "); pdf_print_scaled(pdf_form_height + pdf_1bp); pdf_print_nl("]"); pdf_print_nl("/FormType 1"); pdf_print_nl("/Matrix [1 0 0 1 0 0 1]"); pdf_print("/Name /Fm"); pdf_print_int_nl(form_count); pdf_indirect_nl("Resources", pdf_last_resources) @ @= if get_pdf_compress_level > 0 then begin pdf_print_nl("/Filter /FlateDecode"); pdf_print_nl(">>"); pdf_print_nl("stream"); start_compress; end else begin pdf_print_nl(">>"); pdf_print_nl("stream"); pdf_save_offset := pdf_offset; end; @; @ @= if not pdf_shipping_form then begin prepare_mag; if mag <> 1000 then begin pdf_print_mag; pdf_print(" 0 0 "); pdf_print_mag; pdf_print_nl(" 0 0 cm"); end; end @ @<(in |pdf_ship_out|)Update the values of |max_h| and |max_v|; but if the page is too large...@>= if (height(p)>max_dimen)or@|(depth(p)>max_dimen)or@| (height(p)+depth(p)+v_offset>max_dimen)or@| (width(p)+h_offset>max_dimen) then begin print_err("Huge page cannot be shipped out"); @.Huge page...@> help2("The page just created is more than 18 feet tall or")@/ ("more than 18 feet wide, so I suspect something went wrong."); error; if tracing_output<=0 then begin begin_diagnostic; print_nl("The following box has been deleted:"); @.The following...deleted@> show_box(p); end_diagnostic(true); end; goto done; end; if height(p)+depth(p)+v_offset>max_v then max_v:=height(p)+depth(p)+v_offset; if width(p)+h_offset>max_h then max_h:=width(p)+h_offset @ @= @; @; @; if not pdf_shipping_form then begin @; @; end; @ @ @= pdf_end_text; if get_pdf_compress_level > 0 then finish_compress else pdf_stream_length := pdf_offset - pdf_save_offset; pdf_print_nl("endstream"); pdf_end_obj @ @= pdf_begin_obj(pdf_last_stream + 1); pdf_print_int_nl(pdf_stream_length); pdf_end_obj @ @= k := pdf_image_list; while k <> null do begin i := obj_ptr(info(k)); pdf_begin_dict(info(k)); pdf_print("/Name /Im"); pdf_print_int_nl(obj_info(info(k))); write_image(pdf_image_info(i)); k := link(k); end @ @= pdf_begin_dict(pdf_last_page); pdf_print_nl("/Type /Page"); pdf_indirect_nl("Contents", pdf_last_stream); pdf_indirect_nl("Resources", pdf_last_resources); if pdf_page_attr <> null then pdf_print_nl(tokens_to_string(pdf_page_attr)); @; @; pdf_end_dict @ @= if total_pages mod pages_tree_kids_max = 1 then begin pdf_create_obj(pdf_pages, pages_tree_kids_max); pdf_last_pages := obj_count; end; pdf_indirect_nl("Parent", pdf_last_pages) @ @= if (pdf_annot_text_list <> null) or (pdf_annot_link_list <> null) then begin pdf_print("/Annots ["); k := pdf_annot_text_list; while k <> null do begin pdf_print_int(info(k)); pdf_print(" 0 R "); k := link(k); end; flush_list(pdf_annot_text_list); pdf_annot_text_list := null; k := pdf_annot_link_list; while k <> null do begin pdf_print_int(info(k)); pdf_print(" 0 R "); k := link(k); end; flush_list(pdf_annot_link_list); pdf_annot_link_list := null; pdf_remove_last_char; pdf_print_nl("]"); end; if pdf_bead_list <> null then begin k := pdf_bead_list; pdf_print("/B ["); while k <> null do begin pdf_print_int(info(k)); pdf_print(" 0 R "); k := link(k); end; flush_list(pdf_bead_list); pdf_bead_list := null; pdf_remove_last_char; pdf_print_nl("]"); end @ @= pdf_begin_dict(pdf_last_resources); pdf_print("/ProcSet [/PDF"); if pdf_font_list <> null then pdf_print(" /Text"); if pdf_image_b then pdf_print(" /ImageB"); if pdf_image_c then pdf_print(" /ImageC"); pdf_print_nl("]"); @; @; pdf_end_dict @ @= if pdf_font_list <> null then begin k := pdf_font_list; pdf_print("/Font << "); repeat pdf_print("/F"); pdf_print_int(info(k)); pdf_out(" "); pdf_print_int(pdf_font_num[info(k)]); pdf_print(" 0 R"); k := link(k); pdf_out(" "); until k = null; pdf_print_nl(">>"); flush_list(pdf_font_list); pdf_font_list := null; end @ @= if (pdf_form_list <> null) or (pdf_image_list <> null) then begin pdf_print("/XObject << "); k := pdf_form_list; while k <> null do begin pdf_print("/Fm"); pdf_print_int(obj_info(info(k))); pdf_out(" "); pdf_print_int(info(k)); pdf_print(" 0 R "); k := link(k); end; flush_list(pdf_form_list); pdf_form_list := null; k := pdf_image_list; while k <> null do begin pdf_print("/Im"); pdf_print_int(obj_info(info(k))); pdf_out(" "); pdf_print_int(info(k)); pdf_print(" 0 R"); k := link(k); pdf_out(" "); end; flush_list(pdf_image_list); pdf_image_list := null; pdf_print_nl(">>"); end @ @= pdf_origin_h := -pdf_1in; pdf_origin_v := pdf_page_height - pdf_1in; @; @; @; @ @ @= k := pdf_annot_text_list; while k <> null do begin i := obj_annot_text_ptr(info(k)); {|i| points to |pdf_annot_text_node|} pdf_begin_dict(info(k)); pdf_print_nl("/Type /Annot"); pdf_print_nl("/Subtype /Text"); if pdf_annot_attr(i) <> null then begin pdf_print_nl(tokens_to_string(pdf_annot_attr(i))); flush_string; end; pdf_rectangle(pdf_left(i), pdf_top(i), pdf_right(i), pdf_bottom(i)); pdf_str_entry_nl("Contents", tokens_to_string(pdf_annot_text_title(i))); flush_string; pdf_end_dict; k := link(k); end @ @= k := pdf_annot_link_list; while k <> null do begin i := obj_annot_link_ptr(info(k)); pdf_begin_dict(info(k)); pdf_print_nl("/Type /Annot"); pdf_print_nl("/Subtype /Link"); if pdf_annot_attr(i) <> null then begin pdf_print_nl(tokens_to_string(pdf_annot_attr(i))); flush_string; end; pdf_rectangle(pdf_left(i), pdf_top(i), pdf_right(i), pdf_bottom(i)); pdf_print("/A "); write_action_struct(pdf_annot_link_action(i)); pdf_end_dict; k := link(k); end @ @= k := pdf_dest_list; while k <> null do begin i := obj_dest_ptr(info(k)); if pdf_dest_id_type(i) = pdf_id_num then pdf_begin_obj(info(k)) else begin pdf_begin_dict(info(k)); pdf_print("/D "); end; pdf_out("["); pdf_print_int(pdf_last_page); pdf_print(" 0 R "); case pdf_dest_type(i) of pdf_dest_xyz: begin pdf_print("/XYZ "); pdf_print_scaled(pdf_x(pdf_left(i))); pdf_out(" "); pdf_print_scaled(pdf_y(pdf_top(i))); pdf_out(" "); if pdf_dest_xyz_zoom(i) = 0 then pdf_print("null") else begin pdf_print_int(pdf_dest_xyz_zoom(i) div 1000); pdf_out("."); pdf_print_int((pdf_dest_xyz_zoom(i) mod 1000)); end; end; pdf_dest_fit: pdf_print("/Fit"); pdf_dest_fit_h: begin pdf_print("/FitH "); pdf_print_scaled(pdf_y(pdf_top(i))); end; pdf_dest_fit_v: begin pdf_print("/FitV "); pdf_print_scaled(pdf_x(pdf_left(i))); end; pdf_dest_fitb: pdf_print("/FitB"); pdf_dest_fitb_h: begin pdf_print("/FitBH "); pdf_print_scaled(pdf_y(pdf_top(i))); end; pdf_dest_fitb_v: begin pdf_print("/FitBV "); pdf_print_scaled(pdf_x(pdf_left(i))); end; endcases; pdf_print_nl("]"); if pdf_dest_id_type(i) = pdf_id_num then pdf_end_obj else pdf_end_dict; k := link(k); end; flush_list(pdf_dest_list); pdf_dest_list := null @ @= k := pdf_bead_list; while k <> null do begin pdf_new_obj(pdf_others, 0); i := obj_bead_rect(info(k)); pdf_out("["); pdf_print_scaled(pdf_x(pdf_left(i) - pdf_thread_hoffset)); pdf_out(" "); pdf_print_scaled(pdf_y(pdf_bottom(i) + pdf_thread_voffset)); pdf_out(" "); pdf_print_scaled(pdf_x(pdf_right(i) + pdf_thread_hoffset)); pdf_out(" "); pdf_print_scaled(pdf_y(pdf_top(i) - pdf_thread_voffset)); pdf_print_nl("]"); pdf_end_obj; obj_bead_rect(info(k)) := obj_count; free_node(i, pdf_bead_rect_node_size); k := link(k); end @ Finishing the \.{PDF} output file. @ Generating fonts @p procedure ff_length(l: integer); {print out the length of a portion in \.{T1} font file} begin pdf_new_obj(pdf_others, 0); pdf_print_int_nl(l); pdf_end_obj; end; procedure pdf_fd_key(s: str_number; k, v: integer); begin if pdf_fp(k)(f) <> 0 then pdf_int_entry_nl(s, pdf_fp(k)(f)) else if v <> 0 then pdf_int_entry_nl(s, em_thousandth(v, font_size[f])); end; procedure do_pdf_font; {print out the font resource of font with internal number |f|} var k, l: integer; is_truetype: boolean; {is the current font truetype font?} begin fm_cur := fm_lookup(font_name[f]); if fm_cur < 0 then begin print_nl("Don't know how to process font "); print(font_name[f]); print_ln; fatal_error("Missing information for processing font"); end; pdf_print_nl("/Type /Font"); pdf_print("/Subtype /"); if is_truetype_font then pdf_print_nl("TrueType") else pdf_print_nl("Type1"); if is_reencoded_font then {reencoded font} pdf_indirect_nl("Encoding", enc_tab[fm_tab[fm_cur].encoding].enc_objnum ); if is_base_font then begin {a base font; ignore the next entries} pdf_print("/BaseFont /"); pdf_print_nl(fm_tab[fm_cur].fontbase_name); pdf_end_dict; return; end else if not is_font_file then begin print_nl("Font file not specified ("); print(font_name[f]); print(")"); print_ln; fatal_error("Missing font file"); end; pdf_int_entry_nl("FirstChar", font_bc[f]); pdf_int_entry_nl("LastChar", font_ec[f]); pdf_indirect_nl("Widths", obj_count + 1); { if font is included then |obj_count + 1| is the width array, |obj_count + 2| is the font file stream, if font is TrueType then |obj_count + 3| is the font file length |obj_count + 4| is the font base name, |obj_count + 5| is the font descriptor, else |obj_count + 3..obj_count + 6| are the lengths of sections in font file, |obj_count + 7| is the font base name, |obj_count + 8| is the font descriptor, else |obj_count + 1| is the width array, |obj_count + 2| is the font base name, |obj_count + 3| is the font descriptor, } if is_included_font then begin if is_truetype_font then begin pdf_indirect_nl("BaseFont", obj_count + 4); pdf_indirect_nl("FontDescriptor", obj_count + 5); end else begin pdf_indirect_nl("BaseFont", obj_count + 7); pdf_indirect_nl("FontDescriptor", obj_count + 8); end; end else begin pdf_indirect_nl("BaseFont", obj_count + 2); pdf_indirect_nl("FontDescriptor", obj_count + 3); end; pdf_end_dict; @; if is_included_font then begin @; end else if not is_truetype_font then t1_include; {to scan parameters in font file} @; @; end; @ @= pdf_new_obj(pdf_others, 0); pdf_print("["); for k := font_bc[f] to font_ec[f] do begin pdf_print_int(em_thousandth(char_width(f)(char_info(f)(k)), font_size[f])); if k < font_ec[f] then pdf_out(" "); end; pdf_print_nl("]"); pdf_end_obj @ @= pdf_new_dict(pdf_others, 0); if not is_truetype_font then begin pdf_indirect_nl("Length", obj_count + 1); pdf_indirect_nl("Length1", obj_count + 2); pdf_indirect_nl("Length2", obj_count + 3); pdf_indirect_nl("Length3", obj_count + 4); pdf_print_nl(">>"); pdf_print_nl("stream"); t1_include; pdf_print_nl("endstream"); pdf_end_obj; ff_length(pdf_ff_length1 + pdf_ff_length2 + pdf_ff_length3); ff_length(pdf_ff_length1); ff_length(pdf_ff_length2); ff_length(pdf_ff_length3); end else begin pdf_indirect_nl("Length", obj_count + 1); pdf_indirect_nl("Length1", obj_count + 2); pdf_print_nl(">>"); pdf_print_nl("stream"); ttf_include; pdf_print_nl("endstream"); pdf_end_obj; ff_length(pdf_ff_length1); end @ @= if is_font_file and (not str_eq_str(pdf_fontname(f), fm_tab[fm_cur].fontbase_name)) then begin print_err("Base font name mismatch: "); print("`"); print(pdf_fontname(f)); print("' (in font file) x `"); print(fm_tab[fm_cur].fontbase_name); print("' (in map file)"); print_nl("The name defined in font file was ignored"); print_nl("You can edit your map file to suprress this warning"); print_ln; end; pdf_new_obj(pdf_others, 0); pdf_out("/"); if is_subsetted_font and (not is_truetype_font) then begin pdf_print(subset_prefix(f)); pdf_out("+"); end; pdf_print_nl(fm_tab[fm_cur].fontbase_name); pdf_end_obj @ @= pdf_new_dict(pdf_others, 0); pdf_print_nl("/Type /FontDescriptor"); pdf_fd_key("Ascent", pdf_ascent_code, char_height(f)(height_depth(char_info(f)("h")))); pdf_fd_key("CapHeight", pdf_capheight_code, char_height(f)(height_depth(char_info(f)("H")))); pdf_fd_key("Descent", pdf_descent_code, char_depth(f)(height_depth(char_info(f)("q")))); pdf_indirect_nl("FontName", obj_count - 1); pdf_int_entry_nl("ItalicAngle", pdf_italic_angle(f)); pdf_int_entry_nl("StemV", pdf_stemv(f)); pdf_fd_key("AvgWidth", pdf_avgwidth_code, 0); pdf_fd_key("Leading", pdf_leading_code, 0); pdf_fd_key("MaxWidth", pdf_maxwidth_code, 0); pdf_fd_key("MissingWidth", pdf_missingwidth_code, 0); pdf_fd_key("StemH", pdf_stemh_code, 0); pdf_fd_key("XHeight", pdf_xheight_code, x_height(f)); pdf_print("/FontBBox ["); for k := pdf_fontbbox_code to pdf_fontbbox_code + 3 do begin pdf_print_int(pdf_fp(k)(f)); if k < pdf_fontbbox_code + 3 then pdf_out(" "); end; pdf_print_nl("]"); pdf_print("/Flags "); pdf_print_nl(fm_tab[fm_cur].flags); if is_subsetted_font and is_reencoded_font then begin pdf_print("/CharString ["); l := fm_tab[fm_cur].encoding; for k := 0 to 255 do if (enc_tab[l].enc_glyph_names[k] <> 0) and pdf_is_char_used(f, k) then pdf_print(enc_tab[l].enc_glyph_names[k]); pdf_print_nl("]"); end; if is_included_font then if is_truetype_font then pdf_indirect_nl("FontFile2", obj_count - 3) else pdf_indirect_nl("FontFile", obj_count - 6); pdf_end_dict @ The following procedures sorts the table of destination names by name @p function str_less_str(s1, s2: str_number): boolean; {compare two strings} var j1, j2: pool_pointer; l, i: integer; begin j1 := str_start[s1]; j2 := str_start[s2]; if length(s1) < length(s2) then l := length(s1) else l := length(s2); i := 0; while (i < l) and (str_pool[j1 + i] = str_pool[j2 + i]) do incr(i); if ((i < l) and (str_pool[j1 + i] < str_pool[j2 + i])) or ((i = l) and (length(s1) < length(s2))) then str_less_str := true else str_less_str := false; end; procedure sort_dest_names(l, r: pointer); {sorts |dest_names| by names} var i, j: pointer; s: str_number; x, y: halfword; e: dest_name_entry; begin i := l; j := r; s := dest_names[(l + r) div 2].objname; repeat while str_less_str(dest_names[i].objname, s) do incr(i); while str_less_str(s, dest_names[j].objname) do decr(j); if i <= j then begin e := dest_names[i]; dest_names[i] := dest_names[j]; dest_names[j] := e; incr(i); decr(j); end; until i > j; if l < j then sort_dest_names(l, j); if i < r then sort_dest_names(i, r); end; @ Now the \.{PDF} output file. At this moment all Page object are already written completly to \.{PDF} output file. @= if total_pages=0 then print_nl("No pages of output.") @.No pages of output@> else begin if total_pages mod pages_tree_kids_max <> 0 then obj_info(pdf_last_pages) := total_pages mod pages_tree_kids_max; {last pages object may have less than |pages_tree_kids_max| chilrend} @; @; @; @; @; @; @; @; @; @; @; pdf_flush; print_nl("Output written on "); slow_print(output_file_name); @.Output written on x@> print(" ("); print_int(total_pages); print(" page"); if total_pages<>1 then print_char("s"); print(", "); print_int(pdf_offset); print(" bytes)."); b_close(pdf_file); end @ @= @; k := head_tab[pdf_page]; l := null; repeat i := obj_link(k); obj_link(k) := l; l := k; k := i; until k = null; head_tab[pdf_page] := l; k := head_tab[pdf_pages]; l := null; repeat i := obj_link(k); obj_link(k) := l; l := k; k := i; until k = null; head_tab[pdf_pages] := l @ @= k := head_tab[pdf_page]; while obj_int(k) = 0 do begin print_err("Page "); print_int(obj_info(k)); print(" has been refered to but not created"); print_ln; k := obj_link(k); end; head_tab[pdf_page] := k @ @= k := head_tab[pdf_form]; while k <> null do begin obj_offset(k) := obj_form_offset(k); k := obj_link(k); end @ @= for k := font_base + 1 to font_ptr do if (not font_used[k]) and (pdf_font_num[k] <> 0) then begin i := font_base; repeat incr(i); until font_used[i] and (pdf_font_num[i] = pdf_font_num[k]); for j := 0 to 255 do if pdf_is_char_used(k, j) then pdf_set_char_used(i, j); end; fm_get_info; k := head_tab[pdf_font]; while k <> null do begin f := obj_info(k); pdf_begin_dict(k); do_pdf_font; k := obj_link(k); end @ We will generate in each single step the parents of all Pages/Page objects in the previous level. These new generated Pages object will creat a new level of Pages tree. We will repeat this until search only one Pages object. This one will be the Root object. @= a := obj_count + 1; {all Pages object whose childrend are not Page objects should have index greater than |a|} l := head_tab[pdf_pages]; {|l| is the index of current Pages object which is being output} k := head_tab[pdf_page]; {|k| is the index of current child of |l|} if obj_link(l) = null then is_root := true {only Pages object; total pages is not greater than |pages_tree_kids_max|} else is_root := false; b := obj_count + 1; {to init |c| in next step} repeat i := 0; {counter of Pages object in current level} c := b; {first Pages object in previous level} b := obj_count + 1; {succcesor of last created object} repeat if not is_root then begin if i mod pages_tree_kids_max = 0 then begin {creat a new Pages object for next level} pdf_create_obj(pdf_others, 0); pdf_last_pages := obj_count; obj_info(pdf_last_pages) := obj_info(l); end else obj_info(pdf_last_pages) := obj_info(pdf_last_pages) + obj_info(l); end; @; incr(i); if l < a then l := obj_link(l) else incr(l); until (l = null) or (l = b); if l = null then l := a; if b = obj_count then is_root := true; until false; done: @ @= pdf_begin_dict(l); pdf_print_nl("/Type /Pages"); pdf_int_entry_nl("Count", obj_info(l)); if not is_root then pdf_indirect_nl("Parent", pdf_last_pages); pdf_print("/Kids ["); j := 0; repeat pdf_print_int(k); pdf_print_(" 0 R "); if k < a then {the next Pages/Page object must be |obj_link(k)|} k := obj_link(k) else {|k >= a|, the next Pages object is |k + 1|} incr(k); incr(j); until ((l < a) and (j = obj_info(l))) or (k = null) or (k = c) or (j = pages_tree_kids_max); pdf_remove_last_char; pdf_print_nl("]"); if k = null then begin if head_tab[pdf_pages] <> null then begin {we are in Page objects list} k := head_tab[pdf_pages]; head_tab[pdf_pages] := null; end else {we are in Pages objects list} k := a; end; if is_root then begin if pdf_pages_attr <> null then pdf_print_nl(tokens_to_string(pdf_pages_attr)); pdf_print("/MediaBox [0 0 "); pdf_print_scaled(get_pdf_page_width); pdf_out(" "); pdf_print_scaled(get_pdf_page_height); pdf_print_nl("]"); end; pdf_end_dict; if is_root then goto done @ @= if first_outline <> null then begin pdf_new_dict(pdf_others, 0); outlines := obj_count; l := first_outline; k := 0; repeat incr(k); a := open_subentries(l); if obj_outline_count(l) > 0 then k := k + a; obj_outline_parent(l) := obj_count; l := obj_outline_next(l); until l = null; pdf_print_nl("/Type /Outlines"); pdf_indirect_nl("First", first_outline); pdf_indirect_nl("Last", last_outline); pdf_int_entry_nl("Count", k); pdf_end_dict; @; end else outlines := null @ @= k := head_tab[pdf_outline]; while k <> null do begin if obj_outline_parent(k) = parent_outline then begin if obj_outline_prev(k) = 0 then first_outline := k; if obj_outline_next(k) = 0 then last_outline := k; end; pdf_save_offset := pdf_offset; {we can't use |pdf_begin_dict| because it rewrites the second memory word of this entry} pdf_print_int(k); pdf_print_nl(" 0 obj <<"); pdf_indirect_nl("Title", obj_outline_title(k)); pdf_indirect_nl("A", obj_outline_action_objnum(k)); if obj_outline_parent(k) <> 0 then pdf_indirect_nl("Parent", obj_outline_parent(k)); if obj_outline_prev(k) <> 0 then pdf_indirect_nl("Prev", obj_outline_prev(k)); if obj_outline_next(k) <> 0 then pdf_indirect_nl("Next", obj_outline_next(k)); if obj_outline_first(k) <> 0 then pdf_indirect_nl("First", obj_outline_first(k)); if obj_outline_last(k) <> 0 then pdf_indirect_nl("Last", obj_outline_last(k)); if obj_outline_count(k) <> 0 then pdf_int_entry_nl("Count", obj_outline_count(k)); obj_offset(k) := pdf_save_offset; {write the byte offset after we write out all data} pdf_end_dict; k := obj_link(k); end @ The name tree is very similiar to Pages tree so its construction should be certain from Pages tree construction. For intermediate node |obj_ptr| will be the first name and |obj_ptr| will be the last name in \.{\\Limits} array. Note that |dest_names_ptr| will be less than |obj_count|, so we test if |k < dest_names_ptr| then |k| is index of leaf in |dest_names|; else |k| will be index in |obj_tab| of some intermediate node. @= if dest_names_ptr = 0 then begin name_tree := null; goto done1; end; sort_dest_names(0, dest_names_ptr - 1); a := obj_count + 1; {first intermediate node of name tree} l := a; {index of node being output} k := 0; {index of current child of |l|; if |k < dest_names_ptr| then this is pointer to |dest_names| array; otherwise it is the pointer to |obj_tab| (object number) } repeat c := obj_count + 1; {first node in current level} repeat pdf_create_obj(pdf_others, 0); {creat a new node for next level} @; incr(l); incr(i); until k = c; until false; done1: @ @= pdf_begin_dict(l); j := 0; if k < dest_names_ptr then begin obj_info(l) := dest_names[k].objname; pdf_print("/Names ["); repeat pdf_print_str(dest_names[k].objname); pdf_out(" "); pdf_print_int(dest_names[k].objnum); pdf_print(" 0 R "); incr(j); incr(k); until (j = name_tree_kids_max) or (k = dest_names_ptr); pdf_remove_last_char; pdf_print_nl("]"); obj_link(l) := dest_names[k - 1].objname; if k = dest_names_ptr then k := a; end else begin obj_info(l) := obj_info(k); pdf_print("/Kids ["); repeat pdf_print_int(k); pdf_print(" 0 R "); incr(j); incr(k); until (j = name_tree_kids_max) or (k = c); pdf_remove_last_char; pdf_print_nl("]"); obj_link(l) := obj_link(k - 1); end; if (l > k) or (l = a) then begin pdf_print("/Limits ["); pdf_print_str(obj_info(l)); pdf_out(" "); pdf_print_str(obj_link(l)); pdf_print_nl("]"); pdf_end_dict; end else begin pdf_end_dict; pdf_new_dict(pdf_others, 0); pdf_indirect_nl("Dests", l); pdf_end_dict; name_tree := obj_count; goto done1; end @ @= if head_tab[pdf_thread] <> null then begin k := head_tab[pdf_thread]; pdf_new_obj(pdf_others, 0); pdf_out("["); while k <> null do begin pdf_print_int(k); pdf_print(" 0 R "); k := obj_link(k); end; pdf_remove_last_char; pdf_print_nl("]"); pdf_end_obj; threads := obj_count; k := head_tab[pdf_thread]; while k <> null do begin if obj_thread_first(k) = null then begin print_err("Thread "); if obj_thread_num(k) <= 0 then print(obj_thread_name(k)) else print_int(obj_thread_num(k)); print(" has been refered to but not created"); print_ln; end else begin pdf_save_offset := pdf_offset; {we can't use |pdf_begin_dict| because it rewrites the second memory word of this entry} pdf_print_int(k); pdf_print_nl(" 0 obj <<"); if obj_thread_num(k) <= 0 then begin pdf_print("/I << /Title "); pdf_print_str(obj_thread_name(k)); pdf_print_nl(" >>"); end; pdf_indirect_nl("F", obj_thread_first(k)); pdf_end_dict; @; obj_offset(k) := pdf_save_offset; {write the byte offset after we write out all data} end; k := obj_link(k); end; end else threads := null @ @= l := obj_thread_first(k); a := l; repeat pdf_begin_dict(l); if l = a then pdf_indirect_nl("T", k); pdf_indirect_nl("V", obj_bead_prev(l)); pdf_indirect_nl("N", obj_bead_next(l)); pdf_indirect_nl("P", obj_bead_page(l)); pdf_indirect_nl("R", obj_bead_rect(l)); pdf_end_dict; l := obj_bead_next(l); until l = a @ @= pdf_new_dict(pdf_others, 0); root := obj_count; pdf_print_nl("/Type /Catalog"); pdf_indirect_nl("Pages", pdf_last_pages); if threads <> null then pdf_indirect_nl("Threads", threads); if outlines <> null then pdf_indirect_nl("Outlines", outlines); if name_tree <> null then pdf_indirect_nl("Names", name_tree); if pdf_catalog_pagemode <> 0 then begin pdf_print("/PageMode "); pdf_print_nl(pdf_catalog_pagemode); end; if pdf_catalog_uri <> 0 then begin pdf_print("/URI << /Base ("); pdf_print(pdf_catalog_uri); pdf_print_nl(") >>"); end; pdf_end_dict @ @= pdf_new_dict(pdf_others, 0); pdf_print_nl("/Creator (pdfTeX)"); pdf_print_nl("/Producer (pdfTeX)"); pdf_print("/CreationDate (D:"); pdf_print_int(year); pdf_print_two(month); pdf_print_two(day); pdf_print_two(time div 60); pdf_print_two(time mod 60); pdf_print_nl("00)"); pdf_str_entry_nl("Author", pdf_info_author); pdf_str_entry_nl("Title", pdf_info_title); pdf_str_entry_nl("Subject", pdf_info_subject); pdf_str_entry_nl("Keywords", pdf_info_keywords); pdf_end_dict @ @= pdf_save_offset := pdf_offset; pdf_print_nl("xref"); pdf_print("0 "); pdf_print_int_nl(obj_count + 1); pdf_print_nl("0000000000 65535 f "); for k := 1 to objcount do begin pdf_print_offset(obj_offset(k)); pdf_print_nl(" 00000 n ");@/ {{\bf Important note:} the last space after \.{`n'} and \.{`f'} is required for single end-of-line character} end @ @= pdf_print_nl("trailer"); pdf_print_nl("<<"); pdf_int_entry_nl("Size", obj_count + 1); pdf_indirect_nl("Root", root); pdf_indirect_nl("Info", obj_count); pdf_print_nl(">>"); pdf_print_nl("startxref"); pdf_print_int_nl(pdf_save_offset); pdf_print_nl("%%EOF") @ Here we rename the original procedure |ship_out| to |dvi_ship_out|. We will declare later our new procedure |ship_out| that will call a new procedure called |pdf_ship_out| if integer parametr \.{\\pdfoutput} is positive; otherwise it will call |dvi_ship_out|, which is the original |ship_out|. |pdf_ship_out| do the same as |dvi_ship_out| but for the \.{PDF} output of course. @p procedure dvi_ship_out(@!p:pointer); {output the box |p| to \.{DVI} file} @z @x dvi_out(eop); incr(total_pages); cur_s:=-1; @y dvi_out(eop); incr(total_pages); cur_s:=-1; ifdef ('IPC') if ipc_on>0 then begin if dvi_limit=half_buf then begin write_dvi(half_buf, dvi_buf_size-1); dvi_gone:=dvi_gone+half_buf; end; if dvi_ptr>0 then begin write_dvi(0, dvi_ptr-1); dvi_offset:=dvi_offset+dvi_ptr; dvi_gone:=dvi_gone+dvi_ptr; end; dvi_ptr:=0; dvi_limit:=dvi_buf_size; ipc_page(dvi_gone); end; endif ('IPC'); @z @x k:=4+((dvi_buf_size-dvi_ptr) mod 4); {the number of 223's} @y ifdef ('IPC') k:=7-((3+dvi_offset+dvi_ptr) mod 4); {the number of 223's} endif ('IPC') ifndef ('IPC') k:=4+((dvi_buf_size-dvi_ptr) mod 4); {the number of 223's} endifn ('IPC') @z @x @* \[33] Packaging. @y @ The new procedure |ship_out| will test integer parametr \.{\\pdfoutput} and call |pdf_ship_out| for positive case; otherwise call |dvi_ship_out| @p procedure ship_out(p:pointer); {output the box |p|} begin if get_pdf_output > 0 then pdf_ship_out(p) else dvi_ship_out(p); end; @* \[33] Packaging. @z @x 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 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 i:=char_info(f)(y); @y i:=orig_char_info(f)(y); @z @x 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 @x if abs(fit_class-fitness(r))>1 then d:=d+adj_demerits; @y if abs(toint(fit_class)-toint(fitness(r)))>1 then d:=d+adj_demerits; @z @x begin line_diff:=line_number(r)-best_line; @y begin line_diff:=toint(line_number(r))-toint(best_line); @z @x Comparatively few different number sequences $n_0\ldots n_k$ actually occur, since most of the |n|'s are generally zero. Therefore the number sequences are encoded in such a way that |trie_op|$(z_k)$ is only one byte long. If |trie_op(@t$z_k$@>)<>min_quarterword|, when $p_1\ldots p_k$ has matched the letters in |hc[(l-k+1)..l@,]| of language |t|, we perform all of the required operations for this pattern by carrying out the following little program: Set |v:=trie_op(@t$z_k$@>)|. Then set |v:=v+op_start[t]|, |hyf[l-hyf_distance[v]]:=@tmax@>(hyf[l-hyf_distance[v]], hyf_num[v])|, and |v:=hyf_next[v]|; repeat, if necessary, until |v=min_quarterword|. @y The theory that comparatively few different number sequences $n_0\ldots n_k$ actually occur, since most of the |n|'s are generally zero, seems to fail at least for the large German hyphenation patterns. Therefore the number sequences cannot any longer be encoded in such a way that |trie_op|$(z_k)$ is only one byte long. We have introduced a new constant |max_trie_op| for the maximum allowable hyphenation operation code value; |max_trie_op| might be different for \TeX\ and \.{INITEX} and must not exceed |max_halfword|. An opcode will occupy a halfword if |max_trie_op| exceeds |max_quarterword| or a quarterword otherwise. @^system dependencies@> If |trie_op(@t$z_k$@>)<>min_trie_op|, when $p_1\ldots p_k$ has matched the letters in |hc[(l-k+1)..l@,]| of language |t|, we perform all of the required operations for this pattern by carrying out the following little program: Set |v:=trie_op(@t$z_k$@>)|. Then set |v:=v+op_start[t]|, |hyf[l-hyf_distance[v]]:=@tmax@>(hyf[l-hyf_distance[v]], hyf_num[v])|, and |v:=hyf_next[v]|; repeat, if necessary, until |v=min_trie_op|. @z @x @!trie_pointer=0..trie_size; {an index into |trie|} @y @!trie_pointer=0..ssup_trie_size; {an index into |trie|} @!trie_opcode=0..ssup_trie_opcode; {a trie opcode} @z @x @ @d trie_link(#)==trie[#].rh {``downward'' link in a trie} @d trie_char(#)==trie[#].b1 {character matched at this trie location} @d trie_op(#)==trie[#].b0 {program for hyphenation at this trie location} @y @ For more than 255 trie op codes, the three fields |trie_link|, |trie_char|, and |trie_op| will no longer fit into one memory word; thus using web2c we define |trie| as three array instead of an array of records. The variant will be implented by reusing the opcode field later on with another macro. @d trie_link(#)==trie_trl[#] {``downward'' link in a trie} @d trie_char(#)==trie_trc[#] {character matched at this trie location} @d trie_op(#)==trie_tro[#] {program for hyphenation at this trie location} @z @x @!trie:array[trie_pointer] of two_halves; {|trie_link|, |trie_char|, |trie_op|} @y {We will dynamically allocate these arrays.} @!trie_trl:^trie_pointer; {|trie_link|} @!trie_tro:^trie_pointer; {|trie_op| and |trie_link|} @!trie_trc:^quarterword; {|trie_char|} @z @x @!hyf_next:array[1..trie_op_size] of quarterword; {continuation code} @y @!hyf_next:array[1..trie_op_size] of trie_opcode; {continuation code} @z @x begin if trie_op(z)<>min_quarterword then @y begin if trie_op(z)<>min_trie_op then @z @x until v=min_quarterword; @y until v=min_trie_op; @z @x different from $\alpha$, we can conclude that $\alpha$ is not in the table. @y different from $\alpha$, we can conclude that $\alpha$ is not in the table. This is a clever scheme which saves the need for a hash link array. However, it is difficult to increase the size of the hyphen exception arrays. To make this easier, the ordered hash has been replaced by a simple hash, using an additional array |hyph_link|. The value |0| in |hyph_link[k]| means that there are no more entries corresponding to the specific hash chain. When |hyph_link[k]>0|, the next entry in the hash chain is |hyph_link[k]-1|. This value is used because the arrays start at |0|. @z @x @!hyph_pointer=0..hyph_size; {an index into the ordered hash table} @y @!hyph_pointer=0..ssup_hyph_size; {index into hyphen exceptions hash table; enlarging this requires changing (un)dump code} @z @x @!hyph_word:array[hyph_pointer] of str_number; {exception words} @!hyph_list:array[hyph_pointer] of pointer; {list of hyphen positions} @!hyph_count:hyph_pointer; {the number of words in the exception dictionary} @y @!hyph_word: ^str_number; {exception words} @!hyph_list: ^pointer; {list of hyphen positions} @!hyph_link: ^hyph_pointer; {link array for hyphen exceptions hash table} @!hyph_count:integer; {the number of words in the exception dictionary} @!hyph_next:integer; {next free slot in hyphen exceptions hash table} @z @x for z:=0 to hyph_size do begin hyph_word[z]:=0; hyph_list[z]:=null; end; hyph_count:=0; @y for z:=0 to hyph_size do begin hyph_word[z]:=0; hyph_list[z]:=null; hyph_link[z]:=0; end; hyph_count:=0; hyph_next:=hyph_prime+1; if hyph_next>hyph_size then hyph_next:=hyph_prime; @z @x h:=hc[1]; incr(hn); hc[hn]:=cur_lang; for j:=2 to hn do h:=(h+h+hc[j]) mod hyph_size; loop@+ begin @; if h>0 then decr(h)@+else h:=hyph_size; end; not_found: decr(hn) @y h:=hc[1]; incr(hn); hc[hn]:=cur_lang; for j:=2 to hn do h:=(h+h+hc[j]) mod hyph_prime; loop@+ begin @; h:=hyph_link[h]; if h=0 then goto not_found; decr(h); end; not_found: decr(hn) @z @x @ @= k:=hyph_word[h]; if k=0 then goto not_found; if length(k)hc[j] then goto done; incr(j); incr(u); until j>hn; @; decr(hn); goto found; end; done: @y @ @= {This is now a simple hash list, not an ordered one, so the module title is no longer descriptive.} k:=hyph_word[h]; if k=0 then goto not_found; if length(k)=hn then begin j:=1; u:=str_start[k]; repeat if so(str_pool[u])<>hc[j] then goto done; incr(j); incr(u); until j>hn; @; decr(hn); goto found; end; done: @z @x begin h:=(h+h+hc[j]) mod hyph_size; @y begin h:=(h+h+hc[j]) mod hyph_prime; @z @x @ @= if hyph_count=hyph_size then overflow("exception dictionary",hyph_size); @:TeX capacity exceeded exception dictionary}{\quad exception dictionary@> incr(hyph_count); while hyph_word[h]<>0 do begin @; if h>0 then decr(h)@+else h:=hyph_size; end; hyph_word[h]:=s; hyph_list[h]:=p @y @ @= if hyph_next <= hyph_prime then while (hyph_next>0) and (hyph_word[hyph_next-1]>0) do decr(hyph_next); if (hyph_count=hyph_size)or(hyph_next=0) then overflow("exception dictionary",hyph_size); @:TeX capacity exceeded exception dictionary}{\quad exception dictionary@> incr(hyph_count); while hyph_word[h]<>0 do begin @; if hyph_link[h]=0 then begin hyph_link[h]:=hyph_next; if hyph_next >= hyph_size then hyph_next:=hyph_prime; if hyph_next > hyph_prime then incr(hyph_next); end; h:=hyph_link[h]-1; end; found: hyph_word[h]:=s; hyph_list[h]:=p @z @x @ @= k:=hyph_word[h]; if length(k)length(s) then goto not_found; u:=str_start[k]; v:=str_start[s]; repeat if str_pool[u]str_pool[v] then goto not_found; incr(u); incr(v); until u=str_start[k+1]; found:q:=hyph_list[h]; hyph_list[h]:=p; p:=q;@/ t:=hyph_word[h]; hyph_word[h]:=s; s:=t; not_found: @y @ @= {This is now a simple hash list, not an ordered one, so the module title is no longer descriptive.} k:=hyph_word[h]; if length(k)<>length(s) then goto not_found; u:=str_start[k]; v:=str_start[s]; repeat if str_pool[u]<>str_pool[v] then goto not_found; incr(u); incr(v); until u=str_start[k+1]; {repeat hyphenation exception; flushing old data} flush_string; s:=hyph_word[h]; {avoid |slow_make_string|!} decr(hyph_count); { We could also |flush_list(hyph_list[h]);|, but it interferes with \.{trip.log}. } goto found; not_found: @z @x |hyf_next[@t$v^\prime$@>]=min_quarterword|. @y |hyf_next[@t$v^\prime$@>]=min_trie_op|. @z @x $$\hbox{|@t$v^\prime$@>:=new_trie_op(0,1,min_quarterword)|,\qquad @y $$\hbox{|@t$v^\prime$@>:=new_trie_op(0,1,min_trie_op)|,\qquad @z @x @!init@! trie_op_hash:array[-trie_op_size..trie_op_size] of 0..trie_op_size; @y @!init@! trie_op_hash:array[neg_trie_op_size..trie_op_size] of 0..trie_op_size; @z @x @!trie_used:array[ASCII_code] of quarterword; @y @!trie_used:array[ASCII_code] of trie_opcode; @z @x @!trie_op_val:array[1..trie_op_size] of quarterword; @y @!trie_op_val:array[1..trie_op_size] of trie_opcode; @z @x tini @y tini@; @!max_op_used:trie_opcode; {largest opcode used for any language} @!small_op:boolean; {flag used while dumping or undumping} @z @x |new_trie_op| could return |min_quarterword| (thereby simply ignoring @y |new_trie_op| could return |min_trie_op| (thereby simply ignoring @z @x function new_trie_op(@!d,@!n:small_number;@!v:quarterword):quarterword; label exit; var h:-trie_op_size..trie_op_size; {trial hash location} @!u:quarterword; {trial op code} @y function new_trie_op(@!d,@!n:small_number;@!v:trie_opcode):trie_opcode; label exit; var h:neg_trie_op_size..trie_op_size; {trial hash location} @!u:trie_opcode; {trial op code} @z @x begin h:=abs(n+313*d+361*v+1009*cur_lang) mod (trie_op_size+trie_op_size) - trie_op_size; @y begin h:=abs(toint(n)+313*toint(d)+361*toint(v)+1009*toint(cur_lang)) mod (trie_op_size - neg_trie_op_size) + neg_trie_op_size; @z @x if u=max_quarterword then overflow("pattern memory ops per language", max_quarterword-min_quarterword); incr(trie_op_ptr); incr(u); trie_used[cur_lang]:=u; @y if u=max_trie_op then overflow("pattern memory ops per language", max_trie_op-min_trie_op); incr(trie_op_ptr); incr(u); trie_used[cur_lang]:=u; if u>max_op_used then max_op_used:=u; @z @x op_start[0]:=-min_quarterword; @y op_start[0]:=-min_trie_op; @z @x for k:=0 to 255 do trie_used[k]:=min_quarterword; @y for k:=0 to 255 do trie_used[k]:=min_trie_op; @z @x trie_op_ptr:=0; @y max_op_used:=min_trie_op; trie_op_ptr:=0; @z @x @!init @!trie_c:packed array[trie_pointer] of packed_ASCII_code; {characters to match} @t\hskip10pt@>@!trie_o:packed array[trie_pointer] of quarterword; {operations to perform} @t\hskip10pt@>@!trie_l:packed array[trie_pointer] of trie_pointer; {left subtrie links} @t\hskip10pt@>@!trie_r:packed array[trie_pointer] of trie_pointer; {right subtrie links} @t\hskip10pt@>@!trie_ptr:trie_pointer; {the number of nodes in the trie} @t\hskip10pt@>@!trie_hash:packed array[trie_pointer] of trie_pointer; {used to identify equivalent subtries} tini @y @!init @!trie_c:^packed_ASCII_code; {characters to match} @t\hskip10pt@>@!trie_o:^trie_opcode; {operations to perform} @t\hskip10pt@>@!trie_l:^trie_pointer; {left subtrie links} @t\hskip10pt@>@!trie_r:^trie_pointer; {right subtrie links} @t\hskip10pt@>@!trie_ptr:trie_pointer; {the number of nodes in the trie} @t\hskip10pt@>@!trie_hash:^trie_pointer; {used to identify equivalent subtries} tini @z @x begin h:=abs(trie_c[p]+1009*trie_o[p]+@| 2718*trie_l[p]+3142*trie_r[p]) mod trie_size; @y begin h:=abs(toint(trie_c[p])+1009*toint(trie_o[p])+@| 2718*toint(trie_l[p])+3142*toint(trie_r[p])) mod trie_size; @z @x @d trie_back(#)==trie[#].lh {backward links in |trie| holes} @y @d trie_back(#)==trie_tro[#] {use the opcode field now for backward links} @z @x @!init@!trie_taken:packed array[1..trie_size] of boolean; {does a family start here?} @t\hskip10pt@>@!trie_min:array[ASCII_code] of trie_pointer; {the first possible slot for each character} @t\hskip10pt@>@!trie_max:trie_pointer; {largest location used in |trie|} @t\hskip10pt@>@!trie_not_ready:boolean; {is the trie still in linked form?} tini @y @!init@!trie_taken: ^boolean; {does a family start here?} @t\hskip10pt@>@!trie_min:array[ASCII_code] of trie_pointer; {the first possible slot for each character} @t\hskip10pt@>@!trie_max:trie_pointer; {largest location used in |trie|} @t\hskip10pt@>@!trie_not_ready:boolean; {is the trie still in linked form?} tini @z @x trie_not_ready:=true; trie_root:=0; trie_c[0]:=si(0); trie_ptr:=0; @y trie_not_ready:=true; @z @x @= h.rh:=0; h.b0:=min_quarterword; h.b1:=min_quarterword; {|trie_link:=0|, |trie_op:=min_quarterword|, |trie_char:=qi(0)|} if trie_root=0 then {no patterns were given} begin for r:=0 to 256 do trie[r]:=h; @y @d clear_trie == {clear |trie[r]|} begin trie_link(r):=0; trie_op(r):=min_trie_op; trie_char(r):=min_quarterword; {|trie_char:=qi(0)|} end @= if trie_root=0 then {no patterns were given} begin for r:=0 to 256 do clear_trie; @z @x repeat s:=trie_link(r); trie[r]:=h; r:=s; @y repeat s:=trie_link(r); clear_trie; r:=s; @z @x @!v:quarterword; {trie op code} @y @!v:trie_opcode; {trie op code} @z @x if trie_o[q]<>min_quarterword then @y if trie_o[q]<>min_trie_op then @z @x trie_c[p]:=si(c); trie_o[p]:=min_quarterword; @y trie_c[p]:=si(c); trie_o[p]:=min_trie_op; @z @x l:=k; v:=min_quarterword; @y l:=k; v:=min_trie_op; @z @x @!h:two_halves; {template used to zero out |trie|'s holes} @y @z @x main_loop_move+2:if(cur_chrfont_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)))frozen_control_sequence) then @y if (cur_cs=0)or(cur_cs>eqtb_top)or ((cur_cs>frozen_control_sequence)and(cur_cs<=eqtb_size)) then @z @x @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 @!@: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 othercases print_esc("toksdef") @y char_sub_def_code: print_esc("charsubdef"); othercases print_esc("toksdef") @z @x 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_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 hyph_data: if cur_chr=1 then begin @!init new_patterns; goto done;@;@+tini@/ @y hyph_data: if cur_chr=1 then begin @!init if ini_version then begin new_patterns; goto done; end; @;@+tini@/ @z @x flushable_string:=str_ptr-1; @y @z @x begin if cur_name=flushable_string then begin flush_string; cur_name:=font_name[f]; end; if s>0 then @y begin if s>0 then @z @x interaction:=cur_chr; @y interaction:=cur_chr; if interaction = batch_mode then kpse_make_tex_discard_errors := 1 else kpse_make_tex_discard_errors := 0; @z @x if cur_ext="" then cur_ext:=".tex"; pack_cur_name; if a_open_in(read_file[n]) then read_open[n]:=just_open; @y pack_cur_name; tex_input_type:=0; {Tell |open_input| we are \.{\\openin}.} if a_open_in(read_file[n], kpse_tex_format) then read_open[n]:=just_open; @z @x format_ident:=" (INITEX)"; @y if ini_version then format_ident:=" (INITEX)"; @z @x @!w: four_quarters; {four ASCII codes} @y @z @x @; @y @; dump_int(@"4D4C5458); {ML\TeX's magic constant: "MLTX"} if mltex_p then dump_int(1) else dump_int(0); @z @x @!w: four_quarters; {four ASCII codes} begin @; @y begin @; 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 @d dump_wd(#)==begin fmt_file^:=#; put(fmt_file);@+end @d dump_int(#)==begin fmt_file^.int:=#; put(fmt_file);@+end @d dump_hh(#)==begin fmt_file^.hh:=#; put(fmt_file);@+end @d dump_qqqq(#)==begin fmt_file^.qqqq:=#; put(fmt_file);@+end @y @z @x @d undump_wd(#)==begin get(fmt_file); #:=fmt_file^;@+end @d undump_int(#)==begin get(fmt_file); #:=fmt_file^.int;@+end @d undump_hh(#)==begin get(fmt_file); #:=fmt_file^.hh;@+end @d undump_qqqq(#)==begin get(fmt_file); #:=fmt_file^.qqqq;@+end @y @z @x @d undump_size_end_end(#)==too_small(#)@+else undump_end_end @y @d format_debug_end(#)== write_ln (stderr, ' = ', #); end; @d format_debug(#)== if debug_format_file then begin write (stderr, 'fmtdebug:', #); format_debug_end @d undump_size_end_end(#)== too_small(#)@+else format_debug (#)(x); undump_end_end @z @x dump_int(@$);@/ @y dump_int(@$);@/ dump_int(max_halfword);@/ dump_int(hash_high); @z @x dump_int(hyph_size) @y dump_int(hyph_prime) @z @x x:=fmt_file^.int; if x<>@$ then goto bad_fmt; {check that strings are the same} undump_int(x); if x<>mem_bot then goto bad_fmt; undump_int(x); if x<>mem_top then goto bad_fmt; @y @+init if ini_version then begin libc_free(font_info); libc_free(str_pool); libc_free(str_start); libc_free(yhash); libc_free(zeqtb); libc_free(yzmem); end; @+tini undump_int(x); format_debug('string pool checksum')(x); if x<>@$ then goto bad_fmt; {check that strings are the same} undump_int(x); if x<>max_halfword then goto bad_fmt; {check |max_halfword|} undump_int(hash_high); if (hash_high<0)or(hash_high>sup_hash_extra) then goto bad_fmt; if hash_extramem_bot then goto bad_fmt; undump_int(mem_top); format_debug ('mem_top')(mem_top); if mem_bot+1100>mem_top then goto bad_fmt; head:=contrib_head; tail:=contrib_head; page_tail:=page_head; {page initialization} mem_min := mem_bot - extra_mem_bot; mem_max := mem_top + extra_mem_top; xmalloc_array (yzmem, mem_max - mem_min); zmem := yzmem - mem_min; {this pointer arithmetic fails with some compilers} mem := zmem; @z @x if x<>hyph_size then goto bad_fmt @y if x<>hyph_prime then goto bad_fmt @z @x for k:=0 to str_ptr do dump_int(str_start[k]); k:=0; while k+4eqtb_size @y k:=j+1; dump_int(k-l); until k>eqtb_size; if hash_high>0 then dump_things(eqtb[eqtb_size+1],hash_high); {dump |hash_extra| part} @z @x for j:=k to k+x-1 do undump_wd(eqtb[j]); @y undump_things(eqtb[k], x); @z @x until k>eqtb_size @y until k>eqtb_size; if hash_high>0 then undump_things(eqtb[eqtb_size+1],hash_high); {undump |hash_extra| part} @z @x dump_int(hash_used); cs_count:=frozen_control_sequence-1-hash_used; @y dump_int(hash_used); cs_count:=frozen_control_sequence-1-hash_used+hash_high; @z @x for p:=hash_used+1 to undefined_control_sequence-1 do dump_hh(hash[p]); @y dump_things(hash[hash_used+1], undefined_control_sequence-1-hash_used); if hash_high>0 then dump_things(hash[eqtb_size+1], hash_high); @z @x for p:=hash_used+1 to undefined_control_sequence-1 do undump_hh(hash[p]); @y undump_things (hash[hash_used+1], undefined_control_sequence-1-hash_used); if debug_format_file then begin print_csnames (hash_base, undefined_control_sequence - 1); end; if hash_high > 0 then begin undump_things (hash[eqtb_size+1], hash_high); if debug_format_file then begin print_csnames (eqtb_size + 1, hash_high - (eqtb_size + 1)); end; end; @z @x for k:=0 to fmem_ptr-1 do dump_wd(font_info[k]); dump_int(font_ptr); for k:=null_font to font_ptr do @; @y dump_things(font_info[0], fmem_ptr); dump_int(font_ptr); @; @z @x undump_size(7)(font_mem_size)('font mem size')(fmem_ptr); for k:=0 to fmem_ptr-1 do undump_wd(font_info[k]); undump_size(font_base)(font_max)('font max')(font_ptr); for k:=null_font to font_ptr do @ @y undump_size(7)(sup_font_mem_size)('font mem size')(fmem_ptr); if fmem_ptr>font_mem_size then font_mem_size:=fmem_ptr; xmalloc_array(font_info, font_mem_size); undump_things(font_info[0], fmem_ptr);@/ undump_size(font_base)(font_base+max_font_max)('font max')(font_ptr); {This undumps all of the font info, despite the name.} @; @z @x @ @= begin dump_qqqq(font_check[k]); dump_int(font_size[k]); dump_int(font_dsize[k]); dump_int(font_params[k]);@/ dump_int(hyphen_char[k]); dump_int(skew_char[k]);@/ dump_int(font_name[k]); dump_int(font_area[k]);@/ dump_int(font_bc[k]); dump_int(font_ec[k]);@/ dump_int(char_base[k]); dump_int(width_base[k]); dump_int(height_base[k]);@/ dump_int(depth_base[k]); dump_int(italic_base[k]); dump_int(lig_kern_base[k]);@/ dump_int(kern_base[k]); dump_int(exten_base[k]); dump_int(param_base[k]);@/ dump_int(font_glue[k]);@/ dump_int(bchar_label[k]); dump_int(font_bchar[k]); dump_int(font_false_bchar[k]);@/ print_nl("\font"); print_esc(font_id_text(k)); print_char("="); print_file_name(font_name[k],font_area[k],""); if font_size[k]<>font_dsize[k] then begin print(" at "); print_scaled(font_size[k]); print("pt"); end; end @y @ @= begin dump_things(font_check[null_font], font_ptr+1-null_font); dump_things(font_size[null_font], font_ptr+1-null_font); dump_things(font_dsize[null_font], font_ptr+1-null_font); dump_things(font_params[null_font], font_ptr+1-null_font); dump_things(hyphen_char[null_font], font_ptr+1-null_font); dump_things(skew_char[null_font], font_ptr+1-null_font); dump_things(font_name[null_font], font_ptr+1-null_font); dump_things(font_area[null_font], font_ptr+1-null_font); dump_things(font_bc[null_font], font_ptr+1-null_font); dump_things(font_ec[null_font], font_ptr+1-null_font); dump_things(char_base[null_font], font_ptr+1-null_font); dump_things(width_base[null_font], font_ptr+1-null_font); dump_things(height_base[null_font], font_ptr+1-null_font); dump_things(depth_base[null_font], font_ptr+1-null_font); dump_things(italic_base[null_font], font_ptr+1-null_font); dump_things(lig_kern_base[null_font], font_ptr+1-null_font); dump_things(kern_base[null_font], font_ptr+1-null_font); dump_things(exten_base[null_font], font_ptr+1-null_font); dump_things(param_base[null_font], font_ptr+1-null_font); dump_things(font_glue[null_font], font_ptr+1-null_font); dump_things(bchar_label[null_font], font_ptr+1-null_font); dump_things(font_bchar[null_font], font_ptr+1-null_font); dump_things(font_false_bchar[null_font], font_ptr+1-null_font); for k:=null_font to font_ptr do begin print_nl("\font"); print_esc(font_id_text(k)); print_char("="); print_file_name(font_name[k],font_area[k],""); if font_size[k]<>font_dsize[k] then begin print(" at "); print_scaled(font_size[k]); print("pt"); end; end; end @z @x @ @= begin undump_qqqq(font_check[k]);@/ undump_int(font_size[k]); undump_int(font_dsize[k]); undump(min_halfword)(max_halfword)(font_params[k]);@/ undump_int(hyphen_char[k]); undump_int(skew_char[k]);@/ undump(0)(str_ptr)(font_name[k]); undump(0)(str_ptr)(font_area[k]);@/ undump(0)(255)(font_bc[k]); undump(0)(255)(font_ec[k]);@/ undump_int(char_base[k]); undump_int(width_base[k]); undump_int(height_base[k]);@/ undump_int(depth_base[k]); undump_int(italic_base[k]); undump_int(lig_kern_base[k]);@/ undump_int(kern_base[k]); undump_int(exten_base[k]); undump_int(param_base[k]);@/ undump(min_halfword)(lo_mem_max)(font_glue[k]);@/ undump(0)(fmem_ptr-1)(bchar_label[k]); undump(min_quarterword)(non_char)(font_bchar[k]); undump(min_quarterword)(non_char)(font_false_bchar[k]); end @y @ This module should now be named `Undump all the font arrays'. @= begin {Allocate the font arrays} xmalloc_array(font_check, font_max); xmalloc_array(font_size, font_max); xmalloc_array(font_dsize, font_max); xmalloc_array(font_params, font_max); xmalloc_array(font_name, font_max); xmalloc_array(font_area, font_max); xmalloc_array(font_bc, font_max); xmalloc_array(font_ec, font_max); xmalloc_array(font_glue, font_max); xmalloc_array(hyphen_char, font_max); xmalloc_array(skew_char, font_max); xmalloc_array(bchar_label, font_max); xmalloc_array(font_bchar, font_max); xmalloc_array(font_false_bchar, font_max); xmalloc_array(char_base, font_max); xmalloc_array(width_base, font_max); xmalloc_array(height_base, font_max); xmalloc_array(depth_base, font_max); xmalloc_array(italic_base, font_max); xmalloc_array(lig_kern_base, font_max); xmalloc_array(kern_base, font_max); xmalloc_array(exten_base, font_max); xmalloc_array(param_base, font_max); xmalloc_array(pdf_font_num,font_max); xmalloc_array(pdf_fp_base,font_max); xmalloc_array(pdf_font_included,font_max); xmalloc_array(pdf_font_type,font_max); xmalloc_array(fm_tab,font_max); xmalloc_array(vf_packet_base,font_max); xmalloc_array(vf_default_font,font_max); xmalloc_array(vf_e_fnts,font_max); xmalloc_array(vf_i_fnts,font_max); for font_k := font_base to font_max do begin pdf_font_num[font_k] := 0; pdf_font_included[font_k] := false; pdf_font_type[font_k] := new_font_type; end; undump_things(font_check[null_font], font_ptr+1-null_font); undump_things(font_size[null_font], font_ptr+1-null_font); undump_things(font_dsize[null_font], font_ptr+1-null_font); undump_checked_things(min_halfword, max_halfword, font_params[null_font], font_ptr+1-null_font); undump_things(hyphen_char[null_font], font_ptr+1-null_font); undump_things(skew_char[null_font], font_ptr+1-null_font); undump_upper_check_things(str_ptr, font_name[null_font], font_ptr+1-null_font); undump_upper_check_things(str_ptr, font_area[null_font], font_ptr+1-null_font); {There's no point in checking these values against the range $[0,255]$, since the data type is |unsigned char|, and all values of that type are in that range by definition.} undump_things(font_bc[null_font], font_ptr+1-null_font); undump_things(font_ec[null_font], font_ptr+1-null_font); undump_things(char_base[null_font], font_ptr+1-null_font); undump_things(width_base[null_font], font_ptr+1-null_font); undump_things(height_base[null_font], font_ptr+1-null_font); undump_things(depth_base[null_font], font_ptr+1-null_font); undump_things(italic_base[null_font], font_ptr+1-null_font); undump_things(lig_kern_base[null_font], font_ptr+1-null_font); undump_things(kern_base[null_font], font_ptr+1-null_font); undump_things(exten_base[null_font], font_ptr+1-null_font); undump_things(param_base[null_font], font_ptr+1-null_font); undump_checked_things(min_halfword, lo_mem_max, font_glue[null_font], font_ptr+1-null_font); undump_checked_things(0, fmem_ptr-1, bchar_label[null_font], font_ptr+1-null_font); undump_checked_things(min_quarterword, non_char, font_bchar[null_font], font_ptr+1-null_font); undump_checked_things(min_quarterword, non_char, font_false_bchar[null_font], font_ptr+1-null_font); end @z @x dump_int(hyph_count); for k:=0 to hyph_size do if hyph_word[k]<>0 then begin dump_int(k); dump_int(hyph_word[k]); dump_int(hyph_list[k]); end; @y dump_int(hyph_count); if hyph_next <= hyph_prime then hyph_next:=hyph_size; dump_int(hyph_next);{minumum value of |hyphen_size| needed} for k:=0 to hyph_size do if hyph_word[k]<>0 then begin dump_int(k+65536*hyph_link[k]); {assumes number of hyphen exceptions does not exceed 65535} dump_int(hyph_word[k]); dump_int(hyph_list[k]); end; @z @x for k:=0 to trie_max do dump_hh(trie[k]); dump_int(trie_op_ptr); for k:=1 to trie_op_ptr do begin dump_int(hyf_distance[k]); dump_int(hyf_num[k]); dump_int(hyf_next[k]); end; @y dump_things(trie_trl[0], trie_max+1); dump_things(trie_tro[0], trie_max+1); dump_things(trie_trc[0], trie_max+1); dump_int(trie_op_ptr); dump_things(hyf_distance[1], trie_op_ptr); dump_things(hyf_num[1], trie_op_ptr); dump_things(hyf_next[1], trie_op_ptr); @z @x @= @y {This is only used for the hyphenation tries below, and the size is always |j+1|.} @d xmalloc_and_undump(#) == if not # then xmalloc_array(#, j+1); undump_things(#[0], j+1); @= @z @x undump(0)(hyph_size)(hyph_count); for k:=1 to hyph_count do begin undump(0)(hyph_size)(j); undump(0)(str_ptr)(hyph_word[j]); undump(min_halfword)(max_halfword)(hyph_list[j]); end; @y undump_size(0)(hyph_size)('hyph_size')(hyph_count); undump_size(hyph_prime)(hyph_size)('hyph_size')(hyph_next); j:=0; for k:=1 to hyph_count do begin undump_int(j); if j<0 then goto bad_fmt; if j>65535 then begin hyph_next:= j div 65536; j:=j - hyph_next * 65536; end else hyph_next:=0; if (j>=hyph_size)or(hyph_next>hyph_size) then goto bad_fmt; hyph_link[j]:=hyph_next; undump(0)(str_ptr)(hyph_word[j]); undump(min_halfword)(max_halfword)(hyph_list[j]); end; {|j| is now the largest occupied location in |hyph_word|} incr(j); if j= hyph_size then hyph_next:=hyph_prime else if hyph_next >= hyph_prime then incr(hyph_next); @z @x for k:=0 to j do undump_hh(trie[k]); undump_size(0)(trie_op_size)('trie op size')(j); @+init trie_op_ptr:=j;@+tini for k:=1 to j do begin undump(0)(63)(hyf_distance[k]); {a |small_number|} undump(0)(63)(hyf_num[k]); undump(min_quarterword)(max_quarterword)(hyf_next[k]); end; @y {These first three haven't been allocated yet unless we're \.{INITEX}; we do that precisely so we don't allocate more space than necessary.} xmalloc_and_undump(trie_trl); xmalloc_and_undump(trie_tro); xmalloc_and_undump(trie_trc); undump_size(0)(trie_op_size)('trie op size')(j); @+init trie_op_ptr:=j;@+tini {I'm not sure we have such a strict limitation (64) on these values, so let's leave them unchecked.} undump_things(hyf_distance[1], j); undump_things(hyf_num[1], j); undump_upper_check_things(max_quarterword, hyf_next[1], j); @z @x if (x<>69069)or eof(fmt_file) then goto bad_fmt @y if (x<>69069)or feof(fmt_file) then goto bad_fmt @z @x print(" (preloaded format="); print(job_name); print_char(" "); @y print(" (format="); print(job_name); print_char(" "); @z @x @p begin @!{|start_here|} @y @d const_chk(#)==begin if # < inf_@&# then # := inf_@&# else if # > sup_@&# then # := sup_@&# end {|setup_bound_var| stuff duplicated in \.{mf.ch}.} @d setup_bound_var(#)==bound_default:=#; setup_bound_var_end @d setup_bound_var_end(#)==bound_name:=#; setup_bound_var_end_end @d setup_bound_var_end_end(#)== setup_bound_variable(address_of(#), bound_name, bound_default); @p procedure main_body; begin @!{|start_here|} {Bounds that may be set from the configuration file. We want the user to be able to specify the names with underscores, but \.{TANGLE} removes underscores, so we're stuck giving the names twice, once as a string, once as the identifier. How ugly.} setup_bound_var (250000)('main_memory')(main_memory); {|memory_word|s for |mem| in \.{INITEX}} setup_bound_var (0)('extra_mem_top')(extra_mem_top); {increase high mem in \.{VIRTEX}} setup_bound_var (0)('extra_mem_bot')(extra_mem_bot); {increase low mem in \.{VIRTEX}} setup_bound_var (100000)('pool_size')(pool_size); setup_bound_var (75000)('string_vacancies')(string_vacancies); setup_bound_var (5000)('pool_free')(pool_free); {min pool avail after fmt} setup_bound_var (15000)('max_strings')(max_strings); setup_bound_var (100000)('font_mem_size')(font_mem_size); setup_bound_var (500)('font_max')(font_max); setup_bound_var (20000)('trie_size')(trie_size); {if |ssup_trie_size| increases, recompile} setup_bound_var (659)('hyph_size')(hyph_size); setup_bound_var (3000)('buf_size')(buf_size); setup_bound_var (50)('nest_size')(nest_size); setup_bound_var (15)('max_in_open')(max_in_open); setup_bound_var (60)('param_size')(param_size); setup_bound_var (4000)('save_size')(save_size); setup_bound_var (300)('stack_size')(stack_size); setup_bound_var (16384)('dvi_buf_size')(dvi_buf_size); setup_bound_var (79)('error_line')(error_line); setup_bound_var (50)('half_error_line')(half_error_line); setup_bound_var (79)('max_print_line')(max_print_line); setup_bound_var (0)('hash_extra')(hash_extra); const_chk (main_memory); @+init if ini_version then begin extra_mem_top := 0; extra_mem_bot := 0; end; @+tini if extra_mem_bot>mem_bot then extra_mem_bot:=mem_bot; if extra_mem_bot>sup_main_memory then extra_mem_bot:=sup_main_memory; if extra_mem_top>sup_main_memory then extra_mem_top:=sup_main_memory; mem_top := mem_bot + main_memory; mem_min := mem_bot; mem_max := mem_top; {Check other constants against their sup and inf.} const_chk (trie_size); const_chk (hyph_size); const_chk (buf_size); const_chk (nest_size); const_chk (max_in_open); const_chk (param_size); const_chk (save_size); const_chk (stack_size); const_chk (dvi_buf_size); const_chk (pool_size); const_chk (string_vacancies); const_chk (pool_free); const_chk (max_strings); const_chk (font_mem_size); const_chk (font_max); const_chk (hash_extra); if error_line > ssup_error_line then error_line := ssup_error_line; {array memory allocation} xmalloc_array (buffer, buf_size); xmalloc_array (nest, nest_size); xmalloc_array (save_stack, save_size); xmalloc_array (input_stack, stack_size); xmalloc_array (input_file, max_in_open); xmalloc_array (line_stack, max_in_open); xmalloc_array (param_stack, param_size); xmalloc_array (dvi_buf, dvi_buf_size); xmalloc_array (hyph_word , hyph_size); xmalloc_array (hyph_list , hyph_size); xmalloc_array (hyph_link , hyph_size); @+init if ini_version then begin xmalloc_array (yzmem, mem_top - mem_bot); zmem := yzmem - mem_bot; {Some compilers require |mem_bot=0|} eqtb_top := eqtb_size+hash_extra; if hash_extra=0 then hash_top:=undefined_control_sequence else hash_top:=eqtb_top; xmalloc_array (yhash,1+hash_top-hash_offset); hash:=yhash - hash_offset; {Some compilers require |hash_offset=0|} next(hash_base):=0; text(hash_base):=0; for hash_used:=hash_base+1 to hash_top do hash[hash_used]:=hash[hash_base]; xmalloc_array (zeqtb, eqtb_top); eqtb:=zeqtb; xmalloc_array (str_start, max_strings); xmalloc_array (str_pool, pool_size); xmalloc_array (font_info, font_mem_size); end; @+tini @z @x @!init if not get_strings_started then goto final_end; init_prim; {call |primitive| for each primitive} init_str_ptr:=str_ptr; init_pool_ptr:=pool_ptr; fix_date_and_time; tini@/ @y @+init if ini_version then begin if not get_strings_started then goto final_end; init_prim; {call |primitive| for each primitive} init_str_ptr:=str_ptr; init_pool_ptr:=pool_ptr; fix_date_and_time; end; @+tini@/ @z @x end_of_TEX: close_files_and_terminate; final_end: ready_already:=0; end. @y close_files_and_terminate; final_end: do_final_end; end {|main_body|}; @z @x procedure close_files_and_terminate; var k:integer; {all-purpose index} begin @; @!stat if tracing_stats>0 then @;@;@+tats@/ wake_up_terminal; @; @y procedure close_files_and_terminate; label done, done1; var a, b, c, i, j, k, l:integer; {all-purpose index} is_root: boolean; {|pdf_last_pages| is root of Pages tree?} root, outlines, threads, name_tree: pointer; begin @; @!stat if tracing_stats>0 then @;@;@+tats@/ wake_up_terminal; if get_pdf_output > 0 then begin if history = fatal_error_stop then print_err(" ==> Fatal error occurred, the output PDF file not finished!") else begin @; end; end else begin @; end; @z @x slow_print(log_name); print_char("."); end; end; @y slow_print(log_name); print_char("."); end; end; print_ln; if (edit_name_start<>0) and (interaction>batch_mode) then call_edit(str_pool,edit_name_start,edit_name_length,edit_line); @z @x wlog_ln(' ',cs_count:1,' multiletter control sequences out of ', hash_size:1);@/ @y wlog_ln(' ',cs_count:1,' multiletter control sequences out of ', hash_size:1, '+', hash_extra:1);@/ @z @x begin @!init for c:=top_mark_code to split_bot_mark_code do if cur_mark[c]<>null then delete_token_ref(cur_mark[c]); store_fmt_file; return;@+tini@/ @y begin @!init if ini_version then begin for c:=top_mark_code to split_bot_mark_code do if cur_mark[c]<>null then delete_token_ref(cur_mark[c]); store_fmt_file; return; end;@+tini@/ @z @x w_close(fmt_file); @y w_close(fmt_file); eqtb:=zeqtb; @z @x fix_date_and_time;@/ @y if mltex_enabled_p then begin wterm_ln('MLTeX v2.2 enabled'); end; fix_date_and_time;@/ ifdef ('notdef'); {TCX files are probably a bad idea.} {If a character translation file changed anything $\ldots$ Have to do this after initializing the string pool, since we're rearranging it.} if chars_saved_by_char_set > 0 then begin {Now for the fun part. We want users to be able to specify which characters are printable dynamically---generally, we can't assume that eight-bit characters are printable, but many character sets (ISO Latin 1, ISO Latin 2) do use them. In the original code, the first 256 strings are generated once, at \.{INITEX}-time, using module 49. Well, now we have to regenerate the first 256 strings at the beginning of every run (if we're not using the default character set).} {Save the variables we're going to mess with.} save_str_ptr := str_ptr; save_pool_ptr := pool_ptr; {\TeX\ assumes in many places that string |s| are those characters in |str_pool| which lie between |str_start[s]| and |str_start[s+1]|. So the characters in the string pool must remain consecutive; for example, the last character of string 256 (which is the representation of character \.{0xff}, which may be either a single byte, or the default four characters \.{^^ff}) must come right before the first character of string 257 (which happens to be the word |"buffer"|, from the buffer overflow a few sections previous). The dumped string pool always contains the same first 256 strings; it's not controllable by the user. {\it And} (this is crucial), a character translation \.{.tcx} file can only {\it decrease} the number of characters used, never increase. This is a consequence of the fact that we do not allow \.{.tcx} files to specify certain characters are {\it un\/}printable; therefore, the only changes the user can make will be ones that change a representation from four characters (\.{^^ff}) to one (a single byte with all bits turned on). Conclusion: we can accomodate \TeX's assumptions by regenerating the first 256 strings, starting (in |str_pool|) at the precise index which is the number of characters the character set saved us, and by resetting |str_start[0]| to that position. (\TeX\ does not assume |str_start[0]=0|.) This way, we do not have to touch the bulk of the strings (some 24K), and reshuffle every |str_start|. Fun, huh?} str_ptr := 0; pool_ptr := chars_saved_by_charset; {computed by |setup_char_set|} str_start[0] := pool_ptr; @; {Restore the variables.} str_ptr := save_str_ptr; pool_ptr := save_pool_ptr; end; endif('notdef'); @!init if trie_not_ready then begin {initex without format loaded} xmalloc_array (trie_trl, trie_size); xmalloc_array (trie_tro, trie_size); xmalloc_array (trie_trc, trie_size); xmalloc_array (trie_c, trie_size); xmalloc_array (trie_o, trie_size); xmalloc_array (trie_l, trie_size); xmalloc_array (trie_r, trie_size); xmalloc_array (trie_hash, trie_size); xmalloc_array (trie_taken, trie_size); trie_root:=0; trie_c[0]:=si(0); trie_ptr:=0; {Allocate and initialize font arrays} xmalloc_array(font_check, font_max); xmalloc_array(font_size, font_max); xmalloc_array(font_dsize, font_max); xmalloc_array(font_params, font_max); xmalloc_array(font_name, font_max); xmalloc_array(font_area, font_max); xmalloc_array(font_bc, font_max); xmalloc_array(font_ec, font_max); xmalloc_array(font_glue, font_max); xmalloc_array(hyphen_char, font_max); xmalloc_array(skew_char, font_max); xmalloc_array(bchar_label, font_max); xmalloc_array(font_bchar, font_max); xmalloc_array(font_false_bchar, font_max); xmalloc_array(char_base, font_max); xmalloc_array(width_base, font_max); xmalloc_array(height_base, font_max); xmalloc_array(depth_base, font_max); xmalloc_array(italic_base, font_max); xmalloc_array(lig_kern_base, font_max); xmalloc_array(kern_base, font_max); xmalloc_array(exten_base, font_max); xmalloc_array(param_base, font_max); xmalloc_array(pdf_font_num,font_max); xmalloc_array(pdf_fp_base,font_max); xmalloc_array(pdf_font_included,font_max); xmalloc_array(pdf_font_type,font_max); xmalloc_array(fm_tab,font_max); xmalloc_array(vf_packet_base,font_max); xmalloc_array(vf_default_font,font_max); xmalloc_array(vf_e_fnts,font_max); xmalloc_array(vf_i_fnts,font_max); for font_k := font_base to font_max do begin pdf_font_num[font_k] := 0; pdf_font_included[font_k] := false; pdf_font_type[font_k] := new_font_type; end; font_ptr:=null_font; fmem_ptr:=7; font_name[null_font]:="nullfont"; font_area[null_font]:=""; hyphen_char[null_font]:="-"; skew_char[null_font]:=-1; bchar_label[null_font]:=non_address; font_bchar[null_font]:=non_char; font_false_bchar[null_font]:=non_char; font_bc[null_font]:=1; font_ec[null_font]:=0; font_size[null_font]:=0; font_dsize[null_font]:=0; char_base[null_font]:=0; width_base[null_font]:=0; height_base[null_font]:=0; depth_base[null_font]:=0; italic_base[null_font]:=0; lig_kern_base[null_font]:=0; kern_base[null_font]:=0; exten_base[null_font]:=0; font_glue[null_font]:=null; font_params[null_font]:=7; param_base[null_font]:=-1; for font_k:=0 to 6 do font_info[font_k].sc:=0; end; tini@/ xmalloc_array (font_used, font_max); for font_k:=font_base to font_max do font_used[font_k]:=false; @z @x begin goto breakpoint;@\ {go to every label at least once} breakpoint: m:=0; @{'BREAKPOINT'@}@\ end @y dump_core {do something to cause a core dump} @z @x 5: print_word(font_info[n]); @y 5: begin print_scaled(font_info[n].sc); print_char(" ");@/ print_int(font_info[n].qqqq.b0); print_char(":");@/ print_int(font_info[n].qqqq.b1); print_char(":");@/ print_int(font_info[n].qqqq.b2); print_char(":");@/ print_int(font_info[n].qqqq.b3); end; @z @x @d immediate_code=4 {command modifier for \.{\\immediate}} @d set_language_code=5 {command modifier for \.{\\setlanguage}} @y @d immediate_code=4 {command modifier for \.{\\immediate}} @d set_language_code=5 {command modifier for \.{\\setlanguage}} @d pdf_literal_node == 6 @d pdf_annot_text_node == 7 @d pdf_annot_link_node == 8 @d pdf_end_link_node == 9 @d pdf_outline_code == 10 @d pdf_dest_node == 11 @d pdf_thread_node == 12 @d pdf_end_thread_node == 13 @d pdf_image_node == 14 @d pdf_form_code == 15 @d pdf_ref_form_node == 16 @d pdf_info_code == 17 @d pdf_catalog_code == 18 @z @x primitive("setlanguage",extension,set_language_code);@/ @!@:set_language_}{\.{\\setlanguage} primitive@> @y primitive("setlanguage",extension,set_language_code);@/ @!@:set_language_}{\.{\\setlanguage} primitive@> primitive("pdfliteral",extension,pdf_literal_node);@/ @!@:pdf_literal_}{\.{\\pdfliteral} primitive@> primitive("pdfannottext",extension,pdf_annot_text_node);@/ @!@:pdf_annot_text_}{\.{\\pdfannottext} primitive@> primitive("pdfannotlink",extension,pdf_annot_link_node);@/ @!@:pdf_annot_link_}{\.{\\pdfannotlink} primitive@> primitive("pdfendlink",extension,pdf_end_link_node);@/ @!@:pdf_end_annot_}{\.{\\pdfendlink} primitive@> primitive("pdfoutline",extension,pdf_outline_code);@/ @!@:pdf_outline_}{\.{\\pdfoutline} primitive@> primitive("pdfdest",extension,pdf_dest_node);@/ @!@:pdf_dest_}{\.{\\pdfdest} primitive@> primitive("pdfthread",extension,pdf_thread_node);@/ @!@:pdf_thread_}{\.{\\pdfthread} primitive@> primitive("pdfendthread",extension,pdf_end_thread_node);@/ @!@:pdf_end_thread_}{\.{\\pdfendthread} primitive@> primitive("pdfimage",extension,pdf_image_node);@/ @!@:pdf_image_}{\.{\\pdfimage} primitive@> primitive("pdfform",extension,pdf_form_code);@/ @!@:pdf_form_}{\.{\\pdfform} primitive@> primitive("pdfrefform",extension,pdf_ref_form_node);@/ @!@:pdf_ref_form_}{\.{\\pdfrefform} primitive@> primitive("pdfinfo",extension,pdf_info_code);@/ @!@:pdf_info_}{\.{\\pdfinfo} primitive@> primitive("pdfcatalog",extension,pdf_catalog_code);@/ @!@:pdf_catalog_}{\.{\\pdfcatalog} primitive@> @z @x set_language_code:print_esc("setlanguage"); othercases print("[unknown extension!]") @y set_language_code: print_esc("setlanguage"); pdf_literal_node: print_esc("pdfliteral"); pdf_annot_text_node: print_esc("pdfannottext"); pdf_annot_link_node: print_esc("pdfannotlink"); pdf_end_link_node: print_esc("pdfendlink"); pdf_outline_code: print_esc("pdfoutline"); pdf_dest_node: print_esc("pdfdest"); pdf_thread_node: print_esc("pdfthread"); pdf_end_thread_node: print_esc("pdfendthread"); pdf_image_node: print_esc("pdfimage"); pdf_form_code: print_esc("pdfform"); pdf_ref_form_node: print_esc("pdfrefform"); pdf_info_code: print_esc("pdfinfo"); pdf_catalog_code: print_esc("pdfcatalog"); othercases print("[unknown extension!]") @z @x set_language_code:@; othercases confusion("ext1") @y set_language_code: @; pdf_literal_node: @; pdf_annot_text_node: @; pdf_annot_link_node: @; pdf_end_link_node: @; pdf_outline_code: @; pdf_dest_node: @; pdf_thread_node: @; pdf_end_thread_node: @; pdf_image_node: @; pdf_form_code: @; pdf_ref_form_node: @; pdf_info_code: @; pdf_catalog_code: @; othercases confusion("ext1") @z @x @= procedure new_write_whatsit(@!w:small_number); @y @= procedure pdf_refuse(s: str_number); begin print_err(""); print_esc(s); print(" used when \pdfoutput is zero"); help3 ("You have tried to use a pdf-specific command without activating")@/ ("pdf output; this doesn't make any sense, so I am going to")@/ ("ignore the command and try to skip its parameters");@/ error; pdf_ignore := true; end; procedure new_annot_whatsit(w, s: small_number); {creat a new whatsit node for annotation} label restart, reswitch; var p: pointer; begin new_whatsit(w, s); pdf_width(tail) := null_flag; pdf_depth(tail) := null_flag; pdf_height(tail) := null_flag; reswitch: if scan_keyword("width") then begin scan_normal_dimen; pdf_width(tail):=cur_val; goto reswitch; end; if scan_keyword("height") then begin scan_normal_dimen; pdf_height(tail):=cur_val; goto reswitch; end; if scan_keyword("depth") then begin scan_normal_dimen; pdf_depth(tail):=cur_val; goto reswitch; end; if scan_keyword("attr") then begin p := scan_toks(false, true); pdf_annot_attr(tail) := def_ref; end else pdf_annot_attr(tail) := null; end; function outline_list_count(p: pointer): integer; {return number of outline entries in the same level with |p|} var k: integer; begin k := 1; while obj_outline_prev(p) <> 0 do begin incr(k); p := obj_outline_prev(p); end; outline_list_count := k; end; procedure pdf_remove_tail; begin flush_node_list(tail); tail := save_tail; link(tail) := null; end; procedure pdf_read_info; label reswitch; var p: pointer; begin reswitch: if scan_keyword("author") then begin p := scan_toks(false, true); pdf_info_author := tokens_to_string(def_ref); delete_token_ref(def_ref); goto reswitch; end; if scan_keyword("title") then begin p := scan_toks(false, true); pdf_info_title := tokens_to_string(def_ref); delete_token_ref(def_ref); goto reswitch; end; if scan_keyword("subject") then begin p := scan_toks(false, true); pdf_info_subject := tokens_to_string(def_ref); delete_token_ref(def_ref); goto reswitch; end; if scan_keyword("keywords") then begin p := scan_toks(false, true); pdf_info_keywords := tokens_to_string(def_ref); delete_token_ref(def_ref); goto reswitch; end; end; procedure pdf_read_catalog; label reswitch; var p: pointer; begin reswitch: if scan_keyword("pagemode") then begin p := scan_toks(false, true); pdf_catalog_pagemode := tokens_to_string(def_ref); delete_token_ref(def_ref); goto reswitch; end; if scan_keyword("uri") then begin p := scan_toks(false, true); pdf_catalog_uri := tokens_to_string(def_ref); delete_token_ref(def_ref); goto reswitch; end; end; procedure new_write_whatsit(@!w:small_number); @z @x else if cur_val>15 then cur_val:=16; @y else if (cur_val>15) and (cur_val <> 18) then cur_val:=16; @z @x @ @= @y @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfliteral"); new_whatsit(pdf_literal_node, write_node_size); write_stream(tail) := null; p := scan_toks(false, true); write_tokens(tail) := def_ref; if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfannottext"); new_annot_whatsit(pdf_annot_text_node, pdf_annot_text_node_size); p := scan_toks(false, true); pdf_annot_text_title(tail) := def_ref; if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfannotlink"); new_annot_whatsit(pdf_annot_link_node, pdf_annot_link_node_size); p := get_node(pdf_action_struct_size); read_action_struct(p); pdf_annot_link_action(tail) := p; pdf_annot_link_next(tail) := null; if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfendlink"); new_whatsit(pdf_end_link_node, small_node_size); if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfoutline"); p := get_node(pdf_action_struct_size); read_action_struct(p); if scan_keyword("count") then begin scan_int; k := cur_val; end else k := 0; q := scan_toks(false, true); if pdf_ignore then begin free_action_struct(p); free_node(p, pdf_action_struct_size); return; end; pdf_new_obj(pdf_others, 0); write_action_struct(p); pdf_end_obj; free_action_struct(p); free_node(p, pdf_action_struct_size); pdf_create_obj(pdf_outline, 0); r := obj_count; obj_outline_action_objnum(r) := obj_count - 1; obj_outline_ptr(r) := pdf_get_mem(pdf_outline_struct_size); obj_outline_count(r) := k; pdf_new_obj(pdf_others, 0); pdf_print_str_nl(tokens_to_string(def_ref)); pdf_end_obj; delete_token_ref(def_ref); flush_string; obj_outline_title(r) := obj_count; obj_outline_prev(r) := null; obj_outline_next(r) := null; obj_outline_first(r) := null; obj_outline_last(r) := null; obj_outline_parent(r) := parent_outline; if first_outline = null then first_outline := r; if last_outline = null then begin if parent_outline <> null then obj_outline_first(parent_outline) := r; end else begin obj_outline_next(last_outline) := r; obj_outline_prev(r) := last_outline; end; last_outline := r; if obj_outline_count(r) <> 0 then begin parent_outline := r; last_outline := null; end else if (parent_outline <> null) and (outline_list_count(r) = abs(obj_outline_count(parent_outline))) then begin p := last_outline; repeat obj_outline_last(parent_outline) := p; p := parent_outline; parent_outline := obj_outline_parent(parent_outline); until (parent_outline = null) or (outline_list_count(p) < abs(obj_outline_count(parent_outline))); if parent_outline = null then last_outline := first_outline else last_outline := obj_outline_first(parent_outline); while obj_outline_next(last_outline) <> null do last_outline := obj_outline_next(last_outline); end; end @ Notice that |scan_keyword| doesn't care if two words have same prefix; so we should be careful when scan keywords with same prefix. The main rule: if there are two or more keywords with the same prefix, then always test in order from the longest one to the shortest one. @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfdest"); new_whatsit(pdf_dest_node, pdf_dest_node_size); if scan_keyword("num") then begin pdf_dest_id_type(tail) := pdf_id_num; scan_int; if cur_val < 0 then pdf_confusion("ext1", "num identifier can't be negative"); pdf_dest_num(tail) := cur_val; end else if scan_keyword("name") then begin pdf_dest_id_type(tail) := pdf_id_name; p := scan_toks(false, true); pdf_dest_name(tail) := def_ref; end else pdf_confusion("ext1", "you must specify identifier type"); if scan_keyword("xyz") then begin pdf_dest_type(tail) := pdf_dest_xyz; if scan_keyword("zoom") then begin scan_int; pdf_dest_xyz_zoom(tail) := cur_val; end else pdf_dest_xyz_zoom(tail) := 0; end else if scan_keyword("fitbh") then pdf_dest_type(tail) := pdf_dest_fitb_h else if scan_keyword("fitbv") then pdf_dest_type(tail) := pdf_dest_fitb_v else if scan_keyword("fitb") then pdf_dest_type(tail) := pdf_dest_fitb else if scan_keyword("fith") then pdf_dest_type(tail) := pdf_dest_fit_h else if scan_keyword("fitv") then pdf_dest_type(tail) := pdf_dest_fit_v else if scan_keyword("fit") then pdf_dest_type(tail) := pdf_dest_fit else pdf_confusion("ext1", "you must specify destination type"); if not pdf_ignore then p := find_identifier(find_goto_dest, tail); pdf_dest_objnum(tail) := p; obj_dest_ptr(p) := tail; if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfthread"); new_whatsit(pdf_thread_node, pdf_thread_node_size); if scan_keyword("num") then begin pdf_thread_id_type(tail) := pdf_id_num; scan_int; if cur_val < 0 then pdf_confusion("ext1", "num identifier can't be negative"); pdf_thread_num(tail) := cur_val; end else if scan_keyword("name") then begin pdf_thread_id_type(tail) := pdf_id_name; p := scan_toks(false, true); pdf_thread_name(tail) := def_ref; end else pdf_confusion("ext1", "you must specify identifier type"); if pdf_ignore then pdf_remove_tail else last_thread := find_identifier(find_thread_dest, tail); end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfendthread"); new_whatsit(pdf_end_thread_node, small_node_size); if pdf_ignore then pdf_remove_tail; end @ For the \.{\\pdfimage} primitive we need extract immediately information about image as width and height. The images in page will be written after the content of page is shipout. |pdf_image_list| and |pdf_form_list| hold the list of images and forms in the current page. @d image_file == t1_file {use |t1_file| for image reading} @= procedure scale_image(p: pointer); {scale the size of box containing image to indeed size of image} var image_box: pointer; {pointer to box containing image} x, y, xr, yr: integer; {size and resolution of image} w, h: scaled; {indeed size corresponds to image resolution} a, b: scaled; {accumulators} begin image_box := pdf_image_box(p); x := image_width(pdf_image_info(p)); y := image_height(pdf_image_info(p)); xr := image_x_res(pdf_image_info(p)); yr := image_y_res(pdf_image_info(p)); if (width(image_box) < 0) or (height(image_box) < 0) or (x <= 0) or (y <= 0) or (xr < 0) or (yr < 0) or (x > 65536) or (y > 65536) or (xr > 65536) or (yr > 65536) then pdf_confusion("ext1", "invalid dimension while scaling image"); if (xr > 0) and (yr > 0) then begin {calculates w = (722700*x)/(xr*254) and w = (722700*y)/(yr*254)} a := xn_over_d(361350, x, xr); b := xn_over_d(unity, remainder, xr); w := xn_over_d(a, unity, 127) + x_over_n(b, 127); a := xn_over_d(361350, y, yr); b := xn_over_d(unity, remainder, yr); h := xn_over_d(a, unity, 127) + x_over_n(b, 127); end else begin w := xn_over_d(unity*x, 7227, 7200); h := xn_over_d(unity*y, 7227, 7200); end; if (width(image_box) = 0) and (height(image_box) = 0) then begin width(image_box) := w; height(image_box) := h; end else if (width(image_box) = 0) and (height(image_box) > 0) then width(image_box) := xn_over_d(height(image_box), x, y) else if (width(image_box) > 0) and (height(image_box) = 0) then height(image_box) := xn_over_d(width(image_box), y, x); end; procedure append_image_box; {insert a hbox containing an image to current list} label reswitch; var p, q: pointer; begin p:=new_null_box; tail_append(p); q:=get_node(pdf_image_node_size); type(q):=whatsit_node; subtype(q):=pdf_image_node; list_ptr(p) := q; pdf_image_box(q) := p; reswitch: if scan_keyword("width") then begin scan_normal_dimen; width(p):=cur_val; goto reswitch; end; if scan_keyword("height") then begin scan_normal_dimen; height(p):=cur_val; goto reswitch; end; if scan_keyword("depth") then begin scan_normal_dimen; goto reswitch; {ignore depth specification} end; scan_file_name; {set |cur_name| to desired file name} pack_cur_name; if not texpsheader_b_open_in(t1_file) then begin print_nl("Error in processing image file ("); print_file_name(cur_name,cur_area,cur_ext); print("): "); print("cannot open image file for reading"); update_terminal; fatal_error("Can't handle image file"); return; end; pdf_image_info(q) := read_image_info(image_file); scale_image(q); end; procedure append_form_box; {insert a hbox containing a form to current list} var p, q, r: pointer; begin p:=new_null_box; tail_append(p); q:=get_node(pdf_ref_form_node_size); type(q):=whatsit_node; subtype(q):=pdf_ref_form_node; list_ptr(p) := q; pdf_form_box(q) := p; scan_int; r := head_tab[pdf_form]; while (r <> null) and (r <> cur_val) do r := obj_link(r); if r = null then pdf_confusion("ext1", "can't find form"); pdf_form_objnum(q) := cur_val; width(p) := obj_form_width(cur_val); height(p) := obj_form_height(cur_val); depth(p) := obj_form_depth(cur_val); if (width(p) < 0) or (height(p) < 0) or (depth(p) < 0) then pdf_confusion("ext1", "negative dimension of box with `\pdfrefform'"); end; @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfimage"); append_image_box; if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfform"); scan_eight_bit_int; if pdf_ignore then return; pdf_shipping_form := true; pdf_ship_out(box(cur_val)); box(cur_val) := null; pdf_shipping_form := false; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfrefform"); append_form_box; if pdf_ignore then pdf_remove_tail; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("pdfinfo"); pdf_read_info; end @ @= begin save_tail := tail; if get_pdf_output <= 0 then pdf_refuse("catalog"); pdf_read_catalog; end @ @= @z @x othercases print("whatsit?") @y pdf_literal_node: print_esc("pdfliteral"); pdf_annot_text_node: print_esc("pdfannottext"); pdf_annot_link_node: print_esc("pdfannotlink"); pdf_end_link_node: print_esc("pdfendlink"); pdf_dest_node: print_esc("pdfdest"); pdf_thread_node: print_esc("pdfthread"); pdf_end_thread_node: print_esc("pdfendthread"); pdf_image_node: print_esc("pdfimage"); pdf_ref_form_node: print_esc("pdfrefform"); othercases print("whatsit?") @z @x othercases confusion("ext2") @y pdf_literal_node: begin r := get_node(write_node_size); add_token_ref(write_tokens(p)); words := write_node_size; end; pdf_annot_text_node: begin r := get_node(pdf_annot_text_node_size); add_token_ref(pdf_annot_text_title(p)); if pdf_annot_attr(p) <> null then add_token_ref(pdf_annot_attr(p)); words := pdf_annot_text_node_size; end; pdf_annot_link_node: begin r := get_node(pdf_annot_link_node_size); copy_annot_link_node(p, r); end; pdf_end_link_node: begin r := get_node(small_node_size); words := small_node_size; end; pdf_dest_node: begin r := get_node(pdf_dest_node_size); if pdf_dest_id_type(p) = pdf_id_name then add_token_ref(pdf_dest_name(p)); words := pdf_dest_node_size; end; pdf_thread_node: begin r := get_node(pdf_thread_node_size); if pdf_thread_id_type(p) = pdf_id_name then add_token_ref(pdf_thread_name(p)); words := pdf_thread_node_size; end; pdf_end_thread_node: begin r := get_node(small_node_size); words := small_node_size; end; pdf_image_node: begin r := get_node(pdf_image_node_size); add_image_ref(pdf_image_info(p)); words := pdf_image_node_size; end; pdf_ref_form_node: begin r := get_node(pdf_ref_form_node_size); words := pdf_ref_form_node_size; end; othercases confusion("ext2") @z @x othercases confusion("ext3") @y pdf_literal_node: begin delete_token_ref(write_tokens(p)); free_node(p, write_node_size); end; pdf_annot_text_node: begin delete_token_ref(pdf_annot_text_title(p)); if pdf_annot_attr(p) <> null then delete_token_ref(pdf_annot_attr(p)); free_node(p, pdf_annot_text_node_size); end; pdf_annot_link_node: if link_box_depth = -1 then begin if pdf_annot_attr(p) <> null then delete_token_ref(pdf_annot_attr(p)); free_action_struct(pdf_annot_link_action(p)); free_node(pdf_annot_link_action(p), pdf_action_struct_size); free_annot_link_node(p); end; pdf_end_link_node: free_node(p, small_node_size); pdf_dest_node: begin if pdf_dest_id_type(p) = pdf_id_name then delete_token_ref(pdf_dest_name(p)); free_node(p, pdf_dest_node_size); end; pdf_thread_node: begin if pdf_thread_id_type(p) = pdf_id_name then delete_token_ref(pdf_thread_name(p)); free_node(p, pdf_thread_node_size); end; pdf_end_thread_node: free_node(p, small_node_size); pdf_image_node: begin delete_image_ref(pdf_image_info(p)); free_node(p, pdf_image_node_size); end; pdf_ref_form_node: free_node(p, pdf_ref_form_node_size); othercases confusion("ext3") @z @x begin @= for k:=0 to 15 do if write_open[k] then a_close(write_file[k]) @y @ @= for k:=0 to 15 do if write_open[k] then a_close(write_file[k]) @ Shiping out \.{PDF} mark. @= @!pdf_mem_size=65535; {size of the area for \.{PDF} marks} @!max_dest_names=20000; {maximum number of names in name tree of \.{PDF} output file} @ @= dest_name_entry = record objname: str_number; {destination name} objnum: pointer; {destination object number} end; @ @= @!pdf_ignore: boolean; {ignore \.{PDF} primitives?} @!set_pdf_output: integer; {turn on \.{PDF} output if positive} @!set_pdf_page_width: scaled; {page width of \.{PDF} output file} @!set_pdf_page_height: scaled; {page height of \.{PDF} output file} @!save_tail: pointer; {to save the last value of |tail|} @!pdf_mem: array[0..pdf_mem_size] of memory_word; @!pdf_mem_ptr: pointer; @!link_box_depth: integer; {depth of nesting of box containing link annotation} @!thread_box_depth: integer; {depth of nesting of box containing article thread} @!last_link: pointer; {pointer to the last link annotation} @!last_thread: pointer; {pointer to the last thread} @!last_bead: pointer; {pointer to the last bead} @!pdf_link_ht, pdf_link_dp, pdf_link_wd: scaled; {dimensions of the last link annotation} @!pdf_annot_text_list: pointer; {list of text annotations in the current page} @!pdf_annot_link_list: pointer; {list of link annotations in the current page} @!pdf_dest_list: pointer; {list of destinations in the current page} @!pdf_bead_list: pointer; {list of thread beads in the current page} @!pdf_image_list: pointer; {list of pending images} @!image_count: integer; {counter of images} @!pdf_form_list: pointer; {list of pending forms} @!form_count: integer; {counter of forms} @!first_outline, last_outline, parent_outline: pointer; @!pdf_shipping_form: boolean; {|true| if |pdf_ship_out| is being called for shipping form out; |false| for shipping page out} @!pdf_form_width, @!pdf_form_height, @!pdf_form_depth: scaled; {dimension of of current form} @!pdf_info_author, @!pdf_info_title, @!pdf_info_subject, @!pdf_info_keywords: str_number; {additional keys of Info dictionary} @!pdf_catalog_pagemode, @!pdf_catalog_uri: str_number; {additional keys of Catalog dictionary} @!dest_names_ptr: pointer; {first unused position in |dest_names|} @!dest_names: array[0..max_dest_names] of dest_name_entry; @ @= pdf_ignore := false; set_pdf_output := -1; set_pdf_page_width := -1; set_pdf_page_height := -1; pdf_mem_ptr := 0; link_box_depth := -1; thread_box_depth := -1; first_outline:= null; last_outline:= null; parent_outline:= null; pdf_annot_text_list := null; pdf_annot_link_list := null; pdf_dest_list := null; pdf_image_list := null; pdf_form_list := null; image_count := 0; form_count := 0; pdf_shipping_form := false; dest_names_ptr := 0; pdf_info_author := 0; pdf_info_title := 0; pdf_info_subject := 0; pdf_info_keywords := 0; pdf_catalog_pagemode := 0; pdf_catalog_uri := 0; @ @= procedure pdf_literal(p:pointer); var old_setting:0..max_selector; {holds print |selector|} s: str_number; begin old_setting:=selector; selector:=new_string; show_token_list(link(write_tokens(p)),null,pool_size-pool_ptr); selector:=old_setting; pdf_end_text; pdf_set_origin; {pdf_print_nl("% pdfliteral:");} pdf_print_nl(make_string); flush_string; pdf_f := null_font; pdf_font_changed := true; end; procedure ignore_special(p: pointer); begin print_nl("! Text specified by \special ignored"); print_ln; end; procedure pdf_confusion(t, p: str_number); begin print_nl("! PDF error ("); print(t); print("): "); print(p); succumb; end; procedure do_annot_text(p: pointer); var w, h: scaled; {size of annotation} begin if is_running(pdf_width(p)) then w := 0 else w := pdf_width(p); if is_running(pdf_height(p)) then h := 0 else h := pdf_height(p); if not is_running(pdf_depth(p)) then h := h + pdf_depth(p); pdf_left(p) := cur_h; pdf_top(p) := cur_v; pdf_right(p) := pdf_left(p) + w; pdf_bottom(p) := pdf_top(p) + h; pdf_create_obj(pdf_others, 0); obj_annot_text_ptr(obj_count) := p; pdf_annot_text_list := push_ptr(pdf_annot_text_list, obj_count); end; procedure vbox_link_rect(p: pointer; v, h: scaled); var l: pointer; {pointer to whatsit node of last link annotation} begin l := obj_annot_link_ptr(last_link); pdf_left(l) := h; pdf_top(l) := cur_v - pdf_link_ht; if is_running(pdf_link_wd) then pdf_right(l) := h + width(p) else pdf_right(l) := cur_h + pdf_link_wd; if is_running(pdf_link_dp) then pdf_bottom(l) := v + height(p) + depth(p) else pdf_bottom(l) := cur_v + pdf_link_dp; end; procedure hbox_link_rect(p: pointer; v, h: scaled); var l: pointer; {pointer to whatsit node of last link annotation} begin l := obj_annot_link_ptr(last_link); pdf_left(l) := cur_h; if is_running(pdf_link_dp) then pdf_bottom(l) := v + depth(p) else pdf_bottom(l) := v + pdf_link_dp; if is_running(pdf_link_ht) then pdf_top(l) := v - height(p) else pdf_top(l) := v - pdf_link_ht; if is_running(pdf_link_wd) then pdf_right(l) := h + width(p) else pdf_right(l) := cur_h + pdf_link_wd; end; procedure do_annot_link(p, this_box: pointer; v, h: scaled); var d: pointer; {pointer to destination if already exists} a: pointer; {pointer to action structure} begin if link_box_depth <> -1 then pdf_confusion("ext4", "link annotations can't be nested"); pdf_link_ht := pdf_height(p); pdf_link_dp := pdf_depth(p); pdf_link_wd := pdf_width(p); if (type(this_box) = vlist_node) and is_running(pdf_link_ht) then pdf_link_ht := 0; link_box_depth := cur_s; pdf_create_obj(pdf_others, 0); obj_annot_link_ptr(obj_count) := p; last_link := obj_count; if type(this_box) = vlist_node then vbox_link_rect(this_box, v, h) else hbox_link_rect(this_box, v, h); pdf_annot_link_list := push_ptr(pdf_annot_link_list, last_link); end; procedure end_link(this_box: pointer); begin if link_box_depth <> cur_s then pdf_confusion("ext4", "link ends in different nesting level than its start"); if (type(this_box) = vlist_node) and is_running(pdf_link_dp) then pdf_bottom(obj_annot_link_ptr(last_link)) := cur_v; if (type(this_box) = hlist_node) and is_running(pdf_link_wd) then pdf_right(obj_annot_link_ptr(last_link)) := cur_h; link_box_depth := -1; end; procedure append_annot_link(this_box: pointer; v, h: scaled); {append a new |pdf_annot_link_node| to |pdf_annot_link_next(last_link)| and update |last_link|} var p, l : pointer; begin p := get_node(pdf_annot_link_node_size); l := obj_annot_link_ptr(last_link); pdf_width(p) := pdf_width(l); pdf_height(p) := pdf_height(l); pdf_depth(p) := pdf_depth(l); pdf_annot_attr(p) := pdf_annot_attr(l); pdf_annot_link_action(p) := pdf_annot_link_action(l); pdf_annot_link_next(p) := null; pdf_annot_link_next(l) := p; pdf_create_obj(pdf_others, 0); obj_annot_link_ptr(obj_count) := p; last_link := obj_count; if type(this_box) = vlist_node then vbox_link_rect(this_box, v, h) else hbox_link_rect(this_box, v, h); pdf_annot_link_list := push_ptr(pdf_annot_link_list, last_link); end; procedure append_bead(this_box: pointer; v, h: scaled); var b, q, r: pointer; begin r := get_node(pdf_bead_rect_node_size); pdf_left(r) := cur_h; pdf_top(r) := cur_v; pdf_right(r) := h + width(this_box); pdf_bottom(r) := v + height(this_box) + depth(this_box); q := pdf_get_mem(pdf_bead_struct_size); pdf_create_obj(pdf_others, 0); b := obj_count; obj_ptr(b) := q; obj_bead_page(b) := pdf_last_page; obj_bead_rect(b) := r; if obj_thread_first(last_thread) = null then begin obj_thread_first(last_thread) := b; obj_bead_next(b) := b; obj_bead_prev(b) := b; end else begin q := obj_thread_first(last_thread); r := obj_bead_prev(q); obj_bead_prev(b) := r; obj_bead_next(b) := q; obj_bead_prev(q) := b; obj_bead_next(r) := b; end; last_bead := b; pdf_bead_list := push_ptr(pdf_bead_list, b); end; procedure append_dest_name(s: str_number; n: pointer); begin if dest_names_ptr = max_dest_names then overflow("number of destination names", max_dest_names); dest_names[dest_names_ptr].objname := s; dest_names[dest_names_ptr].objnum := n; incr(dest_names_ptr); end; function tokens_to_string(p: integer): str_number; {return a string from tokens list} begin old_setting:=selector; selector:=new_string; show_token_list(link(p),null,pool_size-pool_ptr); selector:=old_setting; tokens_to_string := make_string; end; function find_identifier(context: small_number; p: pointer): pointer; {find a destination/thread object with given identifier, if not exists then creat it. |context| can be: |find_goto_dest|, |find_thread_dest|, |find_goto_action|, |find_thread_action| or |find_page|. |p| is pointer to corresponding node} var d: pointer; {pointer to find object with given identifier} t: small_number; {type of object to find} idtype, idnum, idname: pointer; {registers to read data from |p|} begin case context of find_goto_dest: begin t := pdf_dest; idtype := pdf_dest_id_type(p); idnum := pdf_dest_num(p); idname := pdf_dest_name(p); end; find_goto_action: begin idtype := pdf_action_id_type(p); if idtype = pdf_id_page then t := pdf_page else t := pdf_dest; idnum := pdf_action_num(p); idname := pdf_action_name(p); end; find_thread_dest: begin t := pdf_thread; idtype := pdf_thread_id_type(p); idnum := pdf_thread_num(p); idname := pdf_thread_name(p); end; find_thread_action: begin t := pdf_thread; idtype := pdf_action_id_type(p); idnum := pdf_action_num(p); idname := pdf_action_name(p); end; find_page: begin t := pdf_page; idtype := pdf_id_page; idnum := p; end; endcases; {check if destination already exists} if (id_type = pdf_id_num) or (id_type = pdf_id_page) then d := pdf_find_objnum(t, idnum) else begin d := pdf_find_objname(t, tokens_to_string(idname)); flush_string; end; if d = null then begin {destination not found, creat it} if (id_type = pdf_id_num) or (id_type = pdf_id_page) then pdf_create_obj(t, idnum) else begin pdf_create_obj(t, -tokens_to_string(idname)); if t = pdf_dest then append_dest_name(-obj_info(obj_count), obj_count); end; d := obj_count; obj_ptr(d) := null; {if identifier is number then it must be non-negative; otherwise |-obj_info| is the string number of this name} end; find_identifier := d; end; procedure read_action_struct(p: pointer); {read an action specification} var t: pointer; s: str_number; begin if scan_keyword("goto") then pdf_action_type(p) := pdf_action_goto else if scan_keyword("thread") then pdf_action_type(p) := pdf_action_thread else if scan_keyword("user") then pdf_action_type(p) := pdf_action_user_defined else pdf_confusion("ext1", "you must specify action type"); if (pdf_action_type(p) = pdf_action_goto) or (pdf_action_type(p) = pdf_action_thread) then begin if scan_keyword("file") then begin t := scan_toks(false, true); pdf_action_file(p) := def_ref; end else pdf_action_file(p) := null; if scan_keyword("name") then begin pdf_action_id_type(p) := pdf_id_name; t := scan_toks(false, true); pdf_action_name(p) := def_ref; end else if scan_keyword("num") then begin if (pdf_action_type(p) = pdf_action_goto) and (pdf_action_file(p) <> 0) then pdf_confusion("ext1", "you can't use both `file' and `num' with GoTo action"); pdf_action_id_type(p) := pdf_id_num; scan_int; if cur_val < 0 then pdf_confusion("ext1", "num identifier can't be negative"); pdf_action_num(p) := cur_val; end else if scan_keyword("page") then begin if pdf_action_type(p) = pdf_action_thread then pdf_confusion("ext1", "you can't use `page' with Thread action"); pdf_action_id_type(p) := pdf_id_page; scan_int; if cur_val <= 0 then pdf_confusion("ext1", "page number must be positive"); pdf_action_num(p) := cur_val; t := scan_toks(false, true); pdf_action_page_dest(p) := def_ref; end else pdf_confusion("ext1", "you must specify identifier type"); end else begin t := scan_toks(false, true); pdf_action_user_defined_str(p) := def_ref; end; if pdf_ignore then return; if pdf_action_file(p) = null then {local destination or thread} if pdf_action_type(p) = pdf_action_goto then begin if pdf_action_id_type(p) = pdf_id_page then pdf_action_objnum(p) := find_identifier(find_page, pdf_action_num(p)) else pdf_action_objnum(p) := find_identifier(find_goto_action, p); end else if pdf_action_type(p) = pdf_action_thread then pdf_action_objnum(p) := find_identifier(find_thread_action, p); end; procedure write_action_struct(p: pointer); {write an action specification} begin pdf_print("<< "); case pdf_action_type(p) of pdf_action_goto: begin if pdf_action_file(p) = null then pdf_print("/S /GoTo ") else begin pdf_print("/S /GoToR "); pdf_str_entry("F", tokens_to_string(pdf_action_file(p))); pdf_out(" "); end; if pdf_action_id_type(p) = pdf_id_num then pdf_indirect("D", pdf_action_objnum(p)) else if pdf_action_id_type(p) = pdf_id_page then begin pdf_print("/D ["); if pdf_action_file(p) = null then begin pdf_print_int(pdf_action_objnum(p)); pdf_print(" 0 R"); end else pdf_print_int(pdf_action_num(p) - 1); pdf_out(" "); pdf_print(tokens_to_string(pdf_action_page_dest(p))); pdf_out("]"); end else begin pdf_str_entry("D", tokens_to_string(pdf_action_name(p))); flush_string; end; end; pdf_action_thread: begin pdf_print("/S /Thread "); if pdf_action_file(p) <> null then begin pdf_str_entry("F", tokens_to_string(pdf_action_file(p))); flush_string; pdf_out(" "); if pdf_action_id_type(p) = pdf_id_name then begin pdf_str_entry("D", tokens_to_string(pdf_action_name(p))); flush_string; end else pdf_int_entry("D", pdf_action_num(p)); end else pdf_indirect("D", pdf_action_objnum(p)); end; pdf_action_user_defined: begin pdf_print(tokens_to_string(pdf_action_user_defined_str(p))); flush_string; end; endcases; pdf_print_nl(" >>"); end; procedure do_thread(p, this_box: pointer; v, h: scaled); begin if thread_box_depth <> -1 then pdf_confusion("ext4", "article threads can't be nested"); thread_box_depth := cur_s; append_bead(this_box, v, h); end; procedure end_thread; begin if thread_box_depth <> cur_s then pdf_confusion("ext4", "thread ends in different nesting level than its start"); {pdf_right(obj_bead_rect(last_bead)) := cur_h;} pdf_bottom(obj_bead_rect(last_bead)) := cur_v; thread_box_depth := -1; end; function open_subentries(p: pointer): integer; var k, c: integer; l, r: pointer; begin k := 0; if obj_outline_first(p) <> 0 then begin l := obj_outline_first(p); repeat incr(k); c := open_subentries(l); if obj_outline_count(l) > 0 then k := k + c; obj_outline_parent(l) := p; r := obj_outline_next(l); if r = null then obj_outline_last(p) := l; l := r; until l = null; end; if obj_outline_count(p) > 0 then obj_outline_count(p) := k else obj_outline_count(p) := -k; open_subentries := k; end; procedure do_dest(p, this_box: pointer); var d: pointer; {pointer to destination if already exists} begin case pdf_dest_type(p) of pdf_dest_xyz: begin pdf_left(p) := cur_h; pdf_top(p) := cur_v - height(this_box); end; pdf_dest_fit_h, pdf_dest_fitb_h: pdf_top(p) := cur_v - height(this_box); pdf_dest_fit_v, pdf_dest_fitb_v: pdf_left(p) := cur_h; pdf_dest_fit, pdf_dest_fitb: do_nothing; endcases; pdf_dest_list := push_ptr(pdf_dest_list, pdf_dest_objnum(p)); end; procedure out_image(p: pointer); begin pdf_end_text; pdf_print_nl("q"); pdf_print_scaled(width(pdf_image_box(p))); pdf_print(" 0 0 "); pdf_print_scaled(height(pdf_image_box(p))); pdf_out(" "); pdf_print_scaled(pdf_x(cur_h)); pdf_out(" "); pdf_print_scaled(pdf_y(cur_v)); pdf_print_nl(" cm"); incr(image_count); pdf_create_obj(pdf_others, image_count); obj_ptr(obj_count) := p; pdf_image_list := push_ptr(pdf_image_list, obj_count); pdf_print("/Im"); pdf_print_int(image_count); pdf_print_nl(" Do"); pdf_print_nl("Q"); end; procedure out_form(p: pointer); var l: pointer; begin pdf_end_text; pdf_print_nl("q"); l := pdf_form_list; while (l <> null) and (info(l) <> pdf_form_objnum(p)) do l := link(l); if l = null then pdf_form_list := push_ptr(pdf_form_list, pdf_form_objnum(p)); l := pdf_form_objnum(p); pdf_print("1 0 0 1 "); pdf_print_scaled(pdf_x(cur_h)); pdf_out(" "); {pdf_print_scaled(pdf_y(cur_v + obj_form_height(l) + obj_form_depth(l)));} pdf_print_scaled(pdf_y(cur_v)); pdf_print_nl(" cm"); pdf_print("/Fm"); pdf_print_int(obj_info(pdf_form_objnum(p))); pdf_out(" "); pdf_print_nl(" Do"); pdf_print_nl("Q"); end; @ @= case subtype(p) of pdf_annot_text_node, pdf_annot_link_node, pdf_end_link_node, pdf_dest_node, pdf_thread_node, pdf_end_thread_node: if pdf_shipping_form then pdf_confusion("ext4", "pdfmarks used inside of forms") else case subtype(p) of pdf_annot_text_node: do_annot_text(p); pdf_annot_link_node: do_annot_link(p, this_box, top_edge, left_edge); pdf_end_link_node: end_link(this_box); pdf_dest_node: do_dest(p, this_box); pdf_thread_node: do_thread(p, this_box, top_edge, left_edge); pdf_end_thread_node: end_thread; endcases; pdf_literal_node: pdf_literal(p); pdf_ref_form_node: @; pdf_image_node: @; special_node: ignore_special(p); othercases out_what(p); endcases @ @= begin cur_v:=cur_v+height(pdf_image_box(p)); save_v:=cur_v; cur_h:=left_edge; out_image(p); cur_v:=save_v; cur_h:=left_edge; end @ @= begin cur_v:=cur_v+obj_form_height(pdf_form_objnum(p)); save_v:=cur_v; cur_h:=left_edge; out_form(p); cur_v:=save_v+obj_form_depth(pdf_form_objnum(p)); cur_h:=left_edge; end @ @= case subtype(p) of pdf_annot_text_node, pdf_annot_link_node, pdf_end_link_node, pdf_dest_node, pdf_thread_node, pdf_end_thread_node: if pdf_shipping_form then pdf_confusion("ext4", "pdfmarks used inside of forms") else case subtype(p) of pdf_annot_text_node: do_annot_text(p); pdf_annot_link_node: do_annot_link(p, this_box, base_line, left_edge); pdf_end_link_node: end_link(this_box); pdf_dest_node: do_dest(p, this_box); pdf_thread_node: do_thread(p, this_box, base_line, left_edge); pdf_end_thread_node: end_thread; endcases; pdf_literal_node: pdf_literal(p); pdf_image_node: @; pdf_ref_form_node: @; special_node: ignore_special(p); othercases out_what(p); endcases @ @= begin cur_v:=base_line; edge:=cur_h; out_image(p); cur_h:=edge+width(pdf_image_box(p)); cur_v:=base_line; end @ @= begin cur_v:=base_line; edge:=cur_h; out_form(p); cur_h:=edge+obj_form_width(pdf_form_objnum(p)); cur_v:=base_line; end @ @= if (link_box_depth <> -1) and (link_box_depth = cur_s) and is_running(pdf_link_dp) then append_annot_link(this_box, top_edge, left_edge); if (thread_box_depth <> -1) and (thread_box_depth = cur_s) then append_bead(this_box, top_edge, left_edge) @ @= if (link_box_depth <> -1) and (link_box_depth = cur_s) and is_running(pdf_link_wd) then append_annot_link(this_box, base_line, left_edge); if (thread_box_depth <> -1) and (thread_box_depth = cur_s) then append_bead(this_box, base_line, left_edge) @z @x @* \[54] System-dependent changes. @y @* \[54/web2c] System-dependent changes for Web2c. Here are extra variables for Web2c. (This numbering of the system-dependent section allows easy integration of Web2c and e-\TeX, etc.) @^ @= @!edit_name_start: pool_pointer; {where the filename to switch to starts} @!edit_name_length,@!edit_line: integer; {what line to start editing at} @!ipc_on: integer; {level of IPC action, 0 for none [default]} @!chars_saved_by_charset: integer; {bytes of |str_pool| that will be unused with tcx files; only needed to declare the identifier, this is never used.} @!extend_jobname: extend_jobname_never .. extend_jobname_always; {if \.{\\jobname} is \.{foo.bar}, do we generate \.{foo.bar.log} or \.{foo.log}?} @ The |edit_name_start| will be set to point into |str_pool| somewhere after its beginning if \TeX\ is supposed to switch to an editor on exit. @= edit_name_start:=0; @ These are used when we regenerate the representation of the first 256 strings. @ = @!save_str_ptr: str_number; @!save_pool_ptr: pool_pointer; @!shell_enabled_p: boolean; @!output_comment: ^char; @!k,l: 0..255; {used by `Make the first 256 strings', etc.} @ When debugging a macro package, it can be useful to see the exact control sequence names in the format file. For example, if ten new csnames appear, it's nice to know what they are, to help pinpoint where they came from. (This isn't a truly ``basic'' printing procedure, but that's a convenient module in which to put it.) @ = procedure print_csnames (hstart:integer; hfinish:integer); var c,h,where:integer; begin write_ln (stderr, 'fmtdebug:csnames from ', hstart, ' to ', hfinish, ':'); for h := hstart to hfinish do begin if text (h) > 0 then begin {if have anything at this position} where := h; repeat for c := str_start[text (where)] to str_start[text (where) + 1] - 1 do begin put_byte (str_pool[c], stderr); {print the characters} end; write_ln (stderr, ''); where := next (where); until where = 0; end; end; end; @ Are we printing extra info as we read the format file? @ = @!debug_format_file: boolean; @* \[54/web2c-string] The string recycling routines. \TeX{} uses 2 upto 4 {\it new\/} strings when scanning a filename in an \.{\\input}, \.{\\openin}, or \.{\\openout} operation. These strings are normally lost because the reference to them are not saved after finishing the operation. |search_string| searches through the string pool for the given string and returns either 0 or the found string number. @= function search_string(@!search:str_number):str_number; label found; var result: str_number; @!s: str_number; {running index} @!len: integer; {length of searched string} begin result:=0; len:=length(search); if len=0 then {trivial case} begin result:=""; goto found; end else begin s:=search-1; {start search with newest string below |s|; |search>1|!} while s>255 do {first 256 strings depend on implementation!!} begin if length(s)=len then if str_eq_str(s,search) then begin result:=s; goto found; end; decr(s); end; end; found:search_string:=result; end; @ The following routine is a variant of |make_string|. It searches the whole string pool for a string equal to the string currently built and returns a found string. Otherwise a new string is created and returned. Be cautious, you can not apply |flush_string| to a replaced string! @= function slow_make_string : str_number; label exit; var s: str_number; {result of |search_string|} @!t: str_number; {new string} begin t:=make_string; s:=search_string(t); if s>0 then begin flush_string; slow_make_string:=s; return; end; slow_make_string:=t; exit:end; @* \[54/ML\TeX] System-dependent changes for ML\TeX. The boolean variable |mltex_p| is set by web2c according to the given command line option (or an entry in the configuration file) before any \TeX{} function is called. @ = @!mltex_p: boolean; @ 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. @= @!mltex_enabled_p:boolean; {enable character substitution} @ @= 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@> @= 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@> @= 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. @= begin @; found: @; @; 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. @= @!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. @= 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. @= 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. @= 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