From b5a71b2504842cbb47f5fb1f2fc6c0b176e15280 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Sat, 21 Sep 2024 17:35:29 +0100 Subject: [PATCH] Scaffolding for -stretch --- Changes | 1 + cpdfcommand.ml | 20 ++++++++++++++++++-- cpdfmanual.tex | 3 +-- cpdfpage.ml | 5 +++++ cpdfpage.mli | 2 ++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Changes b/Changes index 5877a21..5fac41b 100644 --- a/Changes +++ b/Changes @@ -15,6 +15,7 @@ New features: * Add structure information to -typeset, add -typeset-subformat * -typeset can make PDF/UA documents * -jpeg, -png and friends can make PDF/UA documents +o -stretch scales without preserving aspect ratio * = Supported by a grant from NLnet diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 8606a12..fbd2399 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -105,6 +105,7 @@ type op = | ShiftBoxes | Scale | ScaleToFit + | Stretch | ScaleContents of float | AttachFile of string list | RemoveAttachedFiles @@ -257,6 +258,7 @@ let string_of_op = function | ShiftBoxes -> "ShiftBoxes" | Scale -> "Scale" | ScaleToFit -> "ScaleToFit" + | Stretch -> "Stretch" | ScaleContents _ -> "ScaleContents" | AttachFile _ -> "AttachFile" | RemoveAttachedFiles -> "RemoveAttachedFiles" @@ -925,8 +927,8 @@ let banned banlist = function | Merge | Split | SplitOnBookmarks _ | SplitMax _ | Spray | RotateContents _ | Rotate _ | Rotateby _ | Upright | VFlip | HFlip | Impose _ | Chop _ | ChopHV _ -> mem Pdfcrypt.NoAssemble banlist - | TwoUp|TwoUpStack|RemoveBookmarks|AddRectangle|RemoveText| - Draft|Shift|ShiftBoxes | Scale|ScaleToFit|RemoveAttachedFiles| + | TwoUp | TwoUpStack | RemoveBookmarks | AddRectangle | RemoveText| + Draft | Shift | ShiftBoxes | Scale | ScaleToFit|Stretch|RemoveAttachedFiles| RemoveAnnotations|RemoveFonts|Crop|RemoveCrop|Trim|RemoveTrim|Bleed|RemoveBleed|Art|RemoveArt| CopyBox|MediaBox|HardBox _|SetTrapped|SetUntrapped|Presentation| BlackText|BlackLines|BlackFills|CopyFont _|StampOn _|StampUnder _|StampAsXObject _| @@ -1209,6 +1211,10 @@ let setscaletofit s = setop ScaleToFit (); args.coord <- s +let setstretch s = + setop Stretch (); + args.coord <- s + let setattachfile s = match args.op with | Some (AttachFile t) -> @@ -2044,6 +2050,9 @@ let specs = ("-scale-to-fit", Arg.String setscaletofit, " -scale-to-fit \"x y\" scales to page size (x, y)"); + ("-stretch", + Arg.String setstretch, + " -stretch \"x y\" scales without preserving aspect ratio"); ("-scale-contents", Arg.Float setscalecontents, " Scale contents by the given factor"); @@ -4159,6 +4168,13 @@ let go () = let xylist = Cpdfcoord.parse_coordinates pdf args.coord and scale = args.scale in write_pdf false (Cpdfpage.scale_to_fit_pdf ~fast:args.fast args.position scale xylist args.op pdf range) + | Some Stretch -> + let pdf = get_single_pdf args.op false in + let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in + warn_prerotate range pdf; + let pdf = if args.prerotate then prerotate range pdf else pdf in + let xylist = Cpdfcoord.parse_coordinates pdf args.coord in + write_pdf false (Cpdfpage.stretch ~fast:args.fast xylist pdf range) | Some (ScaleContents scale) -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in diff --git a/cpdfmanual.tex b/cpdfmanual.tex index 7d6cfda..74ab3eb 100644 --- a/cpdfmanual.tex +++ b/cpdfmanual.tex @@ -1,5 +1,4 @@ -%Document new drawing stuff for PDF/UA -%Document typesetting for PDF/UA +%Document -stretch \documentclass{book} % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf, % dotnetcpdflibmanual.pdf, jcpdflibmanual.pdf jscpdflibmanual.pdf etc. diff --git a/cpdfpage.ml b/cpdfpage.ml index 2c0fa5a..7e02209 100644 --- a/cpdfpage.ml +++ b/cpdfpage.ml @@ -363,6 +363,8 @@ let scale_page_contents ?(fast=false) scale position pdf pnum page = let scale_contents ?(fast=false) position scale pdf range = process_pages (scale_page_contents ~fast scale position pdf) pdf range + + (* Set media box *) let set_mediabox xywhlist pdf range = let crop_page pnum page = @@ -532,6 +534,9 @@ let scale_pdf ?(fast=false) sxsylist pdf range = in process_pages scale_page pdf range +let stretch ?(fast=false) xylist pdf range = + pdf + (* Scale to fit page of size x * y *) let scale_to_fit_pdf ?(fast=false) position input_scale xylist op pdf range = let scale_page_to_fit pnum page = diff --git a/cpdfpage.mli b/cpdfpage.mli index 3a6f55c..e139882 100644 --- a/cpdfpage.mli +++ b/cpdfpage.mli @@ -79,6 +79,8 @@ 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 -> Cpdfposition.position -> float -> (float * float) list -> 'a -> Pdf.t -> int list -> Pdf.t +val stretch : ?fast:bool -> (float * float) list -> Pdf.t -> int list -> Pdf.t + (** {2 Stamping} *) (** [combine_pages fast under over scaletofit swap equalize] combines the page