Commit 903a86643ad6559ded882fe8d6549c0c715f9fc2

Authored by m-holger
1 parent 0356bcec

Fix code formatting of QPDF::pushInheritedAttributesToPageInternal

Showing 1 changed file with 92 additions and 94 deletions
libqpdf/QPDF_optimization.cc
@@ -168,111 +168,109 @@ QPDF::pushInheritedAttributesToPageInternal( @@ -168,111 +168,109 @@ QPDF::pushInheritedAttributesToPageInternal(
168 bool allow_changes, 168 bool allow_changes,
169 bool warn_skipped_keys) 169 bool warn_skipped_keys)
170 { 170 {
171 - // Make a list of inheritable keys. Only the keys /MediaBox,  
172 - // /CropBox, /Resources, and /Rotate are inheritable  
173 - // attributes. Push this object onto the stack of pages nodes  
174 - // that have values for this attribute.  
175 -  
176 - std::set<std::string> inheritable_keys;  
177 - for (auto const& key: cur_pages.getKeys()) {  
178 - if ((key == "/MediaBox") || (key == "/CropBox") ||  
179 - (key == "/Resources") || (key == "/Rotate")) {  
180 - if (!allow_changes) {  
181 - throw QPDFExc(  
182 - qpdf_e_internal,  
183 - this->m->file->getName(),  
184 - this->m->last_object_description,  
185 - this->m->file->getLastOffset(),  
186 - "optimize detected an "  
187 - "inheritable attribute when called "  
188 - "in no-change mode");  
189 - } 171 + // Make a list of inheritable keys. Only the keys /MediaBox,
  172 + // /CropBox, /Resources, and /Rotate are inheritable
  173 + // attributes. Push this object onto the stack of pages nodes
  174 + // that have values for this attribute.
  175 +
  176 + std::set<std::string> inheritable_keys;
  177 + for (auto const& key: cur_pages.getKeys()) {
  178 + if ((key == "/MediaBox") || (key == "/CropBox") ||
  179 + (key == "/Resources") || (key == "/Rotate")) {
  180 + if (!allow_changes) {
  181 + throw QPDFExc(
  182 + qpdf_e_internal,
  183 + this->m->file->getName(),
  184 + this->m->last_object_description,
  185 + this->m->file->getLastOffset(),
  186 + "optimize detected an "
  187 + "inheritable attribute when called "
  188 + "in no-change mode");
  189 + }
190 190
191 - // This is an inheritable resource  
192 - inheritable_keys.insert(key);  
193 - QPDFObjectHandle oh = cur_pages.getKey(key);  
194 - QTC::TC(  
195 - "qpdf",  
196 - "QPDF opt direct pages resource",  
197 - oh.isIndirect() ? 0 : 1);  
198 - if (!oh.isIndirect()) {  
199 - if (!oh.isScalar()) {  
200 - // Replace shared direct object non-scalar  
201 - // resources with indirect objects to avoid  
202 - // copying large structures around.  
203 - cur_pages.replaceKey(key, makeIndirectObject(oh));  
204 - oh = cur_pages.getKey(key);  
205 - } else {  
206 - // It's okay to copy scalars.  
207 - QTC::TC("qpdf", "QPDF opt inherited scalar");  
208 - }  
209 - }  
210 - key_ancestors[key].push_back(oh);  
211 - if (key_ancestors[key].size() > 1) {  
212 - QTC::TC("qpdf", "QPDF opt key ancestors depth > 1");  
213 - }  
214 - // Remove this resource from this node. It will be  
215 - // reattached at the page level.  
216 - cur_pages.removeKey(key);  
217 - } else if (!((key == "/Type") || (key == "/Parent") ||  
218 - (key == "/Kids") || (key == "/Count"))) {  
219 - // Warn when flattening, but not if the key is at the top  
220 - // level (i.e. "/Parent" not set), as we don't change these;  
221 - // but flattening removes intermediate /Pages nodes.  
222 - if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) {  
223 - QTC::TC("qpdf", "QPDF unknown key not inherited");  
224 - setLastObjectDescription(  
225 - "Pages object", cur_pages.getObjGen());  
226 - warn(  
227 - qpdf_e_pages,  
228 - this->m->last_object_description,  
229 - 0,  
230 - ("Unknown key " + key +  
231 - " in /Pages object"  
232 - " is being discarded as a result of"  
233 - " flattening the /Pages tree")); 191 + // This is an inheritable resource
  192 + inheritable_keys.insert(key);
  193 + QPDFObjectHandle oh = cur_pages.getKey(key);
  194 + QTC::TC(
  195 + "qpdf",
  196 + "QPDF opt direct pages resource",
  197 + oh.isIndirect() ? 0 : 1);
  198 + if (!oh.isIndirect()) {
  199 + if (!oh.isScalar()) {
  200 + // Replace shared direct object non-scalar
  201 + // resources with indirect objects to avoid
  202 + // copying large structures around.
  203 + cur_pages.replaceKey(key, makeIndirectObject(oh));
  204 + oh = cur_pages.getKey(key);
  205 + } else {
  206 + // It's okay to copy scalars.
  207 + QTC::TC("qpdf", "QPDF opt inherited scalar");
234 } 208 }
235 } 209 }
  210 + key_ancestors[key].push_back(oh);
  211 + if (key_ancestors[key].size() > 1) {
  212 + QTC::TC("qpdf", "QPDF opt key ancestors depth > 1");
  213 + }
  214 + // Remove this resource from this node. It will be
  215 + // reattached at the page level.
  216 + cur_pages.removeKey(key);
  217 + } else if (!((key == "/Type") || (key == "/Parent") ||
  218 + (key == "/Kids") || (key == "/Count"))) {
  219 + // Warn when flattening, but not if the key is at the top
  220 + // level (i.e. "/Parent" not set), as we don't change these;
  221 + // but flattening removes intermediate /Pages nodes.
  222 + if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) {
  223 + QTC::TC("qpdf", "QPDF unknown key not inherited");
  224 + setLastObjectDescription("Pages object", cur_pages.getObjGen());
  225 + warn(
  226 + qpdf_e_pages,
  227 + this->m->last_object_description,
  228 + 0,
  229 + ("Unknown key " + key +
  230 + " in /Pages object"
  231 + " is being discarded as a result of"
  232 + " flattening the /Pages tree"));
  233 + }
236 } 234 }
  235 + }
