-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontLoader.js
45 lines (37 loc) · 1.38 KB
/
fontLoader.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
const svgpath = require('svgpath')
// let XMLDOMParser = require('xmldom').DOMParser; use jsdom instead if used on server
module.exports = {
parseFont(element, size = 24) {
let result = [];
const svgFont = element.getElementsByTagName('font')[0];
const svgFontface = element.getElementsByTagName('font-face')[0];
const svgGlyphs = element.getElementsByTagName('glyph');
const fontHorizAdvX = svgFont.getAttribute('horiz-adv-x');
const fontAscent = svgFontface.getAttribute('ascent');
const fontUnitsPerEm = svgFontface.getAttribute('units-per-em') || 1000;
const EM = size // Unit for the height
const scale = EM / fontUnitsPerEm;
for (var i = 0; i < svgGlyphs.length; i++) {
const svgGlyph = svgGlyphs[i]
const d = svgGlyph.getAttribute('d');
const unicode = svgGlyph.getAttribute('unicode');
const name = svgGlyph.getAttribute('glyph-name') || ('glyph' + unicode);
const width = svgGlyph.getAttribute('horiz-adv-x') || fontHorizAdvX;
result[`${unicode}`] = {
d: d ? new svgpath(d)
.translate(0, -fontAscent)
.scale(scale, -scale)
.abs()
// .round(2)
.rel()
//.round(2)
.toString() : null,
unicode: unicode,
name: name,
width: parseFloat(width * scale), //.toFixed(1)),
height: EM
}
}
return result;
}
}