Skip to content

Commit b84c743

Browse files
author
Kenneth Glassey
committed
Improved API.text() code
Made the align code more efficient, and improved the example code. Removed jspdf.debug.js from commit.
1 parent de9aa82 commit b84c743

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

examples/basic.html

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,20 @@ <h2></h2>
346346
<h2><a href="#">Text alignment</a></h2>
347347
<div><p><pre>var pdf = new jsPDF('p', 'pt', 'letter');
348348

349-
pdf.text( 'This text is centered\raround\rthis point.', 150, 50, 'center' );
350-
pdf.text( 'This text is centered\raround\rthis point.', 150, 300, 45, 'center' );
351-
pdf.text( 'This text is\raligned to the\rright.', 150, 400, 'right' );
352-
pdf.text( 'This text is\raligned to the\rright.', 150, 550, 45, 'right' );
349+
pdf.text( 'This text is normally\raligned.', 140, 50 );
350+
351+
pdf.text( 'This text is centered\raround\rthis point.', 140, 120, 'center' );
352+
353+
pdf.text( 'This text is rotated\rand centered around\rthis point.', 140, 300, 45, 'center' );
354+
355+
pdf.text( 'This text is\raligned to the\rright.', 140, 400, 'right' );
356+
357+
pdf.text( 'This text is\raligned to the\rright.', 140, 550, 45, 'right' );
358+
359+
pdf.text( 'This single line is centered', 460, 50, 'center' );
360+
361+
pdf.text( 'This right aligned text\r\rhas an empty line.', 460, 200, 'right' );
353362

354-
pdf.text( 'This text is centered\raround\rthis point.', 400, 50, 'center' );
355-
pdf.text( 'This text is centered\raround\rthis point.', 400, 150, -85, 'center' );
356-
pdf.text( 'This text is\raligned to the\rright.', 400, 400, 'right' );
357-
pdf.text( 'This text is\raligned to the\rright.', 400, 550, -85, 'right' );
358363

359364
pdf.save('Test.pdf');</pre>
360365
<a href="javascript:demoTextAlign()" class="button">Run Code</a></p></div>

examples/js/basic.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,28 @@ function demoFromHTML() {
355355
function demoTextAlign() {
356356
var pdf = new jsPDF('p', 'pt', 'letter');
357357

358-
pdf.text( 'This text is centered\raround\rthis point.', 150, 50, 'center' );
359-
pdf.text( 'This text is centered\raround\rthis point.', 150, 300, 45, 'center' );
360-
pdf.text( 'This text is\raligned to the\rright.', 150, 400, 'right' );
361-
pdf.text( 'This text is\raligned to the\rright.', 150, 550, 45, 'right' );
362-
363-
pdf.text( 'This text is centered\raround\rthis point.', 400, 50, 'center' );
364-
pdf.text( 'This text is centered\raround\rthis point.', 400, 150, -85, 'center' );
365-
pdf.text( 'This text is\raligned to the\rright.', 400, 400, 'right' );
366-
pdf.text( 'This text is\raligned to the\rright.', 400, 550, -85, 'right' );
358+
pdf.setFillColor(0);
359+
pdf.circle( 140, 50, 2, "F" );
360+
pdf.text( 'This text is normally\raligned.', 140, 50 );
361+
362+
pdf.circle( 140, 120, 2, "F" );
363+
pdf.text( 'This text is centered\raround\rthis point.', 140, 120, 'center' );
364+
365+
pdf.circle( 140, 300, 2, "F" );
366+
pdf.text( 'This text is rotated\rand centered around\rthis point.', 140, 300, 45, 'center' );
367+
368+
pdf.circle( 140, 400, 2, "F" );
369+
pdf.text( 'This text is\raligned to the\rright.', 140, 400, 'right' );
370+
371+
pdf.circle( 140, 550, 2, "F" );
372+
pdf.text( 'This text is\raligned to the\rright.', 140, 550, 45, 'right' );
373+
374+
pdf.circle( 460, 50, 2, "F" );
375+
pdf.text( 'This single line is centered', 460, 50, 'center' );
376+
377+
pdf.circle( 460, 200, 2, "F" );
378+
pdf.text( 'This right aligned text\r\rhas an empty line.', 460, 200, 'right' );
379+
367380

368381
pdf.save('Test.pdf');
369382
}

jspdf.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,12 @@ 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-
// Convert text into an array anyway to simplify
1017-
// later code.
10181016
if (typeof text === 'string') {
10191017
if(text.match(/[\n\r]/)) {
10201018
text = text.split( /\r\n|\r|\n/g);
10211019
} else {
1020+
// Convert text into an array anyway
1021+
// to simplify later code.
10221022
text = [text];
10231023
}
10241024
}
@@ -1062,41 +1062,34 @@ var jsPDF = (function(global) {
10621062
}
10631063

10641064
if( align ) {
1065-
var left,
1066-
prevX,
1067-
maxLineLength,
1065+
var prevX,
10681066
leading = activeFontSize * lineHeightProportion,
10691067
lineWidths = text.map( function( v ) {
10701068
return this.getStringUnitWidth( v ) * activeFontSize / k;
10711069
}, this );
1072-
maxLineLength = Math.max.apply( Math, lineWidths );
10731070
// The first line uses the "main" Td setting,
10741071
// and the subsequent lines are offset by the
10751072
// previous line's x coordinate.
10761073
if( align === "center" ) {
10771074
// The passed in x coordinate defines
1078-
// the center point.
1079-
left = x - maxLineLength / 2;
1075+
// the center point.
10801076
x -= lineWidths[0] / 2;
10811077
} else if ( align === "right" ) {
10821078
// The passed in x coordinate defines the
1083-
// rightmost point of the text.
1084-
left = x - maxLineLength;
1079+
// rightmost point of the text.
10851080
x -= lineWidths[0];
10861081
} else {
10871082
throw new Error('Unrecognized alignment option, use "center" or "right".');
10881083
}
10891084
prevX = x;
1090-
text = da[0] + ") Tj\n";
1085+
text = da[0];
10911086
for ( i = 1, len = da.length ; i < len; i++ ) {
1092-
var delta = maxLineLength - lineWidths[i];
1087+
var delta = lineWidths[i-1] - lineWidths[i];
10931088
if( align === "center" ) delta /= 2;
10941089
// 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-
}
1090+
// PDF Spec 1.3 p.288
1091+
text += ") Tj\n" + delta + " -" + leading + " Td (" + da[i];
1092+
prevX += delta;
11001093
}
11011094
} else {
11021095
text = da.join(") Tj\nT* (");

0 commit comments

Comments
 (0)