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