This commit is contained in:
John Whitington 2023-03-27 15:19:30 +01:00
parent d0060d9a5e
commit 36a8107cd0
1 changed files with 35 additions and 25 deletions

View File

@ -1,16 +1,26 @@
open Pdfutil open Pdfutil
open Cpdferror open Cpdferror
(* The content transformed by altering any use of [Op_cm]. But we must also (* When we transfor a page by wrapping in an [Op_cm], we must also
alter any /Matrix entries in pattern dictionaries *) invert any /Matrix entries in pattern dictionaries, including inside xobjects *)
let change_pattern_matrices_resources pdf tr resources = let rec change_pattern_matrices_resources pdf tr resources =
try try
begin match Pdf.lookup_direct pdf "/XObject" resources with
| Some (Pdf.Dictionary elts) ->
iter
(fun (k, v) ->
match v with
| Pdf.Indirect i -> change_pattern_matrices_xobject pdf tr k v i
| _ -> raise (Pdf.PDFError "change_pattern_matrices_page"))
elts
| _ -> ()
end;
begin match Pdf.lookup_direct pdf "/Pattern" resources with begin match Pdf.lookup_direct pdf "/Pattern" resources with
| Some (Pdf.Dictionary patterns) -> | Some (Pdf.Dictionary patterns) ->
let entries = let entries =
map map
(fun (name, p) -> (fun (name, p) ->
(*Printf.printf "Changing matrices of pattern %s\n" name;*) Printf.printf "Changing matrices of pattern %s\n" name;
let old_pattern = Pdf.direct pdf p in let old_pattern = Pdf.direct pdf p in
let new_pattern = let new_pattern =
let existing_tr = Pdf.parse_matrix pdf "/Matrix" old_pattern in let existing_tr = Pdf.parse_matrix pdf "/Matrix" old_pattern in
@ -22,11 +32,26 @@ let change_pattern_matrices_resources pdf tr resources =
in in
Pdf.add_dict_entry resources "/Pattern" (Pdf.Dictionary entries) Pdf.add_dict_entry resources "/Pattern" (Pdf.Dictionary entries)
| _ -> resources | _ -> resources
end end
with with
Pdftransform.NonInvertable -> Pdftransform.NonInvertable ->
Printf.eprintf "Warning: noninvertible matrix\n%!"; Printf.eprintf "Warning: noninvertible matrix\n%!";
resources resources
and change_pattern_matrices_xobject pdf tr k v i =
match Pdf.lookup_direct pdf "/Subtype" v with
| Some (Pdf.Name "/Form") ->
Printf.printf "Processing form xobject %s for patterns\n" k;
let form_xobject = Pdf.lookup_obj pdf i in
begin match Pdf.lookup_direct pdf "/Resources" form_xobject with
| Some resources ->
let form_xobject' =
Pdf.add_dict_entry form_xobject "/Resources" (change_pattern_matrices_resources pdf tr resources)
in
Pdf.addobj_given_num pdf (i, form_xobject')
| _ -> ()
end
| _ -> ()
let change_pattern_matrices_page pdf tr page = let change_pattern_matrices_page pdf tr page =
let page = let page =
@ -37,22 +62,7 @@ let change_pattern_matrices_page pdf tr page =
iter iter
(fun (k, v) -> (fun (k, v) ->
match v with match v with
| Pdf.Indirect i -> | Pdf.Indirect i -> change_pattern_matrices_xobject pdf tr k v i
(* Check if it's a form XObject. If so, rewrite its resources and add back as same number. *)
begin match Pdf.lookup_direct pdf "/Subtype" v with
| Some (Pdf.Name "/Form") ->
(*Printf.printf "Processing form xobject %s for patterns\n" k; *)
let form_xobject = Pdf.lookup_obj pdf i in
begin match Pdf.lookup_direct pdf "/Resources" form_xobject with
| Some resources ->
let form_xobject' =
Pdf.add_dict_entry form_xobject "/Resources" (change_pattern_matrices_resources pdf tr resources)
in
Pdf.addobj_given_num pdf (i, form_xobject')
| _ -> ()
end
| _ -> ()
end;
| _ -> raise (Pdf.PDFError "change_pattern_matrices_page")) | _ -> raise (Pdf.PDFError "change_pattern_matrices_page"))
elts; elts;
page page