2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-22 03:54:41 +00:00

Tidy QPDFAcroFormDocumentHelper::addAndRenameFormFields

This commit is contained in:
m-holger 2023-01-05 12:56:34 +00:00
parent 55abecc42d
commit e7e24fe070

View File

@ -68,53 +68,48 @@ QPDFAcroFormDocumentHelper::addAndRenameFormFields(
{ {
analyze(); analyze();
std::map<std::string, std::string> renames; std::map<std::string, std::string> renames;
std::list<QPDFObjectHandle> queue; QPDFObjGen::set seen;
queue.insert(queue.begin(), fields.begin(), fields.end()); for (std::list<QPDFObjectHandle> queue{fields.begin(), fields.end()};
std::set<QPDFObjGen> seen; !queue.empty();
while (!queue.empty()) { queue.pop_front()) {
QPDFObjectHandle obj = queue.front(); auto& obj = queue.front();
queue.pop_front(); if (seen.add(obj)) {
auto og = obj.getObjGen(); auto kids = obj.getKey("/Kids");
if (seen.count(og)) { if (kids.isArray()) {
// loop for (auto kid: kids.aitems()) {
continue; queue.push_back(kid);
}
seen.insert(og);
auto kids = obj.getKey("/Kids");
if (kids.isArray()) {
for (auto kid: kids.aitems()) {
queue.push_back(kid);
}
}
if (obj.hasKey("/T")) {
// Find something we can append to the partial name that
// makes the fully qualified name unique. When we find
// something, reuse the same suffix for all fields in this
// group with the same name. We can only change the name
// of fields that have /T, and this field's /T is always
// at the end of the fully qualified name, appending to /T
// has the effect of appending the same thing to the fully
// qualified name.
std::string old_name =
QPDFFormFieldObjectHelper(obj).getFullyQualifiedName();
if (renames.count(old_name) == 0) {
std::string new_name = old_name;
int suffix = 0;
std::string append;
while (!getFieldsWithQualifiedName(new_name).empty()) {
++suffix;
append = "+" + std::to_string(suffix);
new_name = old_name + append;
} }
renames[old_name] = append;
} }
std::string append = renames[old_name];
if (!append.empty()) { if (obj.hasKey("/T")) {
obj.replaceKey( // Find something we can append to the partial name that
"/T", // makes the fully qualified name unique. When we find
QPDFObjectHandle::newUnicodeString( // something, reuse the same suffix for all fields in this
obj.getKey("/T").getUTF8Value() + append)); // group with the same name. We can only change the name
// of fields that have /T, and this field's /T is always
// at the end of the fully qualified name, appending to /T
// has the effect of appending the same thing to the fully
// qualified name.
std::string old_name =
QPDFFormFieldObjectHelper(obj).getFullyQualifiedName();
if (renames.count(old_name) == 0) {
std::string new_name = old_name;
int suffix = 0;
std::string append;
while (!getFieldsWithQualifiedName(new_name).empty()) {
++suffix;
append = "+" + std::to_string(suffix);
new_name = old_name + append;
}
renames[old_name] = append;
}
std::string append = renames[old_name];
if (!append.empty()) {
obj.replaceKey(
"/T",
QPDFObjectHandle::newUnicodeString(
obj.getKey("/T").getUTF8Value() + append));
}
} }
} }
} }