From 39bbaa86e3d7dcf77f42a36b78b727c142fcadee Mon Sep 17 00:00:00 2001 From: Tobias Hoffmann Date: Fri, 22 Jun 2012 18:11:25 +0200 Subject: Build this->all_pages while traversing with pushInheritedAttributesToPage --- include/qpdf/QPDF.hh | 1 + libqpdf/QPDF_optimization.cc | 10 ++++++---- libqpdf/QPDF_pages.cc | 10 ++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index ec5f5d7c..fbac2ab2 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -913,6 +913,7 @@ class QPDF void pushInheritedAttributesToPageInternal( QPDFObjectHandle, std::map >&, + std::vector& all_pages, bool allow_changes, bool warn_skipped_keys); void updateObjectMaps(ObjUser const& ou, QPDFObjectHandle oh); void updateObjectMapsInternal(ObjUser const& ou, QPDFObjectHandle oh, diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index 7f7049a4..e6ad2750 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -166,9 +166,8 @@ QPDF::optimize(std::map const& object_stream_data, } // Traverse pages tree pushing all inherited resources down to the - // page level. + // page level. This also initializes this->all_pages. pushInheritedAttributesToPage(allow_changes, false); - getAllPages(); // Traverse pages int n = this->all_pages.size(); @@ -236,9 +235,10 @@ QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) // key_ancestors is a mapping of page attribute keys to a stack of // Pages nodes that contain values for them. std::map > key_ancestors; + this->all_pages.clear(); pushInheritedAttributesToPageInternal( this->trailer.getKey("/Root").getKey("/Pages"), - key_ancestors, allow_changes, warn_skipped_keys); + key_ancestors, this->all_pages, allow_changes, warn_skipped_keys); assert(key_ancestors.empty()); } @@ -246,6 +246,7 @@ void QPDF::pushInheritedAttributesToPageInternal( QPDFObjectHandle cur_pages, std::map >& key_ancestors, + std::vector& pages, bool allow_changes, bool warn_skipped_keys) { // Extract the underlying dictionary object @@ -336,7 +337,7 @@ QPDF::pushInheritedAttributesToPageInternal( for (int i = 0; i < n; ++i) { pushInheritedAttributesToPageInternal( - kids.getArrayItem(i), key_ancestors, + kids.getArrayItem(i), key_ancestors, pages, allow_changes, warn_skipped_keys); } @@ -385,6 +386,7 @@ QPDF::pushInheritedAttributesToPageInternal( QTC::TC("qpdf", "QPDF opt page resource hides ancestor"); } } + pages.push_back(cur_pages); } else { diff --git a/libqpdf/QPDF_pages.cc b/libqpdf/QPDF_pages.cc index 11e21266..818215c4 100644 --- a/libqpdf/QPDF_pages.cc +++ b/libqpdf/QPDF_pages.cc @@ -43,6 +43,8 @@ std::vector const& QPDF::getAllPages() { + // Note that pushInheritedAttributesToPage may also be used to + // initialize this->all_pages. if (this->all_pages.empty()) { getAllPagesInternal(getRoot().getKey("/Pages"), this->all_pages); @@ -101,9 +103,9 @@ QPDF::flattenPagesTree() return; } - // Push inherited objects down to the /Page level + // Push inherited objects down to the /Page level. As a side + // effect this->all_pages will also be generated. pushInheritedAttributesToPage(true, true); - getAllPages(); QPDFObjectHandle pages = getRoot().getKey("/Pages"); @@ -228,14 +230,14 @@ QPDF::addPageAt(QPDFObjectHandle newpage, bool before, void QPDF::addPage(QPDFObjectHandle newpage, bool first) { - getAllPages(); if (first) { insertPage(newpage, 0); } else { - insertPage(newpage, (int)this->all_pages.size()); + insertPage(newpage, + getRoot().getKey("/Pages").getKey("/Count").getIntValue()); } } -- cgit v1.2.3-70-g09d2