This commit is contained in:
John Whitington 2023-05-03 15:49:14 +01:00
parent 9040630487
commit d7b04f0bb0
2 changed files with 32 additions and 18 deletions

View File

@ -1777,9 +1777,30 @@ let whingemalformed () =
(* Drawing operations. *) (* Drawing operations. *)
let drawops = ref [] 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 = let addop o =
match !we_are_saving with
| Some n ->
Hashtbl.replace saved_ops n (o::Hashtbl.find saved_ops n)
| None ->
drawops := o::!drawops 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 let tdeep = ref 0
let addbt () = let addbt () =
@ -1942,13 +1963,8 @@ let setmsheary s =
| [a; b; c] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.ShearY ((a, b), c)])) | [a; b; c] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.ShearY ((a, b), c)]))
| _ | exception _ -> error "-msheary takes three numbers" | _ | exception _ -> error "-msheary takes three numbers"
let saved_ops = ref []
let setxobj s =
saved_ops := []
let usexobj s = let usexobj s =
addop (Cpdfdraw.Use "/X1") addop (Cpdfdraw.Use s)
let obj_of_jpeg_data data = let obj_of_jpeg_data data =
let w, h = Cpdfjpeg.jpeg_dimensions data in let w, h = Cpdfjpeg.jpeg_dimensions data in
@ -2929,7 +2945,8 @@ and specs =
("-mscale", Arg.String setmscale, " Scale the graphics matrix"); ("-mscale", Arg.String setmscale, " Scale the graphics matrix");
("-mshearx", Arg.String setmshearx, " Shear the graphics matrix in X"); ("-mshearx", Arg.String setmshearx, " Shear the graphics matrix in X");
("-msheary", Arg.String setmshearx, " Shear the graphics matrix in Y"); ("-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"); ("-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-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"); ("-draw-png", Arg.String addpng, " Load a PNG from file and name it");

View File

@ -54,9 +54,13 @@ type drawops =
| URL of string | URL of string
| EndURL | EndURL
(* Hash table of (human name, (resources name, object)) for image xobjects *) (* Per page resources *)
let images = null_hash () let images = null_hash ()
let gss = 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. *) (* Fresh XObject names. If we are stamping over another page, manage clashes later. *)
let fresh_xobj_name () = "/Img0" let fresh_xobj_name () = "/Img0"
@ -67,21 +71,15 @@ let fresh_gs_name () =
gsnum += 1; gsnum += 1;
"/gs" ^ string_of_int !gsnum "/gs" ^ string_of_int !gsnum
let current_url = ref None
let fontnum = ref 0 let fontnum = ref 0
let fonts = null_hash ()
let current_font = ref (Pdftext.StandardFont (Pdftext.TimesRoman, Pdftext.WinAnsiEncoding))
let fresh_font_name pdf f = let fresh_font_name pdf f =
fontnum += 1; fontnum += 1;
let n = "/F" ^ string_of_int !fontnum in let n = "/F" ^ string_of_int !fontnum in
Hashtbl.add fonts n (Pdf.Indirect (Pdftext.write_font pdf f)); Hashtbl.add fonts n (Pdf.Indirect (Pdftext.write_font pdf f));
n 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 () = 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] | SetLineJoin j -> [Pdfops.Op_j j]
| SetMiterLimit m -> [Pdfops.Op_M m] | SetMiterLimit m -> [Pdfops.Op_M m]
| SetDashPattern (x, y) -> [Pdfops.Op_d (x, y)] | SetDashPattern (x, y) -> [Pdfops.Op_d (x, y)]
(*| Use l -> | Use n -> [Pdfops.Op_Do n]
[Pdfops.Op_q] @ ops_of_drawops pdf endpage filename bates batespad num page l @ [Pdfops.Op_Q]*)
| Image s -> [Pdfops.Op_Do (try fst (Hashtbl.find images s) with _ -> Cpdferror.error ("Image not found: " ^ s))] | Image s -> [Pdfops.Op_Do (try fst (Hashtbl.find images s) with _ -> Cpdferror.error ("Image not found: " ^ s))]
| ImageXObject (s, obj) -> | ImageXObject (s, obj) ->
Hashtbl.add images s (fresh_xobj_name (), Pdf.addobj pdf obj); Hashtbl.add images s (fresh_xobj_name (), Pdf.addobj pdf obj);