237 236
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 - } 237 + // Process descendant nodes.
  238 + for (auto& kid: cur_pages.getKey("/Kids").aitems()) {
  239 + if (kid.isDictionaryOfType("/Pages")) {
  240 + pushInheritedAttributesToPageInternal(
  241 + kid, key_ancestors, allow_changes, warn_skipped_keys);
  242 + } else {
  243 + // Add all available inheritable attributes not present in
  244 + // this object to this object.
  245 + for (auto const& iter: key_ancestors) {
  246 + std::string const& key = iter.first;
  247 + if (!kid.hasKey(key)) {
  248 + QTC::TC("qpdf", "QPDF opt resource inherited");
  249 + kid.replaceKey(key, iter.second.back());
  250 + } else {
  251 + QTC::TC("qpdf", "QPDF opt page resource hides ancestor");
255 } 252 }
256 } 253 }
257 } 254 }
  255 + }
258 256
259 - // For each inheritable key, pop the stack. If the stack  
260 - // becomes empty, remove it from the map. That way, the  
261 - // invariant that the list of keys in key_ancestors is exactly  
262 - // those keys for which inheritable attributes are available.  
263 -  
264 - if (!inheritable_keys.empty()) {  
265 - QTC::TC("qpdf", "QPDF opt inheritable keys");  
266 - for (auto const& key: inheritable_keys) {  
267 - key_ancestors[key].pop_back();  
268 - if (key_ancestors[key].empty()) {  
269 - QTC::TC("qpdf", "QPDF opt erase empty key ancestor");  
270 - key_ancestors.erase(key);  
271 - } 257 + // For each inheritable key, pop the stack. If the stack
  258 + // becomes empty, remove it from the map. That way, the
  259 + // invariant that the list of keys in key_ancestors is exactly
  260 + // those keys for which inheritable attributes are available.
  261 +
  262 + if (!inheritable_keys.empty()) {
  263 + QTC::TC("qpdf", "QPDF opt inheritable keys");
  264 + for (auto const& key: inheritable_keys) {
  265 + key_ancestors[key].pop_back();
  266 + if (key_ancestors[key].empty()) {
  267 + QTC::TC("qpdf", "QPDF opt erase empty key ancestor");
  268 + key_ancestors.erase(key);
272 } 269 }
273 - } else {  
274 - QTC::TC("qpdf", "QPDF opt no inheritable keys");  
275 } 270 }
  271 + } else {
  272 + QTC::TC("qpdf", "QPDF opt no inheritable keys");
  273 + }
276 } 274 }
277 275
278 void 276 void