-chop coming along

This commit is contained in:
John Whitington 2023-11-20 16:12:33 +02:00
parent 428f037a51
commit c520bf9ff5
1 changed files with 21 additions and 12 deletions

View File

@ -6,24 +6,33 @@ open Cpdferror
(* 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 check non-zero based boxes *)
(* FIXME erase boxes *)
(* FIXME Get mediabox / cropbox properly *)
(* FIXME Check rotated pages just work *)
(* FIXME btt / rtl *) (* FIXME btt / rtl *)
let chop_boxes x y p = let get_box pdf page =
match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
| Some r -> Pdf.parse_rectangle pdf r
| None -> Pdf.parse_rectangle pdf page.Pdfpage.mediabox
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 =
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 p w h dx dy = let move_page mx my p w h dx dy =
(*Printf.printf "move_page %f %f\n" dx dy;*) (*Printf.printf "move_page by %f %f\n" dx dy;*)
let nminx, nminy, nmaxx, nmaxy = (0. +. dx, 0. +. dy, w +. dx, h +. dy) in 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]} (*Printf.printf "new box: %f, %f, %f, %f\n" nminx nminy nmaxx nmaxy;*)
{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 in
let minx, miny, maxx, maxy = 0., 0., 540., 666. in let minx, miny, maxx, maxy = get_box pdf p in
(*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 for ty = y - 1 downto 0 do
for tx = 0 to x - 1 do for tx = 0 to x - 1 do
ps =| move_page p w h (w *. float_of_int tx) (h *. float_of_int ty) ps =| move_page minx miny p w h (w *. float_of_int tx) (h *. float_of_int ty)
done done
done; done;
rev !ps rev !ps
@ -34,7 +43,7 @@ let chop ~x ~y pdf range =
let pages = let pages =
flatten flatten
(map2 (map2
(fun n p -> if mem n range then (chop_boxes x y p) else [p]) (fun n p -> if mem n range then (chop_boxes pdf x y p) else [p])
(ilist 1 (Pdfpage.endpage pdf)) (ilist 1 (Pdfpage.endpage pdf))
pages) pages)
in in