Build /StructParents

This commit is contained in:
John Whitington 2024-09-10 16:40:33 +01:00
parent 188ecd20f2
commit b5e96f426e
1 changed files with 14 additions and 4 deletions

View File

@ -473,6 +473,11 @@ let make_structure_tree pdf items =
more tree stuff. *) more tree stuff. *)
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 =
match lookup pon !parentmap with
| None -> parentmap =| (pon, [this_objnum])
| Some objnums -> parentmap := add pon (this_objnum::objnums) !parentmap
in
let struct_tree_root = Pdf.addobj pdf Pdf.Null in let struct_tree_root = Pdf.addobj pdf Pdf.Null in
let items = let items =
map map
@ -482,9 +487,7 @@ let write_structure_tree pdf st =
Pdf.Dictionary [("/S", Pdf.Name kind); Pdf.Dictionary [("/S", Pdf.Name kind);
("/Pg", Pdf.Indirect pageobjnum); ("/Pg", Pdf.Indirect pageobjnum);
("/P", Pdf.Indirect struct_tree_root); ("/P", Pdf.Indirect struct_tree_root);
("/K", Pdf.Array (map (function StMCID x -> ("/K", Pdf.Array (map (function StMCID x -> add_parentmap pageobjnum this_objnum; Pdf.Integer x
parentmap =| (string_of_int x, Pdf.Array [Pdf.Indirect this_objnum]);
Pdf.Integer x
| _ -> assert false) children))] | _ -> assert false) children))]
in in
Pdf.addobj_given_num pdf (this_objnum, this_obj); Pdf.addobj_given_num pdf (this_objnum, this_obj);
@ -493,9 +496,16 @@ let write_structure_tree pdf st =
) )
st st
in in
iter
(fun (pon, _) ->
Pdf.addobj_given_num pdf (pon, Pdf.add_dict_entry (Pdf.lookup_obj pdf pon) "/StructParent" (Pdf.Integer pon)))
!parentmap;
let parentmap =
map (fun (pon, items) -> (string_of_int pon, Pdf.Array (map (fun x -> Pdf.Indirect x) (rev items)))) !parentmap
in
let st = let st =
Pdf.Dictionary [("/Type", Pdf.Name "/StructTreeRoot"); Pdf.Dictionary [("/Type", Pdf.Name "/StructTreeRoot");
("/ParentTree", Pdf.Indirect (Pdf.addobj pdf (Pdftree.build_name_tree true pdf !parentmap))); ("/ParentTree", Pdf.Indirect (Pdf.addobj pdf (Pdftree.build_name_tree true pdf parentmap)));
("/K", Pdf.Array items)] ("/K", Pdf.Array items)]
in in
Pdf.addobj_given_num pdf (struct_tree_root, st); Pdf.addobj_given_num pdf (struct_tree_root, st);