From 4e9bae2596ced9ce9e3bcfd105f1cc1eccb366b2 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Sat, 21 Sep 2024 18:02:28 +0100 Subject: [PATCH] Adds -stretch implmentation --- cpdfpage.ml | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cpdfpage.ml b/cpdfpage.ml index 7e02209..83e6451 100644 --- a/cpdfpage.ml +++ b/cpdfpage.ml @@ -534,8 +534,27 @@ let scale_pdf ?(fast=false) sxsylist pdf range = in process_pages scale_page pdf range -let stretch ?(fast=false) xylist pdf range = - pdf +(* Scale without regard to aspect ratio. *) +let stretch ?(fast=false) sxsylist pdf range = + let stretch_page pnum page = + let sx, sy = List.nth sxsylist (pnum - 1) in + 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 + let scale_x, scale_y = sx /. (maxx -. minx), sy /. (maxy -. miny) in + let f (xmin, ymin, xmax, ymax) = xmin *. scale_x, ymin *. scale_y, xmax *. scale_x, ymax *. scale_y in + let page = change_boxes f pdf page in + let matrix = Pdftransform.matrix_of_op (Pdftransform.Scale ((0., 0.), scale_x, scale_y)) in + let transform_op = Pdfops.Op_cm matrix in + let page = change_pattern_matrices_page pdf matrix page in + Pdfannot.transform_annotations pdf matrix page.Pdfpage.rest; + (Pdfpage.prepend_operators pdf ~fast [transform_op] page, pnum, matrix) + in + process_pages stretch_page pdf range (* Scale to fit page of size x * y *) let scale_to_fit_pdf ?(fast=false) position input_scale xylist op pdf range =