From 70961eb692cef26f28505b809594960f0abfe973 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 20 Nov 2023 16:42:00 +0200 Subject: [PATCH] first resonable working chop --- cpdfchop.ml | 22 ++++++++++++---------- cpdfchop.mli | 4 ++-- cpdfcommand.ml | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cpdfchop.ml b/cpdfchop.ml index 0611d0f..bfa74cf 100644 --- a/cpdfchop.ml +++ b/cpdfchop.ml @@ -2,11 +2,13 @@ open Pdfutil open Cpdferror (* FIXME Resources - what to do with each. Bookmarks? Annotations - must be duplicated... *) +(* FIXME How to link annotations within the document work? *) +(* FIXME Are 'changes' for change_pages important? *) (* FIXME Test how the sharing affects other cpdf operations - especially with -fast! *) (* Chop a single page into pieces. We prefer the cropbox when available. We set mediabox only, and delete any other boxes. *) -(* FIXME btt / rtl *) +(* FIXME btt / rtl / columns *) let get_box pdf page = match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with | Some r -> Pdf.parse_rectangle pdf r @@ -16,7 +18,7 @@ let erase_boxes d = let f = Pdf.remove_dict_entry in f (f (f (f d "/CropBox") "/BleedBox") "/TrimBox") "/ArtBox" -let chop_boxes pdf x y p = +let chop_boxes pdf x y columns btt rtl p = if x < 1 || y < 1 then Cpdferror.error "chop_boxes bad specification" else let move_page mx my p w h dx dy = (*Printf.printf "move_page by %f %f\n" dx dy;*) @@ -30,22 +32,22 @@ let chop_boxes pdf x y p = (*Printf.printf "minx, miny, maxx, maxy = %f, %f, %f, %f\n" minx miny maxx maxy;*) let w, h = (maxx -. minx) /. float_of_int x, (maxy -. miny) /. float_of_int y in let ps = ref [] in - for ty = y - 1 downto 0 do - for tx = 0 to x - 1 do - ps =| move_page minx miny p w h (w *. float_of_int tx) (h *. float_of_int ty) - done - done; + let move_page = move_page minx miny p w h in + (* columns, btt, rtl *) + for ty = y - 1 downto 0 do for tx = 0 to x - 1 do + ps =| move_page (w *. float_of_int tx) (h *. float_of_int ty) + done done; rev !ps (* Chop pages in the range into pieces *) -let chop ~x ~y pdf range = +let chop ~x ~y ~columns ~btt ~rtl pdf range = let pages = Pdfpage.pages_of_pagetree pdf in let pages = flatten (map2 - (fun n p -> if mem n range then (chop_boxes pdf x y p) else [p]) + (fun n p -> if mem n range then chop_boxes pdf x y columns btt rtl p else [p]) (ilist 1 (Pdfpage.endpage pdf)) pages) in - let changes = [] in (* FIXME *) + let changes = [] in Pdfpage.change_pages ~changes true pdf pages diff --git a/cpdfchop.mli b/cpdfchop.mli index df6a08f..3bf8803 100644 --- a/cpdfchop.mli +++ b/cpdfchop.mli @@ -1,4 +1,4 @@ (** Chop *) -(** Chop *) -val chop : x:int -> y:int -> Pdf.t -> int list -> Pdf.t +(** Chop a page into pieces. *) +val chop : x:int -> y:int -> columns:bool -> btt:bool -> rtl:bool -> Pdf.t -> int list -> Pdf.t diff --git a/cpdfcommand.ml b/cpdfcommand.ml index e78692f..114ceca 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4381,7 +4381,7 @@ let go () = | Some (Chop (x, y)) -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - write_pdf false (Cpdfchop.chop ~x ~y pdf range) + write_pdf false (Cpdfchop.chop ~x ~y ~columns:args.impose_columns ~btt:args.impose_btt ~rtl:args.impose_rtl pdf range) (* Advise the user if a combination of command line flags makes little sense, or error out if it make no sense at all. *)