Skip to content

Commit 5ea72b0

Browse files
committed
ENH: tests: _appearance_stream
Add tests for the TextStreamAppearance.
1 parent 3d00f94 commit 5ea72b0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/test_appearance_stream.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Test the pypdf.generic._appearance_stream module."""
2+
3+
from pypdf.generic._appearance_stream import TextStreamAppearance
4+
5+
6+
def test_scale_text():
7+
rect = (0, 0, 9.1, 55.4)
8+
font_size = 10.1
9+
text = "Hello World"
10+
multiline = False
11+
appearance_stream = TextStreamAppearance(
12+
text, rect=rect, font_size=font_size, multiline=multiline
13+
)
14+
assert (str(font_size) + r" Tf").encode() in appearance_stream.get_data()
15+
text = "This is a very very long sentence that probably will scale below the minimum font size"
16+
font_size = 0.0
17+
appearance_stream = TextStreamAppearance(
18+
text, rect=rect, font_size=font_size, multiline=multiline
19+
)
20+
assert (b"4.0 Tf") in appearance_stream.get_data()
21+
rect = (0, 0, 160, 360)
22+
font_size = 0.0
23+
text = """Welcome to pypdf
24+
pypdf is a free and open source pure-python PDF library capable of splitting, merging, cropping, and
25+
transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF
26+
files. pypdf can retrieve text and metadata from PDFs as well.
27+
28+
See pdfly for a CLI application that uses pypdf to interact with PDFs.
29+
"""
30+
multiline = True
31+
appearance_stream = TextStreamAppearance(
32+
text, rect=rect, font_size=font_size, multiline=multiline
33+
)
34+
assert (b"12 Tf") in appearance_stream.get_data()
35+
assert b"pypdf is a free and open" in appearance_stream.get_data()
36+
rect = (0, 0, 160, 160)
37+
appearance_stream = TextStreamAppearance(
38+
text, rect=rect, font_size=font_size, multiline=multiline
39+
)
40+
assert (b"8.8 Tf") in appearance_stream.get_data()
41+
rect = (0, 0, 160, 12)
42+
appearance_stream = TextStreamAppearance(
43+
text, rect=rect, font_size=font_size, multiline=multiline
44+
)
45+
text = """Option A
46+
Option B
47+
Option C
48+
Option D
49+
"""
50+
selection = "Option A"
51+
assert (b"4.0 Tf") in appearance_stream.get_data()
52+
text = "pneumonoultramicroscopicsilicovolcanoconiosis"
53+
appearance_stream = TextStreamAppearance(
54+
text, selection, rect=rect, font_size=font_size, multiline=multiline
55+
)
56+
assert (b"7.2 Tf") in appearance_stream.get_data()
57+
rect = (0, 0, 10, 100)
58+
text = "OneWord"
59+
appearance_stream = TextStreamAppearance(
60+
text, rect=rect, font_size=font_size, multiline=multiline
61+
)
62+
assert (b"OneWord") in appearance_stream.get_data()

0 commit comments

Comments
 (0)