First rewrite of write_structure_tree

This commit is contained in:
John Whitington 2024-09-18 14:04:50 +01:00
parent d57575850c
commit cc01c8ddc1
1 changed files with 31 additions and 28 deletions

View File

@ -609,8 +609,7 @@ let make_structure_tree pdf items =
in in
make_structure_tree pageobjnums pdf (ref 0) items make_structure_tree pageobjnums pdf (ref 0) items
(* Write such a structure tree to a PDF. We have to make the objects and build (* Write such a structure tree to a PDF. *)
the root and its /K. *)
let write_structure_tree pdf st = let write_structure_tree pdf st =
let parentmap = ref [] in let parentmap = ref [] in
let add_parentmap pon this_objnum = let add_parentmap pon this_objnum =
@ -619,33 +618,37 @@ let write_structure_tree pdf st =
| Some objnums -> parentmap := add pon (this_objnum::objnums) !parentmap | Some objnums -> parentmap := add pon (this_objnum::objnums) !parentmap
in in
let struct_tree_root = Pdf.addobj pdf Pdf.Null in let struct_tree_root = Pdf.addobj pdf Pdf.Null in
let items = let rec mktree struct_tree_parent = function
map | StItem {kind; pageobjnum; alt; children} ->
(function StItem {kind; pageobjnum; alt; children} -> let this_objnum = Pdf.addobj pdf Pdf.Null in
let this_objnum = Pdf.addobj pdf Pdf.Null in begin match pageobjnum with
let alt = | Some p -> add_parentmap p this_objnum
match alt with | _ -> ()
| Some s -> [("/Alt", Pdf.String s)] end;
| None -> [] let alt =
in match alt with
let page = | Some s -> [("/Alt", Pdf.String s)]
match pageobjnum with | None -> []
| Some i -> [("/Pg", Pdf.Indirect i)] in
| None -> [] let page =
in match pageobjnum with
let this_obj = | Some i -> [("/Pg", Pdf.Indirect i)]
Pdf.Dictionary (alt @ page @ | None -> []
[("/S", Pdf.Name kind); in
("/P", Pdf.Indirect struct_tree_root); let this_obj =
("/K", Pdf.Array (map (function StMCID x -> begin match pageobjnum with Some p -> add_parentmap p this_objnum | _ -> () end; Pdf.Integer x Pdf.Dictionary
| _ -> assert false) children))]) (alt
in @ page
Pdf.addobj_given_num pdf (this_objnum, this_obj); @ [("/S", Pdf.Name kind);
Pdf.Indirect this_objnum ("/P", Pdf.Indirect struct_tree_parent);
| _ -> assert false ("/K", Pdf.Array (map (mktree this_objnum) children))])
) in
st Pdf.addobj_given_num pdf (this_objnum, this_obj);
Pdf.Indirect this_objnum
| StMCID x ->
Pdf.Integer x
in in
let items = map (mktree struct_tree_root) st in
iter iter
(fun (pon, _) -> (fun (pon, _) ->
Pdf.addobj_given_num pdf (pon, Pdf.add_dict_entry (Pdf.lookup_obj pdf pon) "/StructParents" (Pdf.Integer pon))) Pdf.addobj_given_num pdf (pon, Pdf.add_dict_entry (Pdf.lookup_obj pdf pon) "/StructParents" (Pdf.Integer pon)))