Commit 87ff43f4207b9ac3a60960d35652c645e80ef7ad

Authored by m-holger
1 parent ce110b43

Refactor `FormField`: replace `getValue` and `getValueAsString` with `V` and `va…

…lue` for clarity, consistency, and improved readability. Update references and method documentation accordingly.
libqpdf/QPDFFormFieldObjectHelper.cc
@@ -210,23 +210,17 @@ FormField::mapping_name() const @@ -210,23 +210,17 @@ FormField::mapping_name() const
210 QPDFObjectHandle 210 QPDFObjectHandle
211 QPDFFormFieldObjectHelper::getValue() 211 QPDFFormFieldObjectHelper::getValue()
212 { 212 {
213 - return Null::if_null(m->getValue());  
214 -}  
215 -  
216 -QPDFObjectHandle  
217 -FormField::getValue()  
218 -{  
219 - return inheritable_value<QPDFObjectHandle>("/V"); 213 + return Null::if_null(m->V<QPDFObjectHandle>());
220 } 214 }
221 215
222 std::string 216 std::string
223 QPDFFormFieldObjectHelper::getValueAsString() 217 QPDFFormFieldObjectHelper::getValueAsString()
224 { 218 {
225 - return m->getValueAsString(); 219 + return m->value();
226 } 220 }
227 221
228 std::string 222 std::string
229 -FormField::getValueAsString() 223 +FormField::value() const
230 { 224 {
231 return inheritable_string("/V"); 225 return inheritable_string("/V");
232 } 226 }
@@ -353,7 +347,7 @@ QPDFFormFieldObjectHelper::isChecked() @@ -353,7 +347,7 @@ QPDFFormFieldObjectHelper::isChecked()
353 bool 347 bool
354 FormField::isChecked() 348 FormField::isChecked()
355 { 349 {
356 - return isCheckbox() && Name(getValue()) != "/Off"; 350 + return isCheckbox() && V<Name>() != "/Off";
357 } 351 }
358 352
359 bool 353 bool
@@ -936,7 +930,7 @@ FormField::generateTextAppearance(QPDFAnnotationObjectHelper&amp; aoh) @@ -936,7 +930,7 @@ FormField::generateTextAppearance(QPDFAnnotationObjectHelper&amp; aoh)
936 } 930 }
937 QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle(); 931 QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle();
938 std::string DA = default_appearance(); 932 std::string DA = default_appearance();
939 - std::string V = getValueAsString(); 933 + std::string V = value();
940 std::vector<std::string> opt; 934 std::vector<std::string> opt;
941 if (isChoice() && (getFlags() & ff_ch_combo) == 0) { 935 if (isChoice() && (getFlags() & ff_ch_combo) == 0) {
942 opt = getChoices(); 936 opt = getChoices();
libqpdf/qpdf/FormField.hh
@@ -189,10 +189,59 @@ namespace qpdf::impl @@ -189,10 +189,59 @@ namespace qpdf::impl
189 /// if the mapping name is absent. 189 /// if the mapping name is absent.
190 std::string mapping_name() const; 190 std::string mapping_name() const;
191 191
192 - QPDFObjectHandle getValue(); 192 + /// @brief Retrieves the field value (`/V` attribute) of a specified field, accounting for
  193 + /// inheritance through thehierarchy of ancestor nodes in the form field tree.
  194 + ///
  195 + /// This function attempts to retrieve the `/V` attribute. If the `inherit`
  196 + /// parameter is set to `true` and the `/V` is not found at the current level, the
  197 + /// method traverses up the parent hierarchy to find the value. The traversal stops when
  198 + /// `/V` is found, when the root node is reached, or when a loop detection mechanism
  199 + /// prevents further traversal.
  200 + ///
  201 + /// @tparam T The return type.
  202 + /// @param inherit If set to `true`, the function will attempt to retrieve `/V` by
  203 + /// inheritance from the parent hierarchy of the form field. Defaults to `true`.
  204 + /// @return Returns the field's value if found; otherwise, returns a default-constructed
  205 + /// value of type `T`.
  206 + template <class T>
  207 + T
  208 + V(bool inherit = true) const
  209 + {
  210 + return inheritable_value<T>("/V", inherit);
  211 + }
193 212
194 - // Return the field's value as a string. If this is called with a field whose value is not a  
195 - std::string getValueAsString(); 213 + /// @brief Retrieves the field value (`/V` attribute) of a specified field, accounting for
  214 + /// inheritance through the hierarchy of ancestor nodes in the form field tree.
  215 + ///
  216 + /// This function attempts to retrieve the `/V` attribute. If the `inherit`
  217 + /// parameter is set to `true` and the `/V` is not found at the current level, the
  218 + /// method traverses up the parent hierarchy to find the value. The traversal stops when
  219 + /// `/V` is found, when the root node is reached, or when a loop detection mechanism
  220 + /// prevents further traversal.
  221 + ///
  222 + /// @param inherit If set to `true`, the function will attempt to retrieve `/V` by
  223 + /// inheritance from the parent hierarchy of the form field. Defaults to `true`.
  224 + /// @return Returns the field's value if found; otherwise, returns a default-constructed
  225 + /// object handle.
  226 + QPDFObjectHandle const&
  227 + V(bool inherit = true) const
  228 + {
  229 + if (auto& v = get("/V")) {
  230 + return v;
  231 + }
  232 + return {inherit ? inherited("/V") : null_oh};
  233 + }
  234 +
  235 + /// @brief Retrieves the field value `/V` attribute of the form field, considering
  236 + /// inheritance, if the value is a String.
  237 + ///
  238 + /// This function extracts the value of the form field, accounting for potential inheritance
  239 + /// through the form hierarchy. It returns the value if it is a String, and an empty string
  240 + /// otherwise.
  241 + ///
  242 + /// @return A string containing the actual or inherited `/V` attribute of the form field, or
  243 + /// an empty string if the value is not present or not a String.
  244 + std::string value() const;
196 245
197 QPDFObjectHandle getDefaultValue(); 246 QPDFObjectHandle getDefaultValue();
198 247