From 69f3cc783d12feaa4a32e8813dd7a4b96024fd27 Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Tue, 22 Oct 2024 16:26:46 +0100 Subject: [PATCH] src/__init__.py tests/: JM_get_widget_properties(): improved detection of field_label. Also recurse to parent if node's string value is empty string. This appears to be what Adobe does. Addresses #3950. --- src/__init__.py | 33 ++++++++++++++++++++++++++++++--- tests/test_widgets.py | 3 +-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 6e8afe44f..24a819494 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -15809,9 +15809,36 @@ def SETATTR_DROP(mod, key, value): field_name = mupdf.pdf_load_field_name(annot_obj) SETATTR_DROP(Widget, "field_name", field_name) - obj = mupdf.pdf_dict_get_inheritable(annot_obj, PDF_NAME('TU')) - if obj.m_internal: - label = mupdf.pdf_to_text_string(obj) + def pdf_dict_get_inheritable_nonempty_label(node, key): + ''' + This is a modified version of MuPDF's pdf_dict_get_inheritable(), with + some changes: + * Returns string from pdf_to_text_string() or None if not found. + * Recurses to parent if current node exists but with empty string + value. + ''' + slow = node + halfbeat = 11 # Don't start moving slow pointer for a while. + while 1: + if not node.m_internal: + return + val = mupdf.pdf_dict_get(node, key) + if val.m_internal: + label = mupdf.pdf_to_text_string(val) + if label: + return label + node = mupdf.pdf_dict_get(node, PDF_NAME('Parent')) + if node.m_internal == slow.m_internal: + raise Exception("cycle in resources") + halfbeat -= 1 + if halfbeat == 0: + slow = mupdf.pdf_dict_get(slow, PDF_NAME('Parent')) + halfbeat = 2 + + # In order to address #3950, we use our modified pdf_dict_get_inheritable() + # to ignore empty-string child values. + label = pdf_dict_get_inheritable_nonempty_label(annot_obj, PDF_NAME('TU')) + if label is not None: SETATTR_DROP(Widget, "field_label", label) fvalue = None diff --git a/tests/test_widgets.py b/tests/test_widgets.py index e5a9c62c2..d14ee8ccb 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -328,8 +328,7 @@ def test_3950(): items.append(widget.field_label) print(f'test_3950(): {widget.field_label=}.') assert items == [ - '', - #'{{ named_insured }}', + '{{ named_insured }}', '{{ policy_period_start_date }}', '{{ policy_period_end_date }}', '{{ insurance_line }}',