Repository navigation
Expand file tree
/
Copy pathTextMaker.js
More file actions
131 lines (122 loc) · 4.24 KB
/
Copy pathTextMaker.js
File metadata and controls
131 lines (122 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as opentype from 'opentype.js';
import * as base64arraybuffer from 'base64-arraybuffer';
import * as THREE from 'three';
import * as exportSTL from 'Doodle3D/ThreeJS-export-STL';
// jspm install npm:opentype.js npm:base64-arraybuffer npm:three github:Doodle3D/ThreeJS-export-STL
// jspm run test7
// -> use three.js shapes for font
// -> dynamically download fonts: https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR-API-KEY
// -> live preview? / html version?
// -> test CSG: https://github.com/chandlerprall/ThreeCSG/blob/master/threeCSG.es6
function glyphToShapes(glyph) {
glyph.getMetrics();
let shapes = [];
let holes = [];
for (let contour of glyph.getContours()) {
let path = new THREE.Path();
let prev = null;
let curr = contour[contour.length - 1];
let next = contour[0];
if (curr.onCurve) {
path.moveTo(curr.x, curr.y);
} else {
if (next.onCurve) {
path.moveTo(next.x, next.y);
} else {
const start = {x: (curr.x + next.x) * 0.5, y: (curr.y + next.y) * 0.5};
path.moveTo(start.x, start.y);
}
}
for (let i = 0; i < contour.length; ++i) {
prev = curr;
curr = next;
next = contour[(i + 1) % contour.length];
if (curr.onCurve) {
path.lineTo(curr.x, curr.y);
} else {
let prev2 = prev;
let next2 = next;
if (!prev.onCurve) {
prev2 = { x: (curr.x + prev.x) * 0.5, y: (curr.y + prev.y) * 0.5 };
path.lineTo(prev2.x, prev2.y);
}
if (!next.onCurve) {
next2 = { x: (curr.x + next.x) * 0.5, y: (curr.y + next.y) * 0.5 };
}
path.lineTo(prev2.x, prev2.y);
path.quadraticCurveTo(curr.x, curr.y, next2.x, next2.y);
}
}
path.closePath();
var sum = 0;
var lastPoint = contour[contour.length-1];
for (let point of contour) {
sum += (lastPoint.x - point.x) * (point.y + lastPoint.y);
lastPoint = point;
}
if (sum > 0) {
holes.push(path);
} else {
let shape = new THREE.Shape();
shape.add(path);
shapes.push(shape);
}
}
for (let shape of shapes) {
shape.holes = holes;
}
return shapes;
}
function glyphToShapes2(glyph) {
let shapes = [];
// holes ?!
var shape = new THREE.Shape();
for (let cmd of glyph.getPath().commands) {
if (cmd.type == 'M') {
shape.moveTo(cmd.x, cmd.y);
} else if (cmd.type == 'L') {
shape.lineTo(cmd.x, cmd.y);
} else if (cmd.type == 'Q') {
shape.quadraticCurveTo(cmd.x1, cmd.y1, cmd.x, cmd.y);
} else if (cmd.type == 'Z') {
shape.closePath();
shapes.push(shape);
shape = new THREE.Shape();
} else {
console.log('CMD', cmd);
}
}
if (shape.curves.length > 0) shapes.push(shape);
return shapes;
}
export function stringToGeometry(font, string, size, width, kerning=0) {
let geometries = [];
var dx = 0;
font.forEachGlyph(string, 0, 0, size, {}, (glyph, x, y) => {
x += dx;
dx += (typeof kerning == 'number') ? kerning : kerning.shift();
let shapes = glyphToShapes(glyph);
let geometry = new THREE.ExtrudeGeometry(shapes, {
steps: 1,
amount: width,
bevelEnabled: false,
});
geometry.applyMatrix( new THREE.Matrix4().makeScale(1 / font.unitsPerEm * size, 1 / font.unitsPerEm * size, 1) );
geometry.applyMatrix( new THREE.Matrix4().makeTranslation(x, y, 0) );
geometries.push(geometry);
});
let geometry = geometries[0];
for (let i of geometries.slice(1)) geometry.merge(i);
return geometry;
}
export function loadFont(arg) {
let font = opentype.parse(arg);
return font;
}
export function geometryToSTL(geometry) {
let tmp = geometry.type;
geometry.type = 'Geometry'; // bug in exportSTL?
let data = exportSTL.fromGeometry(geometry);
geometry.type = tmp;
return data;
}