diff --git a/libqpdf/QPDFFormFieldObjectHelper.cc b/libqpdf/QPDFFormFieldObjectHelper.cc index 5bcdd36..704e368 100644 --- a/libqpdf/QPDFFormFieldObjectHelper.cc +++ b/libqpdf/QPDFFormFieldObjectHelper.cc @@ -18,6 +18,8 @@ using namespace qpdf; using FormField = qpdf::impl::FormField; +const QPDFObjectHandle FormField::null_oh; + class QPDFFormFieldObjectHelper::Members: public FormField { public: @@ -79,22 +81,6 @@ FormField::root_field(bool* is_different) } QPDFObjectHandle -FormField::getFieldFromAcroForm(std::string const& name) -{ - QPDFObjectHandle result = QPDFObjectHandle::newNull(); - // Fields are supposed to be indirect, so this should work. - QPDF* q = oh().getOwningQPDF(); - if (!q) { - return result; - } - auto acroform = q->getRoot().getKey("/AcroForm"); - if (!acroform.isDictionary()) { - return result; - } - return acroform.getKey(name); -} - -QPDFObjectHandle QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name) { return m->getInheritableFieldValue(name); @@ -289,13 +275,13 @@ FormField::getDefaultValueAsString() QPDFObjectHandle QPDFFormFieldObjectHelper::getDefaultResources() { - return m->getDefaultResources(); + return Null::if_null(m->getDefaultResources()); } QPDFObjectHandle FormField::getDefaultResources() { - return getFieldFromAcroForm("/DR"); + return from_AcroForm("/DR"); } std::string @@ -310,7 +296,7 @@ FormField::getDefaultAppearance() auto value = getInheritableFieldValue("/DA"); bool looked_in_acroform = false; if (!value.isString()) { - value = getFieldFromAcroForm("/DA"); + value = from_AcroForm("/DA"); looked_in_acroform = true; } if (value.isString()) { @@ -332,7 +318,7 @@ FormField::getQuadding() QPDFObjectHandle fv = getInheritableFieldValue("/Q"); bool looked_in_acroform = false; if (!fv.isInteger()) { - fv = getFieldFromAcroForm("/Q"); + fv = from_AcroForm("/Q"); looked_in_acroform = true; } if (fv.isInteger()) { diff --git a/libqpdf/qpdf/FormField.hh b/libqpdf/qpdf/FormField.hh index b763caf..13ea524 100644 --- a/libqpdf/qpdf/FormField.hh +++ b/libqpdf/qpdf/FormField.hh @@ -173,12 +173,29 @@ namespace qpdf::impl void generateAppearance(QPDFAnnotationObjectHelper&); private: - QPDFObjectHandle getFieldFromAcroForm(std::string const& name); + /// @brief Retrieves an entry from the document's /AcroForm dictionary using the specified + /// name. + /// + /// The method accesses the AcroForm dictionary within the root object of the PDF document. + /// If the the AcroForm dictionary contains the given field name, it retrieves the + /// corresponding entry. Otherwise, it returns a default-constructed object handle. + /// + /// @param name The name of the form field to retrieve. + /// @return A object handle corresponding to the specified name within the AcroForm + /// dictionary. + QPDFObjectHandle const& + from_AcroForm(std::string const& name) const + { + return {qpdf() ? qpdf()->getRoot()["/AcroForm"][name] : null_oh}; + } + void setRadioButtonValue(QPDFObjectHandle name); void setCheckBoxValue(bool value); void generateTextAppearance(QPDFAnnotationObjectHelper&); QPDFObjectHandle getFontFromResource(QPDFObjectHandle resources, std::string const& font_name); + + static const QPDFObjectHandle null_oh; }; } // namespace qpdf::impl diff --git a/libtests/objects.cc b/libtests/objects.cc index 6f20aee..52da62c 100644 --- a/libtests/objects.cc +++ b/libtests/objects.cc @@ -90,7 +90,6 @@ test_0(QPDF& pdf, char const* arg2) assert_compare_numbers(UINT_MAX, t.getKey("/Q3").getUIntValueAsUInt()); } - static void test_1(QPDF& pdf, char const* arg2) { @@ -121,7 +120,7 @@ test_1(QPDF& pdf, char const* arg2) bool thrown = false; try { - i.at("/A"); + i.at("/A"); } catch (std::runtime_error const&) { thrown = true; } @@ -161,7 +160,9 @@ runtest(int n, char const* filename1, char const* arg2) // the test suite to see how the test is invoked to find the file // that the test is supposed to operate on. - std::set ignore_filename = {1,}; + std::set ignore_filename = { + 1, + }; QPDF pdf; std::shared_ptr file_buf; @@ -175,7 +176,8 @@ runtest(int n, char const* filename1, char const* arg2) } std::map test_functions = { - {0, test_0}, {1, test_1}, + {0, test_0}, + {1, test_1}, }; auto fn = test_functions.find(n);