Made small js code to convert font to xml

This commit is contained in:
Willem 2022-02-09 12:46:02 +01:00
parent 5971c4cdc0
commit c73a8ae2f1
4 changed files with 128 additions and 0 deletions

3
.gitignore vendored
View file

@ -11,6 +11,9 @@ build
.gitignore .gitignore
.gradle .gradle
# Ignore npm resources
node_modules
# Ignore binary files and formats # Ignore binary files and formats
*.o *.o
*.class *.class

47
conv-font/conv-font.js Normal file
View file

@ -0,0 +1,47 @@
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);
let path = glyph.getPath(0, 0, 72);
if (path.commands.length === 0) {
continue;
}
let uni = glyph.unicodes.map(formatUnicode).join(', ');
console.log("\t<glyph unicode=\""+uni+"\" name=\""+glyph.name+"\">"); /*"advanceWidth=\""+glyph.advanceWidth+"\" "+
"xMin=\""+glyph.xMin+"\" xMax=\""+glyph.xMax+"\" yMin=\""+glyph.yMin+"\" yMax=\""+glyph.yMax+"\" "+
"leftSideBearing=\""+glyph.leftSideBearing+"\" pathFill=\""+path.fill+"\" "+
"strokeStyle=\""+path.stroke+"\" lineWidth=\""+path.strokeWidth+"\">"); */
for (i = 0; i < path.commands.length; i += 1) {
cmd = path.commands[i];
if (cmd.type === 'M') {
console.log("\t\t<pathMove x=\""+cmd.x+"\" y=\""+cmd.y+"\"/>");
} else if (cmd.type === 'L') {
console.log("\t\t<pathLine x=\""+cmd.x+"\" y=\""+cmd.y+"\"/>");
} else if (cmd.type === 'C') {
console.log("\t\t<pathBezierCurve x1=\""+cmd.x1+"\" y1=\""+cmd.y1+"\" x2=\""+cmd.x2+"\" y2=\""+cmd.y2+"\" x=\""+cmd.x+"\" y=\""+cmd.y+"\"/>");
} else if (cmd.type === 'Q') {
console.log("\t\t<pathQuadraticCurve x1=\""+cmd.x1+"\" y1=\""+cmd.y1+"\" x=\""+cmd.x+"\" y=\""+cmd.y+"\"/>");
} else if (cmd.type === 'Z') {
console.log("\t\t<pathClose/>");
}
}
console.log("\t</glyph>");
}
console.log("</opentype>");

61
conv-font/package-lock.json generated Normal file
View file

@ -0,0 +1,61 @@
{
"name": "conv-font",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"version": "0.0.1",
"license": "BSD-2-Clause",
"dependencies": {
"opentype.js": "1.3.4"
}
},
"node_modules/opentype.js": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/opentype.js/-/opentype.js-1.3.4.tgz",
"integrity": "sha512-d2JE9RP/6uagpQAVtJoF0pJJA/fgai89Cc50Yp0EJHk+eLp6QQ7gBoblsnubRULNY132I0J1QKMJ+JTbMqz4sw==",
"dependencies": {
"string.prototype.codepointat": "^0.2.1",
"tiny-inflate": "^1.0.3"
},
"bin": {
"ot": "bin/ot"
},
"engines": {
"node": ">= 8.0.0"
}
},
"node_modules/string.prototype.codepointat": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/string.prototype.codepointat/-/string.prototype.codepointat-0.2.1.tgz",
"integrity": "sha512-2cBVCj6I4IOvEnjgO/hWqXjqBGsY+zwPmHl12Srk9IXSZ56Jwwmy+66XO5Iut/oQVR7t5ihYdLB0GMa4alEUcg=="
},
"node_modules/tiny-inflate": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/tiny-inflate/-/tiny-inflate-1.0.3.tgz",
"integrity": "sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw=="
}
},
"dependencies": {
"opentype.js": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/opentype.js/-/opentype.js-1.3.4.tgz",
"integrity": "sha512-d2JE9RP/6uagpQAVtJoF0pJJA/fgai89Cc50Yp0EJHk+eLp6QQ7gBoblsnubRULNY132I0J1QKMJ+JTbMqz4sw==",
"requires": {
"string.prototype.codepointat": "^0.2.1",
"tiny-inflate": "^1.0.3"
}
},
"string.prototype.codepointat": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/string.prototype.codepointat/-/string.prototype.codepointat-0.2.1.tgz",
"integrity": "sha512-2cBVCj6I4IOvEnjgO/hWqXjqBGsY+zwPmHl12Srk9IXSZ56Jwwmy+66XO5Iut/oQVR7t5ihYdLB0GMa4alEUcg=="
},
"tiny-inflate": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/tiny-inflate/-/tiny-inflate-1.0.3.tgz",
"integrity": "sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw=="
}
}
}

17
conv-font/package.json Normal file
View file

@ -0,0 +1,17 @@
{
"name": "conv-font",
"version": "0.0.1",
"description": "Convert font data to XML intermediate.",
"private": true,
"scripts": {
"start": "npm run-script conv0 && npm run-script conv1 && npm run-script conv2 && npm run-script conv3",
"conv0": "node conv-font.js ../main-gdxapp/assets/font/FreeSans.ttf > bin/FreeSans.ttf",
"conv1": "node conv-font.js ../main-gdxapp/assets/font/NotoSansCJKjp-Medium.otf > bin/NotoSansCJKjp-Medium.otf.xml",
"conv2": "node conv-font.js ../main-gdxapp/assets/font/roboto-bold.ttf > bin/roboto-bold.xml",
"conv3": "node conv-font.js ../main-gdxapp/assets/font/arslan-wessam.ttf > bin/arslan-wessam.xml",
"debug": "node conv-font.js ../main-gdxapp/assets/font/roboto-bold.ttf"
},
"dependencies": {
"opentype.js": "1.3.4"
}
}