-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstandard.js
119 lines (108 loc) · 3.06 KB
/
standard.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import AFMFont from './afm';
import PDFFont from '../font';
import fs from 'fs';
// This insanity is so bundlers can inline the font files
const STANDARD_FONTS = {
Courier() {
return fs.readFileSync(__dirname + '/data/Courier.afm', 'utf8');
},
'Courier-Bold'() {
return fs.readFileSync(__dirname + '/data/Courier-Bold.afm', 'utf8');
},
'Courier-Oblique'() {
return fs.readFileSync(__dirname + '/data/Courier-Oblique.afm', 'utf8');
},
'Courier-BoldOblique'() {
return fs.readFileSync(__dirname + '/data/Courier-BoldOblique.afm', 'utf8');
},
Helvetica() {
return fs.readFileSync(__dirname + '/data/Helvetica.afm', 'utf8');
},
'Helvetica-Bold'() {
return fs.readFileSync(__dirname + '/data/Helvetica-Bold.afm', 'utf8');
},
'Helvetica-Oblique'() {
return fs.readFileSync(__dirname + '/data/Helvetica-Oblique.afm', 'utf8');
},
'Helvetica-BoldOblique'() {
return fs.readFileSync(
__dirname + '/data/Helvetica-BoldOblique.afm',
'utf8',
);
},
'Times-Roman'() {
return fs.readFileSync(__dirname + '/data/Times-Roman.afm', 'utf8');
},
'Times-Bold'() {
return fs.readFileSync(__dirname + '/data/Times-Bold.afm', 'utf8');
},
'Times-Italic'() {
return fs.readFileSync(__dirname + '/data/Times-Italic.afm', 'utf8');
},
'Times-BoldItalic'() {
return fs.readFileSync(__dirname + '/data/Times-BoldItalic.afm', 'utf8');
},
Symbol() {
return fs.readFileSync(__dirname + '/data/Symbol.afm', 'utf8');
},
ZapfDingbats() {
return fs.readFileSync(__dirname + '/data/ZapfDingbats.afm', 'utf8');
},
};
class StandardFont extends PDFFont {
constructor(document, name, id) {
super();
this.document = document;
this.name = name;
this.id = id;
this.font = new AFMFont(STANDARD_FONTS[this.name]());
({
ascender: this.ascender,
descender: this.descender,
bbox: this.bbox,
lineGap: this.lineGap,
xHeight: this.xHeight,
capHeight: this.capHeight,
} = this.font);
}
embed() {
this.dictionary.data = {
Type: 'Font',
BaseFont: this.name,
Subtype: 'Type1',
Encoding: 'WinAnsiEncoding',
};
return this.dictionary.end();
}
encode(text) {
const encoded = this.font.encodeText(text);
const glyphs = this.font.glyphsForString(`${text}`);
const advances = this.font.advancesForGlyphs(glyphs);
const positions = [];
for (let i = 0; i < glyphs.length; i++) {
const glyph = glyphs[i];
positions.push({
xAdvance: advances[i],
yAdvance: 0,
xOffset: 0,
yOffset: 0,
advanceWidth: this.font.widthOfGlyph(glyph),
});
}
return [encoded, positions];
}
widthOfString(string, size) {
const glyphs = this.font.glyphsForString(`${string}`);
const advances = this.font.advancesForGlyphs(glyphs);
let width = 0;
for (let advance of advances) {
width += advance;
}
const scale = size / 1000;
return width * scale;
}
static isStandardFont(name) {
return name in STANDARD_FONTS;
}
}
export default StandardFont;