Created
September 8, 2023 22:55
-
-
Save 2sh/b4ff55db0932a73092f9328193cbcc45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// pdf2svg Logography\ for\ Kokanu.pdf input/kokanu_%d.svg all | |
// node kokanu.mjs pre | |
// node kokanu.mjs post | |
import fs from 'fs/promises' | |
import fsx from 'fs' | |
import readline from 'readline' | |
import { spawn } from 'child_process' | |
const inputDirectory = 'input' | |
const cleanedDirectory = 'cleaned' | |
const rasterDirectory = 'raster' | |
async function removeFluff(inFileName, outFileName) | |
{ | |
const fileStream = fsx.createReadStream(`${inputDirectory}/${inFileName}.svg`) | |
const lineReader = readline.createInterface( | |
{ | |
input: fileStream, | |
crlfDelay: Infinity | |
}) | |
const lines = [] | |
for await (const line of lineReader) | |
{ | |
lines.push(line) | |
} | |
const cleaned = lines | |
.filter(line => | |
line.startsWith("<?xml") | |
|| line.startsWith("<svg") | |
|| line.startsWith("</svg>") | |
|| (line.startsWith("<path") | |
&& line.includes("stroke-width"))) | |
.join("\n") | |
await fs.writeFile(`${cleanedDirectory}/${outFileName}.svg`, cleaned) | |
} | |
async function finalize(fileName) | |
{ | |
const mogrifySVG = spawn('mogrify', ['-trim', `${cleanedDirectory}/${fileName}.svg`]) | |
await new Promise((resolve, reject) => { | |
mogrifySVG.on('close', resolve); | |
}); | |
const convertToPNG = spawn('convert', ['-background', 'none', '-format', 'png', `${cleanedDirectory}/${fileName}.svg`, `${rasterDirectory}/${fileName}.png`]) | |
await new Promise((resolve, reject) => { | |
convertToPNG.on('close', resolve); | |
}); | |
} | |
let end = false | |
async function main() | |
{ | |
var args = process.argv.slice(2); | |
const fileStream = fsx.createReadStream(`words.txt`) | |
const lineReader = readline.createInterface( | |
{ | |
input: fileStream, | |
crlfDelay: Infinity | |
}) | |
const words = [] | |
for await (const line of lineReader) | |
{ | |
words.push(line) | |
} | |
await words.forEach(async (word, i) => | |
{ | |
if (args[0] == 'pre') | |
{ | |
await removeFluff(`kokanu_${i+3}`, word) | |
} | |
else if (args[0] == 'post') | |
{ | |
await finalize(word) | |
} | |
}) | |
end = true | |
} | |
main() | |
function wait() | |
{ | |
if (!end) setTimeout(wait, 1000) | |
} | |
wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment