From 64fb6169b95a68b3b16f8e8c6f1c14e516598630 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 2 May 2023 14:47:18 +0100 Subject: [PATCH] Text specials --- cpdfcommand.ml | 15 +++++++++++---- cpdfdraw.ml | 25 ++++++++++++++++++++++--- cpdfdraw.mli | 6 +++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index c482708..3d087bb 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -2068,6 +2068,11 @@ let addtext s = addop (Cpdfdraw.Font (font, args.fontsize)); addop (Cpdfdraw.Text s) +let addspecialtext s = + let font = match args.font with StandardFont s -> s | _ -> error "-text: not a standard font" in + addop (Cpdfdraw.Font (font, args.fontsize)); + addop (Cpdfdraw.SpecialText s) + let addleading f = addop (Cpdfdraw.Leading f) @@ -2938,7 +2943,8 @@ and specs = ("-sopacity", Arg.Float addsopacity, " Set stroke opacity"); ("-bt", Arg.Unit addbt, " Begin text"); ("-et", Arg.Unit addet, " End text"); - ("-text", Arg.String addtext, " Draw text and move position. "); + ("-text", Arg.String addtext, " Draw text "); + ("-stext", Arg.String addspecialtext, " Draw text with %specials"); ("-url", Arg.String addurl, " Begin URL"); ("-endurl", Arg.Unit addendurl, " End URL"); ("-leading", Arg.Float addleading, " Set leading"); @@ -4445,7 +4451,7 @@ let go () = if !tdeep <> 0 then error "Unmatched -bt / -et" else let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - write_pdf false (Cpdfdraw.draw args.fast range pdf (rev (Hashtbl.find drawops "_"))) + write_pdf false (Cpdfdraw.draw ~filename:args.original_filename ~bates:args.bates ~batespad:args.batespad args.fast range pdf (rev (Hashtbl.find drawops "_"))) | Some (Composition json) -> let pdf = get_single_pdf args.op false in let filesize = @@ -4544,14 +4550,15 @@ let go_withargv argv = (*Printf.printf "AND:%b, %s\n" islast (Array.fold_left (fun x y -> x ^ " " ^ y) "" s); flprint "\n";*) reset_arguments (); - Hashtbl.clear drawops; process_env_vars (); parse_argv () s (align_specs specs) anon_fun usage_msg; parse_argv () (Array.of_list ("cpdf"::!control_args)) (align_specs specs) anon_fun usage_msg; let addrange pdf = AlreadyInMemory pdf, args.dashrange, "", "", ref false, None in args.inputs <- rev (map addrange !output_pdfs) @ rev args.inputs; output_pdfs := []; - go ()) + go (); + Hashtbl.clear drawops + ) sets; flush stdout; (*r for Windows *) exit 0 diff --git a/cpdfdraw.ml b/cpdfdraw.ml index da44847..c03c5c9 100644 --- a/cpdfdraw.ml +++ b/cpdfdraw.ml @@ -43,6 +43,7 @@ type drawops = | BT | ET | Text of string + | SpecialText of string | Newline | Leading of float | CharSpace of float @@ -150,6 +151,11 @@ let rec ops_of_drawop pdf endpage filename bates batespad num page = function | BT -> [Pdfops.Op_BT] | ET -> [Pdfops.Op_ET] | Text s -> + let charcodes = + implode (map char_of_int (option_map (Pdftext.charcode_extractor_of_font_real !current_font) (Pdftext.codepoints_of_utf8 s))) + in + [Pdfops.Op_Tj charcodes] + | SpecialText s -> let s = process_specials pdf endpage filename bates batespad num page s in let charcodes = implode (map char_of_int (option_map (Pdftext.charcode_extractor_of_font_real !current_font) (Pdftext.codepoints_of_utf8 s))) @@ -167,10 +173,23 @@ and ops_of_drawops pdf endpage filename bates batespad num page drawops = flatten (map (ops_of_drawop pdf endpage filename bates batespad num page) drawops) (* Draw all the accumulated operators. *) -let draw fast range pdf drawops = +let draw ~filename ~bates ~batespad fast range pdf drawops = time := Cpdfstrftime.current_time (); - let s = Pdfops.string_of_ops (ops_of_drawops pdf 1 "" 0 None 1 (Pdfpage.blankpage Pdfpaper.a4) drawops) in - let pdf = Cpdftweak.append_page_content s false fast range pdf in + let endpage = Pdfpage.endpage pdf in + let pages = Pdfpage.pages_of_pagetree pdf in + let ss = + map2 + (fun n p -> Pdfops.string_of_ops (ops_of_drawops pdf endpage filename bates batespad n p drawops)) + (ilist 1 endpage) + pages + in + let pdf = ref pdf in + iter2 + (fun n s -> + if mem n range then (Printf.printf "Adding ops to page %i\n" n; pdf := Cpdftweak.append_page_content s false fast [n] !pdf)) + (ilist 1 endpage) + ss; + let pdf = !pdf in let images = list_of_hashtbl images in let image_resources = map (fun (_, (n, o)) -> (n, Pdf.Indirect o)) images in let gss_resources = list_of_hashtbl gss in diff --git a/cpdfdraw.mli b/cpdfdraw.mli index e8edee2..c9a4cce 100644 --- a/cpdfdraw.mli +++ b/cpdfdraw.mli @@ -41,6 +41,7 @@ type drawops = | BT | ET | Text of string + | SpecialText of string | Newline | Leading of float | CharSpace of float @@ -51,4 +52,7 @@ type drawops = | URL of string | EndURL -val draw : bool -> int list -> Pdf.t -> drawops list -> Pdf.t +val draw : filename:string -> + bates:int -> + batespad:int option -> + bool -> int list -> Pdf.t -> drawops list -> Pdf.t