From 9e1cf6f78e2b8615b5cfbb870e0c0d07c98ff79f Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 25 Sep 2024 15:12:58 +0100 Subject: [PATCH] Scaffolding for -elt-info --- cpdfcommand.ml | 3 +++ cpdfdraw.ml | 19 ++++++++++++++----- cpdfdraw.mli | 4 ++++ cpdfdrawcontrol.ml | 14 ++++++++++++++ cpdfdrawcontrol.mli | 3 +++ cpdfmanual.tex | 2 ++ 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 07be95e..762971e 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -2841,7 +2841,10 @@ let specs = ("-artifact", Arg.Unit (fun _ -> Cpdfdrawcontrol.artifact ()), " Begin an artifact"); ("-end-artifact", Arg.Unit (fun _ -> Cpdfdrawcontrol.endartifact ()), "End an artifact"); ("-no-auto-artifacts", Arg.Unit (fun _ -> Cpdfdrawcontrol.autoartifacts false), " Don't mark untagged content as artifacts"); + ("-eltinfo", Arg.String (fun s -> Cpdfdrawcontrol.eltinfo s), " Add element information"); + ("-end-eltinfo", Arg.String (fun s -> Cpdfdrawcontrol.endeltinfo s), " Erase element information"); ("-namespace", Arg.String (fun s -> Cpdfdrawcontrol.addnamespace (expand_namespace s)), " Set the structure tree namespace"); + ("-rolemap", Arg.String (fun s -> Cpdfdrawcontrol.setrolemap s), " Set a role map"); ("-rect", Arg.String Cpdfdrawcontrol.addrect, " Draw rectangle"); ("-to", Arg.String Cpdfdrawcontrol.addto, " Move to"); ("-line", Arg.String Cpdfdrawcontrol.addline, " Add line to"); diff --git a/cpdfdraw.ml b/cpdfdraw.ml index 214ea97..e8cdc78 100644 --- a/cpdfdraw.ml +++ b/cpdfdraw.ml @@ -5,6 +5,8 @@ let do_add_artifacts = ref true let do_auto_tag = ref true +let rolemap = ref "" + type colspec = NoCol | RGB of float * float * float @@ -65,6 +67,8 @@ type drawops = | BeginArtifact | EndArtifact | Namespace of string + | EltInfo of string * string + | EndEltInfo of string (*let rec string_of_drawop = function | Qq o -> "Qq (" ^ string_of_drawops o ^ ")" @@ -721,11 +725,16 @@ let write_structure_tree pdf st = | [] -> [] | ns -> [("/Namespaces", Pdf.Array (map (function (_, objnum) -> Pdf.Indirect objnum) ns))] in - Pdf.Dictionary - (namespaces @ - [("/Type", Pdf.Name "/StructTreeRoot"); - ("/ParentTree", Pdf.Indirect (Pdf.addobj pdf (Pdftree.build_name_tree true pdf parentmap))); - ("/K", Pdf.Array items)]) + let rolemap = + match !rolemap with + | "" -> [] + | s -> [("/RoleMap", Pdfread.parse_single_object ("<<" ^ s ^ ">>"))] + in + Pdf.Dictionary + (rolemap @ namespaces @ + [("/Type", Pdf.Name "/StructTreeRoot"); + ("/ParentTree", Pdf.Indirect (Pdf.addobj pdf (Pdftree.build_name_tree true pdf parentmap))); + ("/K", Pdf.Array items)]) in Pdf.addobj_given_num pdf (struct_tree_root, st); Pdf.replace_chain pdf ["/Root"] ("/StructTreeRoot", (Pdf.Indirect struct_tree_root)) diff --git a/cpdfdraw.mli b/cpdfdraw.mli index 78aae37..bfcf18e 100644 --- a/cpdfdraw.mli +++ b/cpdfdraw.mli @@ -60,11 +60,15 @@ type drawops = | BeginArtifact | EndArtifact | Namespace of string + | EltInfo of string * string + | EndEltInfo of string val do_add_artifacts : bool ref val do_auto_tag : bool ref +val rolemap : string ref + (** Calling [draw fast underneath filename bates batespad range pdf drawops] draws on top of all the pages in the range. *) val draw : struct_tree:bool -> diff --git a/cpdfdrawcontrol.ml b/cpdfdrawcontrol.ml index 9562d32..dc7527d 100644 --- a/cpdfdrawcontrol.ml +++ b/cpdfdrawcontrol.ml @@ -123,6 +123,20 @@ let endartifact () = let addnamespace s = addop (Cpdfdraw.Namespace s) +let eltinfo s = + let k, v = + match String.split_on_char '=' s with + | [h; t] -> (h, t) + | _ -> error "Bad -eltinfo format" + in + addop (Cpdfdraw.EltInfo (k, v)) + +let endeltinfo s = + addop (Cpdfdraw.EndEltInfo s) + +let setrolemap s = + Cpdfdraw.rolemap := s + let addrect s = let x, y, w, h = Cpdfcoord.parse_rectangle (Pdf.empty ()) s in addop (Cpdfdraw.Rect (x, y, w, h)) diff --git a/cpdfdrawcontrol.mli b/cpdfdrawcontrol.mli index 92bcfe9..e2338ff 100644 --- a/cpdfdrawcontrol.mli +++ b/cpdfdrawcontrol.mli @@ -20,7 +20,10 @@ val addstag : string -> unit val endtag : unit -> unit val endstag : unit -> unit val autotags : bool -> unit +val eltinfo : string -> unit +val endeltinfo : string -> unit val addnamespace : string -> unit +val setrolemap : string -> unit val artifact : unit -> unit val endartifact : unit -> unit val autoartifacts : bool -> unit diff --git a/cpdfmanual.tex b/cpdfmanual.tex index bf55e8e..ad551eb 100644 --- a/cpdfmanual.tex +++ b/cpdfmanual.tex @@ -1,3 +1,5 @@ +%Document -rolemap +%Document -eltinfo X=, -end-eltinfo X %Document -stretch %Document -redact %Document ?x0 y0 x1 y1 rectangle specifications