Valery Bokov created PDFBOX-6339:
------------------------------------

             Summary: Surrogate pairs in right to left text
                 Key: PDFBOX-6339
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6339
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 3.0.8 PDFBox
            Reporter: Valery Bokov
             Fix For: 3.0.9 PDFBox, 4.0.0


### Problem

When `PDFTextStripper` reverses a right-to-left run in `handleDirection()`, 
characters outside the BMP are corrupted. For Hebrew alef, an emoji (U+1F600) 
and Hebrew bet in visual order, the result is

    05D1 DE00 D83D 05D0

instead of `05D1 D83D DE00 05D0`: the low surrogate comes before the high one, 
so the output contains two unpaired surrogates (shown as `?`). This affects any 
supplementary character in a right-to-left run, e.g. emoji in Arabic or Hebrew 
text, or right-to-left scripts outside the BMP such as Adlam.

### Cause

The run is reversed one `char` at a time (`word.charAt(end)` with `--end`), 
which swaps the two halves of each surrogate pair.

### Fix

The run is reversed one code point at a time, using `codePointBefore()` and 
`appendCodePoint()`. Mirroring is unchanged: `MIRRORING_CHAR_MAP` only has BMP 
characters, so it's only looked up for BMP code points, and the two nested 
`if`s are merged into one condition.

### Test

`TestTextStripper#testSurrogatePairInRightToLeftText` gives a Helvetica font a 
ToUnicode CMap that maps `a b c` to alef, U+1F600 and bet, reloads the 
document, and expects bet, U+1F600, alef with the surrogate pair intact. It 
fails without the fix. `TestTextStripper`, `BidiTest` and 
`PDFTextStripperByAreaTest` pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to