Skip to content

Instantly share code, notes, and snippets.

@pjobson
Created May 30, 2018 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjobson/a62f52879041b683f55341b77df7bc34 to your computer and use it in GitHub Desktop.
Save pjobson/a62f52879041b683f55341b77df7bc34 to your computer and use it in GitHub Desktop.
Script for Generating MidnightFonts Previews
#!/bin/node
// You may need to update your #!
/*
# Public Domain Mark 1.0
https://creativecommons.org/publicdomain/mark/1.0/
## No Copyright
This work has been identified as being free of
known restrictions under copyright law, including
all related and neighboring rights.
You can copy, modify, distribute and perform the
work, even for commercial purposes, all without
asking permission. See Other Information below.
## Other Information
* The work may not be free of known copyright
restrictions in all jurisdictions.
* Persons may have other rights in or related
to the work, such as patent or trademark
rights, and others may have rights in how
the work is used, such as publicity or
privacy rights.
* In some jurisdictions moral rights of the
author may persist beyond the term of
copyright. These rights may include the
right to be identified as the author and the
right to object to derogatory treatments.
* Unless expressly stated otherwise, the person
who identified the work makes no warranties
about the work, and disclaims liability for
all uses of the work, to the fullest extent
permitted by applicable law.
* When using or citing the work, you should not
imply endorsement by the author or the person
who identified the work.
*/
/*
# Destructions
Written with Node 8.9.1 and ImageMagick 6.8.9-9
This script is pretty dumb, it doesn't create any paths
or do anything to help you.
Put this in its own directory
make the following paths in it: fonts output temp
No package.json included, you'll need to manually do npm stuff.
npm install rimraf find
You can run it with node or chmod +x it and run from the path.
./preview_generator.js OR node preview_generator.js
generator
├── fonts <-- path with all fonts in their own paths
│   ├── A Dripping Marker <-- example font
│   │   └── download.zip <-- download.zip file containing the META-INF and system paths
│   └── Zrnic
│   └── download.zip
├── node_modules <-- generated with npm
├── preview_generator.js <-- this script
├── output <-- empty output path
└── temp <-- empty temp path
*/
const { exec } = require('child_process');
const find = require('find');
const rimraf = require('rimraf');
let dirs = find.dirSync(/fonts\//,'.');
const processDir = () => {
if (dirs.length === 0) return;
const dir = dirs.shift();
console.log(`Processing ${dir}`);
const zfile = `${dir}/download.zip`;
exec(`unzip -o -q "${zfile}" -d ./temp`, (error, stdout, stderr) => {
if (error || stderr) {
console.log(`${dir} failed`);
console.error(`exec error: ${error}`);
console.error(`exec stderr: ${stderr}`);
rmTemp();
return;
} else {
processFont(dir.split('/')[1]);
}
});
};
const rmTemp = () => {
rimraf.sync('./temp/*');
setTimeout(() => {
processDir();
},500);
};
const processFont = fname => {
const ttfs = find.fileSync(/.+\.ttf$/, './temp/');
let imcmd = `convert -extent 1000x -background black -fill white -gravity west \\\n`;
imcmd += ` -pointsize 50 -font Courier label:"-- ${fname} --" \\\n`;
ttfs.forEach(font => {
const label = font.split('/').pop().replace(/Roboto-*(.+?)\.ttf/,'$1');
imcmd += ` -pointsize 72 -font "${font}" label:"${label}" \\\n`;
});
imcmd += ` -pointsize 50 -font Courier label:"-- ${fname} --" \\\n`;
imcmd += ` -bordercolor black -border 20x20 \\\n`;
imcmd += ` -append \\\n`;
imcmd += ` "output/${fname}.png"`;
// uncomment this to display what is going on
// console.log(imcmd);
console.log(` Generating ${fname}.png`);
exec(imcmd, (error, stdout, stderr) => {
if (error || stderr) {
console.log(`${dir} failed`);
console.error(`exec error: ${error}`);
console.error(`exec stderr: ${stderr}`);
}
rmTemp();
});
};
processDir();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment