Skip to content
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

fix #2938: textWithLink -> link centered correctly #3026

Merged
merged 9 commits into from
Dec 22, 2020
7 changes: 7 additions & 0 deletions src/modules/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ import { jsPDF } from "../jspdf.js";
//TODO We really need the text baseline height to do this correctly.
// Or ability to draw text on top, bottom, center, or baseline.
y += height * 0.2;
//handle x position based on the align option
if (options.align === "center") {
x = x - width / 2; //since starting from center move the x position by half of text width
}
if (options.align === "right") {
x = x - width;
}
this.link(x, y - height, width, height, options);
return width;
};
Expand Down
Binary file added test/reference/textLinkWithAlignOptions.pdf
Binary file not shown.
36 changes: 35 additions & 1 deletion test/specs/annotations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Module: Annotations", () => {
});

doc.textWithLink("Click me!", 10, 10, {
url: "https://parall.ax/",
url: "https://parall.ax/"
});

doc.addPage("a4");
Expand All @@ -72,4 +72,38 @@ describe("Module: Annotations", () => {

comparePdf(doc.output(), "insertLinkAddPage.pdf", "annotations");
});
it("should align text link based on the align option", () => {
var doc = new jsPDF({
unit: "px",
format: [200, 300],
floatPrecision: 2
});

doc.textWithLink(
"Left aligned Link",
doc.internal.pageSize.getWidth() / 2,
10,
{ align: "left", url: "https://www.google.com" }
);
doc.textWithLink(
"Center aligned Link",
doc.internal.pageSize.getWidth() / 2,
30,
{ align: "center", url: "https://www.google.com" }
);
doc.textWithLink(
"Justify aligned Link",
doc.internal.pageSize.getWidth() / 2,
50,
{ align: "justify", url: "https://www.google.com" }
);
doc.textWithLink(
"Right aligned Link",
doc.internal.pageSize.getWidth() / 2,
70,
{ align: "right", url: "https://www.google.com" }
);

comparePdf(doc.output(), "textLinkWithAlignOptions.pdf", "annotations");
});
});