Skip to content

PDFBOX-5747: Surrogate pairs with combining diacritics are incorrectly ordered on text extraction #200

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions pdfbox/src/main/java/org/apache/pdfbox/text/TextPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,16 +759,25 @@ private void insertDiacritic(int i, TextPosition diacritic)
float[] widths2 = new float[widths.length + 1];
System.arraycopy(widths, 0, widths2, 0, i);

// First we add a zero-width entry for the diacritic in the widths array
widths2[i] = widths[i];
widths2[i + 1] = 0;
System.arraycopy(widths, i + 1, widths2, i + 2, widths.length - i - 1);

// Unicode combining diacritics always go after the base character, regardless of whether
// the string is in presentation order or logical order
sb.append(unicode.charAt(i));
widths2[i] = widths[i];

// If a surrogate starts at the current position, make sure we preserve it
if (i < unicode.length() - 1 && Character.isSurrogatePair(unicode.charAt(i), unicode.charAt(i + 1))) {
sb.append(unicode.charAt(i + 1));
i++;
}

sb.append(combineDiacritic(diacritic.getUnicode()));
widths2[i + 1] = 0;

// get the rest of the string
sb.append(unicode.substring(i + 1));
System.arraycopy(widths, i + 1, widths2, i + 2, widths.length - i - 1);

unicode = sb.toString();
widths = widths2;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Firefox file:///home/pablo/invchar
𝑋̂
1 of 1 18/12/2023, 12:49
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
𝑋̂
Firefox file:///home/pablo/invchar
1 of 1 18/12/2023, 12:49
Loading