-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.js
48 lines (39 loc) · 1.65 KB
/
src.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
'use strict';
import {Parser as XMLParser} from 'xml2js';
import SVGPath from 'svgpath';
export default function(xml) {
const glyphs = [];
(new XMLParser({async: false})).parseString(xml, (err, root) => {
if (err) { throw err; }
// Read http://www.w3.org/TR/SVG/fonts.html for SVG font spec
for (let font of root.svg.defs[0].font) {
const face = font['font-face'][0];
const em = +face.$['units-per-em'] || 1000; // size of the em square
const ascent = +face.$['ascent']; // unaccented height of font above x-axis
const hox = +font.$['horiz-origin-x'] || 0; // x origin of font coordinates
const hoy = +font.$['horiz-origin-y'] || 0; // y origin of font coordinates
const hdx = +font.$['horiz-adv-x'] || em; // width of glyph
const vdy = +font.$['vert-adv-y'] || em; // height of glyph
for (let g of font.glyph) {
if (!g.$.d || !g.$.unicode) { continue; } // skip empty glyph without path data
const path = (new SVGPath(g.$.d))
.translate(-hox, -hoy) // move to origin (0, 0) in font coordinates
.translate(0, -ascent) // move below x-axis
.scale(1, -1) // invert y-axis (font coordinates -> initial coordinates)
.round(1)
.toString();
glyphs.push({
font_id: font.$.id,
font_family: face.$['font-family'],
name: g.$['glyph-name'],
unicode: g.$.unicode,
unicode_hex: g.$.unicode.charCodeAt(0).toString(16),
path: path,
width: +g.$['horiz-adv-x'] || hdx,
height: +g.$['vert-adv-y'] || vdy,
});
}
}
});
return glyphs;
}