Fix bug in LayoutEngine.calculate(...)

The problem had been that `LayoutEngine.calculate(...)` was assuming
that len(char["text"]) would always equal 1, which is not true for
ligatures.

Thanks to @samkit-jain for finding a PDF that raised the error, and to
@bpugnaire for raising issue #683.
This commit is contained in:
Jeremy Singer-Vine
2022-07-14 17:21:55 -04:00
parent fd3f5334f8
commit 12feadb8fd
3 changed files with 24 additions and 1 deletions
+6
View File
@@ -2,6 +2,12 @@
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/).
## [0.7.2] - [unreleased]
### Fixed
- Fix bug in `LayoutEngine.calculate(...)` when processing char objects with len>1 representations, such as ligatures. ([#683](https://github.com/jsvine/pdfplumber/issues/683))
## [0.7.1] - 2022-05-31
### Fixed
+2 -1
View File
@@ -450,7 +450,8 @@ class LayoutEngine:
num_spaces_prepend = max(min(1, line_len), round(x_dist) - line_len)
rendered += [(" ", None)] * num_spaces_prepend
for c in chars:
rendered.append((c["text"], c))
for letter in c["text"]:
rendered.append((letter, c))
line_len += num_spaces_prepend + len(word["text"])
return rendered
+16
View File
@@ -198,3 +198,19 @@ class Test(unittest.TestCase):
with pdfplumber.open(path) as pdf:
annots = pdf.annots
annots[0]["contents"] == "日本語"
def test_issue_683(self):
"""
Page.search ValueError: min() arg is an empty sequence
This ultimately stemmed from a mistaken assumption in
LayoutEngine.calculate(...) that len(char["text"]) would always equal
1, which is not true for ligatures. Issue 683 does not provide a PDF,
but the test PDF triggers the same error, which should now be fixed.
Thank you to @samkit-jain for identifying and writing this test.
"""
path = os.path.join(HERE, "pdfs/issue-71-duplicate-chars-2.pdf")
with pdfplumber.open(path) as pdf:
page = pdf.pages[0]
page.search(r"\d+", regex=True)