-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathfonts.js
More file actions
92 lines (75 loc) · 1.99 KB
/
fonts.js
File metadata and controls
92 lines (75 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import PDFFontFactory from '../font_factory';
export default {
initFonts(defaultFont = 'Helvetica') {
// Lookup table for embedded fonts
this._fontFamilies = {};
this._fontCount = 0;
// Font state
this._fontSize = 12;
this._font = null;
this._registeredFonts = {};
// Set the default font
if (defaultFont) {
this.font(defaultFont);
}
},
font(src, family, size, subset) {
let cacheKey, font;
if (typeof family === 'number') {
size = family;
family = null;
}
// check registered fonts if src is a string
if (typeof src === 'string' && this._registeredFonts[src]) {
cacheKey = src;
({ src, family } = this._registeredFonts[src]);
} else {
cacheKey = family || src;
if (typeof cacheKey !== 'string') {
cacheKey = null;
}
}
if (size != null) {
this.fontSize(size);
}
// fast path: check if the font is already in the PDF
if ((font = this._fontFamilies[cacheKey])) {
this._font = font;
return this;
}
// load the font
const id = `F${++this._fontCount}`;
this._font = PDFFontFactory.open(this, src, family, id, subset);
// check for existing font familes with the same name already in the PDF
// useful if the font was passed as a buffer
if ((font = this._fontFamilies[this._font.name])) {
this._font = font;
return this;
}
// save the font for reuse later
if (cacheKey) {
this._fontFamilies[cacheKey] = this._font;
}
if (this._font.name) {
this._fontFamilies[this._font.name] = this._font;
}
return this;
},
fontSize(_fontSize) {
this._fontSize = _fontSize;
return this;
},
currentLineHeight(includeGap) {
if (includeGap == null) {
includeGap = false;
}
return this._font.lineHeight(this._fontSize, includeGap);
},
registerFont(name, src, family) {
this._registeredFonts[name] = {
src,
family
};
return this;
}
};