Commit 2bd0657ded9d71b1c4485a52717cd9bea8e89423

Authored by m-holger
1 parent 1edbed57

Refactor `QPDF::getAllPages` usage: replace with `Pages::all` across `QPDFJob`.

Showing 1 changed file with 32 additions and 22 deletions
libqpdf/QPDFJob.cc
@@ -30,8 +30,11 @@ @@ -30,8 +30,11 @@
30 30
31 using namespace qpdf; 31 using namespace qpdf;
32 32
  33 +using Doc = QPDF::Doc;
  34 +using Pages = Doc::Pages;
  35 +
33 // JobSetter class is restricted to QPDFJob. 36 // JobSetter class is restricted to QPDFJob.
34 -class QPDF::Doc::JobSetter 37 +class Doc::JobSetter
35 { 38 {
36 public: 39 public:
37 // Enable enhanced warnings for pdf file checking. 40 // Enable enhanced warnings for pdf file checking.
@@ -746,7 +749,7 @@ QPDFJob::doCheck(QPDF& pdf) @@ -746,7 +749,7 @@ QPDFJob::doCheck(QPDF& pdf)
746 bool okay = true; 749 bool okay = true;
747 auto& cout = *m->log->getInfo(); 750 auto& cout = *m->log->getInfo();
748 cout << "checking " << m->infile_name() << "\n"; 751 cout << "checking " << m->infile_name() << "\n";
749 - QPDF::Doc::JobSetter::setCheckMode(pdf, true); 752 + Doc::JobSetter::setCheckMode(pdf, true);
750 try { 753 try {
751 int extension_level = pdf.getExtensionLevel(); 754 int extension_level = pdf.getExtensionLevel();
752 cout << "PDF Version: " << pdf.getPDFVersion(); 755 cout << "PDF Version: " << pdf.getPDFVersion();
@@ -852,7 +855,7 @@ QPDFJob::doShowPages(QPDF&amp; pdf) @@ -852,7 +855,7 @@ QPDFJob::doShowPages(QPDF&amp; pdf)
852 { 855 {
853 int pageno = 0; 856 int pageno = 0;
854 auto& cout = *m->log->getInfo(); 857 auto& cout = *m->log->getInfo();
855 - for (auto& page: pdf.getAllPages()) { 858 + for (auto& page: pdf.doc().pages().all()) {
856 QPDFPageObjectHelper ph(page); 859 QPDFPageObjectHelper ph(page);
857 ++pageno; 860 ++pageno;
858 861
@@ -1040,13 +1043,14 @@ QPDFJob::doJSONObjectinfo(Pipeline* p, bool&amp; first, QPDF&amp; pdf) @@ -1040,13 +1043,14 @@ QPDFJob::doJSONObjectinfo(Pipeline* p, bool&amp; first, QPDF&amp; pdf)
1040 void 1043 void
1041 QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf) 1044 QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
1042 { 1045 {
  1046 + auto& doc = pdf.doc();
1043 JSON::writeDictionaryKey(p, first, "pages", 1); 1047 JSON::writeDictionaryKey(p, first, "pages", 1);
1044 bool first_page = true; 1048 bool first_page = true;
1045 JSON::writeArrayOpen(p, first_page, 2); 1049 JSON::writeArrayOpen(p, first_page, 2);
1046 - auto& pldh = pdf.doc().page_labels();  
1047 - auto& odh = pdf.doc().outlines(); 1050 + auto& pldh = doc.page_labels();
  1051 + auto& odh = doc.outlines();
1048 int pageno = -1; 1052 int pageno = -1;
1049 - for (auto& page: pdf.getAllPages()) { 1053 + for (auto& page: doc.pages().all()) {
1050 ++pageno; 1054 ++pageno;
1051 JSON j_page = JSON::makeDictionary(); 1055 JSON j_page = JSON::makeDictionary();
1052 QPDFPageObjectHelper ph(page); 1056 QPDFPageObjectHelper ph(page);
@@ -1105,9 +1109,10 @@ QPDFJob::doJSONPages(Pipeline* p, bool&amp; first, QPDF&amp; pdf) @@ -1105,9 +1109,10 @@ QPDFJob::doJSONPages(Pipeline* p, bool&amp; first, QPDF&amp; pdf)
1105 void 1109 void
1106 QPDFJob::doJSONPageLabels(Pipeline* p, bool& first, QPDF& pdf) 1110 QPDFJob::doJSONPageLabels(Pipeline* p, bool& first, QPDF& pdf)
1107 { 1111 {
  1112 + auto& doc = pdf.doc();
1108 JSON j_labels = JSON::makeArray(); 1113 JSON j_labels = JSON::makeArray();
1109 - auto& pldh = pdf.doc().page_labels();  
1110 - long long npages = QIntC::to_longlong(pdf.getAllPages().size()); 1114 + auto& pldh = doc.page_labels();
  1115 + long long npages = QIntC::to_longlong(doc.pages().all().size());
1111 if (pldh.hasPageLabels()) { 1116 if (pldh.hasPageLabels()) {
1112 std::vector<QPDFObjectHandle> labels; 1117 std::vector<QPDFObjectHandle> labels;
1113 pldh.getLabelsForPageRange(0, npages - 1, 0, labels); 1118 pldh.getLabelsForPageRange(0, npages - 1, 0, labels);
@@ -1153,27 +1158,29 @@ QPDFJob::addOutlinesToJson( @@ -1153,27 +1158,29 @@ QPDFJob::addOutlinesToJson(
1153 void 1158 void
1154 QPDFJob::doJSONOutlines(Pipeline* p, bool& first, QPDF& pdf) 1159 QPDFJob::doJSONOutlines(Pipeline* p, bool& first, QPDF& pdf)
1155 { 1160 {
  1161 + auto& doc = pdf.doc();
1156 std::map<QPDFObjGen, int> page_numbers; 1162 std::map<QPDFObjGen, int> page_numbers;
1157 int n = 0; 1163 int n = 0;
1158 - for (auto const& oh: pdf.getAllPages()) { 1164 + for (auto const& oh: doc.pages().all()) {
1159 page_numbers[oh] = ++n; 1165 page_numbers[oh] = ++n;
1160 } 1166 }
1161 1167
1162 JSON j_outlines = JSON::makeArray(); 1168 JSON j_outlines = JSON::makeArray();
1163 - addOutlinesToJson(pdf.doc().outlines().getTopLevelOutlines(), j_outlines, page_numbers); 1169 + addOutlinesToJson(doc.outlines().getTopLevelOutlines(), j_outlines, page_numbers);
1164 JSON::writeDictionaryItem(p, first, "outlines", j_outlines, 1); 1170 JSON::writeDictionaryItem(p, first, "outlines", j_outlines, 1);
1165 } 1171 }
1166 1172
1167 void 1173 void
1168 QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf) 1174 QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf)
1169 { 1175 {
  1176 + auto& doc = pdf.doc();
1170 JSON j_acroform = JSON::makeDictionary(); 1177 JSON j_acroform = JSON::makeDictionary();
1171 - auto& afdh = pdf.doc().acroform(); 1178 + auto& afdh = doc.acroform();
1172 j_acroform.addDictionaryMember("hasacroform", JSON::makeBool(afdh.hasAcroForm())); 1179 j_acroform.addDictionaryMember("hasacroform", JSON::makeBool(afdh.hasAcroForm()));
1173 j_acroform.addDictionaryMember("needappearances", JSON::makeBool(afdh.getNeedAppearances())); 1180 j_acroform.addDictionaryMember("needappearances", JSON::makeBool(afdh.getNeedAppearances()));
1174 JSON j_fields = j_acroform.addDictionaryMember("fields", JSON::makeArray()); 1181 JSON j_fields = j_acroform.addDictionaryMember("fields", JSON::makeArray());
1175 int pagepos1 = 0; 1182 int pagepos1 = 0;
1176 - for (auto const& page: pdf.getAllPages()) { 1183 + for (auto const& page: doc.pages().all()) {
1177 ++pagepos1; 1184 ++pagepos1;
1178 for (auto& aoh: afdh.getWidgetAnnotationsForPage({page})) { 1185 for (auto& aoh: afdh.getWidgetAnnotationsForPage({page})) {
1179 QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(aoh); 1186 QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(aoh);
@@ -1851,8 +1858,8 @@ QPDFJob::validateUnderOverlay(QPDF&amp; pdf, UnderOverlay* uo) @@ -1851,8 +1858,8 @@ QPDFJob::validateUnderOverlay(QPDF&amp; pdf, UnderOverlay* uo)
1851 { 1858 {
1852 processFile(uo->pdf, uo->filename.data(), uo->password.data(), true, false); 1859 processFile(uo->pdf, uo->filename.data(), uo->password.data(), true, false);
1853 try { 1860 try {
1854 - uo->to_pagenos =  
1855 - QUtil::parse_numrange(uo->to_nr.data(), static_cast<int>(pdf.getAllPages().size())); 1861 + uo->to_pagenos = QUtil::parse_numrange(
  1862 + uo->to_nr.data(), static_cast<int>(pdf.doc().pages().all().size()));
1856 } catch (std::runtime_error& e) { 1863 } catch (std::runtime_error& e) {
1857 throw std::runtime_error( 1864 throw std::runtime_error(
1858 "parsing numeric range for " + uo->which + " \"to\" pages: " + e.what()); 1865 "parsing numeric range for " + uo->which + " \"to\" pages: " + e.what());
@@ -1861,7 +1868,7 @@ QPDFJob::validateUnderOverlay(QPDF&amp; pdf, UnderOverlay* uo) @@ -1861,7 +1868,7 @@ QPDFJob::validateUnderOverlay(QPDF&amp; pdf, UnderOverlay* uo)
1861 if (uo->from_nr.empty()) { 1868 if (uo->from_nr.empty()) {
1862 uo->from_nr = uo->repeat_nr; 1869 uo->from_nr = uo->repeat_nr;
1863 } 1870 }
1864 - int uo_npages = static_cast<int>(uo->pdf->getAllPages().size()); 1871 + int uo_npages = static_cast<int>(uo->pdf->doc().pages().size());
1865 uo->from_pagenos = QUtil::parse_numrange(uo->from_nr.data(), uo_npages); 1872 uo->from_pagenos = QUtil::parse_numrange(uo->from_nr.data(), uo_npages);
1866 if (!uo->repeat_nr.empty()) { 1873 if (!uo->repeat_nr.empty()) {
1867 uo->repeat_pagenos = QUtil::parse_numrange(uo->repeat_nr.data(), uo_npages); 1874 uo->repeat_pagenos = QUtil::parse_numrange(uo->repeat_nr.data(), uo_npages);
@@ -1887,7 +1894,7 @@ QPDFJob::doUnderOverlayForPage( @@ -1887,7 +1894,7 @@ QPDFJob::doUnderOverlayForPage(
1887 } 1894 }
1888 auto& dest_afdh = dest_page.qpdf()->doc().acroform(); 1895 auto& dest_afdh = dest_page.qpdf()->doc().acroform();
1889 1896
1890 - auto const& pages = uo.pdf->getAllPages(); 1897 + auto const& pages = uo.pdf->doc().pages().all();
1891 std::string content; 1898 std::string content;
1892 int min_suffix = 1; 1899 int min_suffix = 1;
1893 QPDFObjectHandle resources = dest_page.getAttribute("/Resources", true); 1900 QPDFObjectHandle resources = dest_page.getAttribute("/Resources", true);
@@ -1957,7 +1964,7 @@ QPDFJob::handleUnderOverlay(QPDF&amp; pdf) @@ -1957,7 +1964,7 @@ QPDFJob::handleUnderOverlay(QPDF&amp; pdf)
1957 validateUnderOverlay(pdf, &uo); 1964 validateUnderOverlay(pdf, &uo);
1958 } 1965 }
1959 1966
1960 - auto const& dest_pages = pdf.getAllPages(); 1967 + auto const& dest_pages = pdf.doc().pages().all();
1961 1968
1962 // First vector key is 0-based page number. Second is index into the overlay/underlay vector. 1969 // First vector key is 0-based page number. Second is index into the overlay/underlay vector.
1963 // Watch out to not reverse the keys or be off by one. 1970 // Watch out to not reverse the keys or be off by one.
@@ -2348,15 +2355,16 @@ void @@ -2348,15 +2355,16 @@ void
2348 QPDFJob::Input::initialize(Inputs& in, QPDF* a_qpdf) 2355 QPDFJob::Input::initialize(Inputs& in, QPDF* a_qpdf)
2349 { 2356 {
2350 qpdf = a_qpdf ? a_qpdf : qpdf_p.get(); 2357 qpdf = a_qpdf ? a_qpdf : qpdf_p.get();
  2358 + auto& doc = qpdf->doc();
2351 if (qpdf) { 2359 if (qpdf) {
2352 - orig_pages = qpdf->getAllPages(); 2360 + orig_pages = doc.pages().all();
2353 n_pages = static_cast<int>(orig_pages.size()); 2361 n_pages = static_cast<int>(orig_pages.size());
2354 copied_pages = std::vector<bool>(orig_pages.size(), false); 2362 copied_pages = std::vector<bool>(orig_pages.size(), false);
2355 2363
2356 if (in.job.m->remove_unreferenced_page_resources != QPDFJob::re_no) { 2364 if (in.job.m->remove_unreferenced_page_resources != QPDFJob::re_no) {
2357 remove_unreferenced = in.job.shouldRemoveUnreferencedResources(*qpdf); 2365 remove_unreferenced = in.job.shouldRemoveUnreferencedResources(*qpdf);
2358 } 2366 }
2359 - if (qpdf->doc().page_labels().hasPageLabels()) { 2367 + if (doc.page_labels().hasPageLabels()) {
2360 in.any_page_labels = true; 2368 in.any_page_labels = true;
2361 } 2369 }
2362 } 2370 }
@@ -3001,6 +3009,8 @@ QPDFJob::setWriterOptions(QPDFWriter&amp; w) @@ -3001,6 +3009,8 @@ QPDFJob::setWriterOptions(QPDFWriter&amp; w)
3001 void 3009 void
3002 QPDFJob::doSplitPages(QPDF& pdf) 3010 QPDFJob::doSplitPages(QPDF& pdf)
3003 { 3011 {
  3012 + auto& doc = pdf.doc();
  3013 +
3004 // Generate output file pattern 3014 // Generate output file pattern
3005 std::string before; 3015 std::string before;
3006 std::string after; 3016 std::string after;
@@ -3024,9 +3034,9 @@ QPDFJob::doSplitPages(QPDF&amp; pdf) @@ -3024,9 +3034,9 @@ QPDFJob::doSplitPages(QPDF&amp; pdf)
3024 QPDFPageDocumentHelper dh(pdf); 3034 QPDFPageDocumentHelper dh(pdf);
3025 dh.removeUnreferencedResources(); 3035 dh.removeUnreferencedResources();
3026 } 3036 }
3027 - auto& pldh = pdf.doc().page_labels();  
3028 - auto& afdh = pdf.doc().acroform();  
3029 - std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); 3037 + auto& pldh = doc.page_labels();
  3038 + auto& afdh = doc.acroform();
  3039 + std::vector<QPDFObjectHandle> const& pages = doc.pages().all();
3030 size_t pageno_len = std::to_string(pages.size()).length(); 3040 size_t pageno_len = std::to_string(pages.size()).length();
3031 size_t num_pages = pages.size(); 3041 size_t num_pages = pages.size();
3032 for (size_t i = 0; i < num_pages; i += QIntC::to_size(m->split_pages)) { 3042 for (size_t i = 0; i < num_pages; i += QIntC::to_size(m->split_pages)) {