Last active
August 29, 2015 14:01
-
-
Save tmpvar/a17ad8b7101776d345ba to your computer and use it in GitHub Desktop.
turn TTF + text into a a toolpath
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
node text2gcode.js --font=/Users/tmpvar/Desktop/sg12-webfont.ttf "hello world" |
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
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