diff --git a/cpdfcommand.ml b/cpdfcommand.ml index e4fa596..1dc61bc 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1777,8 +1777,29 @@ let whingemalformed () = (* Drawing operations. *) let drawops = ref [] +let saved_ops = + Hashtbl.create 16 + +let we_are_saving = ref None + +let startxobj n = + we_are_saving := Some n; + Hashtbl.add saved_ops n [] + let addop o = - drawops := o::!drawops + match !we_are_saving with + | Some n -> + Hashtbl.replace saved_ops n (o::Hashtbl.find saved_ops n) + | None -> + drawops := o::!drawops + +let endxobj () = + match !we_are_saving with + | Some n -> + we_are_saving := None; + addop (Cpdfdraw.FormXObject (rev (Hashtbl.find saved_ops n))) + | None -> + error "misplaced -endxobj" let tdeep = ref 0 @@ -1942,13 +1963,8 @@ let setmsheary s = | [a; b; c] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.ShearY ((a, b), c)])) | _ | exception _ -> error "-msheary takes three numbers" -let saved_ops = ref [] - -let setxobj s = - saved_ops := [] - let usexobj s = - addop (Cpdfdraw.Use "/X1") + addop (Cpdfdraw.Use s) let obj_of_jpeg_data data = let w, h = Cpdfjpeg.jpeg_dimensions data in @@ -2929,7 +2945,8 @@ and specs = ("-mscale", Arg.String setmscale, " Scale the graphics matrix"); ("-mshearx", Arg.String setmshearx, " Shear the graphics matrix in X"); ("-msheary", Arg.String setmshearx, " Shear the graphics matrix in Y"); - ("-xobj", Arg.String setxobj, " Save a sequence of graphics operators"); + ("-xobj", Arg.String startxobj, " Beign saving a sequence of graphics operators"); + ("-endxobj", Arg.Unit endxobj, " End saving a sequence of graphics operators"); ("-use", Arg.String usexobj, " Use a saved sequence of graphics operators"); ("-draw-jpeg", Arg.String addjpeg, " Load a JPEG from file and name it"); ("-draw-png", Arg.String addpng, " Load a PNG from file and name it"); diff --git a/cpdfdraw.ml b/cpdfdraw.ml index 92f84aa..8a3b803 100644 --- a/cpdfdraw.ml +++ b/cpdfdraw.ml @@ -54,9 +54,13 @@ type drawops = | URL of string | EndURL -(* Hash table of (human name, (resources name, object)) for image xobjects *) +(* Per page resources *) let images = null_hash () let gss = null_hash () +let current_url = ref None +let fonts = null_hash () + +let current_font = ref (Pdftext.StandardFont (Pdftext.TimesRoman, Pdftext.WinAnsiEncoding)) (* Fresh XObject names. If we are stamping over another page, manage clashes later. *) let fresh_xobj_name () = "/Img0" @@ -67,21 +71,15 @@ let fresh_gs_name () = gsnum += 1; "/gs" ^ string_of_int !gsnum -let current_url = ref None - let fontnum = ref 0 -let fonts = null_hash () - -let current_font = ref (Pdftext.StandardFont (Pdftext.TimesRoman, Pdftext.WinAnsiEncoding)) - let fresh_font_name pdf f = fontnum += 1; let n = "/F" ^ string_of_int !fontnum in Hashtbl.add fonts n (Pdf.Indirect (Pdftext.write_font pdf f)); n -(* This will remove fonts, images etc, for moving on to the next page *) +(* FIXME This will remove fonts, images etc, for moving on to the next page *) let reset_state () = () @@ -128,8 +126,7 @@ let rec ops_of_drawop pdf endpage filename bates batespad num page = function | SetLineJoin j -> [Pdfops.Op_j j] | SetMiterLimit m -> [Pdfops.Op_M m] | SetDashPattern (x, y) -> [Pdfops.Op_d (x, y)] - (*| Use l -> - [Pdfops.Op_q] @ ops_of_drawops pdf endpage filename bates batespad num page l @ [Pdfops.Op_Q]*) + | Use n -> [Pdfops.Op_Do n] | Image s -> [Pdfops.Op_Do (try fst (Hashtbl.find images s) with _ -> Cpdferror.error ("Image not found: " ^ s))] | ImageXObject (s, obj) -> Hashtbl.add images s (fresh_xobj_name (), Pdf.addobj pdf obj);