From 17c8e1ae1a9a6511934c4d7fced8a3a04b0fbd59 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 7 Feb 2024 16:55:10 +0000 Subject: [PATCH] Changes for shift_boxes in cpdflib --- cpdfcommand.ml | 17 +++++++---------- cpdfpage.ml | 5 +++++ cpdfpage.mli | 2 ++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 270c19e..5d21fb5 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4034,22 +4034,19 @@ let go () = Cpdfannot.list_annotations range args.encoding pdf | Some Shift -> let pdf = get_single_pdf args.op false in - let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - let dxdylist = Cpdfcoord.parse_coordinates pdf args.coord in - write_pdf false (Cpdfpage.shift_pdf ~fast:args.fast dxdylist pdf range) + let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in + let dxdylist = Cpdfcoord.parse_coordinates pdf args.coord in + write_pdf false (Cpdfpage.shift_pdf ~fast:args.fast dxdylist pdf range) | Some ShiftBoxes -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in let dxdylist = Cpdfcoord.parse_coordinates pdf args.coord in - let dx, dy = match dxdylist with (a, b)::_ -> a, b | _ -> 0.0, 0.0 in - let f (xmin, ymin, xmax, ymax) = (xmin +. dx, ymin +. dy, xmax +. dx, ymax +. dy) in - let fpage _ p = Cpdfpage.change_boxes f pdf p in - write_pdf false (Cpdfpage.process_pages (Pdfpage.ppstub fpage) pdf range) + write_pdf false (Cpdfpage.shift_boxes dxdylist pdf range) | Some Scale -> let pdf = get_single_pdf args.op false in - let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - let sxsylist = Cpdfcoord.parse_coordinates pdf args.coord in - write_pdf false (Cpdfpage.scale_pdf ~fast:args.fast sxsylist pdf range) + let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in + let sxsylist = Cpdfcoord.parse_coordinates pdf args.coord in + write_pdf false (Cpdfpage.scale_pdf ~fast:args.fast sxsylist pdf range) | Some ScaleToFit -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in diff --git a/cpdfpage.ml b/cpdfpage.ml index e0c6662..7781460 100644 --- a/cpdfpage.ml +++ b/cpdfpage.ml @@ -310,6 +310,11 @@ let change_boxes f pdf page = make_mediabox (f (Pdf.parse_rectangle pdf page.Pdfpage.mediabox)); Pdfpage.rest = rest'} +let shift_boxes dxdylist pdf range = + let dx, dy = match dxdylist with (a, b)::_ -> a, b | _ -> 0.0, 0.0 in + let f (xmin, ymin, xmax, ymax) = (xmin +. dx, ymin +. dy, xmax +. dx, ymax +. dy) in + let fpage _ p = change_boxes f pdf p in + process_pages (Pdfpage.ppstub fpage) pdf range (* Scale contents *) let scale_page_contents ?(fast=false) scale position pdf pnum page = diff --git a/cpdfpage.mli b/cpdfpage.mli index 220a369..3574afa 100644 --- a/cpdfpage.mli +++ b/cpdfpage.mli @@ -23,6 +23,8 @@ val hard_box : Pdf.t -> int list -> string -> bool -> bool -> Pdf.t for all pages in pdf. *) val shift_pdf : ?fast:bool -> (float * float) list -> Pdf.t -> int list -> Pdf.t +val shift_boxes : (float * float) list -> Pdf.t -> int list -> Pdf.t + (** Change a page's media box so its minimum x and y are 0, making other operations simpler to think about. Any shift that is done is reflected in other boxes (clip etc.) *)