Commit 0356bcecc5f58b64d2cf02c68ca1934446cea9cb

Authored by m-holger
1 parent ff69773b

Tidy QPDF::pushInheritedAttributesToPageInternal

Remove unnecessary parameters.
Remove code that is unnecessary as result of a prior call to QPDF::getAllPages.
Avoid clearing and rebuilding of m->all_pages.
include/qpdf/QPDF.hh
@@ -1616,10 +1616,8 @@ class QPDF @@ -1616,10 +1616,8 @@ class QPDF
1616 void pushInheritedAttributesToPageInternal( 1616 void pushInheritedAttributesToPageInternal(
1617 QPDFObjectHandle, 1617 QPDFObjectHandle,
1618 std::map<std::string, std::vector<QPDFObjectHandle>>&, 1618 std::map<std::string, std::vector<QPDFObjectHandle>>&,
1619 - std::vector<QPDFObjectHandle>& all_pages,  
1620 bool allow_changes, 1619 bool allow_changes,
1621 - bool warn_skipped_keys,  
1622 - std::set<QPDFObjGen>& visited); 1620 + bool warn_skipped_keys);
1623 void updateObjectMaps( 1621 void updateObjectMaps(
1624 ObjUser const& ou, 1622 ObjUser const& ou,
1625 QPDFObjectHandle oh, 1623 QPDFObjectHandle oh,
libqpdf/QPDF_optimization.cc
@@ -148,15 +148,11 @@ QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) @@ -148,15 +148,11 @@ QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys)
148 // key_ancestors is a mapping of page attribute keys to a stack of 148 // key_ancestors is a mapping of page attribute keys to a stack of
149 // Pages nodes that contain values for them. 149 // Pages nodes that contain values for them.
150 std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors; 150 std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors;
151 - this->m->all_pages.clear();  
152 - std::set<QPDFObjGen> visited;  
153 pushInheritedAttributesToPageInternal( 151 pushInheritedAttributesToPageInternal(
154 this->m->trailer.getKey("/Root").getKey("/Pages"), 152 this->m->trailer.getKey("/Root").getKey("/Pages"),
155 key_ancestors, 153 key_ancestors,
156 - this->m->all_pages,  
157 allow_changes, 154 allow_changes,
158 - warn_skipped_keys,  
159 - visited); 155 + warn_skipped_keys);
160 if (!key_ancestors.empty()) { 156 if (!key_ancestors.empty()) {
161 throw std::logic_error("key_ancestors not empty after" 157 throw std::logic_error("key_ancestors not empty after"
162 " pushing inherited attributes to pages"); 158 " pushing inherited attributes to pages");
@@ -169,35 +165,9 @@ void @@ -169,35 +165,9 @@ void
169 QPDF::pushInheritedAttributesToPageInternal( 165 QPDF::pushInheritedAttributesToPageInternal(
170 QPDFObjectHandle cur_pages, 166 QPDFObjectHandle cur_pages,
171 std::map<std::string, std::vector<QPDFObjectHandle>>& key_ancestors, 167 std::map<std::string, std::vector<QPDFObjectHandle>>& key_ancestors,
172 - std::vector<QPDFObjectHandle>& pages,  
173 bool allow_changes, 168 bool allow_changes,
174 - bool warn_skipped_keys,  
175 - std::set<QPDFObjGen>& visited) 169 + bool warn_skipped_keys)
176 { 170 {
177 - QPDFObjGen this_og = cur_pages.getObjGen();  
178 - if (visited.count(this_og) > 0) {  
179 - throw QPDFExc(  
180 - qpdf_e_pages,  
181 - this->m->file->getName(),  
182 - this->m->last_object_description,  
183 - 0,  
184 - "Loop detected in /Pages structure (inherited attributes)");  
185 - }  
186 - visited.insert(this_og);  
187 -  
188 - if (!cur_pages.isDictionary()) {  
189 - throw QPDFExc(  
190 - qpdf_e_damaged_pdf,  
191 - this->m->file->getName(),  
192 - this->m->last_object_description,  
193 - this->m->file->getLastOffset(),  
194 - "invalid object in page tree");  
195 - }  
196 -  
197 - // Extract the underlying dictionary object  
198 - std::string type = cur_pages.getKey("/Type").getName();  
199 -  
200 - if (type == "/Pages") {  
201 // Make a list of inheritable keys. Only the keys /MediaBox, 171 // Make a list of inheritable keys. Only the keys /MediaBox,
202 // /CropBox, /Resources, and /Rotate are inheritable 172 // /CropBox, /Resources, and /Rotate are inheritable
203 // attributes. Push this object onto the stack of pages nodes 173 // attributes. Push this object onto the stack of pages nodes
@@ -265,17 +235,25 @@ QPDF::pushInheritedAttributesToPageInternal( @@ -265,17 +235,25 @@ QPDF::pushInheritedAttributesToPageInternal(
265 } 235 }
266 } 236 }
267 237
268 - // Visit descendant nodes.  
269 - QPDFObjectHandle kids = cur_pages.getKey("/Kids");  
270 - int n = kids.getArrayNItems();  
271 - for (int i = 0; i < n; ++i) {  
272 - pushInheritedAttributesToPageInternal(  
273 - kids.getArrayItem(i),  
274 - key_ancestors,  
275 - pages,  
276 - allow_changes,  
277 - warn_skipped_keys,  
278 - visited); 238 + // Process descendant nodes.
  239 + for (auto& kid: cur_pages.getKey("/Kids").aitems()) {
  240 + if (kid.isDictionaryOfType("/Pages")) {
  241 + pushInheritedAttributesToPageInternal(
  242 + kid, key_ancestors, allow_changes, warn_skipped_keys);
  243 + } else {
  244 + // Add all available inheritable attributes not present in
  245 + // this object to this object.
  246 + for (auto const& iter: key_ancestors) {
  247 + std::string const& key = iter.first;
  248 + if (!kid.hasKey(key)) {
  249 + QTC::TC("qpdf", "QPDF opt resource inherited");
  250 + kid.replaceKey(key, iter.second.back());
  251 + } else {
  252 + QTC::TC(
  253 + "qpdf", "QPDF opt page resource hides ancestor");
  254 + }
  255 + }
  256 + }
279 } 257 }
280 258
281 // For each inheritable key, pop the stack. If the stack 259 // For each inheritable key, pop the stack. If the stack
@@ -295,28 +273,6 @@ QPDF::pushInheritedAttributesToPageInternal( @@ -295,28 +273,6 @@ QPDF::pushInheritedAttributesToPageInternal(
295 } else { 273 } else {
296 QTC::TC("qpdf", "QPDF opt no inheritable keys"); 274 QTC::TC("qpdf", "QPDF opt no inheritable keys");
297 } 275 }
298 - } else if (type == "/Page") {  
299 - // Add all available inheritable attributes not present in  
300 - // this object to this object.  
301 - for (auto const& iter: key_ancestors) {  
302 - std::string const& key = iter.first;  
303 - if (!cur_pages.hasKey(key)) {  
304 - QTC::TC("qpdf", "QPDF opt resource inherited");  
305 - cur_pages.replaceKey(key, iter.second.back());  
306 - } else {  
307 - QTC::TC("qpdf", "QPDF opt page resource hides ancestor");  
308 - }  
309 - }  
310 - pages.push_back(cur_pages);  
311 - } else {  
312 - throw QPDFExc(  
313 - qpdf_e_damaged_pdf,  
314 - this->m->file->getName(),  
315 - this->m->last_object_description,  
316 - this->m->file->getLastOffset(),  
317 - "invalid Type " + type + " in page tree");  
318 - }  
319 - visited.erase(this_og);  
320 } 276 }
321 277
322 void 278 void