@@ -1053,7 +1053,7 @@ var jsPDF = (function(global) {
1053
1053
* @methodOf jsPDF#
1054
1054
* @name text
1055
1055
*/
1056
- API . text = function ( text , x , y , flags , angle ) {
1056
+ API . text = function ( text , x , y , flags , angle , align ) {
1057
1057
/**
1058
1058
* Inserts something like this into PDF
1059
1059
* BT
@@ -1087,8 +1087,22 @@ var jsPDF = (function(global) {
1087
1087
// the user wanted to print multiple lines, so break the
1088
1088
// text up into an array. If the text is already an array,
1089
1089
// we assume the user knows what they are doing.
1090
- if ( typeof text === 'string' && text . match ( / [ \n \r ] / ) ) {
1091
- text = text . split ( / \r \n | \r | \n / g) ;
1090
+ if ( typeof text === 'string' ) {
1091
+ if ( text . match ( / [ \n \r ] / ) ) {
1092
+ text = text . split ( / \r \n | \r | \n / g) ;
1093
+ } else {
1094
+ // Convert text into an array anyway
1095
+ // to simplify later code.
1096
+ text = [ text ] ;
1097
+ }
1098
+ }
1099
+ if ( typeof angle === 'string' ) {
1100
+ align = angle ;
1101
+ angle = null ;
1102
+ }
1103
+ if ( typeof flags === 'string' ) {
1104
+ align = flags ;
1105
+ flags = null ;
1092
1106
}
1093
1107
if ( typeof flags === 'number' ) {
1094
1108
angle = flags ;
@@ -1124,11 +1138,9 @@ var jsPDF = (function(global) {
1124
1138
this . lastTextWasStroke = false ;
1125
1139
}
1126
1140
1127
- if ( typeof text === 'string' ) {
1128
- text = ESC ( text ) ;
1129
- } else if ( text instanceof Array ) {
1141
+ if ( text instanceof Array ) {
1130
1142
// we don't want to destroy original text array, so cloning it
1131
- var sa = text . concat ( ) , da = [ ] , len = sa . length ;
1143
+ var sa = text . concat ( ) , da = [ ] , i , len = sa . length ;
1132
1144
// we do array.join('text that must not be PDFescaped")
1133
1145
// thus, pdfEscape each component separately
1134
1146
while ( len -- ) {
@@ -1138,7 +1150,40 @@ var jsPDF = (function(global) {
1138
1150
if ( 0 <= linesLeft && linesLeft < da . length + 1 ) {
1139
1151
todo = da . splice ( linesLeft - 1 ) ;
1140
1152
}
1141
- text = da . join ( ") Tj\nT* (" ) ;
1153
+
1154
+ if ( align ) {
1155
+ var prevX ,
1156
+ leading = activeFontSize * lineHeightProportion ,
1157
+ lineWidths = text . map ( function ( v ) {
1158
+ return this . getStringUnitWidth ( v ) * activeFontSize / k ;
1159
+ } , this ) ;
1160
+ // The first line uses the "main" Td setting,
1161
+ // and the subsequent lines are offset by the
1162
+ // previous line's x coordinate.
1163
+ if ( align === "center" ) {
1164
+ // The passed in x coordinate defines
1165
+ // the center point.
1166
+ x -= lineWidths [ 0 ] / 2 ;
1167
+ } else if ( align === "right" ) {
1168
+ // The passed in x coordinate defines the
1169
+ // rightmost point of the text.
1170
+ x -= lineWidths [ 0 ] ;
1171
+ } else {
1172
+ throw new Error ( 'Unrecognized alignment option, use "center" or "right".' ) ;
1173
+ }
1174
+ prevX = x ;
1175
+ text = da [ 0 ] ;
1176
+ for ( i = 1 , len = da . length ; i < len ; i ++ ) {
1177
+ var delta = lineWidths [ i - 1 ] - lineWidths [ i ] ;
1178
+ if ( align === "center" ) delta /= 2 ;
1179
+ // T* = x-offset leading Td ( text )
1180
+ // PDF Spec 1.3 p.288
1181
+ text += ") Tj\n" + delta + " -" + leading + " Td (" + da [ i ] ;
1182
+ prevX += delta ;
1183
+ }
1184
+ } else {
1185
+ text = da . join ( ") Tj\nT* (" ) ;
1186
+ }
1142
1187
} else {
1143
1188
throw new Error ( 'Type of text must be string or Array. "' + text + '" is not recognized.' ) ;
1144
1189
}
0 commit comments