This commit is contained in:
John Whitington 2023-03-23 19:17:16 +00:00
parent f406397cc8
commit c2e89565a2
4 changed files with 10 additions and 90 deletions

View File

@ -146,7 +146,7 @@ let impose_pages fit x y columns rtl btt center margin output_mediabox fast fit_
in in
flatten flatten
(map2 (map2
(fun p t -> Cpdfutil.transform_annotations pdf t p.Pdfpage.rest; transform_stream t p.Pdfpage.content) (fun p t -> Pdfannot.transform_annotations pdf t p.Pdfpage.rest; transform_stream t p.Pdfpage.content)
pages pages
transforms) transforms)
in in

View File

@ -91,7 +91,7 @@ let shift_page ?(fast=false) dxdylist pdf pnum page =
(*let page = (*let page =
Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.mktranslate ~-.dx ~-.dy) page Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.mktranslate ~-.dx ~-.dy) page
in*) in*)
Cpdfutil.transform_annotations pdf (Pdftransform.mktranslate dx dy) page.Pdfpage.rest; Pdfannot.transform_annotations pdf (Pdftransform.mktranslate dx dy) page.Pdfpage.rest;
(Pdfpage.prepend_operators pdf [transform_op] ~fast page, pnum, Pdftransform.mktranslate dx dy) (Pdfpage.prepend_operators pdf [transform_op] ~fast page, pnum, Pdftransform.mktranslate dx dy)
let shift_pdf ?(fast=false) dxdylist pdf range = let shift_pdf ?(fast=false) dxdylist pdf range =
@ -163,7 +163,7 @@ let scale_page_contents ?(fast=false) scale position pdf pnum page =
in in
let transform_op = Pdfops.Op_cm transform in let transform_op = Pdfops.Op_cm transform in
(*let page = Cpdfutil.change_pattern_matrices_page pdf transform page in*) (*let page = Cpdfutil.change_pattern_matrices_page pdf transform page in*)
Cpdfutil.transform_annotations pdf transform page.Pdfpage.rest; Pdfannot.transform_annotations pdf transform page.Pdfpage.rest;
(Pdfpage.prepend_operators pdf [transform_op] ~fast page, pnum, transform) (Pdfpage.prepend_operators pdf [transform_op] ~fast page, pnum, transform)
let scale_contents ?(fast=false) position scale pdf range = let scale_contents ?(fast=false) position scale pdf range =
@ -260,7 +260,7 @@ let transform_boxes tr pdf page =
let transform_contents ?(fast=false) tr pdf page = let transform_contents ?(fast=false) tr pdf page =
let transform_op = Pdfops.Op_cm tr in let transform_op = Pdfops.Op_cm tr in
(*let page = Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.matrix_invert tr) page in*) (*let page = Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.matrix_invert tr) page in*)
Cpdfutil.transform_annotations pdf tr page.Pdfpage.rest; Pdfannot.transform_annotations pdf tr page.Pdfpage.rest;
Pdfpage.prepend_operators pdf [transform_op] ~fast page Pdfpage.prepend_operators pdf [transform_op] ~fast page
(* Change a page's media box so its minimum x and y are 0, making other (* Change a page's media box so its minimum x and y are 0, making other
@ -321,7 +321,7 @@ let rotate_page_contents ~fast rotpoint r pdf pnum page =
in in
let transform_op = Pdfops.Op_cm tr in let transform_op = Pdfops.Op_cm tr in
(*let page = Cpdfutil.change_pattern_matrices_page pdf tr2 page in*) (*let page = Cpdfutil.change_pattern_matrices_page pdf tr2 page in*)
Cpdfutil.transform_annotations pdf tr page.Pdfpage.rest; Pdfannot.transform_annotations pdf tr page.Pdfpage.rest;
(Pdfpage.prepend_operators pdf [transform_op] ~fast page, pnum, tr) (Pdfpage.prepend_operators pdf [transform_op] ~fast page, pnum, tr)
let rotate_contents ?(fast=false) r pdf range = let rotate_contents ?(fast=false) r pdf range =
@ -341,7 +341,7 @@ let scale_pdf ?(fast=false) sxsylist pdf range =
(*and page = (*and page =
Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.matrix_invert matrix) page*) Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.matrix_invert matrix) page*)
in in
Cpdfutil.transform_annotations pdf matrix page.Pdfpage.rest; Pdfannot.transform_annotations pdf matrix page.Pdfpage.rest;
(Pdfpage.prepend_operators pdf ~fast [transform_op] page, pnum, matrix) (Pdfpage.prepend_operators pdf ~fast [transform_op] page, pnum, matrix)
in in
process_pages scale_page pdf range process_pages scale_page pdf range
@ -382,7 +382,7 @@ let scale_to_fit_pdf ?(fast=false) position input_scale xylist op pdf range =
(function (minx, miny, maxx, maxy) -> 0., 0., x, y) (function (minx, miny, maxx, maxy) -> 0., 0., x, y)
pdf page pdf page
in in
Cpdfutil.transform_annotations pdf matrix page.Pdfpage.rest; Pdfannot.transform_annotations pdf matrix page.Pdfpage.rest;
(Pdfpage.prepend_operators pdf [Pdfops.Op_cm matrix] ~fast (Pdfpage.prepend_operators pdf [Pdfops.Op_cm matrix] ~fast
((*Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.matrix_invert matrix)*) page), pnum, matrix) ((*Cpdfutil.change_pattern_matrices_page pdf (Pdftransform.matrix_invert matrix)*) page), pnum, matrix)
in in
@ -423,7 +423,7 @@ let flip_page ?(fast=false) transform_op pdf pnum page =
in in
let tr = transform_op minx miny maxx maxy in let tr = transform_op minx miny maxx maxy in
(*let page = Cpdfutil.change_pattern_matrices_page pdf tr page in*) (*let page = Cpdfutil.change_pattern_matrices_page pdf tr page in*)
Cpdfutil.transform_annotations pdf tr page.Pdfpage.rest; Pdfannot.transform_annotations pdf tr page.Pdfpage.rest;
(Pdfpage.prepend_operators pdf [Pdfops.Op_cm tr] ~fast page, pnum, tr) (Pdfpage.prepend_operators pdf [Pdfops.Op_cm tr] ~fast page, pnum, tr)
let vflip_pdf ?(fast=false) pdf range = let vflip_pdf ?(fast=false) pdf range =
@ -513,7 +513,7 @@ let do_stamp relative_to_cropbox fast position topline midline scale_to_fit isov
(if relative_to_cropbox then [Pdftransform.Translate (txmin, tymin)] else []) @ (if relative_to_cropbox then [Pdftransform.Translate (txmin, tymin)] else []) @
[Pdftransform.Scale ((sxmin, symin), scale, scale)])) [Pdftransform.Scale ((sxmin, symin), scale, scale)]))
in in
Cpdfutil.transform_annotations pdf matrix o.Pdfpage.rest; Pdfannot.transform_annotations pdf matrix o.Pdfpage.rest;
let r = Pdfpage.prepend_operators pdf [Pdfops.Op_cm matrix] ~fast o in let r = Pdfpage.prepend_operators pdf [Pdfops.Op_cm matrix] ~fast o in
(*Cpdfutil.change_pattern_matrices_page pdf matrix*) r (*Cpdfutil.change_pattern_matrices_page pdf matrix*) r
else else
@ -525,7 +525,7 @@ let do_stamp relative_to_cropbox fast position topline midline scale_to_fit isov
((if relative_to_cropbox then [Pdftransform.Translate (txmin, tymin)] else []) @ ((if relative_to_cropbox then [Pdftransform.Translate (txmin, tymin)] else []) @
[Pdftransform.Translate (dx, dy)])) [Pdftransform.Translate (dx, dy)]))
in in
Cpdfutil.transform_annotations pdf matrix o.Pdfpage.rest; Pdfannot.transform_annotations pdf matrix o.Pdfpage.rest;
let r = Pdfpage.prepend_operators pdf [Pdfops.Op_cm matrix] ~fast o in let r = Pdfpage.prepend_operators pdf [Pdfops.Op_cm matrix] ~fast o in
(*Cpdfutil.change_pattern_matrices_page pdf matrix*) r (*Cpdfutil.change_pattern_matrices_page pdf matrix*) r
in in

