mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-08 17:24:06 +00:00
Fix code formatting of QPDF::pushInheritedAttributesToPageInternal
This commit is contained in:
parent
0356bcecc5
commit
903a86643a
@ -168,111 +168,109 @@ QPDF::pushInheritedAttributesToPageInternal(
|
|||||||
bool allow_changes,
|
bool allow_changes,
|
||||||
bool warn_skipped_keys)
|
bool warn_skipped_keys)
|
||||||
{
|
{
|
||||||
// Make a list of inheritable keys. Only the keys /MediaBox,
|
// Make a list of inheritable keys. Only the keys /MediaBox,
|
||||||
// /CropBox, /Resources, and /Rotate are inheritable
|
// /CropBox, /Resources, and /Rotate are inheritable
|
||||||
// attributes. Push this object onto the stack of pages nodes
|
// attributes. Push this object onto the stack of pages nodes
|
||||||
// that have values for this attribute.
|
// that have values for this attribute.
|
||||||
|
|
||||||
std::set<std::string> inheritable_keys;
|
std::set<std::string> inheritable_keys;
|
||||||
for (auto const& key: cur_pages.getKeys()) {
|
for (auto const& key: cur_pages.getKeys()) {
|
||||||
if ((key == "/MediaBox") || (key == "/CropBox") ||
|
if ((key == "/MediaBox") || (key == "/CropBox") ||
|
||||||
(key == "/Resources") || (key == "/Rotate")) {
|
(key == "/Resources") || (key == "/Rotate")) {
|
||||||
if (!allow_changes) {
|
if (!allow_changes) {
|
||||||
throw QPDFExc(
|
throw QPDFExc(
|
||||||
qpdf_e_internal,
|
qpdf_e_internal,
|
||||||
this->m->file->getName(),
|
this->m->file->getName(),
|
||||||
this->m->last_object_description,
|
this->m->last_object_description,
|
||||||
this->m->file->getLastOffset(),
|
this->m->file->getLastOffset(),
|
||||||
"optimize detected an "
|
"optimize detected an "
|
||||||
"inheritable attribute when called "
|
"inheritable attribute when called "
|
||||||
"in no-change mode");
|
"in no-change mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is an inheritable resource
|
// This is an inheritable resource
|
||||||
inheritable_keys.insert(key);
|
inheritable_keys.insert(key);
|
||||||
QPDFObjectHandle oh = cur_pages.getKey(key);
|
QPDFObjectHandle oh = cur_pages.getKey(key);
|
||||||
QTC::TC(
|
QTC::TC(
|
||||||
"qpdf",
|
"qpdf",
|
||||||
"QPDF opt direct pages resource",
|
"QPDF opt direct pages resource",
|
||||||
oh.isIndirect() ? 0 : 1);
|
oh.isIndirect() ? 0 : 1);
|
||||||
if (!oh.isIndirect()) {
|
if (!oh.isIndirect()) {
|
||||||
if (!oh.isScalar()) {
|
if (!oh.isScalar()) {
|
||||||
// Replace shared direct object non-scalar
|
// Replace shared direct object non-scalar
|
||||||
// resources with indirect objects to avoid
|
// resources with indirect objects to avoid
|
||||||
// copying large structures around.
|
// copying large structures around.
|
||||||
cur_pages.replaceKey(key, makeIndirectObject(oh));
|
cur_pages.replaceKey(key, makeIndirectObject(oh));
|
||||||
oh = cur_pages.getKey(key);
|
oh = cur_pages.getKey(key);
|
||||||
} else {
|
} else {
|
||||||
// It's okay to copy scalars.
|
// It's okay to copy scalars.
|
||||||
QTC::TC("qpdf", "QPDF opt inherited scalar");
|
QTC::TC("qpdf", "QPDF opt inherited scalar");
|
||||||
}
|
|
||||||
}
|
|
||||||
key_ancestors[key].push_back(oh);
|
|
||||||
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.
|
|
||||||
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.
|
|
||||||
if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) {
|
|
||||||
QTC::TC("qpdf", "QPDF unknown key not inherited");
|
|
||||||
setLastObjectDescription(
|
|
||||||
"Pages object", cur_pages.getObjGen());
|
|
||||||
warn(
|
|
||||||
qpdf_e_pages,
|
|
||||||
this->m->last_object_description,
|
|
||||||
0,
|
|
||||||
("Unknown key " + key +
|
|
||||||
" in /Pages object"
|
|
||||||
" is being discarded as a result of"
|
|
||||||
" flattening the /Pages tree"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
key_ancestors[key].push_back(oh);
|
||||||
|
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.
|
||||||
|
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.
|
||||||
|
if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) {
|
||||||
|
QTC::TC("qpdf", "QPDF unknown key not inherited");
|
||||||
|
setLastObjectDescription("Pages object", cur_pages.getObjGen());
|
||||||
|
warn(
|
||||||
|
qpdf_e_pages,
|
||||||
|
this->m->last_object_description,
|
||||||
|
0,
|
||||||
|
("Unknown key " + key +
|
||||||
|
" in /Pages object"
|
||||||
|
" is being discarded as a result of"
|
||||||
|
" flattening the /Pages tree"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Process descendant nodes.
|
// Process descendant nodes.
|
||||||
for (auto& kid: cur_pages.getKey("/Kids").aitems()) {
|
for (auto& kid: cur_pages.getKey("/Kids").aitems()) {
|
||||||
if (kid.isDictionaryOfType("/Pages")) {
|
if (kid.isDictionaryOfType("/Pages")) {
|
||||||
pushInheritedAttributesToPageInternal(
|
pushInheritedAttributesToPageInternal(
|
||||||
kid, key_ancestors, allow_changes, warn_skipped_keys);
|
kid, key_ancestors, allow_changes, warn_skipped_keys);
|
||||||
} else {
|
|
||||||
// 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)) {
|
|
||||||
QTC::TC("qpdf", "QPDF opt resource inherited");
|
|
||||||
kid.replaceKey(key, iter.second.back());
|
|
||||||
} else {
|
|
||||||
QTC::TC(
|
|
||||||
"qpdf", "QPDF opt page resource hides ancestor");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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");
|
|
||||||
for (auto const& key: inheritable_keys) {
|
|
||||||
key_ancestors[key].pop_back();
|
|
||||||
if (key_ancestors[key].empty()) {
|
|
||||||
QTC::TC("qpdf", "QPDF opt erase empty key ancestor");
|
|
||||||
key_ancestors.erase(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
QTC::TC("qpdf", "QPDF opt no inheritable keys");
|
// 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)) {
|
||||||
|
QTC::TC("qpdf", "QPDF opt resource inherited");
|
||||||
|
kid.replaceKey(key, iter.second.back());
|
||||||
|
} else {
|
||||||
|
QTC::TC("qpdf", "QPDF opt page resource hides ancestor");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
for (auto const& key: inheritable_keys) {
|
||||||
|
key_ancestors[key].pop_back();
|
||||||
|
if (key_ancestors[key].empty()) {
|
||||||
|
QTC::TC("qpdf", "QPDF opt erase empty key ancestor");
|
||||||
|
key_ancestors.erase(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
QTC::TC("qpdf", "QPDF opt no inheritable keys");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
Reference in New Issue
Block a user