Skip to content

Commit bd898fa

Browse files
committed
ENH: Tests: Test that we have added /ActualText with RTL appearance streams
1 parent 9331528 commit bd898fa

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

tests/test_appearance_stream.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def test_scale_text():
6666

6767
layout.rectangle = (0, 0, 160, 360)
6868
font_size = 0.0
69-
text = """Welcome to pypdf
69+
text = """Welcome to pypdf!
70+
أهلاً بكم في pypdf!
7071
pypdf is a free and open source pure-python PDF library capable of splitting, merging, cropping, and
7172
transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF
7273
files. pypdf can retrieve text and metadata from PDFs as well.
@@ -79,12 +80,13 @@ def test_scale_text():
7980
)
8081
assert b"12 Tf" in appearance_stream.get_data()
8182
assert b"pypdf is a free and open" in appearance_stream.get_data()
83+
assert b"/Span << /ActualText" in appearance_stream.get_data()
8284

8385
layout.rectangle = (0, 0, 160, 160)
8486
appearance_stream = TextStreamAppearance(
8587
layout=layout, text=text, font_size=font_size, is_multiline=is_multiline
8688
)
87-
assert b"9.8 Tf" in appearance_stream.get_data()
89+
assert b"9.6 Tf" in appearance_stream.get_data()
8890

8991
layout.rectangle = (0, 0, 160, 12)
9092
appearance_stream = TextStreamAppearance(
@@ -142,7 +144,12 @@ def test_appearance_stream_rtl():
142144
font_color="0 g",
143145
is_multiline=False
144146
)
145-
hex_glyphs_rtl_enabled = re.findall("<(.+?)>", appearance.get_data().decode())[0]
147+
# The regex returns two groups. The first matches the text in /Span << /ActualText <[group 0]> >> BDC
148+
# The second matches the encoded text data.
149+
decoded_data = re.findall("<([a-zA-Z0-9]+?)> ", appearance.get_data().decode())
150+
actual_text = bytes.fromhex(decoded_data[0]).decode("utf-16-be")
151+
assert actual_text == "\ufeff" + test_string
152+
hex_glyphs_rtl_enabled = decoded_data[1]
146153
assert hex_shaped_test_glyphs == hex_glyphs_rtl_enabled
147154

148155
# RTL support disabled
@@ -157,7 +164,7 @@ def test_appearance_stream_rtl():
157164
font_color="0 g",
158165
is_multiline=False
159166
)
160-
hex_glyphs_rtl_disabled = re.findall("<(.+?)>", appearance.get_data().decode())[0]
167+
hex_glyphs_rtl_disabled = re.findall("<(.+?)>", appearance.get_data().decode())[1]
161168
assert hex_unshaped_test_glyphs == hex_glyphs_rtl_disabled
162169
# The hex glyph sequences should be different when RTL support is enabled vs disabled
163170
assert hex_glyphs_rtl_enabled != hex_glyphs_rtl_disabled

0 commit comments

Comments
 (0)