Commit a43b8312c3409284a6764657e6abe4748a916c16

Authored by m-holger
1 parent 1f5a7cee

Refactor `QPDFJob::Input`: add `initialize` method to centralize member initiali…

…zation, simplify `handlePageSpecs` and `new_section`, and streamline file processing logic.
libqpdf/QPDFJob.cc
@@ -2335,6 +2335,22 @@ added_page(QPDF& pdf, QPDFPageObjectHelper page) @@ -2335,6 +2335,22 @@ added_page(QPDF& pdf, QPDFPageObjectHelper page)
2335 return added_page(pdf, page.getObjectHandle()); 2335 return added_page(pdf, page.getObjectHandle());
2336 } 2336 }
2337 2337
  2338 +// Initialize all members that depend on the QPDF object. If both qpdf and qpdf_p are null do
  2339 +// nothing.
  2340 +void
  2341 +QPDFJob::Input::initialize(Inputs& in, QPDF* a_qpdf)
  2342 +{
  2343 + qpdf = a_qpdf ? a_qpdf : qpdf_p.get();
  2344 + if (qpdf) {
  2345 + orig_pages = qpdf->getAllPages();
  2346 + n_pages = QIntC::to_int(orig_pages.size());
  2347 +
  2348 + if (in.job.m->remove_unreferenced_page_resources != QPDFJob::re_no) {
  2349 + remove_unreferenced = in.job.shouldRemoveUnreferencedResources(*qpdf);
  2350 + }
  2351 + }
  2352 +}
  2353 +
