% This is pdftex.ch in text format % WEB change file containing code for PDFTeX feature extending TeX; % to be applied to tex.web (Version 3.14159) in order to define the % PDFTeX program. % % ==> for Web2c 7.0 (TeX 3.14159) % % Note: Search for "(WEB2C 7.0!)" for changes % depending on tex.ch of Web2c 7.0! % (There are probably more dependencies.) % % % % Create a new PDFTeX master file using TIE with: % % tie -m pdftex.web tex.web tex.ch pdftex.ch % % tex.web is TeX 3.14159, % tex.ch is Web2c 7.0's TeX.web change file (without e-TeX changes), % pdftex.ch is this change file. % % (When using e-TeX's Web2c 7.0 change file `etex-w2c.ch' instead of % `tex.ch', you have to use another pool name change at [1.11] l.412. % Probably more changes are needed?) % Copyright (C) 1996/97 Han The Thanh, Petr Sojka, and % Ji\v{r}\'i Zlatu\v{s}ka % % The TeX program is copyright (C) 1982 by D. E. Knuth. % TeX is a trademark of the American Mathematical Society. @x [1.2] l.186 - This change is made for TeX 3.14159! @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 [1.11] l.412 - Change pool name to "pdftex.pool" (WEB2C 7.0!) @!pool_name=TEX_POOL_NAME; {this is configurable, for the sake of ML-\TeX} @y @!pool_name=PDFTEX_POOL_NAME; {this is configurable, for the sake of ML-\TeX} @z @x [13.202] l.3896 - Update destroying boxes for pdftex @ 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 [14.204] l.3957 - Update copying boxes for pdftex @ 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 [17.230] l.4720 - Define pdftex tokens parameters @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 [17.230] l.4742 @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 [17.230] l.4771 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 [17.231] l.4787 othercases print_esc("errhelp") @y pdf_page_attr_loc: print_esc("pdfpageattr"); pdf_pages_attr_loc: print_esc("pdfpagesattr"); othercases print_esc("errhelp") @z % [17.236] - Define pdftex integer parameters @x (WEB2C 7.0!) @d error_context_lines_code=54 {maximum intermediate line pairs shown} @d char_sub_def_min_code=55 {smallest value in the charsubdef list} @d char_sub_def_max_code=56 {largest value in the charsubdef list} @d tracing_char_sub_def_code=57 {traces changes to a charsubdef def} @d int_pars=58 {total number of integer parameters} @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 [17.236] l.5016 @d error_context_lines==int_par(error_context_lines_code) @y @d error_context_lines==int_par(error_context_lines_code) @d 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) @z @x [17.237] l.5080 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"); @z @x [17.238] l.5199 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@> @z @x [17.247] l.5295 - Define pdftex dimension parameters @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 [17.247] l.5323 @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 [17.247] l.5347 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 [17.248] l.5395 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 [22.307] l.6693 - Define pdf_page_attr_text and pdf_pages_attr_text @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 [22.314] l.6816 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 [32.638] l.12629 - Low-level PDF output subroutines @ 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 [33.644] l.12761 @* \[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 (WEB2C 7.0!) xmalloc_array(param_base, font_max); undump_things(font_check[null_font], font_ptr+1-null_font); @y 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); @z %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % [51.1333] Print new line before termination; switch to editor if % necessary. % Declare the necessary variables for finishing PDF file % Close PDF output if necessary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @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 (WEB2C 7.0!) xmalloc_array(param_base, font_max); font_ptr:=null_font; fmem_ptr:=7; @y 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; @z %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % [53.????] Extensions for PDF output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @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 @ @= @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 @ @= 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