-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathfont_factory.js
More file actions
32 lines (27 loc) · 916 Bytes
/
font_factory.js
File metadata and controls
32 lines (27 loc) · 916 Bytes
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
import fs from 'fs';
import fontkit from 'fontkit';
import StandardFont from './font/standard';
import EmbeddedFont from './font/embedded';
class PDFFontFactory {
static open(document, src, family, id, subset = true) {
let font;
if (typeof src === 'string') {
if (StandardFont.isStandardFont(src)) {
return new StandardFont(document, src, id);
}
src = fs.readFileSync(src);
}
if (Buffer.isBuffer(src)) {
font = fontkit.create(src, family);
} else if (src instanceof Uint8Array) {
font = fontkit.create(Buffer.from(src), family);
} else if (src instanceof ArrayBuffer) {
font = fontkit.create(Buffer.from(new Uint8Array(src)), family);
}
if (font == null) {
throw new Error('Not a supported font format or standard PDF font.');
}
return new EmbeddedFont(document, font, id, subset);
}
}
export default PDFFontFactory;