Commit 4c23d2c09459fbf6772800d30e8ace2ee0e4a9aa

Authored by m-holger
1 parent 5ad81ce6

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

libqpdf/QPDFObjectHandle.cc
@@ -2099,22 +2099,22 @@ bool @@ -2099,22 +2099,22 @@ bool
2099 QPDFObjectHandle::isPageObject() const 2099 QPDFObjectHandle::isPageObject() const
2100 { 2100 {
2101 // See comments in QPDFObjectHandle.hh. 2101 // See comments in QPDFObjectHandle.hh.
2102 - if (getOwningQPDF() == nullptr) { 2102 + if (!qpdf()) {
2103 return false; 2103 return false;
2104 } 2104 }
2105 // getAllPages repairs /Type when traversing the page tree. 2105 // getAllPages repairs /Type when traversing the page tree.
2106 - getOwningQPDF()->getAllPages(); 2106 + (void)qpdf()->doc().pages().all();
2107 return isDictionaryOfType("/Page"); 2107 return isDictionaryOfType("/Page");
2108 } 2108 }
2109 2109
2110 bool 2110 bool
2111 QPDFObjectHandle::isPagesObject() const 2111 QPDFObjectHandle::isPagesObject() const
2112 { 2112 {
2113 - if (getOwningQPDF() == nullptr) { 2113 + if (!qpdf()) {
2114 return false; 2114 return false;
2115 } 2115 }
2116 // getAllPages repairs /Type when traversing the page tree. 2116 // getAllPages repairs /Type when traversing the page tree.
2117 - getOwningQPDF()->getAllPages(); 2117 + (void)qpdf()->doc().pages().all();
2118 return isDictionaryOfType("/Pages"); 2118 return isDictionaryOfType("/Pages");
2119 } 2119 }
2120 2120
libqpdf/QPDFPageDocumentHelper.cc
@@ -29,8 +29,10 @@ QPDFPageDocumentHelper::validate(bool repair) @@ -29,8 +29,10 @@ QPDFPageDocumentHelper::validate(bool repair)
29 std::vector<QPDFPageObjectHelper> 29 std::vector<QPDFPageObjectHelper>
30 QPDFPageDocumentHelper::getAllPages() 30 QPDFPageDocumentHelper::getAllPages()
31 { 31 {
  32 + auto& pp = qpdf.doc().pages();
32 std::vector<QPDFPageObjectHelper> pages; 33 std::vector<QPDFPageObjectHelper> pages;
33 - for (auto const& iter: qpdf.getAllPages()) { 34 + pages.reserve(pp.size());
  35 + for (auto const& iter: pp.all()) {
34 pages.emplace_back(iter); 36 pages.emplace_back(iter);
35 } 37 }
36 return pages; 38 return pages;
libqpdf/QPDF_json.cc
@@ -466,7 +466,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -466,7 +466,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
466 if (key == "qpdf") { 466 if (key == "qpdf") {
467 this->saw_qpdf = true; 467 this->saw_qpdf = true;
468 if (!value.isArray()) { 468 if (!value.isArray()) {
469 - QTC::TC("qpdf", "QPDF_json qpdf not array");  
470 error(value.getStart(), "\"qpdf\" must be an array"); 469 error(value.getStart(), "\"qpdf\" must be an array");
471 } else { 470 } else {
472 next_state = st_qpdf; 471 next_state = st_qpdf;
@@ -489,7 +488,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -489,7 +488,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
489 } 488 }
490 } 489 }
491 if (!okay) { 490 if (!okay) {
492 - QTC::TC("qpdf", "QPDF_json bad pdf version");  
493 error(value.getStart(), "invalid PDF version (must be \"x.y\")"); 491 error(value.getStart(), "invalid PDF version (must be \"x.y\")");
494 } 492 }
495 } else if (key == "jsonversion") { 493 } else if (key == "jsonversion") {
@@ -503,7 +501,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -503,7 +501,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
503 } 501 }
504 } 502 }
505 if (!okay) { 503 if (!okay) {
506 - QTC::TC("qpdf", "QPDF_json bad json version");  
507 error(value.getStart(), "invalid JSON version (must be numeric value 2)"); 504 error(value.getStart(), "invalid JSON version (must be numeric value 2)");
508 } 505 }
509 } else if (key == "pushedinheritedpageresources") { 506 } else if (key == "pushedinheritedpageresources") {
@@ -513,17 +510,15 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -513,17 +510,15 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
513 this->pdf.pushInheritedAttributesToPage(); 510 this->pdf.pushInheritedAttributesToPage();
514 } 511 }
515 } else { 512 } else {
516 - QTC::TC("qpdf", "QPDF_json bad pushedinheritedpageresources");  
517 error(value.getStart(), "pushedinheritedpageresources must be a boolean"); 513 error(value.getStart(), "pushedinheritedpageresources must be a boolean");
518 } 514 }
519 } else if (key == "calledgetallpages") { 515 } else if (key == "calledgetallpages") {
520 bool v; 516 bool v;
521 if (value.getBool(v)) { 517 if (value.getBool(v)) {
522 if (!this->must_be_complete && v) { 518 if (!this->must_be_complete && v) {
523 - this->pdf.getAllPages(); 519 + (void)pdf.doc().pages().all();
524 } 520 }
525 } else { 521 } else {
526 - QTC::TC("qpdf", "QPDF_json bad calledgetallpages");  
527 error(value.getStart(), "calledgetallpages must be a boolean"); 522 error(value.getStart(), "calledgetallpages must be a boolean");
528 } 523 }
529 } else { 524 } else {
@@ -544,7 +539,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -544,7 +539,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
544 next_obj = objects.getObjectForJSON(obj, gen); 539 next_obj = objects.getObjectForJSON(obj, gen);
545 } 540 }
546 } else { 541 } else {
547 - QTC::TC("qpdf", "QPDF_json bad object key");  
548 error(value.getStart(), "object key should be \"trailer\" or \"obj:n n R\""); 542 error(value.getStart(), "object key should be \"trailer\" or \"obj:n n R\"");
549 } 543 }
550 } else if (state == st_object_top) { 544 } else if (state == st_object_top) {
@@ -592,7 +586,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -592,7 +586,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
592 } 586 }
593 } else if (key == "stream") { 587 } else if (key == "stream") {
594 // Don't need to set saw_stream here since there's already an error. 588 // Don't need to set saw_stream here since there's already an error.
595 - QTC::TC("qpdf", "QPDF_json trailer stream");  
596 error(value.getStart(), "the trailer may not be a stream"); 589 error(value.getStart(), "the trailer may not be a stream");
597 } else { 590 } else {
598 // Ignore unknown keys for forward compatibility 591 // Ignore unknown keys for forward compatibility
@@ -618,7 +611,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value) @@ -618,7 +611,6 @@ QPDF::JSONReactor::dictionaryItem(std::string const&amp; key, JSON const&amp; value)
618 this->saw_data = true; 611 this->saw_data = true;
619 std::string v; 612 std::string v;
620 if (!value.getString(v)) { 613 if (!value.getString(v)) {
621 - QTC::TC("qpdf", "QPDF_json stream data not string");  
622 error(value.getStart(), "\"stream.data\" must be a string"); 614 error(value.getStart(), "\"stream.data\" must be a string");
623 tos.object.replaceStreamData("", {}, {}); 615 tos.object.replaceStreamData("", {}, {});
624 } else { 616 } else {
libqpdf/QPDF_objects.cc
@@ -389,13 +389,11 @@ Objects::reconstruct_xref(QPDFExc&amp; e, bool found_startxref) @@ -389,13 +389,11 @@ Objects::reconstruct_xref(QPDFExc&amp; e, bool found_startxref)
389 } 389 }
390 check_warnings(); 390 check_warnings();
391 if (!m->parsed) { 391 if (!m->parsed) {
392 - m->parsed = true;  
393 - qpdf.getAllPages();  
394 - check_warnings();  
395 - if (m->all_pages.empty()) {  
396 - m->parsed = false; 392 + m->parsed = !m->pages.empty();
  393 + if (!m->parsed) {
397 throw damagedPDF("", -1, "unable to find any pages while recovering damaged file"); 394 throw damagedPDF("", -1, "unable to find any pages while recovering damaged file");
398 } 395 }
  396 + check_warnings();
399 } 397 }
400 398
401 // We could iterate through the objects looking for streams and try to find objects inside of 399 // We could iterate through the objects looking for streams and try to find objects inside of
libqpdf/QPDF_pages.cc
@@ -305,7 +305,7 @@ Pages::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) @@ -305,7 +305,7 @@ Pages::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys)
305 305
306 // Calling getAllPages() resolves any duplicated page objects, repairs broken nodes, and detects 306 // Calling getAllPages() resolves any duplicated page objects, repairs broken nodes, and detects
307 // loops, so we don't have to do those activities here. 307 // loops, so we don't have to do those activities here.
308 - qpdf.getAllPages(); 308 + (void)all();
309 309
310 // key_ancestors is a mapping of page attribute keys to a stack of Pages nodes that contain 310 // key_ancestors is a mapping of page attribute keys to a stack of Pages nodes that contain
311 // values for them. 311 // values for them.
libqpdf/qpdf-c.cc
1 #include <qpdf/qpdf-c.h> 1 #include <qpdf/qpdf-c.h>
2 2
3 -#include <qpdf/QPDF.hh> 3 +#include <qpdf/QPDF_private.hh>
4 4
5 #include <qpdf/BufferInputSource.hh> 5 #include <qpdf/BufferInputSource.hh>
6 #include <qpdf/Pl_Buffer.hh> 6 #include <qpdf/Pl_Buffer.hh>
@@ -1804,10 +1804,9 @@ qpdf_oh_replace_stream_data( @@ -1804,10 +1804,9 @@ qpdf_oh_replace_stream_data(
1804 int 1804 int
1805 qpdf_get_num_pages(qpdf_data qpdf) 1805 qpdf_get_num_pages(qpdf_data qpdf)
1806 { 1806 {
1807 - QTC::TC("qpdf", "qpdf-c called qpdf_num_pages");  
1808 int n = -1; 1807 int n = -1;
1809 QPDF_ERROR_CODE code = 1808 QPDF_ERROR_CODE code =
1810 - trap_errors(qpdf, [&n](qpdf_data q) { n = QIntC::to_int(q->qpdf->getAllPages().size()); }); 1809 + trap_errors(qpdf, [&n](qpdf_data q) { n = QIntC::to_int(q->qpdf->doc().pages().size()); });
1811 if (code & QPDF_ERRORS) { 1810 if (code & QPDF_ERRORS) {
1812 return -1; 1811 return -1;
1813 } 1812 }
@@ -1817,10 +1816,10 @@ qpdf_get_num_pages(qpdf_data qpdf) @@ -1817,10 +1816,10 @@ qpdf_get_num_pages(qpdf_data qpdf)
1817 qpdf_oh 1816 qpdf_oh
1818 qpdf_get_page_n(qpdf_data qpdf, size_t i) 1817 qpdf_get_page_n(qpdf_data qpdf, size_t i)
1819 { 1818 {
1820 - QTC::TC("qpdf", "qpdf-c called qpdf_get_page_n");  
1821 qpdf_oh result = 0; 1819 qpdf_oh result = 0;
1822 - QPDF_ERROR_CODE code = trap_errors(  
1823 - qpdf, [&result, i](qpdf_data q) { result = new_object(q, q->qpdf->getAllPages().at(i)); }); 1820 + QPDF_ERROR_CODE code = trap_errors(qpdf, [&result, i](qpdf_data q) {
  1821 + result = new_object(q, q->qpdf->doc().pages().all().at(i));
  1822 + });
1824 if ((code & QPDF_ERRORS) || (result == 0)) { 1823 if ((code & QPDF_ERRORS) || (result == 0)) {
1825 return qpdf_oh_new_uninitialized(qpdf); 1824 return qpdf_oh_new_uninitialized(qpdf);
1826 } 1825 }
qpdf/qpdf.testcov
@@ -383,8 +383,6 @@ qpdf-c warn about oh error 1 @@ -383,8 +383,6 @@ qpdf-c warn about oh error 1
383 qpdf-c cleanup warned about unhandled error 0 383 qpdf-c cleanup warned about unhandled error 0
384 qpdf-c called qpdf_get_object_by_id 0 384 qpdf-c called qpdf_get_object_by_id 0
385 qpdf-c called qpdf_replace_object 0 385 qpdf-c called qpdf_replace_object 0
386 -qpdf-c called qpdf_num_pages 0  
387 -qpdf-c called qpdf_get_page_n 0  
388 qpdf-c called qpdf_update_all_pages_cache 0 386 qpdf-c called qpdf_update_all_pages_cache 0
389 qpdf-c called qpdf_find_page_by_id 0 387 qpdf-c called qpdf_find_page_by_id 0
390 qpdf-c called qpdf_find_page_by_oh 0 388 qpdf-c called qpdf_find_page_by_oh 0
@@ -422,10 +420,7 @@ qpdf-c called qpdf_empty_pdf 0 @@ -422,10 +420,7 @@ qpdf-c called qpdf_empty_pdf 0
422 QPDF_json missing qpdf 0 420 QPDF_json missing qpdf 0
423 QPDF_json missing pdf version 0 421 QPDF_json missing pdf version 0
424 QPDF_json top-level scalar 0 422 QPDF_json top-level scalar 0
425 -QPDF_json bad pdf version 0  
426 QPDF_json top-level array 0 423 QPDF_json top-level array 0
427 -QPDF_json bad object key 0  
428 -QPDF_json trailer stream 0  
429 QPDF_json missing trailer 0 424 QPDF_json missing trailer 0
430 QPDF_json missing objects 0 425 QPDF_json missing objects 0
431 QPDF_json ignoring in st_ignore 0 426 QPDF_json ignoring in st_ignore 0
@@ -446,17 +441,12 @@ QPDF_json ignore unknown key in stream 0 @@ -446,17 +441,12 @@ QPDF_json ignore unknown key in stream 0
446 QPDF_json data and datafile 0 441 QPDF_json data and datafile 0
447 QPDF_json no stream data in update mode 0 442 QPDF_json no stream data in update mode 0
448 QPDF_json updating existing stream 0 443 QPDF_json updating existing stream 0
449 -QPDF_json qpdf not array 0  
450 QPDF_json more than two qpdf elements 0 444 QPDF_json more than two qpdf elements 0
451 QPDF_json missing json version 0 445 QPDF_json missing json version 0
452 -QPDF_json bad json version 0  
453 -QPDF_json bad calledgetallpages 0  
454 -QPDF_json bad pushedinheritedpageresources 0  
455 QPDFPageObjectHelper used fallback without copying 0 446 QPDFPageObjectHelper used fallback without copying 0
456 QPDF skipping cache for known unchecked object 0 447 QPDF skipping cache for known unchecked object 0
457 QPDF recover xref stream 0 448 QPDF recover xref stream 0
458 QPDFJob json over/under no file 0 449 QPDFJob json over/under no file 0
459 QPDF_Array copy 1 450 QPDF_Array copy 1
460 -QPDF_json stream data not string 0  
461 QPDF_json stream datafile not string 0 451 QPDF_json stream datafile not string 0
462 QPDF_json stream not a dictionary 0 452 QPDF_json stream not a dictionary 0