Concluded content sharing work for now

This commit is contained in:
John Whitington 2016-04-03 12:48:44 +01:00
parent 114e069748
commit 849a41f5a1
2 changed files with 46 additions and 43 deletions

90
cpdf.ml
View File

@ -4,6 +4,16 @@ open Pdfio
let debug = ref false let debug = ref false
(* For debugging *)
let report_pdf_size pdf =
Pdf.remove_unreferenced pdf;
Pdfwrite.pdf_to_file_options ~preserve_objstm:false ~generate_objstm:false
~compress_objstm:false false None false pdf "temp.pdf";
let fh = open_in_bin "temp.pdf" in
Printf.printf "Size %i bytes\n" (in_channel_length fh);
flush stdout;
close_in fh
(* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume (* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume
installed at a system place *) installed at a system place *)
let find_cpdflin provided = let find_cpdflin provided =
@ -564,7 +574,8 @@ let map_pages f pdf range =
pages pages
(* Add stack operators to a content stream to ensure it is composeable. FIXME: (* Add stack operators to a content stream to ensure it is composeable. FIXME:
This will go away once we're using a better postpend_content or similar for twoup and do_stamp... *) * This is only used for non-fast, and it blows up shared streams. Fix place
* which use this not to blow up, and remove this code. *)
let protect_removeme pdf resources content = let protect_removeme pdf resources content =
let ops = Pdfops.parse_operators pdf resources content in let ops = Pdfops.parse_operators pdf resources content in
let qs = length (keep (eq Pdfops.Op_q) ops) let qs = length (keep (eq Pdfops.Op_q) ops)
@ -2270,9 +2281,8 @@ let stamp relative_to_cropbox position topline midline fast scale_to_fit isover
(* If [over] has more pages than [under], chop the excess. If the converse, pad (* If [over] has more pages than [under], chop the excess. If the converse, pad
[over] to the same length *) [over] to the same length *)
let equalize_pages under over = let equalize_pages under over =
let length_under = Pdfpage.endpage under let length_under = Pdfpage.endpage under in
in let length_over = Pdfpage.endpage over let length_over = Pdfpage.endpage over in
in
if length_over > length_under then if length_over > length_under then
under, under,
(Pdfpage.change_pages true over (take (Pdfpage.pages_of_pagetree over) length_under)) (Pdfpage.change_pages true over (take (Pdfpage.pages_of_pagetree over) length_under))
@ -2285,43 +2295,22 @@ let equalize_pages under over =
else else
under, over under, over
let report_pdf_size pdf =
Pdf.remove_unreferenced pdf;
Pdfwrite.pdf_to_file_options ~preserve_objstm:false ~generate_objstm:false
~compress_objstm:false false None false pdf "temp.pdf";
let fh = open_in_bin "temp.pdf" in
Printf.printf "Size %i bytes\n" (in_channel_length fh);
flush stdout;
close_in fh
let combine_pages (fast : bool) under over scaletofit swap equalize = let combine_pages (fast : bool) under over scaletofit swap equalize =
(*Printf.printf "combine_pages: fast = %b\n" fast; Pdfpage.add_prefix over (Pdfpage.shortest_unused_prefix under);
flush stdout;
Printf.printf "Under size\n";
report_pdf_size under;
Printf.printf "Over size\n";
report_pdf_size over;*)
let prefix = Pdfpage.shortest_unused_prefix under in
(*Printf.printf "prefix was %s\n" prefix;*)
Pdfpage.add_prefix over prefix;
(*Printf.printf "added prefix\n";
Printf.printf "under now:\n";
report_pdf_size under;
Printf.printf "over now:\n";
report_pdf_size over;*)
let marks_under = Pdfmarks.read_bookmarks under in let marks_under = Pdfmarks.read_bookmarks under in
let marks_over = Pdfmarks.read_bookmarks over in let marks_over = Pdfmarks.read_bookmarks over in
let under, over = if equalize then equalize_pages under over else under, over in let under, over = if equalize then equalize_pages under over else under, over in
let under_length = Pdfpage.endpage under let under_length = Pdfpage.endpage under in
in let over_length = Pdfpage.endpage over in let over_length = Pdfpage.endpage over in
if under_length <> over_length then raise (Pdf.PDFError "combine_pages: not of equal length") else if under_length <> over_length then
let pageseqs_under = ilist 1 (Pdfpage.endpage under) raise (Pdf.PDFError "combine_pages: not of equal length")
in let pageseqs_over = ilist 1 (Pdfpage.endpage over) in else
let pageseqs_under = ilist 1 (Pdfpage.endpage under) in
let pageseqs_over = ilist 1 (Pdfpage.endpage over) in
let merged = let merged =
Pdfmerge.merge_pdfs false false ["a"; "b"] [under; over] [pageseqs_under; pageseqs_over] in Pdfmerge.merge_pdfs
(*Printf.printf "merged\n"; false false ["a"; "b"] [under; over] [pageseqs_under; pageseqs_over]
flush stdout; in
report_pdf_size merged;*)
let under_pages, over_pages = let under_pages, over_pages =
cleave (Pdfpage.pages_of_pagetree merged) under_length cleave (Pdfpage.pages_of_pagetree merged) under_length
in in
@ -2331,22 +2320,35 @@ let combine_pages (fast : bool) under over scaletofit swap equalize =
do_stamp do_stamp
false fast (BottomLeft 0.) false false scaletofit (not swap) false fast (BottomLeft 0.) false false scaletofit (not swap)
merged o u over) merged o u over)
over_pages over_pages under_pages
under_pages
in in
let changed = Pdfpage.change_pages true merged new_pages in
Pdfmarks.add_bookmarks (marks_under @ marks_over) changed
(*Printf.printf "combine_pages: fast = %b\n" fast;
flush stdout;
Printf.printf "Under size\n";
report_pdf_size under;
Printf.printf "Over size\n";
report_pdf_size over;*)
(*Printf.printf "prefix was %s\n" prefix;*)
(*Printf.printf "added prefix\n";
Printf.printf "under now:\n";
report_pdf_size under;
Printf.printf "over now:\n";
report_pdf_size over;*)
(*Printf.printf "merged\n";
flush stdout;
report_pdf_size merged;*)
(*Printf.printf "stamped\n"; (*Printf.printf "stamped\n";
flush stdout;*) flush stdout;*)
let r =
let changed = Pdfpage.change_pages true merged new_pages
in
(*Printf.printf "pages changed\n"; (*Printf.printf "pages changed\n";
flush stdout;*) flush stdout;*)
Pdfmarks.add_bookmarks (marks_under @ marks_over) changed
in
(*report_pdf_size r; (*report_pdf_size r;
Printf.printf "bookmarks added\n"; Printf.printf "bookmarks added\n";
flush stdout;*) flush stdout;*)
r
let nobble_page pdf _ page = let nobble_page pdf _ page =
let minx, miny, maxx, maxy = let minx, miny, maxx, maxy =

View File

@ -2,6 +2,7 @@
%Documents bates enhancements %Documents bates enhancements
%Document -bates-at-range (must come after range given) and after -bates if %Document -bates-at-range (must come after range given) and after -bates if
%given %given
%Docuement all uses of -fast and content stream sharing
\documentclass{book} \documentclass{book}
\usepackage{palatino} \usepackage{palatino}
\usepackage{microtype} \usepackage{microtype}