scale_to_fit_pages positioning

This commit is contained in:
John Whitington 2019-03-19 14:03:28 +00:00
parent 425a2d692f
commit 9b94932c32
3 changed files with 23 additions and 8 deletions

25
cpdf.ml
View File

@ -953,10 +953,13 @@ let verify_bookmarks pdf lastlevel endpage marks =
let rec fixup_characters prev = function let rec fixup_characters prev = function
| [] -> rev prev | [] -> rev prev
| '\\'::'\\'::t -> fixup_characters ('\\'::prev) t | '\\'::'\\'::t -> fixup_characters ('\\'::prev) t
| '\\'::'\"'::t -> fixup_characters ('\"'::prev) t | '\\'::'"'::t -> fixup_characters ('\"'::prev) t
| '\\'::'\n'::t -> fixup_characters ('\n'::prev) t | '\\'::'\n'::t -> fixup_characters ('\n'::prev) t (* This can never have been right? *)
| h::t -> fixup_characters (h::prev) t | 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 (* 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. *) * integer to an indirect of the real page target, and then put it in. *)
let bookmark_of_data pdf i s i' isopen optionaldest = 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 end
| _ -> Pdfpage.target_of_pagenumber pdf i' | _ -> Pdfpage.target_of_pagenumber pdf i'
in 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.level = i;
Pdfmarks.text = Pdftext.pdfdocstring_of_utf8 (implode (fixup_characters [] (explode s))); Pdfmarks.text = Pdftext.pdfdocstring_of_utf8 (implode (fixup_characters [] (explode s)));
Pdfmarks.target = target; Pdfmarks.target = target;
@ -2780,7 +2786,7 @@ let scale_pdf ?(fast=false) sxsylist pdf range =
process_pages scale_page pdf range process_pages scale_page 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) 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 scale_page_to_fit pnum page =
let x, y = List.nth xylist (pnum - 1) in let x, y = List.nth xylist (pnum - 1) in
let matrix = 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 if maxx <= 0. || maxy <= 0. then failwith "Zero-sized pages are invalid" else
let fx = x /. maxx in let fy = y /. maxy in let fx = x /. maxx in let fy = y /. maxy in
let scale = fmin fx fy *. input_scale in let scale = fmin fx fy *. input_scale in
let trans_x = (x -. (maxx *. scale)) /. 2. let trans_x =
in let trans_y = (y -. (maxy *. scale)) /. 2. in 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.matrix_of_transform
[Pdftransform.Translate (trans_x, trans_y); [Pdftransform.Translate (trans_x, trans_y);
Pdftransform.Scale ((0., 0.), scale, scale)]) Pdftransform.Scale ((0., 0.), scale, scale)])

View File

@ -353,10 +353,10 @@ val shift_pdf : ?fast:bool -> (float * float) list -> Pdf.t -> int list -> Pdf.t
for all pages in pdf. *) for all pages in pdf. *)
val scale_pdf : ?fast:bool -> (float * float) list -> Pdf.t -> int list -> Pdf.t 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 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. *) = 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. *) (** 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 val scale_contents : ?fast:bool -> position -> float -> Pdf.t -> int list -> Pdf.t

View File

@ -3745,7 +3745,7 @@ let go () =
let range = parse_pagespec pdf (get_pagespec ()) in let range = parse_pagespec pdf (get_pagespec ()) in
let xylist = parse_coordinates pdf args.coord let xylist = parse_coordinates pdf args.coord
and scale = args.scale in 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) -> | Some (ScaleContents scale) ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in let range = parse_pagespec pdf (get_pagespec ()) in