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 17 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
56 changes: 54 additions & 2 deletions src/jspdf.js
Expand Up @@ -358,6 +358,35 @@ function jsPDF(options) {
apiMode = ApiMode.COMPAT;
}

/**
* @function combineFontStyleAndFontWeight
* @param {string} fontStyle Fontstyle or variant. Example: "italic".
* @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400
* @returns {string}
*/
var combineFontStyleAndFontWeight = function(fontStyle, fontWeight) {
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;
}
return fontStyle;
};

/**
* @callback ApiSwitchBody
* @param {jsPDF} pdf
Expand Down Expand Up @@ -4789,13 +4818,17 @@ function jsPDF(options) {
*
* @param {string} fontName Font name or family. Example: "times".
* @param {string} fontStyle Font style or variant. Example: "italic".
* @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400
* @function
* @instance
* @returns {jsPDF}
* @memberof jsPDF#
* @name setFont
*/
API.setFont = function(fontName, fontStyle) {
API.setFont = function(fontName, fontStyle, fontWeight) {
if (fontWeight) {
fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight);
}
activeFontKey = getFont(fontName, fontStyle, {
disableWarning: false
});
Expand Down Expand Up @@ -4850,14 +4883,33 @@ 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.
* @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) {
fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight);
}
encoding = encoding || "Identity-H";
return addFont.call(this, postScriptName, fontName, fontStyle, encoding);
};
Expand Down
87 changes: 87 additions & 0 deletions test/specs/putTotalPages.spec.js
Expand Up @@ -42,3 +42,90 @@ 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");
});

it("customfont with samevalue in fontweight and fontstyle ", () => {
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", "normal", "Identity-H");

doc.setFont("PTSans",'normal', "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");
});
8 changes: 7 additions & 1 deletion types/index.d.ts
Expand Up @@ -612,6 +612,7 @@ declare module "jspdf" {
postScriptName: string,
id: string,
fontStyle: string,
fontWeight?: string | number,
encoding?:
| "StandardEncoding"
| "MacRomanEncoding"
Expand All @@ -623,6 +624,7 @@ declare module "jspdf" {
url: URL,
id: string,
fontStyle: string,
fontWeight?: string | number,
encoding?:
| "StandardEncoding"
| "MacRomanEncoding"
Expand Down Expand Up @@ -763,7 +765,11 @@ declare module "jspdf" {
setFileId(value: string): jsPDF;
setFillColor(ch1: string): jsPDF;
setFillColor(ch1: number, ch2: number, ch3: number, ch4?: number): jsPDF;
setFont(fontName: string, fontStyle?: string): jsPDF;
setFont(
fontName: string,
fontStyle?: string,
fontWeight?: string | number
): jsPDF;
setFontSize(size: number): jsPDF;
setGState(gState: any): jsPDF;
setLineCap(style: string | number): jsPDF;
Expand Down