From 94a0e76b6951f0cb1e844ebe2ad0bcd32b162f1b Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 10 Nov 2025 14:10:08 +0000 Subject: [PATCH] Refactor `FormField`: replace `getFieldType` and `getInheritableFieldValueAsName` with `FT` method, streamline logic, and update related references for clarity and consistency. --- libqpdf/QPDFFormFieldObjectHelper.cc | 34 ++++++++++++---------------------- libqpdf/qpdf/FormField.hh | 16 ++++++++++------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/libqpdf/QPDFFormFieldObjectHelper.cc b/libqpdf/QPDFFormFieldObjectHelper.cc index ee5eb2c..efb08ed 100644 --- a/libqpdf/QPDFFormFieldObjectHelper.cc +++ b/libqpdf/QPDFFormFieldObjectHelper.cc @@ -122,13 +122,7 @@ FormField::inheritable_string(std::string const& name) const std::string QPDFFormFieldObjectHelper::getInheritableFieldValueAsName(std::string const& name) { - return m->getInheritableFieldValueAsName(name); -} - -std::string -FormField::getInheritableFieldValueAsName(std::string const& name) -{ - if (auto fv = inheritable_value(name)) { + if (auto fv = m->inheritable_value(name)) { return fv; } return {}; @@ -137,13 +131,10 @@ FormField::getInheritableFieldValueAsName(std::string const& name) std::string QPDFFormFieldObjectHelper::getFieldType() { - return m->getFieldType(); -} - -std::string -FormField::getFieldType() -{ - return getInheritableFieldValueAsName("/FT"); + if (auto ft = m->FT()) { + return ft; + } + return {}; } std::string @@ -346,7 +337,7 @@ QPDFFormFieldObjectHelper::isText() bool FormField::isText() { - return getFieldType() == "/Tx"; + return FT() == "/Tx"; } bool @@ -358,7 +349,7 @@ QPDFFormFieldObjectHelper::isCheckbox() bool FormField::isCheckbox() { - return getFieldType() == "/Btn" && (getFlags() & (ff_btn_radio | ff_btn_pushbutton)) == 0; + return FT() == "/Btn" && (getFlags() & (ff_btn_radio | ff_btn_pushbutton)) == 0; } bool @@ -382,7 +373,7 @@ QPDFFormFieldObjectHelper::isRadioButton() bool FormField::isRadioButton() { - return getFieldType() == "/Btn" && (getFlags() & ff_btn_radio) == ff_btn_radio; + return FT() == "/Btn" && (getFlags() & ff_btn_radio) == ff_btn_radio; } bool @@ -394,7 +385,7 @@ QPDFFormFieldObjectHelper::isPushbutton() bool FormField::isPushbutton() { - return getFieldType() == "/Btn" && (getFlags() & ff_btn_pushbutton) == ff_btn_pushbutton; + return FT() == "/Btn" && (getFlags() & ff_btn_pushbutton) == ff_btn_pushbutton; } bool @@ -406,7 +397,7 @@ QPDFFormFieldObjectHelper::isChoice() bool FormField::isChoice() { - return getFieldType() == "/Ch"; + return FT() == "/Ch"; } std::vector @@ -469,7 +460,7 @@ void FormField::setV(QPDFObjectHandle value, bool need_appearances) { Name name = value; - if (getFieldType() == "/Btn") { + if (FT() == "/Btn") { if (isCheckbox()) { if (!name) { warn("ignoring attempt to set a checkbox field to a value whose type is not name"); @@ -632,10 +623,9 @@ QPDFFormFieldObjectHelper::generateAppearance(QPDFAnnotationObjectHelper& aoh) void FormField::generateAppearance(QPDFAnnotationObjectHelper& aoh) { - std::string ft = getFieldType(); // Ignore field types we don't know how to generate appearances for. Button fields don't really // need them -- see code in QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded. - if ((ft == "/Tx") || (ft == "/Ch")) { + if (FT() == "/Tx" || FT() == "/Ch") { generateTextAppearance(aoh); } } diff --git a/libqpdf/qpdf/FormField.hh b/libqpdf/qpdf/FormField.hh index de8b9f6..8d8b25f 100644 --- a/libqpdf/qpdf/FormField.hh +++ b/libqpdf/qpdf/FormField.hh @@ -104,12 +104,16 @@ namespace qpdf::impl /// value does not exist or is not of String type. std::string inheritable_string(std::string const& name) const; - // Get an inherited field value of type name as a string representing the name. If it is not - // a name, silently return the empty string. - std::string getInheritableFieldValueAsName(std::string const& name); - - // Returns the value of /FT if present, otherwise returns the empty string. - std::string getFieldType(); + /// @brief Retrieves the field type (/FT attribute). + /// @param inherit If set to `true`, the function will attempt to retrieve the value by + /// inheritance from the parent hierarchy of the form field. Defaults to `true`. + /// @return Returns the field type if found; otherwise, returns a default-constructed + /// `Name`. + Name + FT(bool inherit = true) const + { + return inheritable_value("/FT"); + } std::string getFullyQualifiedName(); -- libgit2 0.21.4