Skip to content

Commit 6cdae5a

Browse files
committed
Use positive dimensions for text chunks in the text layer (issue #14415).
1 parent 2ca432d commit 6cdae5a

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/core/evaluator.js

+31-15
Original file line numberDiff line numberDiff line change
@@ -2367,8 +2367,8 @@ class PartialEvaluator {
23672367
return {
23682368
str,
23692369
dir: bidiResult.dir,
2370-
width: textChunk.totalWidth,
2371-
height: textChunk.totalHeight,
2370+
width: Math.abs(textChunk.totalWidth),
2371+
height: Math.abs(textChunk.totalHeight),
23722372
transform: textChunk.transform,
23732373
fontName: textChunk.fontName,
23742374
hasEOL: textChunk.hasEOL,
@@ -2459,7 +2459,11 @@ class PartialEvaluator {
24592459
if (textState.font.vertical) {
24602460
const advanceY = (lastPosY - posY) / textContentItem.textAdvanceScale;
24612461
const advanceX = posX - lastPosX;
2462-
if (advanceY < textContentItem.negativeSpaceMax) {
2462+
2463+
// When the total height of the current chunk is negative
2464+
// then we're writing from bottom to top.
2465+
const textOrientation = Math.sign(textContentItem.height);
2466+
if (advanceY < textOrientation * textContentItem.negativeSpaceMax) {
24632467
if (
24642468
Math.abs(advanceX) >
24652469
0.5 * textContentItem.width /* not the same column */
@@ -2476,15 +2480,21 @@ class PartialEvaluator {
24762480
appendEOL();
24772481
return;
24782482
}
2479-
if (advanceY <= textContentItem.trackingSpaceMin) {
2483+
if (advanceY <= textOrientation * textContentItem.trackingSpaceMin) {
24802484
textContentItem.height += advanceY;
2481-
} else if (!addFakeSpaces(advanceY, textContentItem.prevTransform)) {
2485+
} else if (
2486+
!addFakeSpaces(
2487+
advanceY,
2488+
textContentItem.prevTransform,
2489+
textOrientation
2490+
)
2491+
) {
24822492
if (textContentItem.str.length === 0) {
24832493
textContent.items.push({
24842494
str: " ",
24852495
dir: "ltr",
24862496
width: 0,
2487-
height: advanceY,
2497+
height: Math.abs(advanceY),
24882498
transform: textContentItem.prevTransform,
24892499
fontName: textContentItem.fontName,
24902500
hasEOL: false,
@@ -2499,7 +2509,11 @@ class PartialEvaluator {
24992509

25002510
const advanceX = (posX - lastPosX) / textContentItem.textAdvanceScale;
25012511
const advanceY = posY - lastPosY;
2502-
if (advanceX < textContentItem.negativeSpaceMax) {
2512+
2513+
// When the total width of the current chunk is negative
2514+
// then we're writing from right to left.
2515+
const textOrientation = Math.sign(textContentItem.width);
2516+
if (advanceX < textOrientation * textContentItem.negativeSpaceMax) {
25032517
if (
25042518
Math.abs(advanceY) >
25052519
0.5 * textContentItem.height /* not the same line */
@@ -2516,14 +2530,16 @@ class PartialEvaluator {
25162530
return;
25172531
}
25182532

2519-
if (advanceX <= textContentItem.trackingSpaceMin) {
2533+
if (advanceX <= textOrientation * textContentItem.trackingSpaceMin) {
25202534
textContentItem.width += advanceX;
2521-
} else if (!addFakeSpaces(advanceX, textContentItem.prevTransform)) {
2535+
} else if (
2536+
!addFakeSpaces(advanceX, textContentItem.prevTransform, textOrientation)
2537+
) {
25222538
if (textContentItem.str.length === 0) {
25232539
textContent.items.push({
25242540
str: " ",
25252541
dir: "ltr",
2526-
width: advanceX,
2542+
width: Math.abs(advanceX),
25272543
height: 0,
25282544
transform: textContentItem.prevTransform,
25292545
fontName: textContentItem.fontName,
@@ -2652,10 +2668,10 @@ class PartialEvaluator {
26522668
}
26532669
}
26542670

2655-
function addFakeSpaces(width, transf) {
2671+
function addFakeSpaces(width, transf, textOrientation) {
26562672
if (
2657-
textContentItem.spaceInFlowMin <= width &&
2658-
width <= textContentItem.spaceInFlowMax
2673+
textOrientation * textContentItem.spaceInFlowMin <= width &&
2674+
width <= textOrientation * textContentItem.spaceInFlowMax
26592675
) {
26602676
if (textContentItem.initialized) {
26612677
textContentItem.str.push(" ");
@@ -2677,8 +2693,8 @@ class PartialEvaluator {
26772693
// TODO: check if using the orientation from last chunk is
26782694
// better or not.
26792695
dir: "ltr",
2680-
width,
2681-
height,
2696+
width: Math.abs(width),
2697+
height: Math.abs(height),
26822698
transform: transf || getCurrentTextTransform(),
26832699
fontName,
26842700
hasEOL: false,

test/pdfs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,4 @@
500500
!poppler-937-0-fuzzed.pdf
501501
!PDFBOX-3148-2-fuzzed.pdf
502502
!poppler-90-0-fuzzed.pdf
503+
!issue14415.pdf

test/pdfs/issue14415.pdf

52.6 KB
Binary file not shown.

test/test_manifest.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -6191,5 +6191,10 @@
61916191
"link": true,
61926192
"rounds": 1,
61936193
"type": "eq"
6194-
}
6194+
},
6195+
{ "id": "issue14415",
6196+
"file": "pdfs/issue14415.pdf",
6197+
"md5": "fa306a250a3d37fe0e7a4b3bba51c91e",
6198+
"type": "text"
6199+
}
61956200
]

0 commit comments

Comments
 (0)