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

context2d: Fix default font behavior. #3316

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/modules/context2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ import {
jsPDFAPI.events.push([
"initialized",
function() {
this.context2d = new Context2D(this);
let context2d = null;
Object.defineProperty(this, "context2d", {
get: () => {
if (context2d === null) {
context2d = new Context2D(this);
// activate default font
context2d.font = context2d.font;
}
return context2d;
}
});

f2 = this.internal.f2;
getHorizontalCoordinateString = this.internal.getCoordinateString;
Expand Down
24 changes: 24 additions & 0 deletions test/specs/context2d.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ describe("Context2D: standard tests", () => {
comparePdf(doc.output(), "context2d-custom-fonts.pdf", "context2d");
});

it("context2d: default font", () => {
const doc = new jsPDF({
orientation: "p",
unit: "pt",
format: "a4",
floatPrecision: 2
});
const ctx = doc.context2d;

expect(ctx.font).toEqual("10px sans-serif")
expect(doc.getFontSize()).toEqual(10)
expect(doc.getFont().fontName).toEqual("helvetica")

const writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);
ctx.fillText("test", 0, 10);

expect(writeArray).toEqual([
"1. w",
"BT\n/F1 10 Tf\n11.5 TL\n0 g\n0. 831.89 Td\n(test) Tj\nET",
"1. w"
]);
});

it("context2d: css color names", () => {
var doc = new jsPDF({
orientation: "p",
Expand Down