Skip to content

Commit fe2e848

Browse files
committed
add command line help
1 parent 7a46528 commit fe2e848

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
"dependencies": {
77
"gcanvas": "^0.0.24",
88
"opentype.js": "^0.3.0",
9-
"optimist": "^0.6.1"
9+
"yargs": "^1.2.5"
1010
},
1111
"devDependencies": {},
12+
"bin": {
13+
"text2gcode": "text2gcode.js"
14+
},
1215
"scripts": {
1316
"test": "echo \"Error: no test specified\" && exit 1"
1417
},

text2gcode.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env node
2+
3+
var opentype = require('opentype.js');
4+
var argv = require('yargs')
5+
.usage('$0 --font=/path/to/text.ttf --diameter --depth=-5 "hello world"')
6+
.demand('font')
7+
.describe('font', '/path/to/font.ttf')
8+
.demand('depth')
9+
.describe('depth', 'total depth to go (may be negative if your machine is normal)')
10+
.demand('diameter')
11+
.describe('diameter', 'cutting tool diameter')
12+
.describe('fs', 'size of the font in world units (mm/in)')
13+
.default('fs', 20)
14+
.describe('pass', 'total depth taken off per layer (depth per pass)')
15+
.describe('rotate', 'rotate by % TAU (i.e. --rotate=.25 means 90 degrees)')
16+
.default('rotate', 0)
17+
.describe('mirror', 'mirror on the x axis')
18+
.default('mirror', false)
19+
.default('pass', .1)
20+
.default('x', 0)
21+
.describe('x', 'translate on the x')
22+
.default('y', 0)
23+
.describe('y', 'translate on the y')
24+
.argv;
25+
26+
27+
var gcanvas = require('gcanvas');
28+
29+
var text = argv._[0];
30+
var fontFile = argv.font;
31+
var size = text
32+
var ctx = new (require('gcanvas'));
33+
var fontsize = argv.fs || 30;
34+
35+
var TAU = Math.PI*2;
36+
37+
var defined = function(a) {
38+
return typeof a !== 'undefined';
39+
}
40+
41+
opentype.load(argv.font, function(err, font) {
42+
if (err) {
43+
throw err;
44+
}
45+
46+
ctx.depth = argv.depth || -5;
47+
ctx.depthOfCut = argv.pass || .1;
48+
49+
ctx.scale(argv.mirror ? 1 : -1, 1)
50+
ctx.toolDiameter = argv.diameter || 1.6;
51+
52+
var width = 0;
53+
font.forEachGlyph(text + ' ', 0, 0, fontsize, null, function(glyph, x, y) {
54+
width = x;
55+
});
56+
57+
var x = defined(argv.x) ? argv.x : 0;
58+
var y = defined(argv.y) ? argv.y : 0;
59+
60+
61+
ctx.translate((argv.mirror ? 0 : -width) + x, fontsize + y);
62+
var path = font.getPath(text, 0, 0, fontsize, null);
63+
64+
argv.rotate && ctx.rotate(TAU * parseFloat(argv.rotate))
65+
66+
if (argv.stroke) {
67+
path.lineWidth = parseInt(argv.stroke);
68+
path.stroke = 'black'
69+
} else {
70+
path.stroke = null;
71+
}
72+
73+
if (!defined(argv.fill) || argv.fill) {
74+
path.fill = 'black';
75+
} else {
76+
path.fill = null;
77+
}
78+
79+
path.draw(ctx);
80+
});

0 commit comments

Comments
 (0)