Commit 6d5e870287e7a9300d0269ad3fb2c18d4cc7da9f
1 parent
4b08688f
Refactor: replace `getFieldFromAcroForm` with `from_AcroForm`, modernize impleme…
…ntation, and update references in `FormField` methods.
Showing
3 changed files
with
30 additions
and
25 deletions
libqpdf/QPDFFormFieldObjectHelper.cc
| ... | ... | @@ -18,6 +18,8 @@ using namespace qpdf; |
| 18 | 18 | |
| 19 | 19 | using FormField = qpdf::impl::FormField; |
| 20 | 20 | |
| 21 | +const QPDFObjectHandle FormField::null_oh; | |
| 22 | + | |
| 21 | 23 | class QPDFFormFieldObjectHelper::Members: public FormField |
| 22 | 24 | { |
| 23 | 25 | public: |
| ... | ... | @@ -79,22 +81,6 @@ FormField::root_field(bool* is_different) |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | 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 | 84 | QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name) |
| 99 | 85 | { |
| 100 | 86 | return m->getInheritableFieldValue(name); |
| ... | ... | @@ -289,13 +275,13 @@ FormField::getDefaultValueAsString() |
| 289 | 275 | QPDFObjectHandle |
| 290 | 276 | QPDFFormFieldObjectHelper::getDefaultResources() |
| 291 | 277 | { |
| 292 | - return m->getDefaultResources(); | |
| 278 | + return Null::if_null(m->getDefaultResources()); | |
| 293 | 279 | } |
| 294 | 280 | |
| 295 | 281 | QPDFObjectHandle |
| 296 | 282 | FormField::getDefaultResources() |
| 297 | 283 | { |
| 298 | - return getFieldFromAcroForm("/DR"); | |
| 284 | + return from_AcroForm("/DR"); | |
| 299 | 285 | } |
| 300 | 286 | |
| 301 | 287 | std::string |
| ... | ... | @@ -310,7 +296,7 @@ FormField::getDefaultAppearance() |
| 310 | 296 | auto value = getInheritableFieldValue("/DA"); |
| 311 | 297 | bool looked_in_acroform = false; |
| 312 | 298 | if (!value.isString()) { |
| 313 | - value = getFieldFromAcroForm("/DA"); | |
| 299 | + value = from_AcroForm("/DA"); | |
| 314 | 300 | looked_in_acroform = true; |
| 315 | 301 | } |
| 316 | 302 | if (value.isString()) { |
| ... | ... | @@ -332,7 +318,7 @@ FormField::getQuadding() |
| 332 | 318 | QPDFObjectHandle fv = getInheritableFieldValue("/Q"); |
| 333 | 319 | bool looked_in_acroform = false; |
| 334 | 320 | if (!fv.isInteger()) { |
| 335 | - fv = getFieldFromAcroForm("/Q"); | |
| 321 | + fv = from_AcroForm("/Q"); | |
| 336 | 322 | looked_in_acroform = true; |
| 337 | 323 | } |
| 338 | 324 | if (fv.isInteger()) { | ... | ... |
libqpdf/qpdf/FormField.hh
| ... | ... | @@ -173,12 +173,29 @@ namespace qpdf::impl |
| 173 | 173 | void generateAppearance(QPDFAnnotationObjectHelper&); |
| 174 | 174 | |
| 175 | 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 | 192 | void setRadioButtonValue(QPDFObjectHandle name); |
| 178 | 193 | void setCheckBoxValue(bool value); |
| 179 | 194 | void generateTextAppearance(QPDFAnnotationObjectHelper&); |
| 180 | 195 | QPDFObjectHandle |
| 181 | 196 | getFontFromResource(QPDFObjectHandle resources, std::string const& font_name); |
| 197 | + | |
| 198 | + static const QPDFObjectHandle null_oh; | |
| 182 | 199 | }; |
| 183 | 200 | } // namespace qpdf::impl |
| 184 | 201 | ... | ... |
libtests/objects.cc
| ... | ... | @@ -90,7 +90,6 @@ test_0(QPDF& pdf, char const* arg2) |
| 90 | 90 | assert_compare_numbers(UINT_MAX, t.getKey("/Q3").getUIntValueAsUInt()); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - | |
| 94 | 93 | static void |
| 95 | 94 | test_1(QPDF& pdf, char const* arg2) |
| 96 | 95 | { |
| ... | ... | @@ -121,7 +120,7 @@ test_1(QPDF& pdf, char const* arg2) |
| 121 | 120 | |
| 122 | 121 | bool thrown = false; |
| 123 | 122 | try { |
| 124 | - i.at("/A"); | |
| 123 | + i.at("/A"); | |
| 125 | 124 | } catch (std::runtime_error const&) { |
| 126 | 125 | thrown = true; |
| 127 | 126 | } |
| ... | ... | @@ -161,7 +160,9 @@ runtest(int n, char const* filename1, char const* arg2) |
| 161 | 160 | // the test suite to see how the test is invoked to find the file |
| 162 | 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 | 167 | QPDF pdf; |
| 167 | 168 | std::shared_ptr<char> file_buf; |
| ... | ... | @@ -175,7 +176,8 @@ runtest(int n, char const* filename1, char const* arg2) |
| 175 | 176 | } |
| 176 | 177 | |
| 177 | 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 | 183 | auto fn = test_functions.find(n); | ... | ... |