From b676ab49239279c86faab57aad1439210c063b6e Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 24 Sep 2024 20:40:14 +0100 Subject: [PATCH] Optional absolute rectangles --- Changes | 4 ++++ cpdfcoord.ml | 27 ++++++++++++++++++++------- cpdfmanual.tex | 1 + 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Changes b/Changes index d36cb07..023a43c 100644 --- a/Changes +++ b/Changes @@ -19,6 +19,10 @@ New features: o -stretch scales without preserving aspect ratio o -redact removes whole pages +Extended features: + +o Rectangle may be specified as x y x2 y2 rather than x y w h + * = Supported by a grant from NLnet 2.7.1 (July 2024) diff --git a/cpdfcoord.ml b/cpdfcoord.ml index c69f41d..125051a 100644 --- a/cpdfcoord.ml +++ b/cpdfcoord.ml @@ -207,20 +207,33 @@ let parse_units_string pdf page s = fs let parse_rectangle pdf s = - try - match parse_units_string pdf emptypage s with - | [x; y; w; h] -> x, y, w, h - | _ -> error ("Bad rectangle specification " ^ s) - with - e -> error ("Bad rectangle specification " ^ s ^ " : " ^ Printexc.to_string e) + (* If it begins with ? it's absolute *) + let s, absolute = + match explode s with + | '?'::r -> implode r, true + | _ -> s, false + in + try + match parse_units_string pdf emptypage s with + | [x; y; w; h] -> + if absolute then x, y, w -. x, h -. y else x, y, w, h + | _ -> error ("Bad rectangle specification " ^ s) + with + e -> error ("Bad rectangle specification " ^ s ^ " : " ^ Printexc.to_string e) let parse_rectangles pdf s = + (* If it begins with ? it's absolute *) + let s, absolute = + match explode s with + | '?'::r -> implode r, true + | _ -> s, false + in try let pages = Pdfpage.pages_of_pagetree pdf in let groups = List.map (fun page -> parse_units_string pdf page s) pages in List.map (function - | [x; y; w; h] -> (x, y, w, h) + | [x; y; w; h] -> if absolute then x, y, w -. x, h -. y else x, y, w, h | _ -> error ("Bad rectangle specification " ^ s)) groups with diff --git a/cpdfmanual.tex b/cpdfmanual.tex index 653fe0b..03407ca 100644 --- a/cpdfmanual.tex +++ b/cpdfmanual.tex @@ -1,5 +1,6 @@ %Document -stretch %Document -redact +%Document ?x0 y0 x1 y1 rectangle specifications \documentclass{book} % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf, % dotnetcpdflibmanual.pdf, jcpdflibmanual.pdf jscpdflibmanual.pdf etc.