Commit 4d0ccbe46b9457e70a5c897af472657a56f7569c

Authored by m-holger
1 parent ed7f3d5e

Refactor AcroForm handling by introducing `AcroForm` class for better encapsulat…

…ion and leveraging shared components.
libqpdf/QPDFAcroFormDocumentHelper.cc
1 #include <qpdf/QPDFAcroFormDocumentHelper.hh> 1 #include <qpdf/QPDFAcroFormDocumentHelper.hh>
2 2
  3 +#include <qpdf/FormField.hh>
  4 +
3 #include <qpdf/Pl_Buffer.hh> 5 #include <qpdf/Pl_Buffer.hh>
4 #include <qpdf/QPDFObjectHandle_private.hh> 6 #include <qpdf/QPDFObjectHandle_private.hh>
5 #include <qpdf/QPDFPageDocumentHelper.hh> 7 #include <qpdf/QPDFPageDocumentHelper.hh>
@@ -14,29 +16,20 @@ @@ -14,29 +16,20 @@
14 using namespace qpdf; 16 using namespace qpdf;
15 using namespace std::literals; 17 using namespace std::literals;
16 18
17 -class QPDFAcroFormDocumentHelper::Members 19 +using AcroForm = impl::AcroForm;
  20 +
  21 +class QPDFAcroFormDocumentHelper::Members: public AcroForm
18 { 22 {
19 public: 23 public:
20 - Members() = default;  
21 - Members(Members const&) = delete;  
22 - ~Members() = default;  
23 -  
24 - struct FieldData 24 + Members(QPDF& qpdf) :
  25 + AcroForm(qpdf.doc())
25 { 26 {
26 - std::vector<QPDFAnnotationObjectHelper> annotations;  
27 - std::string name;  
28 - };  
29 -  
30 - bool cache_valid{false};  
31 - std::map<QPDFObjGen, FieldData> field_to;  
32 - std::map<QPDFObjGen, QPDFFormFieldObjectHelper> annotation_to_field;  
33 - std::map<std::string, std::set<QPDFObjGen>> name_to_fields;  
34 - std::set<QPDFObjGen> bad_fields; 27 + }
35 }; 28 };
36 29
37 QPDFAcroFormDocumentHelper::QPDFAcroFormDocumentHelper(QPDF& qpdf) : 30 QPDFAcroFormDocumentHelper::QPDFAcroFormDocumentHelper(QPDF& qpdf) :
38 QPDFDocumentHelper(qpdf), 31 QPDFDocumentHelper(qpdf),
39 - m(std::make_shared<Members>()) 32 + m(std::make_shared<Members>(qpdf))
40 { 33 {
41 // We have to analyze up front. Otherwise, when we are adding annotations and fields, we are in 34 // We have to analyze up front. Otherwise, when we are adding annotations and fields, we are in
42 // a temporarily unstable configuration where some widget annotations are not reachable. 35 // a temporarily unstable configuration where some widget annotations are not reachable.
@@ -62,6 +55,7 @@ QPDFAcroFormDocumentHelper::invalidateCache() @@ -62,6 +55,7 @@ QPDFAcroFormDocumentHelper::invalidateCache()
62 m->cache_valid = false; 55 m->cache_valid = false;
63 m->field_to.clear(); 56 m->field_to.clear();
64 m->annotation_to_field.clear(); 57 m->annotation_to_field.clear();
  58 + m->bad_fields.clear();
65 } 59 }
66 60
67 bool 61 bool
libqpdf/qpdf/FormField.hh
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 3
4 #include <qpdf/QPDFObjectHandle_private.hh> 4 #include <qpdf/QPDFObjectHandle_private.hh>
5 #include <qpdf/QPDFObjectHelper.hh> 5 #include <qpdf/QPDFObjectHelper.hh>
  6 +#include <qpdf/QPDF_private.hh>
6 7
7 #include <vector> 8 #include <vector>
8 9
@@ -10,6 +11,34 @@ class QPDFAnnotationObjectHelper; @@ -10,6 +11,34 @@ class QPDFAnnotationObjectHelper;
10 11
11 namespace qpdf::impl 12 namespace qpdf::impl
12 { 13 {
  14 + class AcroForm: public Doc::Common
  15 + {
  16 + public:
  17 + AcroForm() = delete;
  18 + AcroForm(AcroForm const&) = delete;
  19 + AcroForm(AcroForm&&) = delete;
  20 + AcroForm& operator=(AcroForm const&) = delete;
  21 + AcroForm& operator=(AcroForm&&) = delete;
  22 + ~AcroForm() = default;
  23 +
  24 + AcroForm(impl::Doc& doc) :
  25 + Common(doc)
  26 + {
  27 + }
  28 +
  29 + struct FieldData
  30 + {
  31 + std::vector<QPDFAnnotationObjectHelper> annotations;
  32 + std::string name;
  33 + };
  34 +
  35 + bool cache_valid{false};
  36 + std::map<QPDFObjGen, FieldData> field_to;
  37 + std::map<QPDFObjGen, QPDFFormFieldObjectHelper> annotation_to_field;
  38 + std::map<std::string, std::set<QPDFObjGen>> name_to_fields;
  39 + std::set<QPDFObjGen> bad_fields;
  40 + }; // class AcroForm
  41 +
13 /// @class FormNode 42 /// @class FormNode
14 /// @brief Represents a node in the interactive forms tree of a PDF document. 43 /// @brief Represents a node in the interactive forms tree of a PDF document.
15 /// 44 ///
@@ -390,7 +419,7 @@ namespace qpdf::impl @@ -390,7 +419,7 @@ namespace qpdf::impl
390 getFontFromResource(QPDFObjectHandle resources, std::string const& font_name); 419 getFontFromResource(QPDFObjectHandle resources, std::string const& font_name);
391 420
392 static const QPDFObjectHandle null_oh; 421 static const QPDFObjectHandle null_oh;
393 - }; 422 + }; // class FormNode
394 } // namespace qpdf::impl 423 } // namespace qpdf::impl
395 424
396 #endif // FORMFIELD_HH 425 #endif // FORMFIELD_HH