Skip to content

Commit

Permalink
STY: Run black
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Dec 25, 2023
1 parent eb3e22d commit cfb3f93
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/user/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ The PDF form stores form fields as annotations with the subtype "\Widget". This

```python
from pypdf import PdfReader

reader = PdfReader("form.pdf")
fields = reader.get_fields()
```

```python
from pypdf import PdfReader
from pypdf.constants import AnnotationDictionaryAttributes

reader = PdfReader("form.pdf")
fields = []
for page in reader.pages:
Expand Down
1 change: 0 additions & 1 deletion pypdf/_text_extraction/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ def handle_tj(
rtl_dir: bool,
visitor_text: Optional[Callable[[Any, Any, Any, Any, Any], None]],
) -> Tuple[str, bool]:

m = mult(tm_matrix, cm_matrix)
orientation = orient(m)
if orientation in orientations and len(operands) > 0:
Expand Down
8 changes: 6 additions & 2 deletions pypdf/_xobj_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,13 @@ def bits2byte(data: bytes, size: Tuple[int, int], bits: int) -> bytes:
expected_count = 2 * nb
if len(lookup) != expected_count:
if len(lookup) < expected_count:
raise PdfReadError(f"Not enough lookup values: Expected {expected_count}, got {len(lookup)}.")
raise PdfReadError(
f"Not enough lookup values: Expected {expected_count}, got {len(lookup)}."
)
if not check_if_whitespace_only(lookup[expected_count:]):
raise PdfReadError(f"Too many lookup values: Expected {expected_count}, got {len(lookup)}.")
raise PdfReadError(
f"Too many lookup values: Expected {expected_count}, got {len(lookup)}."
)
lookup = lookup[:expected_count]
colors_arr = [lookup[:nb], lookup[nb:]]
arr = b"".join(
Expand Down
54 changes: 41 additions & 13 deletions tests/test_xobject_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ def test_handle_flate__image_mode_1():
data = b"\x00\xe0\x00"
lookup = DecodedStreamObject()
expected_data = [
(66, 66, 66), (66, 66, 66), (66, 66, 66),
(0, 19, 55), (0, 19, 55), (0, 19, 55),
(66, 66, 66), (66, 66, 66), (66, 66, 66)
(66, 66, 66),
(66, 66, 66),
(66, 66, 66),
(0, 19, 55),
(0, 19, 55),
(0, 19, 55),
(66, 66, 66),
(66, 66, 66),
(66, 66, 66),
]

# No trailing data.
Expand All @@ -44,9 +50,11 @@ def test_handle_flate__image_mode_1():
size=(3, 3),
data=data,
mode="1",
color_space=ArrayObject([NameObject("/Indexed"), NameObject("/DeviceRGB"), NumberObject(1), lookup]),
color_space=ArrayObject(
[NameObject("/Indexed"), NameObject("/DeviceRGB"), NumberObject(1), lookup]
),
colors=2,
obj_as_text="dummy"
obj_as_text="dummy",
)
assert expected_data == list(result[0].getdata())

Expand All @@ -56,32 +64,52 @@ def test_handle_flate__image_mode_1():
size=(3, 3),
data=data,
mode="1",
color_space=ArrayObject([NameObject("/Indexed"), NameObject("/DeviceRGB"), NumberObject(1), lookup]),
color_space=ArrayObject(
[NameObject("/Indexed"), NameObject("/DeviceRGB"), NumberObject(1), lookup]
),
colors=2,
obj_as_text="dummy"
obj_as_text="dummy",
)
assert expected_data == list(result[0].getdata())

# Trailing non-whitespace character.
lookup.set_data(b"\x42\x42\x42\x00\x13\x37\x12")
with pytest.raises(PdfReadError, match=r"^Too many lookup values: Expected 6, got 7\.$"):
with pytest.raises(
PdfReadError, match=r"^Too many lookup values: Expected 6, got 7\.$"
):
_handle_flate(
size=(3, 3),
data=data,
mode="1",
color_space=ArrayObject([NameObject("/Indexed"), NameObject("/DeviceRGB"), NumberObject(1), lookup]),
color_space=ArrayObject(
[
NameObject("/Indexed"),
NameObject("/DeviceRGB"),
NumberObject(1),
lookup,
]
),
colors=2,
obj_as_text="dummy"
obj_as_text="dummy",
)

# Not enough lookup data.
lookup.set_data(b"\x42\x42\x42\x00\x13")
with pytest.raises(PdfReadError, match=r"^Not enough lookup values: Expected 6, got 5\.$"):
with pytest.raises(
PdfReadError, match=r"^Not enough lookup values: Expected 6, got 5\.$"
):
_handle_flate(
size=(3, 3),
data=data,
mode="1",
color_space=ArrayObject([NameObject("/Indexed"), NameObject("/DeviceRGB"), NumberObject(1), lookup]),
color_space=ArrayObject(
[
NameObject("/Indexed"),
NameObject("/DeviceRGB"),
NumberObject(1),
lookup,
]
),
colors=2,
obj_as_text="dummy"
obj_as_text="dummy",
)

0 comments on commit cfb3f93

Please sign in to comment.