diff options
author | Jay Berkenbilt <jberkenbilt@users.noreply.github.com> | 2023-05-27 16:43:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-27 16:43:21 +0200 |
commit | 73ee000c33b1c51688f7a9b2a8ce9816da93e7ac (patch) | |
tree | 7e57db2b9113c29b73072d218a526f82a29bc258 /libqpdf/QPDF_optimization.cc | |
parent | 9907700faeca965c1e544af9247102d163a8b7df (diff) | |
parent | db6c09b625690bc83c0ce6cada6f3206096eeceb (diff) | |
download | qpdf-73ee000c33b1c51688f7a9b2a8ce9816da93e7ac.tar.zst |
Merge pull request #975 from m-holger/reflow
Code tidy - reflow comments and strings
Diffstat (limited to 'libqpdf/QPDF_optimization.cc')
-rw-r--r-- | libqpdf/QPDF_optimization.cc | 94 |
1 files changed, 37 insertions, 57 deletions
diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index ab06f158..91da7564 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -64,9 +64,8 @@ QPDF::optimize( return; } - // The PDF specification indicates that /Outlines is supposed to - // be an indirect reference. Force it to be so if it exists and - // is direct. (This has been seen in the wild.) + // The PDF specification indicates that /Outlines is supposed to be an indirect reference. Force + // it to be so if it exists and is direct. (This has been seen in the wild.) QPDFObjectHandle root = getRoot(); if (root.getKey("/Outlines").isDictionary()) { QPDFObjectHandle outlines = root.getKey("/Outlines"); @@ -76,8 +75,8 @@ QPDF::optimize( } } - // Traverse pages tree pushing all inherited resources down to the - // page level. This also initializes m->all_pages. + // Traverse pages tree pushing all inherited resources down to the page level. This also + // initializes m->all_pages. pushInheritedAttributesToPage(allow_changes, false); // Traverse pages @@ -102,12 +101,10 @@ QPDF::optimize( } for (auto const& key: root.getKeys()) { - // Technically, /I keys from /Thread dictionaries are supposed - // to be handled separately, but we are going to disregard - // that specification for now. There is loads of evidence - // that pdlin and Acrobat both disregard things like this from - // time to time, so this is almost certain not to cause any - // problems. + // Technically, /I keys from /Thread dictionaries are supposed to be handled separately, but + // we are going to disregard that specification for now. There is loads of evidence that + // pdlin and Acrobat both disregard things like this from time to time, so this is almost + // certain not to cause any problems. updateObjectMaps( ObjUser(ObjUser::ou_root_key, key), root.getKey(key), skip_stream_parameters); } @@ -130,23 +127,20 @@ QPDF::pushInheritedAttributesToPage() void QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) { - // Traverse pages tree pushing all inherited resources down to the - // page level. + // Traverse pages tree pushing all inherited resources down to the page level. - // The record of whether we've done this is cleared by - // updateAllPagesCache(). If we're warning for skipped keys, - // re-traverse unconditionally. + // The record of whether we've done this is cleared by updateAllPagesCache(). If we're warning + // for skipped keys, re-traverse unconditionally. if (m->pushed_inherited_attributes_to_pages && (!warn_skipped_keys)) { return; } - // Calling getAllPages() resolves any duplicated page objects, - // repairs broken nodes, and detects loops, so we don't have to do - // those activities here. + // Calling getAllPages() resolves any duplicated page objects, repairs broken nodes, and detects + // loops, so we don't have to do those activities here. getAllPages(); - // key_ancestors is a mapping of page attribute keys to a stack of - // Pages nodes that contain values for them. + // key_ancestors is a mapping of page attribute keys to a stack of Pages nodes that contain + // values for them. std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors; pushInheritedAttributesToPageInternal( m->trailer.getKey("/Root").getKey("/Pages"), @@ -168,10 +162,9 @@ QPDF::pushInheritedAttributesToPageInternal( bool allow_changes, bool warn_skipped_keys) { - // Make a list of inheritable keys. Only the keys /MediaBox, - // /CropBox, /Resources, and /Rotate are inheritable - // attributes. Push this object onto the stack of pages nodes - // that have values for this attribute. + // Make a list of inheritable keys. Only the keys /MediaBox, /CropBox, /Resources, and /Rotate + // are inheritable attributes. Push this object onto the stack of pages nodes that have values + // for this attribute. std::set<std::string> inheritable_keys; for (auto const& key: cur_pages.getKeys()) { @@ -183,9 +176,7 @@ QPDF::pushInheritedAttributesToPageInternal( m->file->getName(), m->last_object_description, m->file->getLastOffset(), - "optimize detected an " - "inheritable attribute when called " - "in no-change mode"); + "optimize detected an inheritable attribute when called in no-change mode"); } // This is an inheritable resource @@ -194,9 +185,8 @@ QPDF::pushInheritedAttributesToPageInternal( QTC::TC("qpdf", "QPDF opt direct pages resource", oh.isIndirect() ? 0 : 1); if (!oh.isIndirect()) { if (!oh.isScalar()) { - // Replace shared direct object non-scalar - // resources with indirect objects to avoid - // copying large structures around. + // Replace shared direct object non-scalar resources with indirect objects to + // avoid copying large structures around. cur_pages.replaceKey(key, makeIndirectObject(oh)); oh = cur_pages.getKey(key); } else { @@ -208,14 +198,12 @@ QPDF::pushInheritedAttributesToPageInternal( if (key_ancestors[key].size() > 1) { QTC::TC("qpdf", "QPDF opt key ancestors depth > 1"); } - // Remove this resource from this node. It will be - // reattached at the page level. + // Remove this resource from this node. It will be reattached at the page level. cur_pages.removeKey(key); } else if (!((key == "/Type") || (key == "/Parent") || (key == "/Kids") || (key == "/Count"))) { - // Warn when flattening, but not if the key is at the top - // level (i.e. "/Parent" not set), as we don't change these; - // but flattening removes intermediate /Pages nodes. + // Warn when flattening, but not if the key is at the top level (i.e. "/Parent" not + // set), as we don't change these; but flattening removes intermediate /Pages nodes. if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) { QTC::TC("qpdf", "QPDF unknown key not inherited"); setLastObjectDescription("Pages object", cur_pages.getObjGen()); @@ -224,24 +212,21 @@ QPDF::pushInheritedAttributesToPageInternal( m->last_object_description, 0, ("Unknown key " + key + - " in /Pages object" - " is being discarded as a result of" - " flattening the /Pages tree")); + " in /Pages object is being discarded as a result of flattening the /Pages " + "tree")); } } } - // Process descendant nodes. This method does not perform loop - // detection because all code paths that lead here follow a call - // to getAllPages, which already throws an exception in the event + // Process descendant nodes. This method does not perform loop detection because all code paths + // that lead here follow a call to getAllPages, which already throws an exception in the event // of a loop in the pages tree. for (auto& kid: cur_pages.getKey("/Kids").aitems()) { if (kid.isDictionaryOfType("/Pages")) { pushInheritedAttributesToPageInternal( kid, key_ancestors, allow_changes, warn_skipped_keys); } else { - // Add all available inheritable attributes not present in - // this object to this object. + // Add all available inheritable attributes not present in this object to this object. for (auto const& iter: key_ancestors) { std::string const& key = iter.first; if (!kid.hasKey(key)) { @@ -254,10 +239,9 @@ QPDF::pushInheritedAttributesToPageInternal( } } - // For each inheritable key, pop the stack. If the stack - // becomes empty, remove it from the map. That way, the - // invariant that the list of keys in key_ancestors is exactly - // those keys for which inheritable attributes are available. + // For each inheritable key, pop the stack. If the stack becomes empty, remove it from the map. + // That way, the invariant that the list of keys in key_ancestors is exactly those keys for + // which inheritable attributes are available. if (!inheritable_keys.empty()) { QTC::TC("qpdf", "QPDF opt inheritable keys"); @@ -291,8 +275,7 @@ QPDF::updateObjectMapsInternal( QPDFObjGen::set& visited, bool top) { - // Traverse the object tree from this point taking care to avoid - // crossing page boundaries. + // Traverse the object tree from this point taking care to avoid crossing page boundaries. bool is_page_node = false; @@ -332,8 +315,7 @@ QPDF::updateObjectMapsInternal( for (auto const& key: dict.getKeys()) { if (is_page_node && (key == "/Thumb")) { - // Traverse page thumbnail dictionaries as a special - // case. + // Traverse page thumbnail dictionaries as a special case. updateObjectMapsInternal( ObjUser(ObjUser::ou_thumb, ou.pageno), dict.getKey(key), @@ -345,8 +327,7 @@ QPDF::updateObjectMapsInternal( } else if ( ((ssp >= 1) && (key == "/Length")) || ((ssp >= 2) && ((key == "/Filter") || (key == "/DecodeParms")))) { - // Don't traverse into stream parameters that we are - // not going to write. + // Don't traverse into stream parameters that we are not going to write. } else { updateObjectMapsInternal( ou, dict.getKey(key), skip_stream_parameters, visited, false); @@ -362,9 +343,8 @@ QPDF::filterCompressedObjects(std::map<int, int> const& object_stream_data) return; } - // Transform object_to_obj_users and obj_user_to_objects so that - // they refer only to uncompressed objects. If something is a - // user of a compressed object, then it is really a user of the + // Transform object_to_obj_users and obj_user_to_objects so that they refer only to uncompressed + // objects. If something is a user of a compressed object, then it is really a user of the // object stream that contains it. std::map<ObjUser, std::set<QPDFObjGen>> t_obj_user_to_objects; |