Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Last active August 29, 2015 14:01
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 tmpvar/a17ad8b7101776d345ba to your computer and use it in GitHub Desktop.
Save tmpvar/a17ad8b7101776d345ba to your computer and use it in GitHub Desktop.
turn TTF + text into a a toolpath
node text2gcode.js --font=/Users/tmpvar/Desktop/sg12-webfont.ttf "hello world"
var opentype = require('opentype.js');
var argv = require('optimist').argv;
var gcanvas = require('gcanvas');
var text = argv._[0];
var fontFile = argv.font;
var size = text
var ctx = new (require('gcanvas'));
var fontsize = argv.fs || 30;
var TAU = Math.PI*2;
var defined = function(a) {
return typeof a !== 'undefined';
}
opentype.load(argv.font, function(err, font) {
if (err) {
throw err;
}
ctx.depth = argv.depth || -5;
ctx.depthOfCut = argv.pass || .1;
ctx.scale(argv.mirror ? 1 : -1, 1)
ctx.toolDiameter = argv.diameter || 1.6;
var width = 0;
font.forEachGlyph(text + ' ', 0, 0, fontsize, null, function(glyph, x, y) {
width = x;
});
var x = defined(argv.x) ? argv.x : 0;
var y = defined(argv.y) ? argv.y : 0;
ctx.translate((argv.mirror ? 0 : -width) + x, fontsize + y);
var path = font.getPath(text, 0, 0, fontsize, null);
argv.rotate && ctx.rotate(TAU * parseFloat(argv.rotate))
if (argv.stroke) {
path.lineWidth = parseInt(argv.stroke);
path.stroke = 'black'
} else {
path.stroke = null;
}
if (!defined(argv.fill) || argv.fill) {
path.fill = 'black';
} else {
path.fill = null;
}
path.draw(ctx);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment