Skip to content

Commit

Permalink
Fixed the bug where button_states() returns None when /AP points …
Browse files Browse the repository at this point in the history
…to an indirect object.

Description: Some PDF widget objects may not have their `/AP`(Appearance Dictionary) directly pointing to a dictionary, which is enclosed with '<< >>', but rather pointing to an indirect object, expressed by an xref number in the format 'NNN 0 R'. Therefore, the current button_states() only can correctly handle cases where `/AP` points to a dictionary and cannot handle cases where it points to an indirect object. Consequently, I have introduced additional logic to handle the latter scenario, ensuring that `button_states()` can accurately return the on/off state names for button widgets even when `/AP` points to an indirect object.
Test: I have tested the modified `button_states()` function on the mentioned type of PDF, and it now correctly returns the states instead of None. I have copied the method's code to the same-named method in `src/__init__.py`.
  • Loading branch information
airgalss authored and JorjMcKie committed Dec 15, 2023
1 parent 851fe7b commit 50ce442
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions fitz/helper-fields.i
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,14 @@ class Widget(object):
for x in apnt:
nstates.append(x.split()[0])
states["normal"] = nstates
if APN[0] == "xref":
nstates = []
nxref = int(APN[1].split(" ")[0])
APN = doc.xref_object(nxref)
apnt = APN.split("/")[1:]
for x in apnt:
nstates.append(x.split()[0])
states["normal"] = nstates
APD = doc.xref_get_key(xref, "AP/D")
if APD[0] == "dict":
dstates = []
Expand All @@ -1105,6 +1113,14 @@ class Widget(object):
for x in apdt:
dstates.append(x.split()[0])
states["down"] = dstates
if APD[0] == "xref":
dstates = []
dxref = int(APD[1].split(" ")[0])
APD = doc.xref_object(dxref)
apdt = APD.split("/")[1:]
for x in apdt:
dstates.append(x.split()[0])
states["down"] = dstates
return states

def on_state(self):
Expand Down
16 changes: 16 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6992,6 +6992,14 @@ def button_states(self):
for x in apnt:
nstates.append(x.split()[0])
states["normal"] = nstates
if APN[0] == "xref":
nstates = []
nxref = int(APN[1].split(" ")[0])
APN = doc.xref_object(nxref)
apnt = APN.split("/")[1:]
for x in apnt:
nstates.append(x.split()[0])
states["normal"] = nstates
APD = doc.xref_get_key(xref, "AP/D")
if APD[0] == "dict":
dstates = []
Expand All @@ -7000,6 +7008,14 @@ def button_states(self):
for x in apdt:
dstates.append(x.split()[0])
states["down"] = dstates
if APD[0] == "xref":
dstates = []
dxref = int(APD[1].split(" ")[0])
APD = doc.xref_object(dxref)
apdt = APD.split("/")[1:]
for x in apdt:
dstates.append(x.split()[0])
states["down"] = dstates
return states

@property
Expand Down

0 comments on commit 50ce442

Please sign in to comment.