From 0d98684e6235b2ca31557221fd3c5806e7ba6388 Mon Sep 17 00:00:00 2001 From: Till Kamppeter Date: Sat, 4 Jan 2025 00:52:40 +0100 Subject: [PATCH] Correct skipping even/odd pages for manual duplex Fixes #541 The page-set option with choicers "even" and "odd" is designed for doing manual duplex, by printing the odd pages first, turning the printed pages over and put them back into the input tray and then print the even pages. If the total number of pages to be printed is odd, an empty page needs to be added to the end of the set of even pages to make the total number of pages even, to have a front and back for each sheet and so the correct pairs of pages are on each sheet and always all sheets are taken from the input tray to the output tray. This did not work correctly for 2 reasons: - The skipping of pages for the page-set and for the page-ranges options were done in the same step, the page ranges must be done first and from the resulting pages the even or odd pages need to get skipped. - To determine whether the total amount of pages to be printed is odd, only the actually printed pages, with the even or odd pages already skipped, ar counted, not the pages which would be printed if page-set is not used. This means especially that a 6-page document would have 3 even pages, so in the end 3 pages are counted and so an extra blank page is added (which is wrong as the document has 6 pages which is even). A 5-page document has 2 even pages, so an even number of pages gets counted and so no blank page gets added. Now we apply page-ranges and each page which gets printed according to that we count with the outputno variable. Then we skip the pages with even or odd outputno depending of the page-set option. In the end, output no is the number of all pages to be printed and so we know that if it is odd and we print the even pages, we need to add a blank page. --- .../pdftopdf/pdftopdf-processor-private.h | 3 +- cupsfilters/pdftopdf/pdftopdf-processor.cxx | 55 ++++++++++++------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/cupsfilters/pdftopdf/pdftopdf-processor-private.h b/cupsfilters/pdftopdf/pdftopdf-processor-private.h index 92ed198e5..4931f6bb0 100644 --- a/cupsfilters/pdftopdf/pdftopdf-processor-private.h +++ b/cupsfilters/pdftopdf/pdftopdf-processor-private.h @@ -123,8 +123,9 @@ _cfPDFToPDFProcessingParameters() int copies_to_be_logged; // helper functions + bool even_odd_page(int outno) const; // 1 based bool with_page(int outno) const; // 1 based - bool have_page(int pageno) const; //1 based + bool have_page(int pageno) const; // 1 based void dump(pdftopdf_doc_t *doc) const; }; diff --git a/cupsfilters/pdftopdf/pdftopdf-processor.cxx b/cupsfilters/pdftopdf/pdftopdf-processor.cxx index 3927f379e..c153d752f 100644 --- a/cupsfilters/pdftopdf/pdftopdf-processor.cxx +++ b/cupsfilters/pdftopdf/pdftopdf-processor.cxx @@ -32,7 +32,7 @@ BookletMode_dump(pdftopdf_booklet_mode_e bkm, // }}} bool -_cfPDFToPDFProcessingParameters::with_page(int outno) const // {{{ +_cfPDFToPDFProcessingParameters::even_odd_page(int outno) const // {{{ { if (outno % 2 == 0) { // 1-based @@ -41,6 +41,13 @@ _cfPDFToPDFProcessingParameters::with_page(int outno) const // {{{ } else if (!odd_pages) return (false); + return (true); +} +// }}} + +bool +_cfPDFToPDFProcessingParameters::with_page(int outno) const // {{{ +{ return (page_ranges.contains(outno)); } // }}} @@ -416,18 +423,21 @@ _cfProcessPDFToPDF(_cfPDFToPDFProcessor &proc, { if ((curpage) && (param.with_page(outputpage))) { - curpage->rotate(param.orientation); - if (param.mirror) - curpage->mirror(); - // TODO? update rect? --- not needed any more - proc.add_page(curpage, param.reverse); // reverse -> insert at beginning - // Log page in /var/log/cups/page_log outputno ++; - if (param.page_logging == 1) - if (doc->logfunc) doc->logfunc(doc->logdata, - CF_LOGLEVEL_CONTROL, - "PAGE: %d %d", outputno, - param.copies_to_be_logged); + if (param.even_odd_page(outputno)) + { + curpage->rotate(param.orientation); + if (param.mirror) + curpage->mirror(); + // TODO? update rect? --- not needed any more + proc.add_page(curpage, param.reverse); // reverse -> insert at beginning + // Log page in /var/log/cups/page_log + if (param.page_logging == 1) + if (doc->logfunc) doc->logfunc(doc->logdata, + CF_LOGLEVEL_CONTROL, + "PAGE: %d %d", outputno, + param.copies_to_be_logged); + } } curpage = proc.new_page(param.page.width, param.page.height, doc); outputpage++; @@ -481,16 +491,19 @@ _cfProcessPDFToPDF(_cfPDFToPDFProcessor &proc, } if ((curpage) && (param.with_page(outputpage))) { - curpage->rotate(param.orientation); - if (param.mirror) - curpage->mirror(); - proc.add_page(curpage, param.reverse); // reverse -> insert at beginning - // Log page in /var/log/cups/page_log outputno ++; - if (param.page_logging == 1) - if (doc->logfunc) doc->logfunc(doc->logdata, CF_LOGLEVEL_CONTROL, - "PAGE: %d %d", outputno, - param.copies_to_be_logged); + if (param.even_odd_page(outputno)) + { + curpage->rotate(param.orientation); + if (param.mirror) + curpage->mirror(); + proc.add_page(curpage, param.reverse); // reverse -> insert at beginning + // Log page in /var/log/cups/page_log + if (param.page_logging == 1) + if (doc->logfunc) doc->logfunc(doc->logdata, CF_LOGLEVEL_CONTROL, + "PAGE: %d %d", outputno, + param.copies_to_be_logged); + } } if ((param.even_duplex || !param.odd_pages) && (outputno & 1))