View File

@ -134,81 +134,3 @@ let combine_pdf_resources pdf a b =
(fun dict (k, v) -> Pdf.add_dict_entry dict k v) (fun dict (k, v) -> Pdf.add_dict_entry dict k v)
(Pdf.Dictionary []) (Pdf.Dictionary [])
(unknown_keys_a @ unknown_keys_b @ combined_known_entries) (unknown_keys_a @ unknown_keys_b @ combined_known_entries)
let transform_rect pdf transform rect =
let minx, miny, maxx, maxy = Pdf.parse_rectangle pdf rect in
let (x0, y0) = Pdftransform.transform_matrix transform (minx, miny) in
let (x1, y1) = Pdftransform.transform_matrix transform (maxx, maxy) in
let (x2, y2) = Pdftransform.transform_matrix transform (minx, maxy) in
let (x3, y3) = Pdftransform.transform_matrix transform (maxx, miny) in
let minx = fmin (fmin x0 x1) (fmin x2 x3) in
let miny = fmin (fmin y0 y1) (fmin y2 y3) in
let maxx = fmax (fmax x0 x1) (fmax x2 x3) in
let maxy = fmax (fmax y0 y1) (fmax y2 y3) in
Pdf.Array [Pdf.Real minx; Pdf.Real miny; Pdf.Real maxx; Pdf.Real maxy]
let transform_quadpoint_single pdf transform = function
| [x1; y1; x2; y2; x3; y3; x4; y4] ->
let x1, y1, x2, y2, x3, y3, x4, y4 =
Pdf.getnum pdf x1, Pdf.getnum pdf y1,
Pdf.getnum pdf x2, Pdf.getnum pdf y2,
Pdf.getnum pdf x3, Pdf.getnum pdf y3,
Pdf.getnum pdf x4, Pdf.getnum pdf y4
in
let (x1, y1) = Pdftransform.transform_matrix transform (x1, y1) in
let (x2, y2) = Pdftransform.transform_matrix transform (x2, y2) in
let (x3, y3) = Pdftransform.transform_matrix transform (x3, y3) in
let (x4, y4) = Pdftransform.transform_matrix transform (x4, y4) in
map (fun x -> Pdf.Real x) [x1; y1; x2; y2; x3; y3; x4; y4]
| qp ->
Printf.eprintf "Malformed /QuadPoints format: must be a multiple of 8 entries\n";
qp
let transform_quadpoints pdf transform = function
| Pdf.Array qps ->
Pdf.Array (flatten (map (transform_quadpoint_single pdf transform) (splitinto 8 qps)))
| qp ->
Printf.eprintf "Unknown or malformed /QuadPoints format %s\n" (Pdfwrite.string_of_pdf qp);
qp
(* Apply transformations to any annotations in /Annots (i.e their /Rect and
/QuadPoints entries). Also as a best-effort service, altering other
coordinates, like the endpoints /L in a line annotation. *)
let transform_annotations pdf transform rest =
match Pdf.lookup_direct pdf "/Annots" rest with
| Some (Pdf.Array annots) ->
(* Always indirect references, so alter in place *)
iter
(function
| Pdf.Indirect i ->
let annot = Pdf.lookup_obj pdf i in
let rect' =
match Pdf.lookup_direct pdf "/Rect" annot with
| Some rect -> transform_rect pdf transform rect
| None -> raise (Pdf.PDFError "transform_annotations: no rect")
in
let quadpoints' =
match Pdf.lookup_direct pdf "/QuadPoints" annot with
| Some qp -> Some (transform_quadpoints pdf transform qp)
| None -> None
in
let line' =
match Pdf.lookup_direct pdf "/L" annot with
| Some rect -> Some (transform_rect pdf transform rect)
| _ -> None
in
let annot = Pdf.add_dict_entry annot "/Rect" rect' in
let annot =
match quadpoints' with
| Some qp -> Pdf.add_dict_entry annot "/QuadPoints" qp
| None -> annot
in
let annot =
match line' with
| Some l -> Pdf.add_dict_entry annot "/L" l
| None -> annot
in
Pdf.addobj_given_num pdf (i, annot)
| _ -> Printf.eprintf "transform_annotations: not indirect\n%!")
annots
| _ -> ()

View File

@ -8,6 +8,4 @@ val process_xobjects : Pdf.t ->
val combine_pdf_resources : Pdf.t -> Pdf.pdfobject -> Pdf.pdfobject -> Pdf.pdfobject val combine_pdf_resources : Pdf.t -> Pdf.pdfobject -> Pdf.pdfobject -> Pdf.pdfobject
val transform_annotations : Pdf.t -> Pdftransform.transform_matrix -> Pdf.pdfobject -> unit
val ppstub : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c * 'a * Pdftransform.transform_matrix val ppstub : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c * 'a * Pdftransform.transform_matrix