Text specials

This commit is contained in:
John Whitington 2023-05-02 14:47:18 +01:00
parent 3de294119b
commit 64fb6169b9
3 changed files with 38 additions and 8 deletions

View File

@ -2068,6 +2068,11 @@ let addtext s =
addop (Cpdfdraw.Font (font, args.fontsize)); addop (Cpdfdraw.Font (font, args.fontsize));
addop (Cpdfdraw.Text s) 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 = let addleading f =
addop (Cpdfdraw.Leading f) addop (Cpdfdraw.Leading f)
@ -2938,7 +2943,8 @@ and specs =
("-sopacity", Arg.Float addsopacity, " Set stroke opacity"); ("-sopacity", Arg.Float addsopacity, " Set stroke opacity");
("-bt", Arg.Unit addbt, " Begin text"); ("-bt", Arg.Unit addbt, " Begin text");
("-et", Arg.Unit addet, " End 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"); ("-url", Arg.String addurl, " Begin URL");
("-endurl", Arg.Unit addendurl, " End URL"); ("-endurl", Arg.Unit addendurl, " End URL");
("-leading", Arg.Float addleading, " Set leading"); ("-leading", Arg.Float addleading, " Set leading");
@ -4445,7 +4451,7 @@ let go () =
if !tdeep <> 0 then error "Unmatched -bt / -et" else if !tdeep <> 0 then error "Unmatched -bt / -et" else
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec_allow_empty pdf (get_pagespec ()) 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) -> | Some (Composition json) ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let filesize = 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); (*Printf.printf "AND:%b, %s\n" islast (Array.fold_left (fun x y -> x ^ " " ^ y) "" s);
flprint "\n";*) flprint "\n";*)
reset_arguments (); reset_arguments ();
Hashtbl.clear drawops;
process_env_vars (); process_env_vars ();
parse_argv () s (align_specs specs) anon_fun usage_msg; 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; 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 let addrange pdf = AlreadyInMemory pdf, args.dashrange, "", "", ref false, None in
args.inputs <- rev (map addrange !output_pdfs) @ rev args.inputs; args.inputs <- rev (map addrange !output_pdfs) @ rev args.inputs;
output_pdfs := []; output_pdfs := [];
go ()) go ();
Hashtbl.clear drawops
)
sets; sets;
flush stdout; (*r for Windows *) flush stdout; (*r for Windows *)
exit 0 exit 0

View File

@ -43,6 +43,7 @@ type drawops =
| BT | BT
| ET | ET
| Text of string | Text of string
| SpecialText of string
| Newline | Newline
| Leading of float | Leading of float
| CharSpace 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] | BT -> [Pdfops.Op_BT]
| ET -> [Pdfops.Op_ET] | ET -> [Pdfops.Op_ET]
| Text s -> | 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 s = process_specials pdf endpage filename bates batespad num page s in
let charcodes = let charcodes =
implode (map char_of_int (option_map (Pdftext.charcode_extractor_of_font_real !current_font) (Pdftext.codepoints_of_utf8 s))) 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) flatten (map (ops_of_drawop pdf endpage filename bates batespad num page) drawops)
(* Draw all the accumulated operators. *) (* Draw all the accumulated operators. *)
let draw fast range pdf drawops = let draw ~filename ~bates ~batespad fast range pdf drawops =
time := Cpdfstrftime.current_time (); 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 endpage = Pdfpage.endpage pdf in
let pdf = Cpdftweak.append_page_content s false fast range 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 images = list_of_hashtbl images in
let image_resources = map (fun (_, (n, o)) -> (n, Pdf.Indirect o)) images in let image_resources = map (fun (_, (n, o)) -> (n, Pdf.Indirect o)) images in
let gss_resources = list_of_hashtbl gss in let gss_resources = list_of_hashtbl gss in

View File

@ -41,6 +41,7 @@ type drawops =
| BT | BT
| ET | ET
| Text of string | Text of string
| SpecialText of string
| Newline | Newline
| Leading of float | Leading of float
| CharSpace of float | CharSpace of float
@ -51,4 +52,7 @@ type drawops =
| URL of string | URL of string
| EndURL | 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