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

Add fonts with different font-weights #3036

Merged
merged 18 commits into from Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/jspdf.js
Expand Up @@ -4795,7 +4795,8 @@ function jsPDF(options) {
* @memberof jsPDF#
* @name setFont
*/
API.setFont = function(fontName, fontStyle) {
API.setFont = function(fontName, fontStyle, fontWeight) {
fontStyle = !fontWeight ? fontStyle : fontStyle + "" + fontWeight;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the same logic as in addFont here.

activeFontKey = getFont(fontName, fontStyle, {
disableWarning: false
});
Expand Down Expand Up @@ -4850,14 +4851,52 @@ function jsPDF(options) {
* @param {string} postScriptName PDF specification full name for the font.
* @param {string} id PDF-document-instance-specific label assinged to the font.
* @param {string} fontStyle Style of the Font.
* @param {number || string} fontWeight Weight of the Font.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number|string

* @param {Object} encoding Encoding_name-to-Font_metrics_object mapping.
* @function
* @instance
* @memberof jsPDF#
* @name addFont
* @returns {string} fontId
*/
API.addFont = function(postScriptName, fontName, fontStyle, encoding) {
API.addFont = function(
postScriptName,
fontName,
fontStyle,
fontWeight,
encoding
) {
var encodingOptions = [
"StandardEncoding",
"MacRomanEncoding",
"Identity-H",
"WinAnsiEncoding"
];
if (arguments[3] && encodingOptions.indexOf(arguments[3]) !== -1) {
//IE 11 fix
encoding = arguments[3];
} else if (arguments[3] && encodingOptions.indexOf(arguments[3]) == -1) {
//if weird combination of fontweight and font style throw error
if (
(fontStyle == "bold" && fontWeight == "normal") ||
(fontStyle == "bold" && fontWeight == 400) ||
(fontStyle == "normal" && fontWeight == "italic") ||
(fontStyle == "bold" && fontWeight == "italic")
) {
throw new Error("Invalid Combination of fontweight and fontstyle");
}
if (fontWeight && fontStyle !== fontWeight) {
//if fontstyle is normal and fontweight is normal too no need to append the font-weight
fontStyle =
fontWeight == 400
? fontStyle == "italic"
? "italic"
: "normal"
: fontWeight == 700 && fontStyle !== "italic"
? "bold"
: fontStyle + "" + fontWeight;
}
}
encoding = encoding || "Identity-H";
return addFont.call(this, postScriptName, fontName, fontStyle, encoding);
};
Expand Down
65 changes: 65 additions & 0 deletions test/specs/putTotalPages.spec.js
Expand Up @@ -42,3 +42,68 @@ describe("Module: putTotalPages", () => {
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
});
});

it("customfont with encoding without passing fontWeight", () => {
var PTSans = loadBinaryResource("reference/PTSans.ttf");
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
var totalPagesExp = "{totalPages}";

doc.addFileToVFS("PTSans.ttf", PTSans);
doc.addFont("PTSans.ttf", "PTSans", "normal", "Identity-H");

doc.setFont("PTSans");

doc.text(10, 10, "Page 1 of {totalPages}");
doc.addPage();

doc.text(10, 10, "Page 2 of {totalPages}");

if (typeof doc.putTotalPages === "function") {
doc.putTotalPages(totalPagesExp);
}

comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
});

it("customfont check without passing fontweight in setfont", () => {
var PTSans = loadBinaryResource("reference/PTSans.ttf");
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
var totalPagesExp = "{totalPages}";

doc.addFileToVFS("PTSans.ttf", PTSans);
doc.addFont("PTSans.ttf", "PTSans", "normal");

doc.setFont("PTSans",'normal');

doc.text(10, 10, "Page 1 of {totalPages}");
doc.addPage();

doc.text(10, 10, "Page 2 of {totalPages}");

if (typeof doc.putTotalPages === "function") {
doc.putTotalPages(totalPagesExp);
}
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
});


it("customfont with fontweight", () => {
var PTSans = loadBinaryResource("reference/PTSans.ttf");
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
var totalPagesExp = "{totalPages}";

doc.addFileToVFS("PTSans.ttf", PTSans);
doc.addFont("PTSans.ttf", "PTSans", "normal",200, "Identity-H");

doc.setFont("PTSans",'normal',200);

doc.text(10, 10, "Page 1 of {totalPages}");
doc.addPage();

doc.text(10, 10, "Page 2 of {totalPages}");

if (typeof doc.putTotalPages === "function") {
doc.putTotalPages(totalPagesExp);
}
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
});