@@ -3806,23 +3806,23 @@ function jsPDF(options) {
38063806 flags = Object . assign ( { autoencode : true , noBOM : true } , options . flags ) ;
38073807
38083808 var wordSpacingPerLine = [ ] ;
3809-
3809+ var findWidth = function ( v ) {
3810+ return (
3811+ ( scope . getStringUnitWidth ( v , {
3812+ font : activeFont ,
3813+ charSpace : charSpace ,
3814+ fontSize : activeFontSize ,
3815+ doKerning : false
3816+ } ) *
3817+ activeFontSize ) /
3818+ scaleFactor
3819+ ) ;
3820+ } ;
38103821 if ( Object . prototype . toString . call ( text ) === "[object Array]" ) {
38113822 da = transformTextToSpecialArray ( text ) ;
38123823 var newY ;
38133824 if ( align !== "left" ) {
3814- lineWidths = da . map ( function ( v ) {
3815- return (
3816- ( scope . getStringUnitWidth ( v , {
3817- font : activeFont ,
3818- charSpace : charSpace ,
3819- fontSize : activeFontSize ,
3820- doKerning : false
3821- } ) *
3822- activeFontSize ) /
3823- scaleFactor
3824- ) ;
3825- } ) ;
3825+ lineWidths = da . map ( findWidth ) ;
38263826 }
38273827 //The first line uses the "main" Td setting,
38283828 //and the subsequent lines are offset by the
@@ -3869,11 +3869,41 @@ function jsPDF(options) {
38693869 for ( var h = 0 ; h < len ; h ++ ) {
38703870 text . push ( da [ h ] ) ;
38713871 }
3872+ } else if ( align === "justify" && activeFont . encoding === "Identity-H" ) {
3873+ // when using unicode fonts, wordSpacePerLine does not apply
3874+ text = [ ] ;
3875+ len = da . length ;
3876+ maxWidth = maxWidth !== 0 ? maxWidth : pageWidth ;
3877+ let backToStartX = 0 ;
3878+ for ( var l = 0 ; l < len ; l ++ ) {
3879+ newY = l === 0 ? getVerticalCoordinate ( y ) : - leading ;
3880+ newX = l === 0 ? getHorizontalCoordinate ( x ) : backToStartX ;
3881+ if ( l < len - 1 ) {
3882+ let spacing = scale (
3883+ ( maxWidth - lineWidths [ l ] ) / ( da [ l ] . split ( " " ) . length - 1 )
3884+ ) ;
3885+ let words = da [ l ] . split ( " " ) ;
3886+ text . push ( [ words [ 0 ] + " " , newX , newY ] ) ;
3887+ backToStartX = 0 ; // distance to reset back to the left
3888+ for ( let i = 1 ; i < words . length ; i ++ ) {
3889+ let shiftAmount =
3890+ ( findWidth ( words [ i - 1 ] + " " + words [ i ] ) -
3891+ findWidth ( words [ i ] ) ) *
3892+ scaleFactor +
3893+ spacing ;
3894+ if ( i == words . length - 1 ) text . push ( [ words [ i ] , shiftAmount , 0 ] ) ;
3895+ else text . push ( [ words [ i ] + " " , shiftAmount , 0 ] ) ;
3896+ backToStartX -= shiftAmount ;
3897+ }
3898+ } else {
3899+ text . push ( [ da [ l ] , newX , newY ] ) ;
3900+ }
3901+ }
3902+ text . push ( [ "" , backToStartX , 0 ] ) ;
38723903 } else if ( align === "justify" ) {
38733904 text = [ ] ;
38743905 len = da . length ;
38753906 maxWidth = maxWidth !== 0 ? maxWidth : pageWidth ;
3876-
38773907 for ( var l = 0 ; l < len ; l ++ ) {
38783908 newY = l === 0 ? getVerticalCoordinate ( y ) : - leading ;
38793909 newX = l === 0 ? getHorizontalCoordinate ( x ) : 0 ;
0 commit comments