gdxapp4d/conv-font/conv-font.js

48 lines
1.6 KiB
JavaScript
Raw Normal View History

const opentype = require('opentype.js');
const args = process.argv.slice(2);
const font = opentype.loadSync(args[0]);
function formatUnicode(unicode) {
unicode = unicode.toString(16);
if (unicode.length > 4) {
return ("000000" + unicode.toUpperCase()).substr(-6)
} else {
return ("0000" + unicode.toUpperCase()).substr(-4)
}
}
console.log("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
console.log("<opentype>");
let num = font.numGlyphs;
for(let glyphIndex = 0; glyphIndex < num; glyphIndex++) {
let glyph = font.glyphs.get(glyphIndex);
2022-02-11 01:46:44 +00:00
2022-02-11 12:47:04 +00:00
// JS NOTE: without calling xMin the getContours returns empty set.... :''(
2022-02-11 01:46:44 +00:00
glyph.xMin;
var contours = glyph.getContours();
if (contours.length === 0) {
2022-02-11 12:47:04 +00:00
continue; // TODO: add compound support
}
2022-02-09 11:55:39 +00:00
if (glyph.unicodes.length === 0) {
2022-02-11 12:47:04 +00:00
continue; // with cmds, these are compound glyphs by index base only
2022-02-09 11:55:39 +00:00
}
let uni = glyph.unicodes.map(formatUnicode).join(', ');
2022-02-11 01:46:44 +00:00
console.log("\t<glyph unicode=\""+uni+"\" name=\""+glyph.name+"\" advanceWidth=\""+glyph.advanceWidth+"\" "+
"xMin=\""+glyph.xMin+"\" xMax=\""+glyph.xMax+"\" yMin=\""+glyph.yMin+"\" yMax=\""+glyph.yMax+"\" "+
2022-02-11 01:46:44 +00:00
"leftSideBearing=\""+glyph.leftSideBearing+"\">");
for (let contourIndex = 0; contourIndex < contours.length; ++contourIndex) {
const contour = contours[contourIndex];
console.log("\t\t<contour>");
for (let i = 0; i < contour.length; ++i) {
let curr = contour[i];
console.log("\t\t\t<point x=\""+curr.x+"\" y=\""+curr.y+"\" onCurve=\""+curr.onCurve+"\"/>");
}
console.log("\t\t</contour>");
}
console.log("\t</glyph>");
}
console.log("</opentype>");