diff --git a/cpdfcommand.ml b/cpdfcommand.ml index a5ecee4..a4c3351 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1777,7 +1777,7 @@ let whingemalformed () = exit 1 (* Drawing operations. *) -let drawops = ref [("main", [])] +let drawops = ref [("_MAIN", [])] let startxobj n = drawops := (n, [])::!drawops @@ -1797,17 +1797,27 @@ let endxobj () = let a, b, c, d = args.xobj_bbox in addop (Cpdfdraw.FormXObject (a, b, c, d, n, rev ops)) | [] -> - error "too many -endxobj" - -let tdeep = ref 0 + error "too many -endxobj or -et" let addbt () = - addop Cpdfdraw.BT; - tdeep +=1 + drawops := ("_TEXT", [])::!drawops let addet () = - addop Cpdfdraw.ET; - tdeep -=1 + match !drawops with + | ("_TEXT", ops)::t -> + drawops := t; + addop (Cpdfdraw.TextSection (rev ops)) + | _ -> error "not in a text section at -et" + +let push () = + drawops := ("_PUSH", [])::!drawops + +let pop () = + match !drawops with + | ("_PUSH", ops)::t -> + drawops := t; + addop (Cpdfdraw.Qq (rev ops)) + | _ -> error "not in a Q section at -q" let readfloats s = map float_of_string (String.split_on_char ' ' s) @@ -1922,11 +1932,6 @@ let setdash s = with _ -> error "Dash pattern elements must one or more numbers" -let push () = - addop Cpdfdraw.Push - -let pop () = - addop Cpdfdraw.Pop let setmatrix s = match readfloats s with @@ -4460,10 +4465,9 @@ let go () = let w = (float rawwidth *. args.fontsize) /. 1000. in Printf.printf "%f\n" w | Some Draw -> - 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 - let ops = match !drawops with [("main", ops)] -> rev ops | _ -> error "not enough -endxobj" in + let ops = match !drawops with [("_MAIN", ops)] -> rev ops | _ -> error "not enough -endxobj or -et" in write_pdf false (Cpdfdraw.draw ~filename:args.original_filename ~bates:args.bates ~batespad:args.batespad args.fast range pdf ops) diff --git a/cpdfdraw.ml b/cpdfdraw.ml index 3048691..03c01d4 100644 --- a/cpdfdraw.ml +++ b/cpdfdraw.ml @@ -20,8 +20,7 @@ type drawops = | SetMiterLimit of float | SetDashPattern of float list * float | Matrix of Pdftransform.transform_matrix - | Push - | Pop + | Qq of drawops list | Fill | FillEvenOdd | Stroke @@ -37,8 +36,7 @@ type drawops = | Opacity of float | SOpacity of float | Font of Pdftext.standard_font * float - | BT - | ET + | TextSection of drawops list | Text of string | SpecialText of string | Newline @@ -146,8 +144,7 @@ let update_resources pdf old_resources = (Pdf.Dictionary new_fonts) let rec ops_of_drawop pdf endpage filename bates batespad num page = function - | Push -> [Pdfops.Op_q] - | Pop -> [Pdfops.Op_Q] + | Qq ops -> [Pdfops.Op_q] @ ops_of_drawops pdf endpage filename bates batespad num page ops @ [Pdfops.Op_Q] | Matrix m -> [Pdfops.Op_cm m] | Rect (x, y, w, h) -> [Pdfops.Op_re (x, y, w, h)] | Bezier (a, b, c, d, e, f) -> [Pdfops.Op_c (a, b, c, d, e, f)] @@ -208,8 +205,7 @@ let rec ops_of_drawop pdf endpage filename bates batespad num page = function (res ()).current_font <- font; (res ()).page_names <- n::(res ()).page_names; [Pdfops.Op_Tf (n, f)] - | BT -> [Pdfops.Op_BT] - | ET -> [Pdfops.Op_ET] + | TextSection ops -> [Pdfops.Op_BT] @ ops_of_drawops pdf endpage filename bates batespad num page ops @ [Pdfops.Op_ET] | Text s -> [Pdfops.Op_Tj (charcodes_of_utf8 s)] | SpecialText s -> let s = process_specials pdf endpage filename bates batespad num page s in diff --git a/cpdfdraw.mli b/cpdfdraw.mli index b519ec8..04cd45e 100644 --- a/cpdfdraw.mli +++ b/cpdfdraw.mli @@ -18,8 +18,7 @@ type drawops = | SetMiterLimit of float | SetDashPattern of float list * float | Matrix of Pdftransform.transform_matrix - | Push - | Pop + | Qq of drawops list | Fill | FillEvenOdd | Stroke @@ -35,8 +34,7 @@ type drawops = | Opacity of float | SOpacity of float | Font of Pdftext.standard_font * float - | BT - | ET + | TextSection of drawops list | Text of string | SpecialText of string | Newline diff --git a/cpdfimpose.ml b/cpdfimpose.ml index 1a3531f..46211df 100644 --- a/cpdfimpose.ml +++ b/cpdfimpose.ml @@ -140,7 +140,8 @@ let impose_pages fit x y columns rtl btt center margin output_mediabox fast fit_ [Pdfops.stream_of_ops [Pdfops.Op_q; Pdfops.Op_cm transform]] @ contents @ [Pdfops.stream_of_ops [Pdfops.Op_Q]] else (* If slow, use protect from Pdfpage. *) - let ops = Pdfpage.protect pdf resources' contents @ Pdfops.parse_operators pdf resources' contents in + let ours = Pdfops.parse_operators pdf resources' contents in + let ops = Pdfpage.protect ours @ ours in [Pdfops.stream_of_ops ([Pdfops.Op_q] @ [Pdfops.Op_cm transform] @ ops @ [Pdfops.Op_Q])] in flatten