Commit b56890dbfc2d7289996b5cccfb8a1476892a77b0

Authored by m-holger
1 parent 69d21018

Refactor `QPDFJob`: remove redundant `orig_pages` member in `Selection`, consoli…

…date page handling logic in `Input`, and optimize page range processing.
libqpdf/QPDFJob.cc
... ... @@ -2346,7 +2346,10 @@ QPDFJob::new_selection(
2346 2346 bool
2347 2347 QPDFJob::handlePageSpecs(QPDF& pdf)
2348 2348 {
2349   - m->inputs.files[m->infilename].qpdf = &pdf;
  2349 + auto& in_file = m->inputs.files[m->infilename];
  2350 + in_file.qpdf = &pdf;
  2351 + in_file.orig_pages = pdf.getAllPages();
  2352 + in_file.n_pages = QIntC::to_int(in_file.orig_pages.size());
2350 2353  
2351 2354 // Parse all page specifications and translate them into lists of actual pages.
2352 2355  
... ... @@ -2409,6 +2412,8 @@ QPDFJob::handlePageSpecs(QPDF& pdf)
2409 2412 true);
2410 2413 }
2411 2414 input.qpdf = input.qpdf_p.get();
  2415 + input.orig_pages = input.qpdf->getAllPages();
  2416 + input.n_pages = QIntC::to_int(input.orig_pages.size());
2412 2417 if (input.cfis) {
2413 2418 input.cfis->stayOpen(false);
2414 2419 }
... ... @@ -2419,11 +2424,11 @@ QPDFJob::handlePageSpecs(QPDF& pdf)
2419 2424 for (auto& selection: m->selections) {
2420 2425 // Read original pages from the PDF, and parse the page range associated with this
2421 2426 // occurrence of the file.
2422   - selection.qpdf = m->inputs.files[selection.filename].qpdf;
2423   - selection.orig_pages = selection.qpdf->getAllPages();
  2427 + auto const& input = m->inputs.files[selection.filename];
  2428 + selection.qpdf = input.qpdf;
2424 2429 try {
2425   - selection.selected_pages = QUtil::parse_numrange(
2426   - selection.range.data(), static_cast<int>(selection.orig_pages.size()));
  2430 + selection.selected_pages =
  2431 + QUtil::parse_numrange(selection.range.data(), input.n_pages);
2427 2432 } catch (std::runtime_error& e) {
2428 2433 throw std::runtime_error(
2429 2434 "parsing numeric range for " + selection.filename + ": " + e.what());
... ... @@ -2519,14 +2524,12 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2519 2524 // Pages are specified from 1 but numbered from 0 in the vector
2520 2525 int pageno = pageno_iter - 1;
2521 2526 pldh.getLabelsForPageRange(pageno, pageno, out_pageno++, new_labels);
2522   - QPDFPageObjectHelper to_copy = page_data.orig_pages.at(QIntC::to_size(pageno));
  2527 + QPDFPageObjectHelper to_copy = input.orig_pages.at(QIntC::to_size(pageno));
2523 2528 QPDFObjGen to_copy_og = to_copy.getObjectHandle().getObjGen();
2524   - unsigned long long from_uuid = page_data.qpdf->getUniqueId();
  2529 + unsigned long long from_uuid = input.qpdf->getUniqueId();
2525 2530 if (copied_pages[from_uuid].contains(to_copy_og)) {
2526 2531 QTC::TC(
2527   - "qpdf",
2528   - "QPDFJob copy same page more than once",
2529   - (page_data.qpdf == &pdf) ? 0 : 1);
  2532 + "qpdf", "QPDFJob copy same page more than once", (input.qpdf == &pdf) ? 0 : 1);
2530 2533 to_copy = to_copy.shallowCopyPage();
2531 2534 } else {
2532 2535 copied_pages[from_uuid].insert(to_copy_og);
... ... @@ -2536,7 +2539,7 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2536 2539 }
2537 2540 pdf.addPage(to_copy, false);
2538 2541 bool first_copy_from_orig = false;
2539   - bool this_file = (page_data.qpdf == &pdf);
  2542 + bool this_file = input.qpdf == &pdf;
2540 2543 if (this_file) {
2541 2544 // This is a page from the original file. Keep track of the fact that we are using
2542 2545 // it.
... ...
libqpdf/qpdf/QPDFJob_private.hh
... ... @@ -21,7 +21,6 @@ struct QPDFJob::Selection
21 21 filename(other.filename),
22 22 // range and password are no longer required when this constructor is called.
23 23 qpdf(other.qpdf),
24   - orig_pages(other.orig_pages),
25 24 selected_pages({page})
26 25 {
27 26 }
... ... @@ -30,7 +29,6 @@ struct QPDFJob::Selection
30 29 std::string password;
31 30 std::string range;
32 31 QPDF* qpdf;
33   - std::vector<QPDFObjectHandle> orig_pages;
34 32 std::vector<int> selected_pages;
35 33 };
36 34  
... ... @@ -44,6 +42,8 @@ struct QPDFJob::Input
44 42 std::unique_ptr<QPDF> qpdf_p;
45 43 QPDF* qpdf;
46 44 ClosedFileInputSource* cfis{};
  45 + std::vector<QPDFObjectHandle> orig_pages;
  46 + int n_pages;
47 47 };
48 48  
49 49 // All PDF input files for a job.
... ...