From 199506b437c07fc0ec39ea5dfb97616a5f44b9dd Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 5 Feb 2024 14:38:05 +0000 Subject: [PATCH] Beginnings of -chop-v / -chop-h implementation --- cpdfchop.ml | 43 ++++++++++++++++++++++++++----------------- cpdfchop.mli | 2 +- cpdfcommand.ml | 4 ++-- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/cpdfchop.ml b/cpdfchop.ml index 8b3d534..d106f06 100644 --- a/cpdfchop.ml +++ b/cpdfchop.ml @@ -32,26 +32,33 @@ let make_pages x y columns btt rtl w h ps move_page = in if btt then for ty = 0 to y - 1 do row ty done else for ty = y - 1 downto 0 do row ty done -let chop_boxes pdf x y columns btt rtl p = - if x < 1 || y < 1 then error "chop_boxes bad specification" else - let move_page mx my p w h dx dy = - let nminx, nminy, nmaxx, nmaxy = (mx +. dx, my +. dy, mx +. w +. dx, my +. h +. dy) in - {p with - Pdfpage.mediabox = Pdf.Array [Pdf.Real nminx; Pdf.Real nminy; Pdf.Real nmaxx; Pdf.Real nmaxy]; - Pdfpage.rest = erase_boxes p.Pdfpage.rest} - in - let minx, miny, maxx, maxy = get_box pdf p in - let w, h = (maxx -. minx) /. float_of_int x, (maxy -. miny) /. float_of_int y in - let ps = ref [] in - make_pages x y columns btt rtl w h ps (move_page minx miny p w h); - rev !ps +let chop_boxes line pdf x y columns btt rtl p = + let move_page mx my p w h dx dy = + let nminx, nminy, nmaxx, nmaxy = (mx +. dx, my +. dy, mx +. w +. dx, my +. h +. dy) in + {p with + Pdfpage.mediabox = Pdf.Array [Pdf.Real nminx; Pdf.Real nminy; Pdf.Real nmaxx; Pdf.Real nmaxy]; + Pdfpage.rest = erase_boxes p.Pdfpage.rest} + in + if x = 0 then + (* horizontal split at line *) + [] + else + if y = 0 then + (* vertical split at line *) + [] + else + let minx, miny, maxx, maxy = get_box pdf p in + let w, h = (maxx -. minx) /. float_of_int x, (maxy -. miny) /. float_of_int y in + let ps = ref [] in + make_pages x y columns btt rtl w h ps (move_page minx miny p w h); + rev !ps -let chop ~x ~y ~columns ~btt ~rtl pdf range = +let chop_inner ~line ~x ~y ~columns ~btt ~rtl pdf range = let pages = Pdfpage.pages_of_pagetree pdf in let pages_out = flatten (map2 - (fun n p -> if mem n range then chop_boxes pdf x y columns btt rtl p else [p]) + (fun n p -> if mem n range then chop_boxes line pdf x y columns btt rtl p else [p]) (ilist 1 (Pdfpage.endpage pdf)) pages) in @@ -68,6 +75,8 @@ let chop ~x ~y ~columns ~btt ~rtl pdf range = in Pdfpage.change_pages ~changes true pdf pages_out +let chop ~x ~y ~columns ~btt ~rtl pdf range = + chop_inner ~line:0. ~x ~y ~columns ~btt ~rtl pdf range -let chop_hv ~is_h ~p ~columns pdf range = - pdf +let chop_hv ~is_h ~line ~columns pdf range = + chop_inner ~line ~x:(if is_h then 0 else 1) ~y:(if is_h then 1 else 0) ~columns ~btt:false ~rtl:false pdf range diff --git a/cpdfchop.mli b/cpdfchop.mli index 4e93861..5154650 100644 --- a/cpdfchop.mli +++ b/cpdfchop.mli @@ -3,4 +3,4 @@ (** Chop a page into pieces. *) val chop : x:int -> y:int -> columns:bool -> btt:bool -> rtl:bool -> Pdf.t -> int list -> Pdf.t -val chop_hv : is_h:bool -> p:float -> columns:bool -> Pdf.t -> int list -> Pdf.t +val chop_hv : is_h:bool -> line:float -> columns:bool -> Pdf.t -> int list -> Pdf.t diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 591d754..08479ff 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4460,10 +4460,10 @@ let go () = 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 ~columns:args.impose_columns ~btt:args.impose_btt ~rtl:args.impose_rtl pdf range) - | Some (ChopHV (is_h, p)) -> + | Some (ChopHV (is_h, line)) -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - write_pdf false (Cpdfchop.chop_hv ~is_h ~p ~columns:args.impose_columns pdf range) + write_pdf false (Cpdfchop.chop_hv ~is_h ~line ~columns:args.impose_columns pdf range) | Some ProcessImages -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in