@@ -2367,8 +2367,8 @@ class PartialEvaluator {
2367
2367
return {
2368
2368
str,
2369
2369
dir : bidiResult . dir ,
2370
- width : textChunk . totalWidth ,
2371
- height : textChunk . totalHeight ,
2370
+ width : Math . abs ( textChunk . totalWidth ) ,
2371
+ height : Math . abs ( textChunk . totalHeight ) ,
2372
2372
transform : textChunk . transform ,
2373
2373
fontName : textChunk . fontName ,
2374
2374
hasEOL : textChunk . hasEOL ,
@@ -2459,7 +2459,11 @@ class PartialEvaluator {
2459
2459
if ( textState . font . vertical ) {
2460
2460
const advanceY = ( lastPosY - posY ) / textContentItem . textAdvanceScale ;
2461
2461
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 ) {
2463
2467
if (
2464
2468
Math . abs ( advanceX ) >
2465
2469
0.5 * textContentItem . width /* not the same column */
@@ -2476,15 +2480,21 @@ class PartialEvaluator {
2476
2480
appendEOL ( ) ;
2477
2481
return ;
2478
2482
}
2479
- if ( advanceY <= textContentItem . trackingSpaceMin ) {
2483
+ if ( advanceY <= textOrientation * textContentItem . trackingSpaceMin ) {
2480
2484
textContentItem . height += advanceY ;
2481
- } else if ( ! addFakeSpaces ( advanceY , textContentItem . prevTransform ) ) {
2485
+ } else if (
2486
+ ! addFakeSpaces (
2487
+ advanceY ,
2488
+ textContentItem . prevTransform ,
2489
+ textOrientation
2490
+ )
2491
+ ) {
2482
2492
if ( textContentItem . str . length === 0 ) {
2483
2493
textContent . items . push ( {
2484
2494
str : " " ,
2485
2495
dir : "ltr" ,
2486
2496
width : 0 ,
2487
- height : advanceY ,
2497
+ height : Math . abs ( advanceY ) ,
2488
2498
transform : textContentItem . prevTransform ,
2489
2499
fontName : textContentItem . fontName ,
2490
2500
hasEOL : false ,
@@ -2499,7 +2509,11 @@ class PartialEvaluator {
2499
2509
2500
2510
const advanceX = ( posX - lastPosX ) / textContentItem . textAdvanceScale ;
2501
2511
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 ) {
2503
2517
if (
2504
2518
Math . abs ( advanceY ) >
2505
2519
0.5 * textContentItem . height /* not the same line */
@@ -2516,14 +2530,16 @@ class PartialEvaluator {
2516
2530
return ;
2517
2531
}
2518
2532
2519
- if ( advanceX <= textContentItem . trackingSpaceMin ) {
2533
+ if ( advanceX <= textOrientation * textContentItem . trackingSpaceMin ) {
2520
2534
textContentItem . width += advanceX ;
2521
- } else if ( ! addFakeSpaces ( advanceX , textContentItem . prevTransform ) ) {
2535
+ } else if (
2536
+ ! addFakeSpaces ( advanceX , textContentItem . prevTransform , textOrientation )
2537
+ ) {
2522
2538
if ( textContentItem . str . length === 0 ) {
2523
2539
textContent . items . push ( {
2524
2540
str : " " ,
2525
2541
dir : "ltr" ,
2526
- width : advanceX ,
2542
+ width : Math . abs ( advanceX ) ,
2527
2543
height : 0 ,
2528
2544
transform : textContentItem . prevTransform ,
2529
2545
fontName : textContentItem . fontName ,
@@ -2652,10 +2668,10 @@ class PartialEvaluator {
2652
2668
}
2653
2669
}
2654
2670
2655
- function addFakeSpaces ( width , transf ) {
2671
+ function addFakeSpaces ( width , transf , textOrientation ) {
2656
2672
if (
2657
- textContentItem . spaceInFlowMin <= width &&
2658
- width <= textContentItem . spaceInFlowMax
2673
+ textOrientation * textContentItem . spaceInFlowMin <= width &&
2674
+ width <= textOrientation * textContentItem . spaceInFlowMax
2659
2675
) {
2660
2676
if ( textContentItem . initialized ) {
2661
2677
textContentItem . str . push ( " " ) ;
@@ -2677,8 +2693,8 @@ class PartialEvaluator {
2677
2693
// TODO: check if using the orientation from last chunk is
2678
2694
// better or not.
2679
2695
dir : "ltr" ,
2680
- width,
2681
- height,
2696
+ width : Math . abs ( width ) ,
2697
+ height : Math . abs ( height ) ,
2682
2698
transform : transf || getCurrentTextTransform ( ) ,
2683
2699
fontName,
2684
2700
hasEOL : false ,
0 commit comments