mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-03 15:17:29 +00:00
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.
This commit is contained in:
parent
ff69773b35
commit
0356bcecc5
@ -1616,10 +1616,8 @@ class QPDF
|
|||||||
void pushInheritedAttributesToPageInternal(
|
void pushInheritedAttributesToPageInternal(
|
||||||
QPDFObjectHandle,
|
QPDFObjectHandle,
|
||||||
std::map<std::string, std::vector<QPDFObjectHandle>>&,
|
std::map<std::string, std::vector<QPDFObjectHandle>>&,
|
||||||
std::vector<QPDFObjectHandle>& all_pages,
|
|
||||||
bool allow_changes,
|
bool allow_changes,
|
||||||
bool warn_skipped_keys,
|
bool warn_skipped_keys);
|
||||||
std::set<QPDFObjGen>& visited);
|
|
||||||
void updateObjectMaps(
|
void updateObjectMaps(
|
||||||
ObjUser const& ou,
|
ObjUser const& ou,
|
||||||
QPDFObjectHandle oh,
|
QPDFObjectHandle oh,
|
||||||
|
@ -148,15 +148,11 @@ QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys)
|
|||||||
// key_ancestors is a mapping of page attribute keys to a stack of
|
// key_ancestors is a mapping of page attribute keys to a stack of
|
||||||
// Pages nodes that contain values for them.
|
// Pages nodes that contain values for them.
|
||||||
std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors;
|
std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors;
|
||||||
this->m->all_pages.clear();
|
|
||||||
std::set<QPDFObjGen> visited;
|
|
||||||
pushInheritedAttributesToPageInternal(
|
pushInheritedAttributesToPageInternal(
|
||||||
this->m->trailer.getKey("/Root").getKey("/Pages"),
|
this->m->trailer.getKey("/Root").getKey("/Pages"),
|
||||||
key_ancestors,
|
key_ancestors,
|
||||||
this->m->all_pages,
|
|
||||||
allow_changes,
|
allow_changes,
|
||||||
warn_skipped_keys,
|
warn_skipped_keys);
|
||||||
visited);
|
|
||||||
if (!key_ancestors.empty()) {
|
if (!key_ancestors.empty()) {
|
||||||
throw std::logic_error("key_ancestors not empty after"
|
throw std::logic_error("key_ancestors not empty after"
|
||||||
" pushing inherited attributes to pages");
|
" pushing inherited attributes to pages");
|
||||||
@ -169,35 +165,9 @@ void
|
|||||||
QPDF::pushInheritedAttributesToPageInternal(
|
QPDF::pushInheritedAttributesToPageInternal(
|
||||||
QPDFObjectHandle cur_pages,
|
QPDFObjectHandle cur_pages,
|
||||||
std::map<std::string, std::vector<QPDFObjectHandle>>& key_ancestors,
|
std::map<std::string, std::vector<QPDFObjectHandle>>& key_ancestors,
|
||||||
std::vector<QPDFObjectHandle>& pages,
|
|
||||||
bool allow_changes,
|
bool allow_changes,
|
||||||
bool warn_skipped_keys,
|
bool warn_skipped_keys)
|
||||||
std::set<QPDFObjGen>& visited)
|
|
||||||
{
|
{
|
||||||
QPDFObjGen this_og = cur_pages.getObjGen();
|
|
||||||
if (visited.count(this_og) > 0) {
|
|
||||||
throw QPDFExc(
|
|
||||||
qpdf_e_pages,
|
|
||||||
this->m->file->getName(),
|
|
||||||
this->m->last_object_description,
|
|
||||||
0,
|
|
||||||
"Loop detected in /Pages structure (inherited attributes)");
|
|
||||||
}
|
|
||||||
visited.insert(this_og);
|
|
||||||
|
|
||||||
if (!cur_pages.isDictionary()) {
|
|
||||||
throw QPDFExc(
|
|
||||||
qpdf_e_damaged_pdf,
|
|
||||||
this->m->file->getName(),
|
|
||||||
this->m->last_object_description,
|
|
||||||
this->m->file->getLastOffset(),
|
|
||||||
"invalid object in page tree");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the underlying dictionary object
|
|
||||||
std::string type = cur_pages.getKey("/Type").getName();
|
|
||||||
|
|
||||||
if (type == "/Pages") {
|
|
||||||
// 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
|
||||||
@ -265,17 +235,25 @@ QPDF::pushInheritedAttributesToPageInternal(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visit descendant nodes.
|
// Process descendant nodes.
|
||||||
QPDFObjectHandle kids = cur_pages.getKey("/Kids");
|
for (auto& kid: cur_pages.getKey("/Kids").aitems()) {
|
||||||
int n = kids.getArrayNItems();
|
if (kid.isDictionaryOfType("/Pages")) {
|
||||||
for (int i = 0; i < n; ++i) {
|
|
||||||
pushInheritedAttributesToPageInternal(
|
pushInheritedAttributesToPageInternal(
|
||||||
kids.getArrayItem(i),
|
kid, key_ancestors, allow_changes, warn_skipped_keys);
|
||||||
key_ancestors,
|
} else {
|
||||||
pages,
|
// Add all available inheritable attributes not present in
|
||||||
allow_changes,
|
// this object to this object.
|
||||||
warn_skipped_keys,
|
for (auto const& iter: key_ancestors) {
|
||||||
visited);
|
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
|
// For each inheritable key, pop the stack. If the stack
|
||||||
@ -295,28 +273,6 @@ QPDF::pushInheritedAttributesToPageInternal(
|
|||||||
} else {
|
} else {
|
||||||
QTC::TC("qpdf", "QPDF opt no inheritable keys");
|
QTC::TC("qpdf", "QPDF opt no inheritable keys");
|
||||||
}
|
}
|
||||||
} else if (type == "/Page") {
|
|
||||||
// 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 (!cur_pages.hasKey(key)) {
|
|
||||||
QTC::TC("qpdf", "QPDF opt resource inherited");
|
|
||||||
cur_pages.replaceKey(key, iter.second.back());
|
|
||||||
} else {
|
|
||||||
QTC::TC("qpdf", "QPDF opt page resource hides ancestor");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pages.push_back(cur_pages);
|
|
||||||
} else {
|
|
||||||
throw QPDFExc(
|
|
||||||
qpdf_e_damaged_pdf,
|
|
||||||
this->m->file->getName(),
|
|
||||||
this->m->last_object_description,
|
|
||||||
this->m->file->getLastOffset(),
|
|
||||||
"invalid Type " + type + " in page tree");
|
|
||||||
}
|
|
||||||
visited.erase(this_og);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
Reference in New Issue
Block a user