-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfont_metric.js
352 lines (282 loc) · 9.08 KB
/
font_metric.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// AFM Helper
//
// File Format Documentation:
// http://partners.adobe.com/public/developer/en/font/5004.AFM_Spec.pdf
//
// Download AFM files:
// ftp://ftp.adobe.com/pub/adobe/type/win/all/afmfiles/
// http://tug.ctan.org/fonts/lato/fonts/afm/public/
(function() {
'use strict';
var FontMetric = function (options) {
this.init(options);
};
FontMetric.prototype = {
// init {{{
'init': function(options) {
this.initDefaults();
this.initSettings(options);
this.load();
},
'load': function() {
if (Object.keys(this.config.afm.glyphlist).length <= 0) {
this.parseGlyphlistFile();
}
this.parseAfmFile();
},
'initSettings': function(options) {
this.config.files.afmSource = options.file || '';
this.config.files.glyphlistSource = options.glyphlist || '';
if (options.glyphlist && typeof options.glyphlist === 'object') {
this.config.afm.glyphlist = options.glyphlist;
}
this.config.settings.fontSize = Number(options.fontSize) || 12;
},
'initDefaults': function() {
this.config = {
files: {},
settings: {
fontSize: 12
},
afm: {
glyphlist: {},
charMetrics: {},
nonCodedMetrics: [],
kernPairs: {},
fontMetrics: {
ascender: 0, // 718 // top of the lower case 'd'
descender: 0, // -207 // bottom of the lower case 'p'
xHeight: 0, // 523 // top of the lower case 'x'
capHeight: 0 // 718 // top of the upper case 'H'
}
}
};
},
// }}}
// user interface {{{
'wrap': function(text, width, fontSize) {
var self = this;
var i = 0;
var lines = [''];
var lineNumber = 0;
var lineWidth = 0;
var fontSize = Number(fontSize) || this.config.settings.fontSize || 12;
var chars = text.split('');
var sizes = chars.map(function(c) {
return self.width(c, fontSize);
});
sizes.forEach(function(s, i) {
if (lineWidth + s >= width) {
lineNumber += 1;
lines[lineNumber] = '';
lineWidth = 0;
}
lines[lineNumber] += chars[i];
lineWidth += s;
});
return lines;
},
'width': function(text, fontSize) {
var fontSize = Number(fontSize) || this.config.afm.fontSize || 12;
var scale = fontSize / 1000;
var width = 0;
var em = this.config.afm.emSize * scale;
for (var i = 0; i < text.length; i++) {
var charWidth = em;
var charCode = text.charCodeAt(i);
var charMetric = this.config.afm.charMetrics[charCode];
if (charMetric && charMetric.WX) {
var charWidth = Number(charMetric.WX) * scale;
}
width += Number(charWidth);
}
return width;
},
// }}}
// parsing {{{
'parseGlyphlistFile': function() {
var self = this;
var src = this.config.files.glyphlistSource;
var glyphlist = this.config.afm.glyphlist;
if (!src && typeof src === 'string') {
throw 'Could not find a valid glyphlist.txt (Adobe Glyph List) source file.';
return;
}
if (!glyphlist && typeof glyphlist === 'object') {
throw 'Could not find a valid glyphlist object.';
return;
}
var lines = src.split("\n").map(function(line) {
return line.trim();
}).filter(function(line) {
return line;
}).filter(function(line) {
return !line.match(/^\s*#/);
});
lines.forEach(function(line) {
var parts = line.trim().split(';');
var name = parts[0];
var code = parseInt("0x"+parts[1]);
glyphlist[name] = code;
});
},
'parseAfmFile': function(file) {
var self = this;
var src = this.config.files.afmSource;
if (!(src && typeof src === 'string')) {
throw 'Could not find a valid AFM (Adobe Font Metrics) source file.';
return;
}
var lines = src.trim().split("\n");
if (lines.length <= 0) {
throw 'AFM (Adobe Font Metrics) source file was empty or invalid.';
}
this.config.afm.kernPairs = {};
this.config.afm.charMetrics = {};
this.config.afm.nonCodedMetrics = [];
this.config.afm.emSize = 850;
this.parseGlobalFontMetrics(src);
this.parseCharMetrics(lines);
this.parseKernPairs(lines);
this.parseEmSize();
},
'parseEmSize': function() {
var emMetric = this.config.afm.charMetrics["M".charCodeAt(0)];
if (emMetric && typeof emMetric === 'object' && emMetric.WX) {
this.config.afm.emSize = Number(emMetric.WX);
}
},
'parseGlobalFontMetrics': function(src) {
var ascMatch = src.match(/Ascender (-?\d+)/);
if (ascMatch) {
this.config.afm.fontMetrics.ascender = Number(ascMatch[1]);
}
var descMatch = src.match(/Descender (-?\d+)/);
if (descMatch) {
this.config.afm.fontMetrics.descender = Number(descMatch[1]);
}
var xhMatch = src.match(/XHeight (-?\d+)/);
if (xhMatch) {
this.config.afm.fontMetrics.xHeight = Number(xhMatch[1]);
}
var chMatch = src.match(/CapHeight (-?\d+)/);
if (chMatch) {
this.config.afm.fontMetrics.capHeight = Number(chMatch[1]);
}
},
'parseKernPairs': function(lines) {
var kernPairs = this.config.afm.kernPairs;
var kernPairLines = this.getKernPairLines(lines);
kernPairLines.forEach(function(line) {
var parts = line.split(/\s+/);
var letter = parts[1];
var to = parts[2];
var offset = Number(parts[3]);
if (!kernPairs[letter]) {
kernPairs[letter] = {};
}
kernPairs[letter][to] = offset;
});
},
'parseCharMetrics': function(lines) {
var charMetricLines = this.getCharMetricLines(lines);
var charMetricSettings = charMetricLines.map(function(line) {
var charMetric = {};
line.split(/\s*;\s*/).forEach(function(kvPair) {
var kvMatches = kvPair.trim().match(/([A-Z]*)\s*(.*)/);
var key = kvMatches[1];
var val = kvMatches[2];
charMetric[key] = val;
});
if (charMetric.C) {
charMetric.C = Number(charMetric.C);
}
if (charMetric.WX) {
charMetric.WX = Number(charMetric.WX);
}
if (charMetric.B && typeof charMetric.B === 'string') {
charMetric.B = charMetric.B.split(' ').map(function(b) {
return Number(b);
});
}
return charMetric;
});
var charMetrics = this.config.afm.charMetrics;
var glyphlist = this.config.afm.glyphlist;
var nonCodedMetrics = this.config.afm.nonCodedMetrics;
charMetricSettings.forEach(function(charMetric) {
if (charMetric.C > 0) {
charMetrics[charMetric.C] = charMetric;
} else {
if (charMetric.N && typeof charMetric.N === 'string') {
var glyphCode = glyphlist[charMetric.N];
if (glyphCode) {
charMetrics[glyphCode] = charMetric;
}
} else {
// these probably won't be useful in reality
// but it may be useful to know that some characters
// cannot be reliably identified by the glyphlist
nonCodedMetrics.push(charMetric);
}
}
});
},
'getCharMetricLines': function(lines) {
var startCharMetricsIndex = findIndex(lines, function(line) {
return line.match(/StartCharMetrics/);
});
var endCharMetricsIndex = findIndex(lines, function(line) {
return line.match(/EndCharMetrics/);
});
var charMetricLines = lines.slice(startCharMetricsIndex, endCharMetricsIndex-1).map(function(line) {
return line.trim().replace(/\s*;\s*$/, '');
}).filter(function(line) {
return line;
});
return charMetricLines;
},
'getKernPairLines': function(lines) {
var startKernPairsIndex = findIndex(lines, function(line) {
return line.match(/StartKernPairs/);
});
var endKernPairsIndex = findIndex(lines, function(line) {
return line.match(/EndKernPairs/);
});
var kernPairLines = lines.slice(startKernPairsIndex, endKernPairsIndex-1).map(function(line) {
return line.trim().replace(/\s*;\s*$/, '');
}).filter(function(line) {
return line;
});
return kernPairLines;
}
// }}}
};
// utilities {{{
var findIndex = function(arr, callback) {
if (!Array.isArray(arr)) {
return;
}
for (var i = 0; i < arr.length; i++) {
if (callback(arr[i])) {
return i;
}
}
return null;
};
// }}}
if (
typeof module === 'object' && module !== null &&
typeof module.exports === 'object' && module.exports !== null
) {
module.exports = FontMetric;
} else if (
typeof define === 'function'
) {
define(function() { return FontMetric; });
} else if (
typeof window === 'object' && window !== null
) {
window.FontMetric = FontMetric;
}
})();