Skip to content

Commit 7403adb

Browse files
Add fonts with different font-weights (#3036)
Co-authored-by: Lukas Holländer <[email protected]>
1 parent 451e131 commit 7403adb

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

src/jspdf.js

+54-2
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,35 @@ function jsPDF(options) {
358358
apiMode = ApiMode.COMPAT;
359359
}
360360

361+
/**
362+
* @function combineFontStyleAndFontWeight
363+
* @param {string} fontStyle Fontstyle or variant. Example: "italic".
364+
* @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400
365+
* @returns {string}
366+
*/
367+
var combineFontStyleAndFontWeight = function(fontStyle, fontWeight) {
368+
if (
369+
(fontStyle == "bold" && fontWeight == "normal") ||
370+
(fontStyle == "bold" && fontWeight == 400) ||
371+
(fontStyle == "normal" && fontWeight == "italic") ||
372+
(fontStyle == "bold" && fontWeight == "italic")
373+
) {
374+
throw new Error("Invalid Combination of fontweight and fontstyle");
375+
}
376+
if (fontWeight && fontStyle !== fontWeight) {
377+
//if fontstyle is normal and fontweight is normal too no need to append the font-weight
378+
fontStyle =
379+
fontWeight == 400
380+
? fontStyle == "italic"
381+
? "italic"
382+
: "normal"
383+
: fontWeight == 700 && fontStyle !== "italic"
384+
? "bold"
385+
: fontStyle + "" + fontWeight;
386+
}
387+
return fontStyle;
388+
};
389+
361390
/**
362391
* @callback ApiSwitchBody
363392
* @param {jsPDF} pdf
@@ -4789,13 +4818,17 @@ function jsPDF(options) {
47894818
*
47904819
* @param {string} fontName Font name or family. Example: "times".
47914820
* @param {string} fontStyle Font style or variant. Example: "italic".
4821+
* @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400
47924822
* @function
47934823
* @instance
47944824
* @returns {jsPDF}
47954825
* @memberof jsPDF#
47964826
* @name setFont
47974827
*/
4798-
API.setFont = function(fontName, fontStyle) {
4828+
API.setFont = function(fontName, fontStyle, fontWeight) {
4829+
if (fontWeight) {
4830+
fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight);
4831+
}
47994832
activeFontKey = getFont(fontName, fontStyle, {
48004833
disableWarning: false
48014834
});
@@ -4850,14 +4883,33 @@ function jsPDF(options) {
48504883
* @param {string} postScriptName PDF specification full name for the font.
48514884
* @param {string} id PDF-document-instance-specific label assinged to the font.
48524885
* @param {string} fontStyle Style of the Font.
4886+
* @param {number | string} fontWeight Weight of the Font.
48534887
* @param {Object} encoding Encoding_name-to-Font_metrics_object mapping.
48544888
* @function
48554889
* @instance
48564890
* @memberof jsPDF#
48574891
* @name addFont
48584892
* @returns {string} fontId
48594893
*/
4860-
API.addFont = function(postScriptName, fontName, fontStyle, encoding) {
4894+
API.addFont = function(
4895+
postScriptName,
4896+
fontName,
4897+
fontStyle,
4898+
fontWeight,
4899+
encoding
4900+
) {
4901+
var encodingOptions = [
4902+
"StandardEncoding",
4903+
"MacRomanEncoding",
4904+
"Identity-H",
4905+
"WinAnsiEncoding"
4906+
];
4907+
if (arguments[3] && encodingOptions.indexOf(arguments[3]) !== -1) {
4908+
//IE 11 fix
4909+
encoding = arguments[3];
4910+
} else if (arguments[3] && encodingOptions.indexOf(arguments[3]) == -1) {
4911+
fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight);
4912+
}
48614913
encoding = encoding || "Identity-H";
48624914
return addFont.call(this, postScriptName, fontName, fontStyle, encoding);
48634915
};

test/specs/putTotalPages.spec.js

+87
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,90 @@ describe("Module: putTotalPages", () => {
4242
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
4343
});
4444
});
45+
46+
it("customfont with encoding without passing fontWeight", () => {
47+
var PTSans = loadBinaryResource("reference/PTSans.ttf");
48+
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
49+
var totalPagesExp = "{totalPages}";
50+
51+
doc.addFileToVFS("PTSans.ttf", PTSans);
52+
doc.addFont("PTSans.ttf", "PTSans", "normal", "Identity-H");
53+
54+
doc.setFont("PTSans");
55+
56+
doc.text(10, 10, "Page 1 of {totalPages}");
57+
doc.addPage();
58+
59+
doc.text(10, 10, "Page 2 of {totalPages}");
60+
61+
if (typeof doc.putTotalPages === "function") {
62+
doc.putTotalPages(totalPagesExp);
63+
}
64+
65+
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
66+
});
67+
68+
69+
it("customfont check without passing fontweight in setfont", () => {
70+
var PTSans = loadBinaryResource("reference/PTSans.ttf");
71+
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
72+
var totalPagesExp = "{totalPages}";
73+
74+
doc.addFileToVFS("PTSans.ttf", PTSans);
75+
doc.addFont("PTSans.ttf", "PTSans", "normal");
76+
77+
doc.setFont("PTSans",'normal');
78+
79+
doc.text(10, 10, "Page 1 of {totalPages}");
80+
doc.addPage();
81+
82+
doc.text(10, 10, "Page 2 of {totalPages}");
83+
84+
if (typeof doc.putTotalPages === "function") {
85+
doc.putTotalPages(totalPagesExp);
86+
}
87+
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
88+
});
89+
90+
91+
it("customfont with fontweight", () => {
92+
var PTSans = loadBinaryResource("reference/PTSans.ttf");
93+
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
94+
var totalPagesExp = "{totalPages}";
95+
96+
doc.addFileToVFS("PTSans.ttf", PTSans);
97+
doc.addFont("PTSans.ttf", "PTSans", "normal",200, "Identity-H");
98+
99+
doc.setFont("PTSans",'normal',200);
100+
101+
doc.text(10, 10, "Page 1 of {totalPages}");
102+
doc.addPage();
103+
104+
doc.text(10, 10, "Page 2 of {totalPages}");
105+
106+
if (typeof doc.putTotalPages === "function") {
107+
doc.putTotalPages(totalPagesExp);
108+
}
109+
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
110+
});
111+
112+
it("customfont with samevalue in fontweight and fontstyle ", () => {
113+
var PTSans = loadBinaryResource("reference/PTSans.ttf");
114+
var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 });
115+
var totalPagesExp = "{totalPages}";
116+
117+
doc.addFileToVFS("PTSans.ttf", PTSans);
118+
doc.addFont("PTSans.ttf", "PTSans", "normal", "normal", "Identity-H");
119+
120+
doc.setFont("PTSans",'normal', "normal");
121+
122+
doc.text(10, 10, "Page 1 of {totalPages}");
123+
doc.addPage();
124+
125+
doc.text(10, 10, "Page 2 of {totalPages}");
126+
127+
if (typeof doc.putTotalPages === "function") {
128+
doc.putTotalPages(totalPagesExp);
129+
}
130+
comparePdf(doc.output(), "customfont.pdf", "putTotalPages");
131+
});

types/index.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ declare module "jspdf" {
612612
postScriptName: string,
613613
id: string,
614614
fontStyle: string,
615+
fontWeight?: string | number,
615616
encoding?:
616617
| "StandardEncoding"
617618
| "MacRomanEncoding"
@@ -623,6 +624,7 @@ declare module "jspdf" {
623624
url: URL,
624625
id: string,
625626
fontStyle: string,
627+
fontWeight?: string | number,
626628
encoding?:
627629
| "StandardEncoding"
628630
| "MacRomanEncoding"
@@ -763,7 +765,11 @@ declare module "jspdf" {
763765
setFileId(value: string): jsPDF;
764766
setFillColor(ch1: string): jsPDF;
765767
setFillColor(ch1: number, ch2: number, ch3: number, ch4?: number): jsPDF;
766-
setFont(fontName: string, fontStyle?: string): jsPDF;
768+
setFont(
769+
fontName: string,
770+
fontStyle?: string,
771+
fontWeight?: string | number
772+
): jsPDF;
767773
setFontSize(size: number): jsPDF;
768774
setGState(gState: any): jsPDF;
769775
setLineCap(style: string | number): jsPDF;

0 commit comments

Comments
 (0)