From c73a8ae2f1b130adefac91da0de8c3784e35aa4e Mon Sep 17 00:00:00 2001 From: Willem Date: Wed, 9 Feb 2022 12:46:02 +0100 Subject: [PATCH] Made small js code to convert font to xml --- .gitignore | 3 ++ conv-font/conv-font.js | 47 ++++++++++++++++++++++++++++ conv-font/package-lock.json | 61 +++++++++++++++++++++++++++++++++++++ conv-font/package.json | 17 +++++++++++ 4 files changed, 128 insertions(+) create mode 100644 conv-font/conv-font.js create mode 100644 conv-font/package-lock.json create mode 100644 conv-font/package.json diff --git a/.gitignore b/.gitignore index 173d9211..a9686307 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ build .gitignore .gradle +# Ignore npm resources +node_modules + # Ignore binary files and formats *.o *.class diff --git a/conv-font/conv-font.js b/conv-font/conv-font.js new file mode 100644 index 00000000..b5ca24fc --- /dev/null +++ b/conv-font/conv-font.js @@ -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(""); +console.log(""); + +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"); /*"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"); + } else if (cmd.type === 'L') { + console.log("\t\t"); + } else if (cmd.type === 'C') { + console.log("\t\t"); + } else if (cmd.type === 'Q') { + console.log("\t\t"); + } else if (cmd.type === 'Z') { + console.log("\t\t"); + } + } + console.log("\t"); +} +console.log(""); diff --git a/conv-font/package-lock.json b/conv-font/package-lock.json new file mode 100644 index 00000000..03bdafe9 --- /dev/null +++ b/conv-font/package-lock.json @@ -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==" + } + } +} diff --git a/conv-font/package.json b/conv-font/package.json new file mode 100644 index 00000000..e77dd0d0 --- /dev/null +++ b/conv-font/package.json @@ -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" + } +}