Commit 263afd976d7d2c033e215f59efb0381a01e7f18f

Authored by m-holger
1 parent 87ff43f4

Refactor `FormField`: replace `getDefaultValue` and `getDefaultValueAsString` wi…

…th `DV` and `default_value` methods. Simplify logic, improve clarity, and update references.
libqpdf/QPDFFormFieldObjectHelper.cc
@@ -228,23 +228,17 @@ FormField::value() const @@ -228,23 +228,17 @@ FormField::value() const
228 QPDFObjectHandle 228 QPDFObjectHandle
229 QPDFFormFieldObjectHelper::getDefaultValue() 229 QPDFFormFieldObjectHelper::getDefaultValue()
230 { 230 {
231 - return Null::if_null(m->getDefaultValue());  
232 -}  
233 -  
234 -QPDFObjectHandle  
235 -FormField::getDefaultValue()  
236 -{  
237 - return inheritable_value<QPDFObjectHandle>("/DV"); 231 + return Null::if_null(m->DV());
238 } 232 }
239 233
240 std::string 234 std::string
241 QPDFFormFieldObjectHelper::getDefaultValueAsString() 235 QPDFFormFieldObjectHelper::getDefaultValueAsString()
242 { 236 {
243 - return m->getDefaultValueAsString(); 237 + return m->default_value();
244 } 238 }
245 239
246 std::string 240 std::string
247 -FormField::getDefaultValueAsString() 241 +FormField::default_value() const
248 { 242 {
249 return inheritable_string("/DV"); 243 return inheritable_string("/DV");
250 } 244 }
libqpdf/qpdf/FormField.hh
@@ -185,8 +185,8 @@ namespace qpdf::impl @@ -185,8 +185,8 @@ namespace qpdf::impl
185 /// If the mapping name (/TM) is present, it is returned as a UTF-8 string. If not, it falls 185 /// If the mapping name (/TM) is present, it is returned as a UTF-8 string. If not, it falls
186 /// back to the 'alternative name', which is obtained using the `alternative_name()` method. 186 /// back to the 'alternative name', which is obtained using the `alternative_name()` method.
187 /// 187 ///
188 - /// @return The mapping field name (/TM) as a UTF-8 string or the alternative name  
189 - /// if the mapping name is absent. 188 + /// @return The mapping field name (/TM) as a UTF-8 string or the alternative name if the
  189 + /// mapping name is absent.
190 std::string mapping_name() const; 190 std::string mapping_name() const;
191 191
192 /// @brief Retrieves the field value (`/V` attribute) of a specified field, accounting for 192 /// @brief Retrieves the field value (`/V` attribute) of a specified field, accounting for
@@ -243,11 +243,40 @@ namespace qpdf::impl @@ -243,11 +243,40 @@ namespace qpdf::impl
243 /// an empty string if the value is not present or not a String. 243 /// an empty string if the value is not present or not a String.
244 std::string value() const; 244 std::string value() const;
245 245
246 - QPDFObjectHandle getDefaultValue(); 246 + /// @brief Retrieves the field default value (`/DV` attribute) of a specified field,
  247 + /// accounting for inheritance through the hierarchy of ancestor nodes in the form
  248 + /// field tree.
  249 + ///
  250 + /// This function attempts to retrieve the `/DV` attribute. If the `inherit` parameter is
  251 + /// set to `true` and the `/DV` is not found at the current level, the method traverses up
  252 + /// the parent hierarchy to find the value. The traversal stops when
  253 + /// `/DV` is found, when the root node is reached, or when a loop detection mechanism
  254 + /// prevents further traversal.
  255 + ///
  256 + /// @tparam T The return type.
  257 + /// @param inherit If set to `true`, the function will attempt to retrieve `/DV` by
  258 + /// inheritance from the parent hierarchy of the form field. Defaults to `true`.
  259 + /// @return Returns the field's default value if found; otherwise, returns a
  260 + /// default-constructed value of type `T`.
  261 + QPDFObjectHandle const&
  262 + DV(bool inherit = true) const
  263 + {
  264 + if (auto& v = get("/DV")) {
  265 + return v;
  266 + }
  267 + return {inherit ? inherited("/DV") : null_oh};
  268 + }
247 269
248 - // Return the field's default value as a string. If this is called with a field whose value  
249 - // is not a string, the empty string will be silently returned.  
250 - std::string getDefaultValueAsString(); 270 + /// @brief Retrieves the default value `/DV` attribute of the form field, considering
  271 + /// inheritance, if the default value is a String.
  272 + ///
  273 + /// This function extracts the default value of the form field, accounting for potential
  274 + /// inheritance through the form hierarchy. It returns the value if it is a String, and an
  275 + /// empty string otherwise.
  276 + ///
  277 + /// @return A string containing the actual or inherited `/V` attribute of the form field, or
  278 + /// an empty string if the value is not present or not a String.
  279 + std::string default_value() const;
251 280
252 /// @brief Returns the default appearance string for the form field, considering inheritance 281 /// @brief Returns the default appearance string for the form field, considering inheritance
253 /// from the field tree hierarchy and the document's /AcroForm dictionary. 282 /// from the field tree hierarchy and the document's /AcroForm dictionary.