Skip to content

Commit

Permalink
src/__init__.py tests/: JM_get_widget_properties(): improved detectio…
Browse files Browse the repository at this point in the history
…n of field_label.

Also recurse to parent if node's string value is empty string. This appears to
be what Adobe does.

Addresses #3950.
  • Loading branch information
julian-smith-artifex-com committed Oct 22, 2024
1 parent 1e6e763 commit 69f3cc7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
33 changes: 30 additions & 3 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}',
Expand Down

0 comments on commit 69f3cc7

Please sign in to comment.