@@ -367,6 +367,7 @@ class QuoteGenerate {
367367 const breakMatch = / < b r > | \n | \r /
368368 const spaceMatch = / [ \f \n \r \t \v \u0020 \u1680 \u2000 - \u200a \u2028 \u2029 \u205f \u3000 ] /
369369 const CJKMatch = / [ \u1100 - \u11ff \u2e80 - \u2eff \u3000 - \u303f \u3040 - \u309f \u30a0 - \u30ff \u3100 - \u312f \u3130 - \u318f \u3190 - \u319f \u31a0 - \u31bf \u31c0 - \u31ef \u31f0 - \u31ff \u3200 - \u32ff \u3300 - \u33ff \u3400 - \u4dbf \u4e00 - \u9fff \uac00 - \ud7af \uf900 - \ufaff ] /
370+ const RTLMatch = / [ \u0591 - \u07FF \u200F \u202B \u202E \uFB1D - \uFDFD \uFE70 - \uFEFC ] /
370371
371372 for ( let index = 0 ; index < styledChar . length ; index ++ ) {
372373 const charStyle = styledChar [ index ]
@@ -449,6 +450,7 @@ class QuoteGenerate {
449450 }
450451
451452 let breakWrite = false
453+ let lineDirection = styledWords [ 0 ] . word . match ( RTLMatch ) ? 'rtl' : 'ltr'
452454 for ( let index = 0 ; index < styledWords . length ; index ++ ) {
453455 const styledWord = styledWords [ index ]
454456
@@ -533,6 +535,11 @@ class QuoteGenerate {
533535
534536 lineX = textX
535537 lineY += lineHeight
538+ if ( index < styledWords . length - 1 ) {
539+ let nextLineDirection = styledWords [ index + 1 ] . word . match ( RTLMatch ) ? 'rtl' : 'ltr'
540+ if ( lineDirection != nextLineDirection ) textWidth = maxWidth - fontSize * 2
541+ lineDirection = nextLineDirection
542+ }
536543 }
537544 }
538545
@@ -541,13 +548,15 @@ class QuoteGenerate {
541548 if ( lineWidth > textWidth ) textWidth = lineWidth
542549 if ( textWidth > maxWidth ) textWidth = maxWidth
543550
551+ let wordX = ( lineDirection == 'rtl' ) ? maxWidth - lineX - wordlWidth - fontSize * 2 : lineX
552+
544553 if ( emojiImage ) {
545- canvasCtx . drawImage ( emojiImage , lineX , lineY - fontSize + ( fontSize * 0.15 ) , fontSize + ( fontSize * 0.22 ) , fontSize + ( fontSize * 0.22 ) )
554+ canvasCtx . drawImage ( emojiImage , wordX , lineY - fontSize + ( fontSize * 0.15 ) , fontSize + ( fontSize * 0.22 ) , fontSize + ( fontSize * 0.22 ) )
546555 } else {
547- canvasCtx . fillText ( styledWord . word , lineX , lineY )
556+ canvasCtx . fillText ( styledWord . word , wordX , lineY )
548557
549- if ( styledWord . style . includes ( 'strikethrough' ) ) canvasCtx . fillRect ( lineX , lineY - fontSize / 2.8 , canvasCtx . measureText ( styledWord . word ) . width , fontSize * 0.1 )
550- if ( styledWord . style . includes ( 'underline' ) ) canvasCtx . fillRect ( lineX , lineY + 2 , canvasCtx . measureText ( styledWord . word ) . width , fontSize * 0.1 )
558+ if ( styledWord . style . includes ( 'strikethrough' ) ) canvasCtx . fillRect ( wordX , lineY - fontSize / 2.8 , canvasCtx . measureText ( styledWord . word ) . width , fontSize * 0.1 )
559+ if ( styledWord . style . includes ( 'underline' ) ) canvasCtx . fillRect ( wordX , lineY + 2 , canvasCtx . measureText ( styledWord . word ) . width , fontSize * 0.1 )
551560 }
552561
553562 lineX = lineWidth
@@ -558,7 +567,8 @@ class QuoteGenerate {
558567 const canvasResize = createCanvas ( textWidth , lineY + fontSize )
559568 const canvasResizeCtx = canvasResize . getContext ( '2d' )
560569
561- canvasResizeCtx . drawImage ( canvas , 0 , 0 )
570+ let dx = ( lineDirection == 'rtl' ) ? textWidth - maxWidth + fontSize * 2 : 0
571+ canvasResizeCtx . drawImage ( canvas , dx , 0 )
562572
563573 return canvasResize
564574 }
0 commit comments