@@ -3806,23 +3806,23 @@ function jsPDF(options) {
3806
3806
flags = Object . assign ( { autoencode : true , noBOM : true } , options . flags ) ;
3807
3807
3808
3808
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
+ } ;
3810
3821
if ( Object . prototype . toString . call ( text ) === "[object Array]" ) {
3811
3822
da = transformTextToSpecialArray ( text ) ;
3812
3823
var newY ;
3813
3824
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 ) ;
3826
3826
}
3827
3827
//The first line uses the "main" Td setting,
3828
3828
//and the subsequent lines are offset by the
@@ -3869,11 +3869,41 @@ function jsPDF(options) {
3869
3869
for ( var h = 0 ; h < len ; h ++ ) {
3870
3870
text . push ( da [ h ] ) ;
3871
3871
}
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 ] ) ;
3872
3903
} else if ( align === "justify" ) {
3873
3904
text = [ ] ;
3874
3905
len = da . length ;
3875
3906
maxWidth = maxWidth !== 0 ? maxWidth : pageWidth ;
3876
-
3877
3907
for ( var l = 0 ; l < len ; l ++ ) {
3878
3908
newY = l === 0 ? getVerticalCoordinate ( y ) : - leading ;
3879
3909
newX = l === 0 ? getHorizontalCoordinate ( x ) : 0 ;
0 commit comments