Commit 39bbaa86e3d7dcf77f42a36b78b727c142fcadee

Authored by Tobias Hoffmann
Committed by Jay Berkenbilt
1 parent e2dedde4

Build this->all_pages while traversing with pushInheritedAttributesToPage

include/qpdf/QPDF.hh
... ... @@ -913,6 +913,7 @@ class QPDF
913 913 void pushInheritedAttributesToPageInternal(
914 914 QPDFObjectHandle,
915 915 std::map<std::string, std::vector<QPDFObjectHandle> >&,
  916 + std::vector<QPDFObjectHandle>& all_pages,
916 917 bool allow_changes, bool warn_skipped_keys);
917 918 void updateObjectMaps(ObjUser const& ou, QPDFObjectHandle oh);
918 919 void updateObjectMapsInternal(ObjUser const& ou, QPDFObjectHandle oh,
... ...
libqpdf/QPDF_optimization.cc
... ... @@ -166,9 +166,8 @@ QPDF::optimize(std::map&lt;int, int&gt; const&amp; object_stream_data,
166 166 }
167 167  
168 168 // Traverse pages tree pushing all inherited resources down to the
169   - // page level.
  169 + // page level. This also initializes this->all_pages.
170 170 pushInheritedAttributesToPage(allow_changes, false);
171   - getAllPages();
172 171  
173 172 // Traverse pages
174 173 int n = this->all_pages.size();
... ... @@ -236,9 +235,10 @@ QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys)
236 235 // key_ancestors is a mapping of page attribute keys to a stack of
237 236 // Pages nodes that contain values for them.
238 237 std::map<std::string, std::vector<QPDFObjectHandle> > key_ancestors;
  238 + this->all_pages.clear();
239 239 pushInheritedAttributesToPageInternal(
240 240 this->trailer.getKey("/Root").getKey("/Pages"),
241   - key_ancestors, allow_changes, warn_skipped_keys);
  241 + key_ancestors, this->all_pages, allow_changes, warn_skipped_keys);
242 242 assert(key_ancestors.empty());
243 243 }
244 244  
... ... @@ -246,6 +246,7 @@ void
246 246 QPDF::pushInheritedAttributesToPageInternal(
247 247 QPDFObjectHandle cur_pages,
248 248 std::map<std::string, std::vector<QPDFObjectHandle> >& key_ancestors,
  249 + std::vector<QPDFObjectHandle>& pages,
249 250 bool allow_changes, bool warn_skipped_keys)
250 251 {
251 252 // Extract the underlying dictionary object
... ... @@ -336,7 +337,7 @@ QPDF::pushInheritedAttributesToPageInternal(
336 337 for (int i = 0; i < n; ++i)
337 338 {
338 339 pushInheritedAttributesToPageInternal(
339   - kids.getArrayItem(i), key_ancestors,
  340 + kids.getArrayItem(i), key_ancestors, pages,
340 341 allow_changes, warn_skipped_keys);
341 342 }
342 343  
... ... @@ -385,6 +386,7 @@ QPDF::pushInheritedAttributesToPageInternal(
385 386 QTC::TC("qpdf", "QPDF opt page resource hides ancestor");
386 387 }
387 388 }
  389 + pages.push_back(cur_pages);
388 390 }
389 391 else
390 392 {
... ...
libqpdf/QPDF_pages.cc
... ... @@ -43,6 +43,8 @@
43 43 std::vector<QPDFObjectHandle> const&
44 44 QPDF::getAllPages()
45 45 {
  46 + // Note that pushInheritedAttributesToPage may also be used to
  47 + // initialize this->all_pages.
46 48 if (this->all_pages.empty())
47 49 {
48 50 getAllPagesInternal(getRoot().getKey("/Pages"), this->all_pages);
... ... @@ -101,9 +103,9 @@ QPDF::flattenPagesTree()
101 103 return;
102 104 }
103 105  
104   - // Push inherited objects down to the /Page level
  106 + // Push inherited objects down to the /Page level. As a side
  107 + // effect this->all_pages will also be generated.
105 108 pushInheritedAttributesToPage(true, true);
106   - getAllPages();
107 109  
108 110 QPDFObjectHandle pages = getRoot().getKey("/Pages");
109 111  
... ... @@ -228,14 +230,14 @@ QPDF::addPageAt(QPDFObjectHandle newpage, bool before,
228 230 void
229 231 QPDF::addPage(QPDFObjectHandle newpage, bool first)
230 232 {
231   - getAllPages();
232 233 if (first)
233 234 {
234 235 insertPage(newpage, 0);
235 236 }
236 237 else
237 238 {
238   - insertPage(newpage, (int)this->all_pages.size());
  239 + insertPage(newpage,
  240 + getRoot().getKey("/Pages").getKey("/Count").getIntValue());
239 241 }
240 242 }
241 243  
... ...