Skip to content

Commit 5f482ab

Browse files
committed
Refactored API.text() for text alignment support closes #383
1 parent 4600508 commit 5f482ab

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

jspdf.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ var jsPDF = (function(global) {
979979
* @methodOf jsPDF#
980980
* @name text
981981
*/
982-
API.text = function(text, x, y, flags, angle) {
982+
API.text = function(text, x, y, flags, angle, align) {
983983
/**
984984
* Inserts something like this into PDF
985985
* BT
@@ -1013,8 +1013,22 @@ var jsPDF = (function(global) {
10131013
// the user wanted to print multiple lines, so break the
10141014
// text up into an array. If the text is already an array,
10151015
// we assume the user knows what they are doing.
1016-
if (typeof text === 'string' && text.match(/[\n\r]/)) {
1017-
text = text.split(/\r\n|\r|\n/g);
1016+
// Convert text into an array anyway to simplify
1017+
// later code.
1018+
if (typeof text === 'string') {
1019+
if(text.match(/[\n\r]/)) {
1020+
text = text.split( /\r\n|\r|\n/g);
1021+
} else {
1022+
text = [text];
1023+
}
1024+
}
1025+
if (typeof angle === 'string') {
1026+
align = angle;
1027+
angle = null;
1028+
}
1029+
if (typeof flags === 'string') {
1030+
align = flags;
1031+
flags = null;
10181032
}
10191033
if (typeof flags === 'number') {
10201034
angle = flags;
@@ -1034,11 +1048,9 @@ var jsPDF = (function(global) {
10341048
if (!('autoencode' in flags))
10351049
flags.autoencode = true;
10361050

1037-
if (typeof text === 'string') {
1038-
text = ESC(text);
1039-
} else if (text instanceof Array) {
1051+
if (text instanceof Array) {
10401052
// we don't want to destroy original text array, so cloning it
1041-
var sa = text.concat(), da = [], len = sa.length;
1053+
var sa = text.concat(), da = [], i, len = sa.length;
10421054
// we do array.join('text that must not be PDFescaped")
10431055
// thus, pdfEscape each component separately
10441056
while (len--) {
@@ -1048,7 +1060,47 @@ var jsPDF = (function(global) {
10481060
if (0 <= linesLeft && linesLeft < da.length + 1) {
10491061
todo = da.splice(linesLeft-1);
10501062
}
1051-
text = da.join(") Tj\nT* (");
1063+
1064+
if( align ) {
1065+
var left,
1066+
prevX,
1067+
maxLineLength,
1068+
leading = activeFontSize * lineHeightProportion,
1069+
lineWidths = text.map( function( v ) {
1070+
return this.getStringUnitWidth( v ) * activeFontSize / k;
1071+
}, this );
1072+
maxLineLength = Math.max.apply( Math, lineWidths );
1073+
// The first line uses the "main" Td setting,
1074+
// and the subsequent lines are offset by the
1075+
// previous line's x coordinate.
1076+
if( align === "center" ) {
1077+
// The passed in x coordinate defines
1078+
// the center point.
1079+
left = x - maxLineLength / 2;
1080+
x -= lineWidths[0] / 2;
1081+
} else if ( align === "right" ) {
1082+
// The passed in x coordinate defines the
1083+
// rightmost point of the text.
1084+
left = x - maxLineLength;
1085+
x -= lineWidths[0];
1086+
} else {
1087+
throw new Error('Unrecognized alignment option, use "center" or "right".');
1088+
}
1089+
prevX = x;
1090+
text = da[0] + ") Tj\n";
1091+
for ( i = 1, len = da.length ; i < len; i++ ) {
1092+
var delta = maxLineLength - lineWidths[i];
1093+
if( align === "center" ) delta /= 2;
1094+
// T* = x-offset leading Td ( text )
1095+
text += ( ( left - prevX ) + delta ) + " -" + leading + " Td (" + da[i];
1096+
prevX = left + delta;
1097+
if( i < len - 1 ) {
1098+
text += ") Tj\n";
1099+
}
1100+
}
1101+
} else {
1102+
text = da.join(") Tj\nT* (");
1103+
}
10521104
} else {
10531105
throw new Error('Type of text must be string or Array. "' + text + '" is not recognized.');
10541106
}

0 commit comments

Comments
 (0)