From 9ed078f62dd4f0d57c4d7d8862b3b609576be4cf Mon Sep 17 00:00:00 2001 From: Thomas Leitner Date: Tue, 17 Sep 2024 22:54:20 +0200 Subject: [PATCH] Set the quadding of a variable text field by default on creation The quadding /Q of a field has a default value, so it seems that it is not necessary to explicitly set the field to its default value of 0. There are (at least) two reasons why it still makes sense to set it explicitly: * In certain circumstances [1] the default value for an unset /Q might be taken from somewhere else. * When HexaPDF generates an appearance, it retrieves the value of the quadding via field.text_alignment which automatically sets /Q to its default value if it is not set. In case of adding a field to a PDF but *not* filling it (so no explicit /Q value), then signing the PDF, then filling in the field and again signing it, the initial signature will be invalided by the now explicitly set /Q. [1]: https://github.com/gettalong/hexapdf/issues/89#issuecomment-548716180 --- CHANGELOG.md | 2 +- lib/hexapdf/type/acro_form/form.rb | 2 +- test/hexapdf/type/acro_form/test_form.rb | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6688a43c..0314865c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Changed * [HexaPDF::Type::AcroForm::Form] text-like field creation methods to always set - a default appearance string + a default appearance string and the quadding * Convenience methods for accessing resources to not add the deprecated /ProcSet entry by default * [HexaPDF::DigitalSignature::CMSHandler] to add informational output regarding diff --git a/lib/hexapdf/type/acro_form/form.rb b/lib/hexapdf/type/acro_form/form.rb index dcecaee8..26223616 100644 --- a/lib/hexapdf/type/acro_form/form.rb +++ b/lib/hexapdf/type/acro_form/form.rb @@ -558,7 +558,7 @@ def apply_variable_text_properties(field, font: nil, font_options: nil, font_siz font_options: font_options || {}, font_size: font_size || 0, font_color: font_color || 0) - field.text_alignment(align) if align + field.text_alignment(align || :left) end def perform_validation # :nodoc: diff --git a/test/hexapdf/type/acro_form/test_form.rb b/test/hexapdf/type/acro_form/test_form.rb index c9dfd34e..e04942f8 100644 --- a/test/hexapdf/type/acro_form/test_form.rb +++ b/test/hexapdf/type/acro_form/test_form.rb @@ -164,6 +164,7 @@ def applies_variable_text_properties(method, **args) assert_equal(:'Helvetica', @acro_form.default_resources.font(font_name)[:BaseFont]) assert_equal(0, font_size) assert_equal(HexaPDF::Content::ColorSpace::DeviceGray.new.color(0), font_color) + assert_equal(0, field.value[:Q]) field = @acro_form.send(method, "field", **args, font: 'Times') font_name, font_size, font_color = field.parse_default_appearance_string