first resonable working chop

This commit is contained in:
John Whitington 2023-11-20 16:42:00 +02:00
parent c520bf9ff5
commit 70961eb692
3 changed files with 15 additions and 13 deletions

View File

@ -2,11 +2,13 @@ open Pdfutil
open Cpdferror open Cpdferror
(* FIXME Resources - what to do with each. Bookmarks? Annotations - must be duplicated... *) (* 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! *) (* 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 (* Chop a single page into pieces. We prefer the cropbox when available. We set
mediabox only, and delete any other boxes. *) mediabox only, and delete any other boxes. *)
(* FIXME btt / rtl *) (* FIXME btt / rtl / columns *)
let get_box pdf page = let get_box pdf page =
match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
| Some r -> Pdf.parse_rectangle pdf r | Some r -> Pdf.parse_rectangle pdf r
@ -16,7 +18,7 @@ let erase_boxes d =
let f = Pdf.remove_dict_entry in let f = Pdf.remove_dict_entry in
f (f (f (f d "/CropBox") "/BleedBox") "/TrimBox") "/ArtBox" 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 if x < 1 || y < 1 then Cpdferror.error "chop_boxes bad specification" else
let move_page mx my p w h dx dy = let move_page mx my p w h dx dy =
(*Printf.printf "move_page by %f %f\n" 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;*) (*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 w, h = (maxx -. minx) /. float_of_int x, (maxy -. miny) /. float_of_int y in
let ps = ref [] in let ps = ref [] in
for ty = y - 1 downto 0 do let move_page = move_page minx miny p w h in
for tx = 0 to x - 1 do (* columns, btt, rtl *)
ps =| move_page minx miny p w h (w *. float_of_int tx) (h *. float_of_int ty) for ty = y - 1 downto 0 do for tx = 0 to x - 1 do
done ps =| move_page (w *. float_of_int tx) (h *. float_of_int ty)
done; done done;
rev !ps rev !ps
(* Chop pages in the range into pieces *) (* 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 = Pdfpage.pages_of_pagetree pdf in
let pages = let pages =
flatten flatten
(map2 (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)) (ilist 1 (Pdfpage.endpage pdf))
pages) pages)
in in
let changes = [] in (* FIXME *) let changes = [] in
Pdfpage.change_pages ~changes true pdf pages Pdfpage.change_pages ~changes true pdf pages

View File

@ -1,4 +1,4 @@
(** Chop *) (** Chop *)
(** Chop *) (** Chop a page into pieces. *)
val chop : x:int -> y:int -> Pdf.t -> int list -> Pdf.t val chop : x:int -> y:int -> columns:bool -> btt:bool -> rtl:bool -> Pdf.t -> int list -> Pdf.t

View File

@ -4381,7 +4381,7 @@ let go () =
| Some (Chop (x, y)) -> | Some (Chop (x, y)) ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec_allow_empty pdf (get_pagespec ()) 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, (* Advise the user if a combination of command line flags makes little sense,
or error out if it make no sense at all. *) or error out if it make no sense at all. *)