From 08f7e14fab2b226349d14078315438445f8a490d Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 3 Jun 2015 17:30:59 +0100 Subject: [PATCH] Clipping for -twoup-stack --- cpdf.ml | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index 9a28179..b883ea6 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -2769,13 +2769,24 @@ let twoup_pages_stack pdf = function Pdf.remove_dict_entry h.Pdfpage.rest "/CropBox" in let content' = - let transform_stream contents transform = - let ops = Pdfops.parse_operators pdf resources contents in - Pdfops.stream_of_ops - ([Pdfops.Op_q] @ [Pdfops.Op_cm transform] @ ops @ [Pdfops.Op_Q]) + let transform_stream clipbox contents transform = + let clipops = + let minx, miny, maxx, maxy = Pdf.parse_rectangle clipbox in + [Pdfops.Op_re (minx, miny, maxx -. minx, maxy -. miny); + Pdfops.Op_n; + Pdfops.Op_W] + in + let ops = Pdfops.parse_operators pdf resources contents in + Pdfops.stream_of_ops + ([Pdfops.Op_q] @ [Pdfops.Op_cm transform] @ clipops @ ops @ [Pdfops.Op_Q]) in map2 - (fun p -> transform_stream p.Pdfpage.content) + (fun p -> + transform_stream + (match Pdf.lookup_direct pdf "/CropBox" p.Pdfpage.rest with + None -> p.Pdfpage.mediabox + | Some box -> box) + p.Pdfpage.content) pages (take (twoup_stack_transforms h.Pdfpage.mediabox) (length pages)) in