Collation

This commit is contained in:
John Whitington 2021-10-16 15:47:41 +01:00
parent c9bd93e42e
commit 2b3be7890b
2 changed files with 35 additions and 11 deletions

View File

@ -454,7 +454,8 @@ type args =
mutable ocgrenamefrom : string; mutable ocgrenamefrom : string;
mutable ocgrenameto : string; mutable ocgrenameto : string;
mutable dedup : bool; mutable dedup : bool;
mutable dedup_per_page : bool} mutable dedup_per_page : bool;
mutable collate : bool}
let args = let args =
{op = None; {op = None;
@ -559,7 +560,8 @@ let args =
ocgrenamefrom = ""; ocgrenamefrom = "";
ocgrenameto = ""; ocgrenameto = "";
dedup = false; dedup = false;
dedup_per_page = false} dedup_per_page = false;
collate = false;}
let reset_arguments () = let reset_arguments () =
args.op <- None; args.op <- None;
@ -649,7 +651,8 @@ let reset_arguments () =
args.ocgrenamefrom <- ""; args.ocgrenamefrom <- "";
args.ocgrenameto <- ""; args.ocgrenameto <- "";
args.dedup <- false; args.dedup <- false;
args.dedup_per_page <- false args.dedup_per_page <- false;
args.collate <- false
(* Do not reset original_filename or cpdflin or was_encrypted or (* Do not reset original_filename or cpdflin or was_encrypted or
* was_decrypted_with_owner or recrypt or producer or creator or path_to_* or * was_decrypted_with_owner or recrypt or producer or creator or path_to_* or
* gs_malformed or gs_quiet, since we want these to work across ANDs. Or * gs_malformed or gs_quiet, since we want these to work across ANDs. Or
@ -954,8 +957,6 @@ let displaydoctitle b =
try setop (DisplayDocTitle (bool_of_string b)) () with try setop (DisplayDocTitle (bool_of_string b)) () with
_ -> failwith "DisplayDocTitle: must use true or false" _ -> failwith "DisplayDocTitle: must use true or false"
let setsplitbookmarks i = setop (SplitOnBookmarks i) () let setsplitbookmarks i = setop (SplitOnBookmarks i) ()
let setstdout () = args.out <- Stdout let setstdout () = args.out <- Stdout
let setstdin () = args.inputs <- [StdIn, "all", "", "", ref false, None] let setstdin () = args.inputs <- [StdIn, "all", "", "", ref false, None]
@ -1519,6 +1520,9 @@ let set_dedup () =
let set_dedup_per_page () = let set_dedup_per_page () =
args.dedup_per_page <- true args.dedup_per_page <- true
let setcollate () =
args.collate <- true
let whingemalformed () = let whingemalformed () =
prerr_string "Command line must be of exactly the form\ncpdf <infile> -gs <path> -gs-malformed-force -o <outfile>\n"; prerr_string "Command line must be of exactly the form\ncpdf <infile> -gs <path> -gs-malformed-force -o <outfile>\n";
exit 1 exit 1
@ -1561,6 +1565,9 @@ and specs =
("-range", ("-range",
Arg.String setrange, Arg.String setrange,
" Explicitly add a range"); " Explicitly add a range");
("-collate",
Arg.Unit setcollate,
" Collate ranges when merging");
("-revision", ("-revision",
Arg.Int setrevision, Arg.Int setrevision,
""); "");
@ -3413,6 +3420,21 @@ let write_json output pdf =
pdf; pdf;
close_out f close_out f
let collate (names, pdfs, ranges) =
let ois = map ref (combine3 names pdfs ranges) in
let nis = ref [] in
while flatten (map (fun {contents = (_, _, r)} -> r) ois) <> [] do
iter
(fun ({contents = (name, pdf, range)} as r) ->
match range with
| [] -> ()
| h::t ->
nis := (name, pdf, [h])::!nis;
r := (name, pdf, t))
ois
done;
split3 (rev !nis)
(* Main function *) (* Main function *)
let go () = let go () =
match args.op with match args.op with
@ -3471,10 +3493,14 @@ let go () =
in in
(* If args.keep_this_id is set, change the ID to the one from the kept one *) (* If args.keep_this_id is set, change the ID to the one from the kept one *)
let rangenums = map2 parse_pagespec pdfs ranges in let rangenums = map2 parse_pagespec pdfs ranges in
(* At this point, we have the information for collation. *)
let names = map string_of_input_kind names in
let names, pdfs, rangenums =
(if args.collate then collate else Fun.id) (names, pdfs, rangenums)
in
let outpdf = let outpdf =
Pdfmerge.merge_pdfs Pdfmerge.merge_pdfs
args.retain_numbering args.remove_duplicate_fonts args.retain_numbering args.remove_duplicate_fonts names pdfs rangenums
(map string_of_input_kind names) pdfs rangenums
in in
write_pdf false outpdf write_pdf false outpdf
end end
@ -4351,10 +4377,6 @@ let gs_malformed_force fi fo =
| 0 -> exit 0 | 0 -> exit 0
| _ -> Printf.eprintf "Failed to mend file.\n%!"; exit 2 | _ -> Printf.eprintf "Failed to mend file.\n%!"; exit 2
(* FIXME: Now we call this repeatedly from interactive programs, careful to
ensure that all memory is cleaned. See clearance of filenames hashtable, for
example. *)
let process_env_vars () = let process_env_vars () =
match Sys.getenv_opt "CPDF_DEBUG" with match Sys.getenv_opt "CPDF_DEBUG" with
| Some "true" -> args.debug <- true | Some "true" -> args.debug <- true

View File

@ -1,5 +1,7 @@
%Document the new JSON format, -j and new -output-json options %Document the new JSON format, -j and new -output-json options
%Document -decrypt-force %Document -decrypt-force
%Document -collate
\documentclass{book} \documentclass{book}
% Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf etc. % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf etc.
\usepackage{comment}\excludecomment{cpdflib}\excludecomment{pycpdflib} \usepackage{comment}\excludecomment{cpdflib}\excludecomment{pycpdflib}