Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB: Ignore non-numbers for width when building font width map #3158

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pypdf/_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ def build_font_width_map(
# C_first C_last same_W
en = second
width = w[2].get_object()
if not isinstance(width, (int, float)):
logger_warning(f"Expected numeric value for width, got {width}. Ignoring it.", __name__)
w = w[3:]
continue
for c_code in range(st, en + 1):
font_width_map[chr(c_code)] = width
w = w[3:]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,15 @@ def test_standard_encoding(caplog):
page = reader.pages[0]
assert page.extract_text() == "Lorem ipsum"
assert "Advanced encoding" not in caplog.text


@pytest.mark.enable_socket
def test_function_in_font_widths(caplog):
"""Tests for #3153"""
url = "https://github.com/user-attachments/files/18945709/Marseille_pypdf_level_0.2._compressed.pdf"
name = "issue3153.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))

page = reader.pages[455]
assert "La vulnérabilité correspond aux conséquences potentielles" in page.extract_text()
assert "Expected numeric value for width, got {'/Bounds': [0.25, 0.25]," in caplog.text
Loading