Commit 6d5e870287e7a9300d0269ad3fb2c18d4cc7da9f

Authored by m-holger
1 parent 4b08688f

Refactor: replace `getFieldFromAcroForm` with `from_AcroForm`, modernize impleme…

…ntation, and update references in `FormField` methods.
libqpdf/QPDFFormFieldObjectHelper.cc
@@ -18,6 +18,8 @@ using namespace qpdf; @@ -18,6 +18,8 @@ using namespace qpdf;
18 18
19 using FormField = qpdf::impl::FormField; 19 using FormField = qpdf::impl::FormField;
20 20
  21 +const QPDFObjectHandle FormField::null_oh;
  22 +
21 class QPDFFormFieldObjectHelper::Members: public FormField 23 class QPDFFormFieldObjectHelper::Members: public FormField
22 { 24 {
23 public: 25 public:
@@ -79,22 +81,6 @@ FormField::root_field(bool* is_different) @@ -79,22 +81,6 @@ FormField::root_field(bool* is_different)
79 } 81 }
80 82
81 QPDFObjectHandle 83 QPDFObjectHandle
82 -FormField::getFieldFromAcroForm(std::string const& name)  
83 -{  
84 - QPDFObjectHandle result = QPDFObjectHandle::newNull();  
85 - // Fields are supposed to be indirect, so this should work.  
86 - QPDF* q = oh().getOwningQPDF();  
87 - if (!q) {  
88 - return result;  
89 - }  
90 - auto acroform = q->getRoot().getKey("/AcroForm");  
91 - if (!acroform.isDictionary()) {  
92 - return result;  
93 - }  
94 - return acroform.getKey(name);  
95 -}  
96 -  
97 -QPDFObjectHandle  
98 QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name) 84 QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name)
99 { 85 {
100 return m->getInheritableFieldValue(name); 86 return m->getInheritableFieldValue(name);
@@ -289,13 +275,13 @@ FormField::getDefaultValueAsString() @@ -289,13 +275,13 @@ FormField::getDefaultValueAsString()
289 QPDFObjectHandle 275 QPDFObjectHandle
290 QPDFFormFieldObjectHelper::getDefaultResources() 276 QPDFFormFieldObjectHelper::getDefaultResources()
291 { 277 {
292 - return m->getDefaultResources(); 278 + return Null::if_null(m->getDefaultResources());
293 } 279 }
294 280
295 QPDFObjectHandle 281 QPDFObjectHandle
296 FormField::getDefaultResources() 282 FormField::getDefaultResources()
297 { 283 {
298 - return getFieldFromAcroForm("/DR"); 284 + return from_AcroForm("/DR");
299 } 285 }
300 286
301 std::string 287 std::string
@@ -310,7 +296,7 @@ FormField::getDefaultAppearance() @@ -310,7 +296,7 @@ FormField::getDefaultAppearance()
310 auto value = getInheritableFieldValue("/DA"); 296 auto value = getInheritableFieldValue("/DA");
311 bool looked_in_acroform = false; 297 bool looked_in_acroform = false;
312 if (!value.isString()) { 298 if (!value.isString()) {
313 - value = getFieldFromAcroForm("/DA"); 299 + value = from_AcroForm("/DA");
314 looked_in_acroform = true; 300 looked_in_acroform = true;
315 } 301 }
316 if (value.isString()) { 302 if (value.isString()) {
@@ -332,7 +318,7 @@ FormField::getQuadding() @@ -332,7 +318,7 @@ FormField::getQuadding()
332 QPDFObjectHandle fv = getInheritableFieldValue("/Q"); 318 QPDFObjectHandle fv = getInheritableFieldValue("/Q");
333 bool looked_in_acroform = false; 319 bool looked_in_acroform = false;
334 if (!fv.isInteger()) { 320 if (!fv.isInteger()) {
335 - fv = getFieldFromAcroForm("/Q"); 321 + fv = from_AcroForm("/Q");
336 looked_in_acroform = true; 322 looked_in_acroform = true;
337 } 323 }
338 if (fv.isInteger()) { 324 if (fv.isInteger()) {
libqpdf/qpdf/FormField.hh
@@ -173,12 +173,29 @@ namespace qpdf::impl @@ -173,12 +173,29 @@ namespace qpdf::impl
173 void generateAppearance(QPDFAnnotationObjectHelper&); 173 void generateAppearance(QPDFAnnotationObjectHelper&);
174 174
175 private: 175 private:
176 - QPDFObjectHandle getFieldFromAcroForm(std::string const& name); 176 + /// @brief Retrieves an entry from the document's /AcroForm dictionary using the specified
  177 + /// name.
  178 + ///
  179 + /// The method accesses the AcroForm dictionary within the root object of the PDF document.
  180 + /// If the the AcroForm dictionary contains the given field name, it retrieves the
  181 + /// corresponding entry. Otherwise, it returns a default-constructed object handle.
  182 + ///
  183 + /// @param name The name of the form field to retrieve.
  184 + /// @return A object handle corresponding to the specified name within the AcroForm
  185 + /// dictionary.
  186 + QPDFObjectHandle const&
  187 + from_AcroForm(std::string const& name) const
  188 + {
  189 + return {qpdf() ? qpdf()->getRoot()["/AcroForm"][name] : null_oh};
  190 + }
  191 +
177 void setRadioButtonValue(QPDFObjectHandle name); 192 void setRadioButtonValue(QPDFObjectHandle name);
178 void setCheckBoxValue(bool value); 193 void setCheckBoxValue(bool value);
179 void generateTextAppearance(QPDFAnnotationObjectHelper&); 194 void generateTextAppearance(QPDFAnnotationObjectHelper&);
180 QPDFObjectHandle 195 QPDFObjectHandle
181 getFontFromResource(QPDFObjectHandle resources, std::string const& font_name); 196 getFontFromResource(QPDFObjectHandle resources, std::string const& font_name);
  197 +
  198 + static const QPDFObjectHandle null_oh;
182 }; 199 };
183 } // namespace qpdf::impl 200 } // namespace qpdf::impl
184 201
libtests/objects.cc
@@ -90,7 +90,6 @@ test_0(QPDF& pdf, char const* arg2) @@ -90,7 +90,6 @@ test_0(QPDF& pdf, char const* arg2)
90 assert_compare_numbers(UINT_MAX, t.getKey("/Q3").getUIntValueAsUInt()); 90 assert_compare_numbers(UINT_MAX, t.getKey("/Q3").getUIntValueAsUInt());
91 } 91 }
92 92
93 -  
94 static void 93 static void
95 test_1(QPDF& pdf, char const* arg2) 94 test_1(QPDF& pdf, char const* arg2)
96 { 95 {
@@ -121,7 +120,7 @@ test_1(QPDF& pdf, char const* arg2) @@ -121,7 +120,7 @@ test_1(QPDF& pdf, char const* arg2)
121 120
122 bool thrown = false; 121 bool thrown = false;
123 try { 122 try {
124 - i.at("/A"); 123 + i.at("/A");
125 } catch (std::runtime_error const&) { 124 } catch (std::runtime_error const&) {
126 thrown = true; 125 thrown = true;
127 } 126 }
@@ -161,7 +160,9 @@ runtest(int n, char const* filename1, char const* arg2) @@ -161,7 +160,9 @@ runtest(int n, char const* filename1, char const* arg2)
161 // the test suite to see how the test is invoked to find the file 160 // the test suite to see how the test is invoked to find the file
162 // that the test is supposed to operate on. 161 // that the test is supposed to operate on.
163 162
164 - std::set<int> ignore_filename = {1,}; 163 + std::set<int> ignore_filename = {
  164 + 1,
  165 + };
165 166
166 QPDF pdf; 167 QPDF pdf;
167 std::shared_ptr<char> file_buf; 168 std::shared_ptr<char> file_buf;
@@ -175,7 +176,8 @@ runtest(int n, char const* filename1, char const* arg2) @@ -175,7 +176,8 @@ runtest(int n, char const* filename1, char const* arg2)
175 } 176 }
176 177
177 std::map<int, void (*)(QPDF&, char const*)> test_functions = { 178 std::map<int, void (*)(QPDF&, char const*)> test_functions = {
178 - {0, test_0}, {1, test_1}, 179 + {0, test_0},
  180 + {1, test_1},
179 }; 181 };
180 182
181 auto fn = test_functions.find(n); 183 auto fn = test_functions.find(n);