First implementation for center_to_fit

This commit is contained in:
John Whitington 2024-10-31 14:27:01 +00:00
parent f85f2f67b0
commit 5d7da8687c
1 changed files with 36 additions and 19 deletions

View File

@ -556,13 +556,44 @@ let stretch ?(fast=false) sxsylist pdf range =
in in
process_pages stretch_page pdf range process_pages stretch_page pdf range
(* Cropping *)
let crop_pdf ?(box="/CropBox") xywhlist pdf range =
let crop_page pagenum page =
{page with
Pdfpage.rest =
(Pdf.add_dict_entry
page.Pdfpage.rest
box
(let x, y, w, h = List.nth xywhlist (pagenum - 1) in
(Pdf.Array
[Pdf.Real x; Pdf.Real y;
Pdf.Real (x +. w); Pdf.Real (y +. h)])))}
in
process_pages (Pdfpage.ppstub crop_page) pdf range
(* Centre page content (crop box) on given page size, with no scaling. *) (* Centre page content (crop box) on given page size, with no scaling. *)
let center_to_fit sxsylist pdf range = let center_to_fit sxsylist pdf range =
(* Make dxdylist, suitable for shift_boxes *) let list4 = map (fun (x, y) -> (0., 0., x, y)) sxsylist in
(* Set the new page size, as media and crop *) let pdf = set_mediabox list4 pdf range in
(* Call shift_boxes to move each page. *) let pdf = crop_pdf list4 pdf range in
let dxdylist = sxsylist in let pdf = remove_bleed_pdf pdf range in
shift_boxes dxdylist pdf range let pdf = remove_art_pdf pdf range in
let pdf = remove_bleed_pdf pdf range in
let dxdylist =
let tx, ty = hd sxsylist in
map
(fun page ->
let (minx, miny, maxx, maxy) =
Pdf.parse_rectangle
pdf
(match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
| Some r -> r
| None -> page.Pdfpage.mediabox)
in
(tx -. (maxx -. minx)) /. 2., (ty -. (maxy -. miny)) /. 2.)
(Pdfpage.pages_of_pagetree pdf)
in
shift_boxes dxdylist pdf range
(* Scale to fit page of size x * y *) (* Scale to fit page of size x * y *)
let scale_to_fit_pdf ?(fast=false) position input_scale xylist op pdf range = let scale_to_fit_pdf ?(fast=false) position input_scale xylist op pdf range =
@ -887,20 +918,6 @@ let setBox box minx maxx miny maxy pdf range =
in in
process_pages (Pdfpage.ppstub set_box_page) pdf range process_pages (Pdfpage.ppstub set_box_page) pdf range
(* Cropping *)
let crop_pdf ?(box="/CropBox") xywhlist pdf range =
let crop_page pagenum page =
{page with
Pdfpage.rest =
(Pdf.add_dict_entry
page.Pdfpage.rest
box
(let x, y, w, h = List.nth xywhlist (pagenum - 1) in
(Pdf.Array
[Pdf.Real x; Pdf.Real y;
Pdf.Real (x +. w); Pdf.Real (y +. h)])))}
in
process_pages (Pdfpage.ppstub crop_page) pdf range
(* Add rectangles on top of pages to show Media, Crop, Art, Trim, Bleed boxes. (* Add rectangles on top of pages to show Media, Crop, Art, Trim, Bleed boxes.
* *