First working replace_obj

This commit is contained in:
John Whitington 2024-09-04 14:53:14 +01:00
parent 8a9572c38f
commit 4db3f81555
1 changed files with 33 additions and 24 deletions

View File

@ -3477,31 +3477,39 @@ let print_obj pdf objspec =
(* Empty string is trailerdict. Begins with / and it's a chain separated by commas. *) (* Empty string is trailerdict. Begins with / and it's a chain separated by commas. *)
let replace_obj pdf objspec obj = let replace_obj pdf objspec obj =
Printf.printf "objspec = %s\n" objspec; (*Printf.printf "objspec = %s\n" objspec;*)
let key, chain = let rec find_max_existing to_fake chain =
let r, rest = cleavewhile (neq '/') (rev (explode objspec)) in (*Printf.printf "find_max_existing: %s\n" (String.concat "" chain);*)
(implode ('/'::rev r), split_chain (implode (rev (tl rest)))) if chain = [] then (chain, to_fake) else
match Pdf.lookup_chain pdf pdf.Pdf.trailerdict chain with
| None -> find_max_existing (hd (rev chain)::to_fake) (rev (tl (rev chain)))
| _ -> (chain, to_fake)
in in
let rec find_max_existing to_fake chain = let rec wrap_obj obj = function
if chain = [] then (chain, to_fake) else | [] -> obj
match Pdf.lookup_chain pdf pdf.Pdf.trailerdict chain with | h::t -> Pdf.Dictionary [(h, wrap_obj obj t)]
| None -> find_max_existing (hd chain::to_fake) (tl chain) in
| _ -> (chain, to_fake) let chain, to_fake = find_max_existing [] (split_chain objspec) in
in (*Printf.printf "chain:\n";
let rec wrap_obj obj = function iter (Printf.printf "%s ") chain;
| [] -> obj Printf.printf "\n";
| h::t -> Pdf.Dictionary [(h, wrap_obj obj t)] Printf.printf "to_fake is:\n";
in iter (Printf.printf "%s ") to_fake;
let chain, to_fake = find_max_existing [] chain in Printf.printf "\n";*)
Printf.printf "to_fake is:\n"; let chain, key, obj =
iter (Printf.printf "%s ") to_fake; match to_fake with
Printf.printf "\n"; | [] ->
let obj = wrap_obj obj to_fake in (* If chain is complete (i.e to_fake empty), split the key off, and the obj is unaltered *)
Printf.printf "obj is %s\n" (Pdfwrite.string_of_pdf obj); (rev (tl (rev chain)), hd (rev chain), obj)
Printf.printf "chain is:\n"; | h::t ->
(* Otherwise to_fake has head. That's the key, and the object is the rest wrapped. *)
(chain, h, wrap_obj obj t)
in
(*Printf.printf "final chain:\n";
iter (Printf.printf "%s ") chain; iter (Printf.printf "%s ") chain;
Printf.printf "\n"; Printf.printf "key is %s\n" key;
Pdf.replace_chain pdf chain (key, obj) Printf.printf "obj is %s\n" (Pdfwrite.string_of_pdf obj);*)
Pdf.replace_chain pdf chain (key, obj)
(* Main function *) (* Main function *)
let go () = let go () =
@ -4530,7 +4538,8 @@ let go () =
| Some (ReplaceObj (a, b)) -> | Some (ReplaceObj (a, b)) ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let pdfobj = Cpdfjson.object_of_json (Cpdfyojson.Safe.from_string b) in let pdfobj = Cpdfjson.object_of_json (Cpdfyojson.Safe.from_string b) in
replace_obj pdf a pdfobj replace_obj pdf a pdfobj;
write_pdf false pdf
| Some (Verify standard) -> | Some (Verify standard) ->
begin match standard with begin match standard with
| "PDF/UA-1(matterhorn)" -> | "PDF/UA-1(matterhorn)" ->