From d8d0e9c2b2cb967df2a25f57eca3a5e00e4b8cbd Mon Sep 17 00:00:00 2001 From: John Whitington Date: Thu, 12 Sep 2024 16:08:05 +0100 Subject: [PATCH] Scaffolding for paragraphs --- cpdfcommand.ml | 1 + cpdfdraw.ml | 7 +++++++ cpdfdraw.mli | 4 ++++ cpdfdrawcontrol.ml | 19 +++++++++++++++++++ cpdfdrawcontrol.mli | 1 + 5 files changed, 32 insertions(+) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 5682eb1..1e695ea 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -2839,6 +2839,7 @@ let specs = ("-et", Arg.Unit Cpdfdrawcontrol.addet, " End text"); ("-text", Arg.String Cpdfdrawcontrol.addtext, " Draw text"); ("-stext", Arg.String Cpdfdrawcontrol.addspecialtext, " Draw text with %specials"); + ("-para", Arg.String Cpdfdrawcontrol.addpara, " Add a paragraph of text"); ("-leading", Arg.Float (fun f -> Cpdfdrawcontrol.addop (Cpdfdraw.Leading f)), " Set leading"); ("-charspace", Arg.Float (fun f -> Cpdfdrawcontrol.addop (Cpdfdraw.CharSpace f)), " Set character spacing"); ("-wordspace", Arg.Float (fun f -> Cpdfdrawcontrol.addop (Cpdfdraw.WordSpace f)), " Set word space"); diff --git a/cpdfdraw.ml b/cpdfdraw.ml index 39d50e1..ecf6d2e 100644 --- a/cpdfdraw.ml +++ b/cpdfdraw.ml @@ -7,6 +7,9 @@ type colspec = | Grey of float | CYMK of float * float * float * float +type justification = + Left | Right | Centre + type drawops = | Rect of float * float * float * float | Bezier of float * float * float * float * float * float @@ -43,6 +46,7 @@ type drawops = | TextSection of drawops list | Text of string | SpecialText of string + | Para of justification * float * string | Newline | Leading of float | CharSpace of float @@ -327,6 +331,9 @@ let rec ops_of_drawop struct_tree dryrun pdf endpage filename bates batespad num let s = process_specials pdf endpage filename bates batespad num page s in if dryrun then iter (fun c -> Hashtbl.replace (res ()).current_fontpack_codepoints c ()) (Pdftext.codepoints_of_utf8 s); runs_of_utf8 s + | Para (j, w, s) -> + if dryrun then iter (fun c -> Hashtbl.replace (res ()).current_fontpack_codepoints c ()) (Pdftext.codepoints_of_utf8 s); + [] | Leading f -> [Pdfops.Op_TL f] | CharSpace f -> [Pdfops.Op_Tc f] | WordSpace f -> [Pdfops.Op_Tw f] diff --git a/cpdfdraw.mli b/cpdfdraw.mli index 8db8cbd..285429b 100644 --- a/cpdfdraw.mli +++ b/cpdfdraw.mli @@ -6,6 +6,9 @@ type colspec = | Grey of float | CYMK of float * float * float * float +type justification = + Left | Right | Centre + type drawops = | Rect of float * float * float * float | Bezier of float * float * float * float * float * float @@ -42,6 +45,7 @@ type drawops = | TextSection of drawops list | Text of string | SpecialText of string + | Para of justification * float * string | Newline | Leading of float | CharSpace of float diff --git a/cpdfdrawcontrol.ml b/cpdfdrawcontrol.ml index 041dbb4..1591f8d 100644 --- a/cpdfdrawcontrol.ml +++ b/cpdfdrawcontrol.ml @@ -321,3 +321,22 @@ let addspecialtext s = add_default_fontpack (!getfontname ()); addop (Cpdfdraw.Font (!getfontname (), !getfontsize ())); addop (Cpdfdraw.SpecialText s) + +(* "L200pt=....." *) +let addpara s = + begin match !drawops with _::_::_ -> () | _ -> error "-stext must be in a -bt / -et section" end; + add_default_fontpack (!getfontname ()); + addop (Cpdfdraw.Font (!getfontname (), !getfontsize ())); + let j, rest = + match explode s with + | 'L'::t -> (Cpdfdraw.Left, t) + | 'R'::t -> (Cpdfdraw.Right, t) + | 'C'::t -> (Cpdfdraw.Centre, t) + | _ -> error "Unknown justification specification" + in + let w, s = + match String.split_on_char '=' (implode rest) with + | [w; s] -> (Cpdfcoord.parse_single_number (Pdf.empty ()) w, s) + | _ -> error "addjpeg: bad file specification" + in + addop (Cpdfdraw.Para (j, w, s)) diff --git a/cpdfdrawcontrol.mli b/cpdfdrawcontrol.mli index 49da980..74f4ad1 100644 --- a/cpdfdrawcontrol.mli +++ b/cpdfdrawcontrol.mli @@ -66,6 +66,7 @@ val addnewline : unit -> unit val add_default_fontpack : string -> unit val addtext : string -> unit val addspecialtext : string -> unit +val addpara : string -> unit (** This the beginnings of separation between cpdfcommand and cpdfdraw when drawing, for use in cpdflib. It is presently undocumented. *)