From df493c352f3936b7b6597791c289175dffb3db57 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 21 Jun 2012 09:12:51 -0400 Subject: Refactor optimizePagesTree Split optimizePagesTree into a simpler top-level routine and a recursive internal routine. --- libqpdf/QPDF_optimization.cc | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index 572cf1ae..10b56b60 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -167,15 +167,7 @@ QPDF::optimize(std::map const& object_stream_data, // Traverse pages tree pushing all inherited resources down to the // page level. - - // key_ancestors is a mapping of page attribute keys to a stack of - // Pages nodes that contain values for them. pageno is the - // current page sequence number numbered from 0. - std::map > key_ancestors; - int pageno = 0; - optimizePagesTree(this->trailer.getKey("/Root").getKey("/Pages"), - key_ancestors, pageno, allow_changes); - assert(key_ancestors.empty()); + optimizePagesTree(allow_changes); // Traverse document-level items std::set keys = this->trailer.getKeys(); @@ -220,7 +212,23 @@ QPDF::optimize(std::map const& object_stream_data, } void -QPDF::optimizePagesTree( +QPDF::optimizePagesTree(bool allow_changes) +{ + // Traverse pages tree pushing all inherited resources down to the + // page level. + + // key_ancestors is a mapping of page attribute keys to a stack of + // Pages nodes that contain values for them. pageno is the + // current page sequence number numbered from 0. + std::map > key_ancestors; + int pageno = 0; + optimizePagesTreeInternal(this->trailer.getKey("/Root").getKey("/Pages"), + key_ancestors, pageno, allow_changes); + assert(key_ancestors.empty()); +} + +void +QPDF::optimizePagesTreeInternal( QPDFObjectHandle cur_pages, std::map >& key_ancestors, int& pageno, bool allow_changes) @@ -293,8 +301,8 @@ QPDF::optimizePagesTree( int n = kids.getArrayNItems(); for (int i = 0; i < n; ++i) { - optimizePagesTree(kids.getArrayItem(i), key_ancestors, pageno, - allow_changes); + optimizePagesTreeInternal( + kids.getArrayItem(i), key_ancestors, pageno, allow_changes); } // For each inheritable key, pop the stack. If the stack -- cgit v1.2.3-54-g00ecf