2338 void 2354 void
2339 QPDFJob::new_selection( 2355 QPDFJob::new_selection(
2340 std::string const& filename, std::string const& password, std::string const& range) 2356 std::string const& filename, std::string const& password, std::string const& range)
@@ -2369,13 +2385,7 @@ QPDFJob::Inputs::process(std::string const& filename, QPDFJob::Input& input) @@ -2369,13 +2385,7 @@ QPDFJob::Inputs::process(std::string const& filename, QPDFJob::Input& input)
2369 password.data(), 2385 password.data(),
2370 true); 2386 true);
2371 } 2387 }
2372 - input.qpdf = input.qpdf_p.get();  
2373 - input.orig_pages = input.qpdf->getAllPages();  
2374 - input.n_pages = QIntC::to_int(input.orig_pages.size());  
2375 -  
2376 - if (job.m->remove_unreferenced_page_resources != QPDFJob::re_no) {  
2377 - input.remove_unreferenced = job.shouldRemoveUnreferencedResources(*input.qpdf);  
2378 - } 2388 + input.initialize(*this);
2379 2389
2380 if (input.cfis) { 2390 if (input.cfis) {
2381 input.cfis->stayOpen(false); 2391 input.cfis->stayOpen(false);
@@ -2408,15 +2418,8 @@ QPDFJob::Inputs::process_all() @@ -2408,15 +2418,8 @@ QPDFJob::Inputs::process_all()
2408 bool 2418 bool
2409 QPDFJob::handlePageSpecs(QPDF& pdf) 2419 QPDFJob::handlePageSpecs(QPDF& pdf)
2410 { 2420 {
2411 - auto& in_file = m->inputs.files[m->infilename];  
2412 - in_file.qpdf = &pdf;  
2413 - in_file.orig_pages = pdf.getAllPages();  
2414 - in_file.n_pages = QIntC::to_int(in_file.orig_pages.size());  
2415 - if (m->remove_unreferenced_page_resources != QPDFJob::re_no) {  
2416 - in_file.remove_unreferenced = shouldRemoveUnreferencedResources(pdf);  
2417 - }  
2418 -  
2419 - // Parse all page specifications and translate them into lists of actual pages. 2421 + auto& main_input = m->inputs.files[m->infilename];
  2422 + main_input.initialize(m->inputs, &pdf);
2420 2423
2421 // Handle "." as a shortcut for the input file. 2424 // Handle "." as a shortcut for the input file.
2422 for (auto& selection: m->selections) { 2425 for (auto& selection: m->selections) {
@@ -2456,8 +2459,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf) @@ -2456,8 +2459,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf)
2456 doIfVerbose([&](Pipeline& v, std::string const& prefix) { 2459 doIfVerbose([&](Pipeline& v, std::string const& prefix) {
2457 v << prefix << ": removing unreferenced pages from primary input\n"; 2460 v << prefix << ": removing unreferenced pages from primary input\n";
2458 }); 2461 });
2459 - auto orig_pages = pdf.getAllPages();  
2460 - for (auto const& page: orig_pages) { 2462 + for (auto const& page: main_input.orig_pages) {
2461 pdf.removePage(page); 2463 pdf.removePage(page);
2462 } 2464 }
2463 2465
@@ -2496,7 +2498,6 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2496,7 +2498,6 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2496 } 2498 }
2497 } 2499 }
2498 2500
2499 - std::vector<Selection>& page_specs = new_specs.empty() ? m->selections : new_specs;  
2500 // Add all the pages from all the files in the order specified. Keep track of any pages from the 2501 // Add all the pages from all the files in the order specified. Keep track of any pages from the
2501 // original file that we are selecting. 2502 // original file that we are selecting.
2502 std::set<int> selected_from_orig; 2503 std::set<int> selected_from_orig;
@@ -2505,20 +2506,20 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2505,20 +2506,20 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2505 int out_pageno = 0; 2506 int out_pageno = 0;
2506 auto& this_afdh = pdf.acroform(); 2507 auto& this_afdh = pdf.acroform();
2507 std::set<QPDFObjGen> referenced_fields; 2508 std::set<QPDFObjGen> referenced_fields;
2508 - for (auto& page_data: page_specs) {  
2509 - auto& input = m->inputs.files[page_data.filename]; 2509 + for (auto& selection: new_specs.empty() ? m->selections : new_specs) {
  2510 + auto& input = m->inputs.files[selection.filename];
2510 if (input.cfis) { 2511 if (input.cfis) {
2511 input.cfis->stayOpen(true); 2512 input.cfis->stayOpen(true);
2512 } 2513 }
2513 - auto& pldh = page_data.qpdf->page_labels();  
2514 - auto& other_afdh = page_data.qpdf->acroform(); 2514 + auto& pldh = selection.qpdf->page_labels();
  2515 + auto& other_afdh = selection.qpdf->acroform();
2515 if (pldh.hasPageLabels()) { 2516 if (pldh.hasPageLabels()) {
2516 any_page_labels = true; 2517 any_page_labels = true;
2517 } 2518 }
2518 doIfVerbose([&](Pipeline& v, std::string const& prefix) { 2519 doIfVerbose([&](Pipeline& v, std::string const& prefix) {
2519 - v << prefix << ": adding pages from " << page_data.filename << "\n"; 2520 + v << prefix << ": adding pages from " << selection.filename << "\n";
2520 }); 2521 });
2521 - for (auto pageno_iter: page_data.selected_pages) { 2522 + for (auto pageno_iter: selection.selected_pages) {
2522 // Pages are specified from 1 but numbered from 0 in the vector 2523 // Pages are specified from 1 but numbered from 0 in the vector
2523 int pageno = pageno_iter - 1; 2524 int pageno = pageno_iter - 1;
2524 pldh.getLabelsForPageRange(pageno, pageno, out_pageno++, new_labels); 2525 pldh.getLabelsForPageRange(pageno, pageno, out_pageno++, new_labels);
@@ -2583,7 +2584,7 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf) @@ -2583,7 +2584,7 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf)
2583 // preserved by being referred to from other places, such as the outlines dictionary. Also make 2584 // preserved by being referred to from other places, such as the outlines dictionary. Also make
2584 // sure we keep form fields from pages we preserved. 2585 // sure we keep form fields from pages we preserved.
2585 int page_idx = 0; 2586 int page_idx = 0;
2586 - for (auto const& page: orig_pages) { 2587 + for (auto const& page: main_input.orig_pages) {
2587 if (selected_from_orig.contains(page_idx)) { 2588 if (selected_from_orig.contains(page_idx)) {
2588 for (auto field: this_afdh.getFormFieldsForPage(page)) { 2589 for (auto field: this_afdh.getFormFieldsForPage(page)) {
2589 referenced_fields.insert(field); 2590 referenced_fields.insert(field);
libqpdf/qpdf/QPDFJob_private.hh
@@ -38,6 +38,8 @@ struct QPDFJob::Selection @@ -38,6 +38,8 @@ struct QPDFJob::Selection
38 // filename. This is a documented work-around. 38 // filename. This is a documented work-around.
39 struct QPDFJob::Input 39 struct QPDFJob::Input
40 { 40 {
  41 + void initialize(Inputs& in, QPDF* qpdf = nullptr);
  42 +
41 std::string password; 43 std::string password;
42 std::unique_ptr<QPDF> qpdf_p; 44 std::unique_ptr<QPDF> qpdf_p;
43 QPDF* qpdf; 45 QPDF* qpdf;
@@ -50,6 +52,7 @@ struct QPDFJob::Input @@ -50,6 +52,7 @@ struct QPDFJob::Input
50 // All PDF input files for a job. 52 // All PDF input files for a job.
51 struct QPDFJob::Inputs 53 struct QPDFJob::Inputs
52 { 54 {
  55 + friend struct Input;
53 // These default values are duplicated in help and docs. 56 // These default values are duplicated in help and docs.
54 static int constexpr DEFAULT_KEEP_FILES_OPEN_THRESHOLD = 200; 57 static int constexpr DEFAULT_KEEP_FILES_OPEN_THRESHOLD = 200;
55 58