Commit faed776c963288945c33fd770ede40ab5240ef68
1 parent
e64965e3
Refactor `QPDFJob`: centralize `QPDFAcroFormDocumentHelper` access via `acroform…
…()` method, remove redundant helper functions, and streamline related logic for consistency and maintainability.
Showing
2 changed files
with
19 additions
and
44 deletions
libqpdf/QPDFJob.cc
| @@ -1888,17 +1888,6 @@ QPDFJob::validateUnderOverlay(QPDF& pdf, UnderOverlay* uo) | @@ -1888,17 +1888,6 @@ QPDFJob::validateUnderOverlay(QPDF& pdf, UnderOverlay* uo) | ||
| 1888 | } | 1888 | } |
| 1889 | } | 1889 | } |
| 1890 | 1890 | ||
| 1891 | -static QPDFAcroFormDocumentHelper* | ||
| 1892 | -get_afdh_for_qpdf( | ||
| 1893 | - std::map<unsigned long long, std::shared_ptr<QPDFAcroFormDocumentHelper>>& afdh_map, QPDF* q) | ||
| 1894 | -{ | ||
| 1895 | - auto uid = q->getUniqueId(); | ||
| 1896 | - if (!afdh_map.contains(uid)) { | ||
| 1897 | - afdh_map[uid] = std::make_shared<QPDFAcroFormDocumentHelper>(*q); | ||
| 1898 | - } | ||
| 1899 | - return afdh_map[uid].get(); | ||
| 1900 | -} | ||
| 1901 | - | ||
| 1902 | std::string | 1891 | std::string |
| 1903 | QPDFJob::doUnderOverlayForPage( | 1892 | QPDFJob::doUnderOverlayForPage( |
| 1904 | QPDF& pdf, | 1893 | QPDF& pdf, |
| @@ -1914,13 +1903,7 @@ QPDFJob::doUnderOverlayForPage( | @@ -1914,13 +1903,7 @@ QPDFJob::doUnderOverlayForPage( | ||
| 1914 | if (!(pagenos.contains(pageno) && pagenos[pageno].contains(uo_idx))) { | 1903 | if (!(pagenos.contains(pageno) && pagenos[pageno].contains(uo_idx))) { |
| 1915 | return ""; | 1904 | return ""; |
| 1916 | } | 1905 | } |
| 1917 | - | ||
| 1918 | - std::map<unsigned long long, std::shared_ptr<QPDFAcroFormDocumentHelper>> afdh; | ||
| 1919 | - auto make_afdh = [&](QPDFPageObjectHelper& ph) { | ||
| 1920 | - QPDF& q = ph.getObjectHandle().getQPDF(); | ||
| 1921 | - return get_afdh_for_qpdf(afdh, &q); | ||
| 1922 | - }; | ||
| 1923 | - auto dest_afdh = make_afdh(dest_page); | 1906 | + auto& dest_afdh = dest_page.qpdf()->acroform(); |
| 1924 | 1907 | ||
| 1925 | std::string content; | 1908 | std::string content; |
| 1926 | int min_suffix = 1; | 1909 | int min_suffix = 1; |
| @@ -1940,7 +1923,7 @@ QPDFJob::doUnderOverlayForPage( | @@ -1940,7 +1923,7 @@ QPDFJob::doUnderOverlayForPage( | ||
| 1940 | QPDFMatrix cm; | 1923 | QPDFMatrix cm; |
| 1941 | std::string new_content = dest_page.placeFormXObject( | 1924 | std::string new_content = dest_page.placeFormXObject( |
| 1942 | fo[from_pageno][uo_idx], name, dest_page.getTrimBox().getArrayAsRectangle(), cm); | 1925 | fo[from_pageno][uo_idx], name, dest_page.getTrimBox().getArrayAsRectangle(), cm); |
| 1943 | - dest_page.copyAnnotations(from_page, cm, dest_afdh, make_afdh(from_page)); | 1926 | + dest_page.copyAnnotations(from_page, cm, &dest_afdh, &from_page.qpdf()->acroform()); |
| 1944 | if (!new_content.empty()) { | 1927 | if (!new_content.empty()) { |
| 1945 | resources.mergeResources("<< /XObject << >> >>"_qpdf); | 1928 | resources.mergeResources("<< /XObject << >> >>"_qpdf); |
| 1946 | auto xobject = resources.getKey("/XObject"); | 1929 | auto xobject = resources.getKey("/XObject"); |
| @@ -2182,15 +2165,15 @@ void | @@ -2182,15 +2165,15 @@ void | ||
| 2182 | QPDFJob::handleTransformations(QPDF& pdf) | 2165 | QPDFJob::handleTransformations(QPDF& pdf) |
| 2183 | { | 2166 | { |
| 2184 | QPDFPageDocumentHelper dh(pdf); | 2167 | QPDFPageDocumentHelper dh(pdf); |
| 2185 | - std::shared_ptr<QPDFAcroFormDocumentHelper> afdh; | ||
| 2186 | - auto make_afdh = [&]() { | ||
| 2187 | - if (!afdh.get()) { | ||
| 2188 | - afdh = std::make_shared<QPDFAcroFormDocumentHelper>(pdf); | 2168 | + QPDFAcroFormDocumentHelper* afdh_ptr = nullptr; |
| 2169 | + auto afdh = [&]() -> QPDFAcroFormDocumentHelper& { | ||
| 2170 | + if (!afdh_ptr) { | ||
| 2171 | + afdh_ptr = &pdf.acroform(); | ||
| 2189 | } | 2172 | } |
| 2173 | + return *afdh_ptr; | ||
| 2190 | }; | 2174 | }; |
| 2191 | if (m->remove_restrictions) { | 2175 | if (m->remove_restrictions) { |
| 2192 | - make_afdh(); | ||
| 2193 | - afdh->disableDigitalSignatures(); | 2176 | + afdh().disableDigitalSignatures(); |
| 2194 | } | 2177 | } |
| 2195 | if (m->externalize_inline_images || (m->optimize_images && (!m->keep_inline_images))) { | 2178 | if (m->externalize_inline_images || (m->optimize_images && (!m->keep_inline_images))) { |
| 2196 | for (auto& ph: dh.getAllPages()) { | 2179 | for (auto& ph: dh.getAllPages()) { |
| @@ -2225,8 +2208,7 @@ QPDFJob::handleTransformations(QPDF& pdf) | @@ -2225,8 +2208,7 @@ QPDFJob::handleTransformations(QPDF& pdf) | ||
| 2225 | } | 2208 | } |
| 2226 | } | 2209 | } |
| 2227 | if (m->generate_appearances) { | 2210 | if (m->generate_appearances) { |
| 2228 | - make_afdh(); | ||
| 2229 | - afdh->generateAppearancesIfNeeded(); | 2211 | + afdh().generateAppearancesIfNeeded(); |
| 2230 | } | 2212 | } |
| 2231 | if (m->flatten_annotations) { | 2213 | if (m->flatten_annotations) { |
| 2232 | dh.flattenAnnotations(m->flatten_annotations_required, m->flatten_annotations_forbidden); | 2214 | dh.flattenAnnotations(m->flatten_annotations_required, m->flatten_annotations_forbidden); |
| @@ -2237,9 +2219,8 @@ QPDFJob::handleTransformations(QPDF& pdf) | @@ -2237,9 +2219,8 @@ QPDFJob::handleTransformations(QPDF& pdf) | ||
| 2237 | } | 2219 | } |
| 2238 | } | 2220 | } |
| 2239 | if (m->flatten_rotation) { | 2221 | if (m->flatten_rotation) { |
| 2240 | - make_afdh(); | ||
| 2241 | for (auto& page: dh.getAllPages()) { | 2222 | for (auto& page: dh.getAllPages()) { |
| 2242 | - page.flattenRotation(afdh.get()); | 2223 | + page.flattenRotation(&afdh()); |
| 2243 | } | 2224 | } |
| 2244 | } | 2225 | } |
| 2245 | if (m->remove_page_labels) { | 2226 | if (m->remove_page_labels) { |
| @@ -2559,8 +2540,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | @@ -2559,8 +2540,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | ||
| 2559 | std::vector<QPDFObjectHandle> new_labels; | 2540 | std::vector<QPDFObjectHandle> new_labels; |
| 2560 | bool any_page_labels = false; | 2541 | bool any_page_labels = false; |
| 2561 | int out_pageno = 0; | 2542 | int out_pageno = 0; |
| 2562 | - std::map<unsigned long long, std::shared_ptr<QPDFAcroFormDocumentHelper>> afdh_map; | ||
| 2563 | - auto this_afdh = get_afdh_for_qpdf(afdh_map, &pdf); | 2543 | + auto& this_afdh = pdf.acroform(); |
| 2564 | std::set<QPDFObjGen> referenced_fields; | 2544 | std::set<QPDFObjGen> referenced_fields; |
| 2565 | for (auto& page_data: parsed_specs) { | 2545 | for (auto& page_data: parsed_specs) { |
| 2566 | ClosedFileInputSource* cis = nullptr; | 2546 | ClosedFileInputSource* cis = nullptr; |
| @@ -2569,7 +2549,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | @@ -2569,7 +2549,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | ||
| 2569 | cis->stayOpen(true); | 2549 | cis->stayOpen(true); |
| 2570 | } | 2550 | } |
| 2571 | QPDFPageLabelDocumentHelper pldh(*page_data.qpdf); | 2551 | QPDFPageLabelDocumentHelper pldh(*page_data.qpdf); |
| 2572 | - auto other_afdh = get_afdh_for_qpdf(afdh_map, page_data.qpdf); | 2552 | + auto& other_afdh = page_data.qpdf->acroform(); |
| 2573 | if (pldh.hasPageLabels()) { | 2553 | if (pldh.hasPageLabels()) { |
| 2574 | any_page_labels = true; | 2554 | any_page_labels = true; |
| 2575 | } | 2555 | } |
| @@ -2611,15 +2591,15 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | @@ -2611,15 +2591,15 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | ||
| 2611 | // the original file until all copy operations are completed, any foreign pages that | 2591 | // the original file until all copy operations are completed, any foreign pages that |
| 2612 | // conflict with original pages will be adjusted. If we copy any page from the original | 2592 | // conflict with original pages will be adjusted. If we copy any page from the original |
| 2613 | // file more than once, that page would be in conflict with the previous copy of itself. | 2593 | // file more than once, that page would be in conflict with the previous copy of itself. |
| 2614 | - if ((!this_file && other_afdh->hasAcroForm()) || !first_copy_from_orig) { | 2594 | + if ((!this_file && other_afdh.hasAcroForm()) || !first_copy_from_orig) { |
| 2615 | if (!this_file) { | 2595 | if (!this_file) { |
| 2616 | QTC::TC("qpdf", "QPDFJob copy fields not this file"); | 2596 | QTC::TC("qpdf", "QPDFJob copy fields not this file"); |
| 2617 | } else if (!first_copy_from_orig) { | 2597 | } else if (!first_copy_from_orig) { |
| 2618 | QTC::TC("qpdf", "QPDFJob copy fields non-first from orig"); | 2598 | QTC::TC("qpdf", "QPDFJob copy fields non-first from orig"); |
| 2619 | } | 2599 | } |
| 2620 | try { | 2600 | try { |
| 2621 | - this_afdh->fixCopiedAnnotations( | ||
| 2622 | - new_page, to_copy.getObjectHandle(), *other_afdh, &referenced_fields); | 2601 | + this_afdh.fixCopiedAnnotations( |
| 2602 | + new_page, to_copy.getObjectHandle(), other_afdh, &referenced_fields); | ||
| 2623 | } catch (std::exception& e) { | 2603 | } catch (std::exception& e) { |
| 2624 | pdf.warn( | 2604 | pdf.warn( |
| 2625 | qpdf_e_damaged_pdf, | 2605 | qpdf_e_damaged_pdf, |
| @@ -2647,7 +2627,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | @@ -2647,7 +2627,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | ||
| 2647 | for (size_t pageno = 0; pageno < orig_pages.size(); ++pageno) { | 2627 | for (size_t pageno = 0; pageno < orig_pages.size(); ++pageno) { |
| 2648 | auto page = orig_pages.at(pageno); | 2628 | auto page = orig_pages.at(pageno); |
| 2649 | if (selected_from_orig.contains(QIntC::to_int(pageno))) { | 2629 | if (selected_from_orig.contains(QIntC::to_int(pageno))) { |
| 2650 | - for (auto field: this_afdh->getFormFieldsForPage(page)) { | 2630 | + for (auto field: this_afdh.getFormFieldsForPage(page)) { |
| 2651 | QTC::TC("qpdf", "QPDFJob pages keeping field from original"); | 2631 | QTC::TC("qpdf", "QPDFJob pages keeping field from original"); |
| 2652 | referenced_fields.insert(field.getObjectHandle().getObjGen()); | 2632 | referenced_fields.insert(field.getObjectHandle().getObjGen()); |
| 2653 | } | 2633 | } |
| @@ -2656,7 +2636,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | @@ -2656,7 +2636,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea | ||
| 2656 | } | 2636 | } |
| 2657 | } | 2637 | } |
| 2658 | // Remove unreferenced form fields | 2638 | // Remove unreferenced form fields |
| 2659 | - if (this_afdh->hasAcroForm()) { | 2639 | + if (this_afdh.hasAcroForm()) { |
| 2660 | auto acroform = pdf.getRoot().getKey("/AcroForm"); | 2640 | auto acroform = pdf.getRoot().getKey("/AcroForm"); |
| 2661 | auto fields = acroform.getKey("/Fields"); | 2641 | auto fields = acroform.getKey("/Fields"); |
| 2662 | if (fields.isArray()) { | 2642 | if (fields.isArray()) { |
| @@ -3025,10 +3005,7 @@ QPDFJob::doSplitPages(QPDF& pdf) | @@ -3025,10 +3005,7 @@ QPDFJob::doSplitPages(QPDF& pdf) | ||
| 3025 | } | 3005 | } |
| 3026 | QPDF outpdf; | 3006 | QPDF outpdf; |
| 3027 | outpdf.emptyPDF(); | 3007 | outpdf.emptyPDF(); |
| 3028 | - std::shared_ptr<QPDFAcroFormDocumentHelper> out_afdh; | ||
| 3029 | - if (afdh.hasAcroForm()) { | ||
| 3030 | - out_afdh = std::make_shared<QPDFAcroFormDocumentHelper>(outpdf); | ||
| 3031 | - } | 3008 | + QPDFAcroFormDocumentHelper* out_afdh = afdh.hasAcroForm() ? &outpdf.acroform() : nullptr; |
| 3032 | if (m->suppress_warnings) { | 3009 | if (m->suppress_warnings) { |
| 3033 | outpdf.setSuppressWarnings(true); | 3010 | outpdf.setSuppressWarnings(true); |
| 3034 | } | 3011 | } |
| @@ -3036,8 +3013,7 @@ QPDFJob::doSplitPages(QPDF& pdf) | @@ -3036,8 +3013,7 @@ QPDFJob::doSplitPages(QPDF& pdf) | ||
| 3036 | QPDFObjectHandle page = pages.at(pageno - 1); | 3013 | QPDFObjectHandle page = pages.at(pageno - 1); |
| 3037 | outpdf.addPage(page, false); | 3014 | outpdf.addPage(page, false); |
| 3038 | auto new_page = added_page(outpdf, page); | 3015 | auto new_page = added_page(outpdf, page); |
| 3039 | - if (out_afdh.get()) { | ||
| 3040 | - QTC::TC("qpdf", "QPDFJob copy form fields in split_pages"); | 3016 | + if (out_afdh) { |
| 3041 | try { | 3017 | try { |
| 3042 | out_afdh->fixCopiedAnnotations(new_page, page, afdh); | 3018 | out_afdh->fixCopiedAnnotations(new_page, page, afdh); |
| 3043 | } catch (std::exception& e) { | 3019 | } catch (std::exception& e) { |
qpdf/qpdf.testcov
| @@ -519,7 +519,6 @@ QPDFPageObjectHelper flatten inherit rotate 0 | @@ -519,7 +519,6 @@ QPDFPageObjectHelper flatten inherit rotate 0 | ||
| 519 | QPDFAcroFormDocumentHelper copy annotation 3 | 519 | QPDFAcroFormDocumentHelper copy annotation 3 |
| 520 | QPDFAcroFormDocumentHelper field with parent 3 | 520 | QPDFAcroFormDocumentHelper field with parent 3 |
| 521 | QPDFAcroFormDocumentHelper modify ap matrix 0 | 521 | QPDFAcroFormDocumentHelper modify ap matrix 0 |
| 522 | -QPDFJob copy form fields in split_pages 0 | ||
| 523 | QPDFJob pages keeping field from original 0 | 522 | QPDFJob pages keeping field from original 0 |
| 524 | QPDFObjectHandle merge reuse 0 | 523 | QPDFObjectHandle merge reuse 0 |
| 525 | QPDFObjectHandle merge generate 0 | 524 | QPDFObjectHandle merge generate 0 |