From 9b94932c3221449aff726c362c2e0fe74f52059e Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 19 Mar 2019 14:03:28 +0000 Subject: [PATCH] scale_to_fit_pages positioning --- cpdf.ml | 25 ++++++++++++++++++++----- cpdf.mli | 4 ++-- cpdfcommand.ml | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index 1585c5a..74d86dc 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -953,10 +953,13 @@ let verify_bookmarks pdf lastlevel endpage marks = let rec fixup_characters prev = function | [] -> rev prev | '\\'::'\\'::t -> fixup_characters ('\\'::prev) t - | '\\'::'\"'::t -> fixup_characters ('\"'::prev) t - | '\\'::'\n'::t -> fixup_characters ('\n'::prev) t + | '\\'::'"'::t -> fixup_characters ('\"'::prev) t + | '\\'::'\n'::t -> fixup_characters ('\n'::prev) t (* This can never have been right? *) | h::t -> fixup_characters (h::prev) t +let debug_bookmark_string s = + Printf.printf "STR: %s\n" s + (* If optionaldest = [Pdfgenlex.LexString s], we parse the string, convert the * integer to an indirect of the real page target, and then put it in. *) let bookmark_of_data pdf i s i' isopen optionaldest = @@ -983,6 +986,9 @@ let bookmark_of_data pdf i s i' isopen optionaldest = end | _ -> Pdfpage.target_of_pagenumber pdf i' in + (*debug_bookmark_string s; + debug_bookmark_string (implode (fixup_characters [] (explode s))); + debug_bookmark_string (Pdftext.pdfdocstring_of_utf8 (implode (fixup_characters [] (explode s))));*) {Pdfmarks.level = i; Pdfmarks.text = Pdftext.pdfdocstring_of_utf8 (implode (fixup_characters [] (explode s))); Pdfmarks.target = target; @@ -2780,7 +2786,7 @@ let scale_pdf ?(fast=false) sxsylist pdf range = process_pages scale_page pdf range (* Scale to fit page of size x * y *) -let scale_to_fit_pdf ?(fast=false) input_scale xylist op pdf range = +let scale_to_fit_pdf ?(fast=false) position input_scale xylist op pdf range = let scale_page_to_fit pnum page = let x, y = List.nth xylist (pnum - 1) in let matrix = @@ -2794,8 +2800,17 @@ let scale_to_fit_pdf ?(fast=false) input_scale xylist op pdf range = if maxx <= 0. || maxy <= 0. then failwith "Zero-sized pages are invalid" else let fx = x /. maxx in let fy = y /. maxy in let scale = fmin fx fy *. input_scale in - let trans_x = (x -. (maxx *. scale)) /. 2. - in let trans_y = (y -. (maxy *. scale)) /. 2. in + let trans_x = + match position with + Left _ -> 0. + | Right _ -> (x -. (maxx *. scale)) + | _ -> (x -. (maxx *. scale)) /. 2. + and trans_y = + match position with + | Top _ -> (y -. (maxy *. scale)) + | Bottom _ -> 0. + | _ -> (y -. (maxy *. scale)) /. 2. + in (Pdftransform.matrix_of_transform [Pdftransform.Translate (trans_x, trans_y); Pdftransform.Scale ((0., 0.), scale, scale)]) diff --git a/cpdf.mli b/cpdf.mli index 3eec02b..11450fa 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -353,10 +353,10 @@ val shift_pdf : ?fast:bool -> (float * float) list -> Pdf.t -> int list -> Pdf.t for all pages in pdf. *) val scale_pdf : ?fast:bool -> (float * float) list -> Pdf.t -> int list -> Pdf.t -(** [scale_to_fit_pdf input_scale x y op pdf range] scales a page to fit the +(** [scale_to_fit_pdf fast position input_scale x y op pdf range] scales a page to fit the page size given by (x, y) and by the [input_scale] (e.g 1.0 = scale to fit, 0.9 = scale to fit leaving a border etc.). [op] is unused. *) -val scale_to_fit_pdf : ?fast:bool -> float -> (float * float) list -> 'a -> Pdf.t -> int list -> Pdf.t +val scale_to_fit_pdf : ?fast:bool -> position -> float -> (float * float) list -> 'a -> Pdf.t -> int list -> Pdf.t (** Scale the contents of a page by a given factor centred around a given point in a given range. *) val scale_contents : ?fast:bool -> position -> float -> Pdf.t -> int list -> Pdf.t diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 3be0a7e..fc31ba9 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3745,7 +3745,7 @@ let go () = let range = parse_pagespec pdf (get_pagespec ()) in let xylist = parse_coordinates pdf args.coord and scale = args.scale in - write_pdf false (Cpdf.scale_to_fit_pdf ~fast:args.fast scale xylist args.op pdf range) + write_pdf false (Cpdf.scale_to_fit_pdf ~fast:args.fast args.position scale xylist args.op pdf range) | Some (ScaleContents scale) -> let pdf = get_single_pdf args.op false in let range = parse_pagespec pdf (get_pagespec ()) in