Commit df493c352f3936b7b6597791c289175dffb3db57

Authored by Jay Berkenbilt
1 parent 5d3f93be

Refactor optimizePagesTree

Split optimizePagesTree into a simpler top-level routine and a
recursive internal routine.
include/qpdf/QPDF.hh
@@ -873,7 +873,8 @@ class QPDF @@ -873,7 +873,8 @@ class QPDF
873 873
874 // Methods to support optimization 874 // Methods to support optimization
875 875
876 - void optimizePagesTree( 876 + void optimizePagesTree(bool allow_changes);
  877 + void optimizePagesTreeInternal(
877 QPDFObjectHandle, 878 QPDFObjectHandle,
878 std::map<std::string, std::vector<QPDFObjectHandle> >&, 879 std::map<std::string, std::vector<QPDFObjectHandle> >&,
879 int& pageno, bool allow_changes); 880 int& pageno, bool allow_changes);
libqpdf/QPDF_optimization.cc
@@ -167,15 +167,7 @@ QPDF::optimize(std::map&lt;int, int&gt; const&amp; object_stream_data, @@ -167,15 +167,7 @@ QPDF::optimize(std::map&lt;int, int&gt; const&amp; object_stream_data,
167 167
168 // Traverse pages tree pushing all inherited resources down to the 168 // Traverse pages tree pushing all inherited resources down to the
169 // page level. 169 // page level.
170 -  
171 - // key_ancestors is a mapping of page attribute keys to a stack of  
172 - // Pages nodes that contain values for them. pageno is the  
173 - // current page sequence number numbered from 0.  
174 - std::map<std::string, std::vector<QPDFObjectHandle> > key_ancestors;  
175 - int pageno = 0;  
176 - optimizePagesTree(this->trailer.getKey("/Root").getKey("/Pages"),  
177 - key_ancestors, pageno, allow_changes);  
178 - assert(key_ancestors.empty()); 170 + optimizePagesTree(allow_changes);
179 171
180 // Traverse document-level items 172 // Traverse document-level items
181 std::set<std::string> keys = this->trailer.getKeys(); 173 std::set<std::string> keys = this->trailer.getKeys();
@@ -220,7 +212,23 @@ QPDF::optimize(std::map&lt;int, int&gt; const&amp; object_stream_data, @@ -220,7 +212,23 @@ QPDF::optimize(std::map&lt;int, int&gt; const&amp; object_stream_data,
220 } 212 }
221 213
222 void 214 void
223 -QPDF::optimizePagesTree( 215 +QPDF::optimizePagesTree(bool allow_changes)
  216 +{
  217 + // Traverse pages tree pushing all inherited resources down to the
  218 + // page level.
  219 +
  220 + // key_ancestors is a mapping of page attribute keys to a stack of
  221 + // Pages nodes that contain values for them. pageno is the
  222 + // current page sequence number numbered from 0.
  223 + std::map<std::string, std::vector<QPDFObjectHandle> > key_ancestors;
  224 + int pageno = 0;
  225 + optimizePagesTreeInternal(this->trailer.getKey("/Root").getKey("/Pages"),
  226 + key_ancestors, pageno, allow_changes);
  227 + assert(key_ancestors.empty());
  228 +}
  229 +
  230 +void
  231 +QPDF::optimizePagesTreeInternal(
224 QPDFObjectHandle cur_pages, 232 QPDFObjectHandle cur_pages,
225 std::map<std::string, std::vector<QPDFObjectHandle> >& key_ancestors, 233 std::map<std::string, std::vector<QPDFObjectHandle> >& key_ancestors,
226 int& pageno, bool allow_changes) 234 int& pageno, bool allow_changes)
@@ -293,8 +301,8 @@ QPDF::optimizePagesTree( @@ -293,8 +301,8 @@ QPDF::optimizePagesTree(
293 int n = kids.getArrayNItems(); 301 int n = kids.getArrayNItems();
294 for (int i = 0; i < n; ++i) 302 for (int i = 0; i < n; ++i)
295 { 303 {
296 - optimizePagesTree(kids.getArrayItem(i), key_ancestors, pageno,  
297 - allow_changes); 304 + optimizePagesTreeInternal(
  305 + kids.getArrayItem(i), key_ancestors, pageno, allow_changes);
298 } 306 }
299 307
300 // For each inheritable key, pop the stack. If the stack 308 // For each inheritable key, pop the stack. If the stack