Skip to content

Commit 5fb8f4c

Browse files
committed
Tidy up .text()
1 parent ea1e5ce commit 5fb8f4c

1 file changed

Lines changed: 13 additions & 28 deletions

File tree

jspdf.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,20 +1174,16 @@ PubSub implementation
11741174
ET
11751175
*/
11761176

1177-
var undef, _first, _second, _third, newtext, str, i;
11781177
// Pre-August-2012 the order of arguments was function(x, y, text, flags)
11791178
// in effort to make all calls have similar signature like
11801179
// function(data, coordinates... , miscellaneous)
11811180
// this method had its args flipped.
11821181
// code below allows backward compatibility with old arg order.
11831182
if (typeof text === 'number') {
1184-
_first = y;
1185-
_second = text;
1186-
_third = x;
1187-
1188-
text = _first;
1189-
x = _second;
1190-
y = _third;
1183+
var tmp = y;
1184+
y = x;
1185+
x = text;
1186+
text = tmp;
11911187
}
11921188

11931189
// If there are any newlines in text, we assume
@@ -1197,32 +1193,21 @@ PubSub implementation
11971193
if (typeof text === 'string' && text.match(/[\n\r]/)) {
11981194
text = text.split(/\r\n|\r|\n/g);
11991195
}
1200-
1201-
if (typeof flags === 'undefined') {
1202-
flags = {'noBOM': true, 'autoencode': true};
1203-
} else {
1204-
1205-
if (flags.noBOM === undef) {
1206-
flags.noBOM = true;
1207-
}
1208-
1209-
if (flags.autoencode === undef) {
1210-
flags.autoencode = true;
1211-
}
1212-
1213-
}
1196+
flags = flags || {};
1197+
if(!('noBOM' in flags)) flags.noBOM = true;
1198+
if(!('autoencode' in flags)) flags.autoencode = true;
12141199

12151200
if (typeof text === 'string') {
1216-
str = pdfEscape(text, flags);
1201+
text = pdfEscape(text, flags);
12171202
} else if (text instanceof Array) { /* Array */
12181203
// we don't want to destroy original text array, so cloning it
1219-
newtext = text.concat();
1204+
var sa = text.concat(), da = [], len = sa.length;
12201205
// we do array.join('text that must not be PDFescaped")
12211206
// thus, pdfEscape each component separately
1222-
for (i = newtext.length - 1; i !== -1; i--) {
1223-
newtext[i] = pdfEscape(newtext[i], flags);
1207+
while(len--) {
1208+
da.push(pdfEscape( sa.shift(), flags));
12241209
}
1225-
str = newtext.join(") Tj\nT* (");
1210+
text = da.join(") Tj\nT* (");
12261211
} else {
12271212
throw new Error('Type of text must be string or Array. "' + text + '" is not recognized.');
12281213
}
@@ -1239,7 +1224,7 @@ PubSub implementation
12391224
(activeFontSize * lineHeightProportion) + ' TL\n' + // line spacing
12401225
textColor +
12411226
'\n' + f2(x * k) + ' ' + f2((pageHeight - y) * k) + ' Td\n(' +
1242-
str +
1227+
text +
12431228
') Tj\nET'
12441229
);
12451230
return this;

0 commit comments

Comments
 (0)