Skip to content

Commit 0727008

Browse files
Kenneth GlasseyFlamenco
authored andcommitted
Improved API.text() code
Made the align code more efficient, and improved the example code. Removed jspdf.debug.js from commit.
1 parent 2cd9c0e commit 0727008

3 files changed

Lines changed: 45 additions & 60 deletions

File tree

examples/basic.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,28 @@ <h2><a href="#">Text alignment</a></h2>
381381
pdf.save('Test.pdf');</pre>
382382
<a href="javascript:demoTextAlign()" class="button">Run Code</a></p></div>
383383

384+
385+
<h2><a href="#">Text alignment</a></h2>
386+
<div><p><pre>var pdf = new jsPDF('p', 'pt', 'letter');
387+
388+
pdf.text( 'This text is normally\raligned.', 140, 50 );
389+
390+
pdf.text( 'This text is centered\raround\rthis point.', 140, 120, 'center' );
391+
392+
pdf.text( 'This text is rotated\rand centered around\rthis point.', 140, 300, 45, 'center' );
393+
394+
pdf.text( 'This text is\raligned to the\rright.', 140, 400, 'right' );
395+
396+
pdf.text( 'This text is\raligned to the\rright.', 140, 550, 45, 'right' );
397+
398+
pdf.text( 'This single line is centered', 460, 50, 'center' );
399+
400+
pdf.text( 'This right aligned text\r\rhas an empty line.', 460, 200, 'right' );
401+
402+
403+
pdf.save('Test.pdf');</pre>
404+
<a href="javascript:demoTextAlign()" class="button">Run Code</a></p></div>
405+
384406
</div>
385407
</div>
386408

examples/js/basic.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,28 @@ function demoTextAngles() {
396396
function demoTextAlign() {
397397
var pdf = new jsPDF('p', 'pt', 'letter');
398398

399-
pdf.text( 'This text is centered\raround\rthis point.', 150, 50, 'center' );
400-
pdf.text( 'This text is centered\raround\rthis point.', 150, 300, 45, 'center' );
401-
pdf.text( 'This text is\raligned to the\rright.', 150, 400, 'right' );
402-
pdf.text( 'This text is\raligned to the\rright.', 150, 550, 45, 'right' );
403-
404-
pdf.text( 'This text is centered\raround\rthis point.', 400, 50, 'center' );
405-
pdf.text( 'This text is centered\raround\rthis point.', 400, 150, -85, 'center' );
406-
pdf.text( 'This text is\raligned to the\rright.', 400, 400, 'right' );
407-
pdf.text( 'This text is\raligned to the\rright.', 400, 550, -85, 'right' );
399+
pdf.setFillColor(0);
400+
pdf.circle( 140, 50, 2, "F" );
401+
pdf.text( 'This text is normally\raligned.', 140, 50 );
402+
403+
pdf.circle( 140, 120, 2, "F" );
404+
pdf.text( 'This text is centered\raround\rthis point.', 140, 120, 'center' );
405+
406+
pdf.circle( 140, 300, 2, "F" );
407+
pdf.text( 'This text is rotated\rand centered around\rthis point.', 140, 300, 45, 'center' );
408+
409+
pdf.circle( 140, 400, 2, "F" );
410+
pdf.text( 'This text is\raligned to the\rright.', 140, 400, 'right' );
411+
412+
pdf.circle( 140, 550, 2, "F" );
413+
pdf.text( 'This text is\raligned to the\rright.', 140, 550, 45, 'right' );
414+
415+
pdf.circle( 460, 50, 2, "F" );
416+
pdf.text( 'This single line is centered', 460, 50, 'center' );
417+
418+
pdf.circle( 460, 200, 2, "F" );
419+
pdf.text( 'This right aligned text\r\rhas an empty line.', 460, 200, 'right' );
420+
408421

409422
pdf.save('Test.pdf');
410423
}

jspdf.js

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,22 +1095,6 @@ var jsPDF = (function(global) {
10951095
// the user wanted to print multiple lines, so break the
10961096
// text up into an array. If the text is already an array,
10971097
// we assume the user knows what they are doing.
1098-
if (typeof text === 'string') {
1099-
if(text.match(/[\n\r]/)) {
1100-
text = text.split( /\r\n|\r|\n/g);
1101-
} else {
1102-
// Convert text into an array anyway
1103-
// to simplify later code.
1104-
text = [text];
1105-
}
1106-
}
1107-
if (typeof angle === 'string') {
1108-
align = angle;
1109-
angle = null;
1110-
}
1111-
if (typeof flags === 'string') {
1112-
align = flags;
1113-
flags = null;
11141098
// Convert text into an array anyway to simplify
11151099
// later code.
11161100
if (typeof text === 'string') {
@@ -1165,7 +1149,7 @@ var jsPDF = (function(global) {
11651149
text = ESC(text);
11661150
} else if (text instanceof Array) {
11671151
// we don't want to destroy original text array, so cloning it
1168-
var sa = text.concat(), da = [], i, len = sa.length;
1152+
var sa = text.concat(), da = [], len = sa.length;
11691153
// we do array.join('text that must not be PDFescaped")
11701154
// thus, pdfEscape each component separately
11711155
while (len--) {
@@ -1176,40 +1160,6 @@ var jsPDF = (function(global) {
11761160
todo = da.splice(linesLeft-1);
11771161
}
11781162

1179-
if( align ) {
1180-
var prevX,
1181-
leading = activeFontSize * lineHeightProportion,
1182-
lineWidths = text.map( function( v ) {
1183-
return this.getStringUnitWidth( v ) * activeFontSize / k;
1184-
}, this );
1185-
// The first line uses the "main" Td setting,
1186-
// and the subsequent lines are offset by the
1187-
// previous line's x coordinate.
1188-
if( align === "center" ) {
1189-
// The passed in x coordinate defines
1190-
// the center point.
1191-
x -= lineWidths[0] / 2;
1192-
} else if ( align === "right" ) {
1193-
// The passed in x coordinate defines the
1194-
// rightmost point of the text.
1195-
x -= lineWidths[0];
1196-
} else {
1197-
throw new Error('Unrecognized alignment option, use "center" or "right".');
1198-
}
1199-
prevX = x;
1200-
text = da[0];
1201-
for ( i = 1, len = da.length ; i < len; i++ ) {
1202-
var delta = lineWidths[i-1] - lineWidths[i];
1203-
if( align === "center" ) delta /= 2;
1204-
// T* = x-offset leading Td ( text )
1205-
// PDF Spec 1.3 p.288
1206-
text += ") Tj\n" + delta + " -" + leading + " Td (" + da[i];
1207-
prevX += delta;
1208-
}
1209-
} else {
1210-
text = da.join(") Tj\nT* (");
1211-
}
1212-
12131163
if( align ) {
12141164
var left,
12151165
prevX,

0 commit comments

Comments
 (0)