Skip to content

Commit 85eed31

Browse files
Align TextFinder positions with character offsets (#7217)
Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
1 parent ae12932 commit 85eed31

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

app/core/src/main/java/stirling/software/SPDF/pdf/TextFinder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@ protected void startPage(PDPage page) throws IOException {
4747
@Override
4848
protected void writeString(String text, List<TextPosition> textPositions) {
4949
pageTextBuilder.append(text);
50-
pageTextPositions.addAll(textPositions);
50+
// Matches are located by indexing pageTextPositions with offsets into
51+
// pageTextBuilder, so the two must stay aligned character-for-character. A
52+
// TextPosition can decode to more than one character (ligatures such as "fi",
53+
// or any glyph with a multi-character ToUnicode mapping), so repeat it once
54+
// per character it contributes instead of adding it a single time.
55+
for (TextPosition position : textPositions) {
56+
String unicode = position != null ? position.getUnicode() : null;
57+
int charCount = unicode != null ? Math.max(1, unicode.length()) : 1;
58+
for (int i = 0; i < charCount; i++) {
59+
pageTextPositions.add(position);
60+
}
61+
}
5162
}
5263

5364
@Override

0 commit comments

Comments
 (0)