MrRio/jsPDF — pull request #406 by Gavvers (2014-11-27 02:59:27+00:00)
Title: Text Alignment Feature

Hello again.

I have added text alignment to **API.text()**, hope you like it. Btw, I had also edited jspdf.debug.js for testing purposes, let me know if that is a problem for this pull.

So for the new feature. I added a parameter **align** to **API.text()** that takes either "center" or "right" as arguments. For any alignment, the **x** parameter defines the origin for the alignment. Seems kind of obvious to me, but I feel that it might be best to spell that out.

Another notable change I made to **.text()** is to convert **text** to an array even if it is just a single line string, but only if it is actually a string so that the type validation still works. I did this so that I could eliminate the nested if-else on typeof later in the function and only test if **text** is an array by that point.

The main functionality resides in that if statement, where all valid cases are handled generally. If no alignment was specified, it simply handles the case as before. If an alignment was specified, it gets the width(s) of the line(s) involved and calculates all the necessary x-offsets. The first line is set normally and the offsets are set explicitly by taking advantage of the fact that *T\** is equivalent to *x-offset leading Td (text)* per the PDF spec.

This feature also works perfectly with angled text. You probably already knew that, but I hadn't taken a good look at the angling functionality until after I wrote my initial changes.

I also added an example in the *Text Elements* tab to demonstrate all this functionality.

Thanks!