diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 006998f48..7c97d2141 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -2535,6 +2535,16 @@ class PartialEvaluator { let textState, currentTextState; let lastFakeSpaceExtraCharId = -1; + // For a reflected transform with d > 0, move the origin along the baseline + // by the item width, then reverse the baseline direction. + function getUnmirroredTransform([a, b, c, d, e, f], width) { + if (a * d - b * c >= 0 || d <= 0) { + return null; + } + const scale = width / Math.hypot(a, b); + return [-a, -b, c, d, e + a * scale, f + b * scale]; + } + function pushWhitespace({ width = 0, height = 0, @@ -2542,6 +2552,9 @@ class PartialEvaluator { fontName = textContentItem.fontName, }) { intersector?.addExtraChar(" "); + if (width) { + transform = getUnmirroredTransform(transform, width) || transform; + } textContent.items.push({ str: " ", dir: "ltr", @@ -2679,13 +2692,21 @@ class PartialEvaluator { } function runBidiTransform(textChunk) { - const bidiResult = bidi(textChunk.str, -1, textChunk.vertical); + let { str, transform } = textChunk; + const width = Math.abs(textChunk.totalWidth); + const unmirrored = + !textChunk.vertical && getUnmirroredTransform(transform, width); + if (unmirrored) { + str = str.toReversed(); + transform = unmirrored; + } + const bidiResult = bidi(str, -1, textChunk.vertical); return { str: bidiResult.str, dir: bidiResult.dir, - width: Math.abs(textChunk.totalWidth), + width, height: Math.abs(textChunk.totalHeight), - transform: textChunk.transform, + transform, fontName: textChunk.fontName, hasEOL: textChunk.hasEOL, }; @@ -3106,13 +3127,14 @@ class PartialEvaluator { for (let i = 0, ii = glyphs.length; i < ii; i++) { const glyph = glyphs[i]; const { category } = glyph; + const glyphWidth = font.vertical ? glyph.vmetric[0] : glyph.width; + const { isInvisibleFormatMark } = category; - if (category.isInvisibleFormatMark) { + if (isInvisibleFormatMark && !glyphWidth) { continue; } let charSpacing = baseCharSpacing + (i + 1 === ii ? extraSpacing : 0); - const glyphWidth = font.vertical ? glyph.vmetric[0] : glyph.width; let scaledDim = glyphWidth * scale; if (glyph.isSpace) { @@ -3159,13 +3181,14 @@ class PartialEvaluator { scaledDim = 0; } + const glyphUnicode = isInvisibleFormatMark ? "" : glyph.unicode; if (!font.vertical) { scaledDim *= textState.textHScale; intersector?.addGlyph( getCurrentTextTransform(), scaledDim, 0, - glyph.unicode + glyphUnicode ); textState.translateTextMatrix(scaledDim, 0); textChunk.width += scaledDim; @@ -3174,7 +3197,7 @@ class PartialEvaluator { getCurrentTextTransform(), 0, scaledDim, - glyph.unicode + glyphUnicode ); textState.translateTextMatrix(0, scaledDim); scaledDim = Math.abs(scaledDim); @@ -3182,27 +3205,25 @@ class PartialEvaluator { } if (scaledDim) { - // Save the position of the last visible character. + // Save the position after advancing by this glyph's width. textChunk.prevTransform = getCurrentTextTransform(); textChunk.prevGlyphWidth = scaledDim; textChunk.prevTextRise = textState.textRise; } - const glyphUnicode = glyph.unicode; - if (saveLastChar(glyphUnicode)) { - // The two last chars are a non-whitespace followed by a whitespace - // and then this non-whitespace, so we insert a whitespace here. - // Replaces all whitespaces with standard spaces (0x20), to avoid - // alignment issues between the textLayer and the canvas if the text - // contains e.g. tabs (fixes issue6612.pdf). - textChunk.str.push(" "); - intersector?.addExtraChar(" "); - } + if (!isInvisibleFormatMark) { + if (saveLastChar(glyphUnicode)) { + // Insert a standard space (U+0020) between non-whitespace glyphs + // when the preceding whitespace was skipped above. + textChunk.str.push(" "); + intersector?.addExtraChar(" "); + } - if (!intersector) { - textChunk.str.push( - disableNormalization ? glyphUnicode : glyph.normalizedUnicode - ); + if (!intersector) { + textChunk.str.push( + disableNormalization ? glyphUnicode : glyph.normalizedUnicode + ); + } } if (charSpacing) { diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 9bc6f6be0..cb1a9d806 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -1071,3 +1071,4 @@ !structure_form_xobject_mcid.pdf !bug2021583.pdf !issue16880.pdf +!issue16754.pdf diff --git a/test/pdfs/issue16754.pdf b/test/pdfs/issue16754.pdf new file mode 100644 index 000000000..4a199f018 --- /dev/null +++ b/test/pdfs/issue16754.pdf @@ -0,0 +1,58 @@ +%PDF-1.7 +1 0 obj +<< /Type /Catalog /Pages 2 0 R >> +endobj +2 0 obj +<< /Type /Pages /Kids [3 0 R] /Count 1 >> +endobj +3 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 250 150] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >> +endobj +4 0 obj +<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /FirstChar 97 /LastChar 101 /Widths [556 556 500 556 556] /ToUnicode 6 0 R >> +endobj +5 0 obj +<< /Length 74 >> +stream +BT /F1 20 Tf 1 0 0 1 20 100 Tm (abcd) Tj -1 0 0 1 200 100 Tm (abcde) Tj ET +endstream +endobj +6 0 obj +<< /Length 379 >> +stream +/CIDInit /ProcSet findresource begin +12 dict begin +begincmap +/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def +/CMapName /Adobe-Identity-UCS def +/CMapType 2 def +1 begincodespacerange +<00> +endcodespacerange +5 beginbfchar +<61> <05D1> +<62> <05D0> +<63> <05D7> +<64> <05E8> +<65> <200E> +endbfchar +endcmap +CMapName currentdict /CMap defineresource pop +end +end +endstream +endobj +xref +0 7 +0000000000 65535 f +0000000009 00000 n +0000000058 00000 n +0000000115 00000 n +0000000241 00000 n +0000000386 00000 n +0000000510 00000 n +trailer +<< /Size 7 /Root 1 0 R >> +startxref +940 +%%EOF diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index fb01fc60f..72cdd33c2 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -5878,6 +5878,26 @@ have written that much by now. So, here’s to squashing bugs.`); await loadingTask.destroy(); }); + it("gets text content with mirrored RTL text (issue 16754)", async function () { + const loadingTask = getDocument(buildGetDocumentParams("issue16754.pdf")); + const pdfDoc = await loadingTask.promise; + const pdfPage = await pdfDoc.getPage(1); + const { items } = await pdfPage.getTextContent(); + // The same Hebrew character sequence uses normal and mirrored text + // matrices; the mirrored run ends with an LRM (U+200E). + expect(items.map(({ str, dir }) => ({ str, dir }))).toEqual([ + { str: "רחאב", dir: "rtl" }, + { str: "באחר", dir: "rtl" }, + ]); + const { transform, width } = items[1]; + [20, 0, 0, 20, 145.52, 100].forEach((value, i) => { + expect(transform[i]).toBeCloseTo(value, 2); + }); + expect(width).toBeCloseTo(54.48, 2); + + await loadingTask.destroy(); + }); + it("gets empty structure tree", async function () { const tree = await page.getStructTree();