Skip to content

Conversation

Gavvers
Copy link
Contributor

@Gavvers Gavvers commented Nov 27, 2014

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!

To demo text alignment and angling.
Removed text angling demo since that was not really relevant to my
additions.
@diegocr
Copy link
Contributor

diegocr commented Nov 28, 2014

Looking good! Thanks @Gavvers

Currently there is a merge conflict, sorry about that, could you please fix it? Also, take the chance to move jspdf.debug.js out of your PR since that file is automatically generated.

Made the align code more efficient, and improved the example code.

Removed jspdf.debug.js from commit.
@Gavvers
Copy link
Contributor Author

Gavvers commented Dec 2, 2014

Ok, I've resolved that merge conflict. Thankfully, the strokeoption code did not depend on text, so that was easy enough to work around.

@diegocr
Copy link
Contributor

diegocr commented Dec 8, 2014

Thanks!

diegocr added a commit that referenced this pull request Dec 8, 2014
@diegocr diegocr merged commit ec65985 into parallax:master Dec 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants