Text Alignment Feature #406
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!