From e64965e31be2e3b7059a9b8fe31bb345bddb4e2f Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 7 Aug 2025 22:30:14 +0100 Subject: [PATCH] Refactor `QPDF`: centralize access to `QPDFAcroFormDocumentHelper` using a dedicated `acroform()` method and update related references for improved performance, consistency and maintainability. --- include/qpdf/QPDF.hh | 2 ++ libqpdf/QPDFFormFieldObjectHelper.cc | 3 ++- libqpdf/QPDFJob.cc | 4 ++-- libqpdf/QPDFPageDocumentHelper.cc | 3 ++- libqpdf/qpdf/QPDF_private.hh | 13 +++++++++++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index d42b2e6..590257e 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -57,6 +57,7 @@ class BitWriter; class BufferInputSource; class QPDFLogger; class QPDFParser; +class QPDFAcroFormDocumentHelper; class QPDF { @@ -792,6 +793,7 @@ class QPDF class JobSetter; inline bool reconstructed_xref() const; + inline QPDFAcroFormDocumentHelper& acroform(); // For testing only -- do not add to DLL static bool test_json_validators(); diff --git a/libqpdf/QPDFFormFieldObjectHelper.cc b/libqpdf/QPDFFormFieldObjectHelper.cc index 2f990bc..59d12dc 100644 --- a/libqpdf/QPDFFormFieldObjectHelper.cc +++ b/libqpdf/QPDFFormFieldObjectHelper.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -337,7 +338,7 @@ QPDFFormFieldObjectHelper::setV(QPDFObjectHandle value, bool need_appearances) QPDF& qpdf = oh().getQPDF( "QPDFFormFieldObjectHelper::setV called with need_appearances = " "true on an object that is not associated with an owning QPDF"); - QPDFAcroFormDocumentHelper(qpdf).setNeedAppearances(true); + qpdf.acroform().setNeedAppearances(true); } } diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index c6feb31..f9c2e43 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -1180,7 +1180,7 @@ void QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf) { JSON j_acroform = JSON::makeDictionary(); - QPDFAcroFormDocumentHelper afdh(pdf); + auto& afdh = pdf.acroform(); j_acroform.addDictionaryMember("hasacroform", JSON::makeBool(afdh.hasAcroForm())); j_acroform.addDictionaryMember("needappearances", JSON::makeBool(afdh.getNeedAppearances())); JSON j_fields = j_acroform.addDictionaryMember("fields", JSON::makeArray()); @@ -3013,7 +3013,7 @@ QPDFJob::doSplitPages(QPDF& pdf) dh.removeUnreferencedResources(); } QPDFPageLabelDocumentHelper pldh(pdf); - QPDFAcroFormDocumentHelper afdh(pdf); + auto& afdh = pdf.acroform(); std::vector const& pages = pdf.getAllPages(); size_t pageno_len = std::to_string(pages.size()).length(); size_t num_pages = pages.size(); diff --git a/libqpdf/QPDFPageDocumentHelper.cc b/libqpdf/QPDFPageDocumentHelper.cc index 12841ac..3d0fbdb 100644 --- a/libqpdf/QPDFPageDocumentHelper.cc +++ b/libqpdf/QPDFPageDocumentHelper.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -55,7 +56,7 @@ QPDFPageDocumentHelper::removePage(QPDFPageObjectHelper page) void QPDFPageDocumentHelper::flattenAnnotations(int required_flags, int forbidden_flags) { - QPDFAcroFormDocumentHelper afdh(qpdf); + auto& afdh = qpdf.acroform(); if (afdh.getNeedAppearances()) { qpdf.getRoot() .getKey("/AcroForm") diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index 55bffce..cd51ec2 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -3,6 +3,7 @@ #include +#include #include #include @@ -547,6 +548,9 @@ class QPDF::Members // Optimization data std::map> obj_user_to_objects; std::map> object_to_obj_users; + + // Document Helpers; + std::unique_ptr acroform; }; // JobSetter class is restricted to QPDFJob. @@ -569,4 +573,13 @@ QPDF::reconstructed_xref() const return m->reconstructed_xref; } +inline QPDFAcroFormDocumentHelper& +QPDF::acroform() +{ + if (!m->acroform) { + m->acroform = std::make_unique(*this); + } + return *m->acroform; +} + #endif // QPDF_PRIVATE_HH -- libgit2 0.21.4