-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UPDATE: textWithLink method to cover multi-line annotated text #3281
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I think we might be able to simplify it a little: there is a getTextDimensions
function that returns width and height.
The tests seem to fail with this exception:
Invalid argument passed to jsPDF.scale in dist/jspdf.umd.min.js (line 86)
The current implementation for fetching the height of a single line of link is much better than getting the height value from the Here's a comparison using the Height using the current implementation.It accounts for the full height including the ascenders and the descenders of the font. Height using the
|
I figured out the reason for the failure of this test. If |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks for the clarification. Then I think the implementation is still flawed for some edge cases:
var numOfRows = Math.ceil(textWidth / maxWidth);
might give wrong results for longer texts, because each line might be a little shorter than maxWidth
. With many lines, this difference adds up and we might get less lines than there actually are. You can use splitTextToSize
to calculate the number of lines.
Please also add a test case.
Yeah, I just saw that. The link breaks after 9 lines. The 10th line isn't supported unless |
Hey @HackbrettXXX. |
See https://github.com/parallax/jsPDF/blob/master/CONTRIBUTING.md#building-and-testing-jspdf. Apart from that just have a look at the other tests ;) |
Hey, @HackbrettXXX I don't think I need to write the test because there already exists one tests which checks for drawing the link using the // test/specs/annotations.spec.js line 58
it("should draw a link on the text with link after add page", () => {
const doc = new jsPDF({
unit: "px",
format: [200, 300],
floatPrecision: 2
});
doc.textWithLink("Click me!", 10, 10, {
url: "https://parall.ax/"
});
doc.addPage("a4");
doc.text("New page with difference size", 10, 10);
comparePdf(doc.output(), "insertLinkAddPage.pdf", "annotations");
}); I think that should address this issue and the previous test which was failing is also passing now. I've also updated the implementation to find the total height using the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we should still add another test case with a multiline link. The code looks fine now.
Hey @HackbrettXXX. I think that should do it. I've added a test case under the annotations suite. Kindly review them and let me know if there's any another changes to be made. |
Okay? The test is passing in my system. I don't understand what's going wrong here. The failing test in the integration suite is the one that I wrote. And, it's successfully passing in my system. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the test case. Looks good. I think the tests fail because of floating point differences. You can make them green by passing {floatPrecision: 2}
to the jsPDF constructor and updating the reference file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, looks good now, I'll merge it.
This pull request addresses the issue #3253 and provides multi-line support for
textWithLink()
method.