Commit e7e24fe0706f40b76058bb4b2b46c0307cb6255d
1 parent
55abecc4
Tidy QPDFAcroFormDocumentHelper::addAndRenameFormFields
Showing
1 changed file
with
39 additions
and
44 deletions
libqpdf/QPDFAcroFormDocumentHelper.cc
| @@ -68,53 +68,48 @@ QPDFAcroFormDocumentHelper::addAndRenameFormFields( | @@ -68,53 +68,48 @@ QPDFAcroFormDocumentHelper::addAndRenameFormFields( | ||
| 68 | { | 68 | { |
| 69 | analyze(); | 69 | analyze(); |
| 70 | std::map<std::string, std::string> renames; | 70 | std::map<std::string, std::string> renames; |
| 71 | - std::list<QPDFObjectHandle> queue; | ||
| 72 | - queue.insert(queue.begin(), fields.begin(), fields.end()); | ||
| 73 | - std::set<QPDFObjGen> seen; | ||
| 74 | - while (!queue.empty()) { | ||
| 75 | - QPDFObjectHandle obj = queue.front(); | ||
| 76 | - queue.pop_front(); | ||
| 77 | - auto og = obj.getObjGen(); | ||
| 78 | - if (seen.count(og)) { | ||
| 79 | - // loop | ||
| 80 | - continue; | ||
| 81 | - } | ||
| 82 | - seen.insert(og); | ||
| 83 | - auto kids = obj.getKey("/Kids"); | ||
| 84 | - if (kids.isArray()) { | ||
| 85 | - for (auto kid: kids.aitems()) { | ||
| 86 | - queue.push_back(kid); | 71 | + QPDFObjGen::set seen; |
| 72 | + for (std::list<QPDFObjectHandle> queue{fields.begin(), fields.end()}; | ||
| 73 | + !queue.empty(); | ||
| 74 | + queue.pop_front()) { | ||
| 75 | + auto& obj = queue.front(); | ||
| 76 | + if (seen.add(obj)) { | ||
| 77 | + auto kids = obj.getKey("/Kids"); | ||
| 78 | + if (kids.isArray()) { | ||
| 79 | + for (auto kid: kids.aitems()) { | ||
| 80 | + queue.push_back(kid); | ||
| 81 | + } | ||
| 87 | } | 82 | } |
| 88 | - } | ||
| 89 | 83 | ||
| 90 | - if (obj.hasKey("/T")) { | ||
| 91 | - // Find something we can append to the partial name that | ||
| 92 | - // makes the fully qualified name unique. When we find | ||
| 93 | - // something, reuse the same suffix for all fields in this | ||
| 94 | - // group with the same name. We can only change the name | ||
| 95 | - // of fields that have /T, and this field's /T is always | ||
| 96 | - // at the end of the fully qualified name, appending to /T | ||
| 97 | - // has the effect of appending the same thing to the fully | ||
| 98 | - // qualified name. | ||
| 99 | - std::string old_name = | ||
| 100 | - QPDFFormFieldObjectHelper(obj).getFullyQualifiedName(); | ||
| 101 | - if (renames.count(old_name) == 0) { | ||
| 102 | - std::string new_name = old_name; | ||
| 103 | - int suffix = 0; | ||
| 104 | - std::string append; | ||
| 105 | - while (!getFieldsWithQualifiedName(new_name).empty()) { | ||
| 106 | - ++suffix; | ||
| 107 | - append = "+" + std::to_string(suffix); | ||
| 108 | - new_name = old_name + append; | 84 | + if (obj.hasKey("/T")) { |
| 85 | + // Find something we can append to the partial name that | ||
| 86 | + // makes the fully qualified name unique. When we find | ||
| 87 | + // something, reuse the same suffix for all fields in this | ||
| 88 | + // group with the same name. We can only change the name | ||
| 89 | + // of fields that have /T, and this field's /T is always | ||
| 90 | + // at the end of the fully qualified name, appending to /T | ||
| 91 | + // has the effect of appending the same thing to the fully | ||
| 92 | + // qualified name. | ||
| 93 | + std::string old_name = | ||
| 94 | + QPDFFormFieldObjectHelper(obj).getFullyQualifiedName(); | ||
| 95 | + if (renames.count(old_name) == 0) { | ||
| 96 | + std::string new_name = old_name; | ||
| 97 | + int suffix = 0; | ||
| 98 | + std::string append; | ||
| 99 | + while (!getFieldsWithQualifiedName(new_name).empty()) { | ||
| 100 | + ++suffix; | ||
| 101 | + append = "+" + std::to_string(suffix); | ||
| 102 | + new_name = old_name + append; | ||
| 103 | + } | ||
| 104 | + renames[old_name] = append; | ||
| 105 | + } | ||
| 106 | + std::string append = renames[old_name]; | ||
| 107 | + if (!append.empty()) { | ||
| 108 | + obj.replaceKey( | ||
| 109 | + "/T", | ||
| 110 | + QPDFObjectHandle::newUnicodeString( | ||
| 111 | + obj.getKey("/T").getUTF8Value() + append)); | ||
| 109 | } | 112 | } |
| 110 | - renames[old_name] = append; | ||
| 111 | - } | ||
| 112 | - std::string append = renames[old_name]; | ||
| 113 | - if (!append.empty()) { | ||
| 114 | - obj.replaceKey( | ||
| 115 | - "/T", | ||
| 116 | - QPDFObjectHandle::newUnicodeString( | ||
| 117 | - obj.getKey("/T").getUTF8Value() + append)); | ||
| 118 | } | 113 | } |
| 119 | } | 114 | } |
| 120 | } | 115 | } |