Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
38 changes: 37 additions & 1 deletion src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4850,14 +4850,50 @@ 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
) {
let encodingOptions = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't use let. We need to write es5 code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

"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 weired combination of fontweight and font style throw error
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

if (
(fontStyle == "normal" && fontWeight == "bold") ||
(fontStyle == "bold" && fontWeight == "normal") ||
(fontStyle == "bold" && fontWeight == 400) ||
(fontStyle == "normal" && fontWeight == 700)
Copy link
Collaborator

Choose a reason for hiding this comment

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

normal+700 as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

) {
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
? "normal"
: fontWeight == 700
? "bold"
: 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.

What about "italic" fontStyle? E.g. "italic" + "400" should result in "italic".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}
}
encoding = encoding || "Identity-H";
return addFont.call(this, postScriptName, fontName, fontStyle, encoding);
};
Expand Down
43 changes: 43 additions & 0 deletions test/specs/putTotalPages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,46 @@ 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 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",'normal200');

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");
});