Commit 1a000fce7bee86f79ced0653de93b41495b1c255

Authored by m-holger
1 parent 92b29095

Refactor `QPDFJob::handlePageSpecs`: add `any_page_labels` to `Input`, simplify …

…page label processing logic, centralize label handling, and adjust related resource management.
libqpdf/QPDFJob.cc
... ... @@ -2344,6 +2344,9 @@ QPDFJob::Input::initialize(Inputs& in, QPDF* a_qpdf)
2344 2344 if (in.job.m->remove_unreferenced_page_resources != QPDFJob::re_no) {
2345 2345 remove_unreferenced = in.job.shouldRemoveUnreferencedResources(*qpdf);
2346 2346 }
  2347 + if (qpdf->page_labels().hasPageLabels()) {
  2348 + in.any_page_labels = true;
  2349 + }
2347 2350 }
2348 2351 }
2349 2352  
... ... @@ -2570,7 +2573,6 @@ QPDFJob::handlePageSpecs(QPDF& pdf)
2570 2573 // Add all the pages from all the files in the order specified. Keep track of any pages from the
2571 2574 // original file that we are selecting.
2572 2575 std::vector<QPDFObjectHandle> new_labels;
2573   - bool any_page_labels = false;
2574 2576 int out_pageno = 0;
2575 2577 auto& this_afdh = pdf.acroform();
2576 2578 std::set<QPDFObjGen> referenced_fields;
... ... @@ -2579,25 +2581,23 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2579 2581 if (input.cfis) {
2580 2582 input.cfis->stayOpen(true);
2581 2583 }
2582   - auto& pldh = input.qpdf->page_labels();
  2584 + auto* pldh = m->inputs.any_page_labels ? &input.qpdf->page_labels() : nullptr;
2583 2585 auto& other_afdh = input.qpdf->acroform();
2584   - if (pldh.hasPageLabels()) {
2585   - any_page_labels = true;
2586   - }
2587 2586 doIfVerbose([&](Pipeline& v, std::string const& prefix) {
2588 2587 v << prefix << ": adding pages from " << selection.filename() << "\n";
2589 2588 });
  2589 + const bool this_file = input.qpdf == &pdf;
2590 2590 for (PageNo page: selection.selected_pages) {
2591   - const bool this_file = input.qpdf == &pdf;
2592 2591 bool first_copy_from_orig = this_file && !main_input.copied_pages[page.idx];
2593 2592  
2594 2593 // Pages are specified from 1 but numbered from 0 in the vector
2595 2594 int pageno = page.no - 1;
2596   - pldh.getLabelsForPageRange(pageno, pageno, out_pageno++, new_labels);
  2595 + if (pldh) {
  2596 + pldh->getLabelsForPageRange(pageno, pageno, out_pageno++, new_labels);
  2597 + }
2597 2598 QPDFPageObjectHelper to_copy = input.orig_pages.at(page.idx);
2598 2599 if (input.copied_pages[page.idx]) {
2599   - QTC::TC(
2600   - "qpdf", "QPDFJob copy same page more than once", (input.qpdf == &pdf) ? 0 : 1);
  2600 + QTC::TC("qpdf", "QPDFJob copy same page more than once", this_file ? 0 : 1);
2601 2601 to_copy = to_copy.shallowCopyPage();
2602 2602 } else {
2603 2603 input.copied_pages[page.idx] = true;
... ... @@ -2637,7 +2637,7 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2637 2637 input.cfis->stayOpen(false);
2638 2638 }
2639 2639 }
2640   - if (any_page_labels) {
  2640 + if (m->inputs.any_page_labels) {
2641 2641 pdf.getRoot().replaceKey("/PageLabels", Dictionary({{"/Nums", Array(new_labels)}}));
2642 2642 }
2643 2643  
... ...
libqpdf/qpdf/QPDFJob_private.hh
... ... @@ -87,6 +87,8 @@ struct QPDFJob::Inputs
87 87 std::map<std::string, Input> files;
88 88 std::vector<Selection> selections;
89 89  
  90 + bool any_page_labels{false};
  91 +
90 92 private:
91 93 QPDFJob& job;
92 94 };
... ...