-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsketch.js
More file actions
288 lines (228 loc) · 7.45 KB
/
sketch.js
File metadata and controls
288 lines (228 loc) · 7.45 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// GridFont by Anders Hoff (2019)
// A grid-based system for designing simple symbols and fonts.
// Includes example fonts. Mainly intended for plotter drawings.
// From: https://github.com/inconvergent/gridfont
// Ported to p5.js by Golan Levin, April 2025
// Font data is stored in the files:
// hoff_gridfont_original-all-data.json
// hoff_gridfont_smooth-all-data.json
// Note that Hoff's fonts only support lowercase.
let originalJson;
let smoothJson;
let gridfontOriginal;
let gridfontSmooth;
DEFAULT_SCALE = 5.0; // Default scale (pixels per grid unit)
DEFAULT_XDST = 7.0; // Fixed extra spacing between glyphs (pixels)
function preload() {
originalJson = loadJSON("hoff_gridfont_original-all-data.json");
smoothJson = loadJSON("hoff_gridfont_smooth-all-data.json");
}
function setup() {
createCanvas(800,650);
gridfontOriginal = new AndersHoffGridFont(originalJson);
gridfontSmooth = new AndersHoffGridFont(smoothJson);
}
function draw() {
background("black");
stroke("white");
strokeJoin(ROUND);
noFill();
let sc = DEFAULT_SCALE;
let th = 1; // stroke weight
gridfontOriginal.drawString("abcdefghijklmnopqrstuvwxyz", 50, 50, sc, th);
gridfontOriginal.drawString("1234567890", 50, 100, sc, th);
gridfontOriginal.drawString("*&+${}()/[]^=-!.,<>'#|_:;@%?~\"", 50, 150, sc, th);
gridfontOriginal.drawString("the quick (brown) fox?", 50, 200, sc, th);
gridfontOriginal.drawString("jumps over the lazy dog.", 50, 250, sc, th);
gridfontSmooth.drawString("abcdefghijklmnopqrstuvwxyz", 50, 350, sc, th);
gridfontSmooth.drawString("1234567890", 50, 400, sc, th);
gridfontSmooth.drawString("*&+${}()/[]^=-!.,<>'#|_:;@%?~\"", 50, 450, sc, th);
gridfontSmooth.drawString("the quick (brown) fox?", 50, 500, sc, th);
gridfontSmooth.drawString("jumps over the lazy dog.", 50, 550, sc, th);
}
// ---------------------------
class AndersHoffGridFont {
constructor(fontData) {
this.special = fontData.special;
this.compass = fontData.compass;
this.groups = fontData.groups;
this.symbols = fontData.symbols;
this.commands = new Set([]);
for (let key in this.compass) {
this.commands.add(key);
}
for (let key in this.special) {
this.commands.add(this.special[key]);
}
this.tokenizer = this.buildTokenizer();
}
buildTokenizer() {
// Build a regex pattern to recognize all commands
// A command is a letter like 'L', 'N', 'D', 'M', etc.,
// optionally followed by numbers, fractions, or commas
// (e.g., "L3", "p1/2", "M0,6")
// 1: Turn the command Set into a string like 'LNRSeptqDMWHZ'
let commandLetters = Array.from(this.commands).join("");
// 2: Build a regex pattern
// This means: a command letter, followed by optional
// numbers, commas, dots, slashes
let pattern = `[${commandLetters}][0-9,./]*`;
// 3: Create and return the regular expression
return new RegExp(pattern, "g");
}
parseRaw(symbolObj) {
// Parse the 'raw' mini-language of a symbol
// into a set of drawable paths
// If the symbol has no raw definition, return an empty array
if (!symbolObj.raw) return [];
// Step 1: Split the raw string into two parts:
// - info: like 'S4,9' (size of the symbol grid)
// - rawPaths: drawing instructions like 'M3,2DS4|(c-arc)'
let [info, rawPaths] = symbolObj.raw.split(":");
// Step 2: Parse the info to get the grid width and height
let [gw, gh] = this.parseInfo(info);
// Step 3: Split the rawPaths by '|'
// (each '|' separates a different drawing path)
// Trim whitespace, then parse each individual path
let paths = rawPaths
.split("|")
.map((path) => this.parsePath((gw, gh), path.trim()));
// Step 4: Return the parsed paths
return paths;
}
parseInfo(info) {
if (!info.startsWith("S")) throw new Error("Info must start with S");
let parts = info.substring(1).split(",");
return [parseInt(parts[0]), parseInt(parts[1])];
}
expandGroups(path) {
let expanded = path;
let changed = true;
while (changed) {
changed = false;
for (let [key, val] of Object.entries(this.groups)) {
if (expanded.includes(key)) {
expanded = expanded.replaceAll(key, val);
changed = true;
}
}
}
return expanded;
}
parsePath(bbox, rawPath) {
let path = [];
let state = [0, 0, false]; // x, y, penDown
rawPath = this.expandGroups(rawPath);
let tokens = rawPath.match(this.tokenizer) || [];
for (let token of tokens) {
let cmd = token[0];
let arg = token.substring(1);
let parsedArg = this.parseArg(arg);
state = this.doCommand(
path.length > 0 ? path[0] : [0, 0],
state,
bbox,
cmd,
parsedArg,
path
);
}
return path;
}
parseArg(arg) {
if (!arg) return 1;
if (arg.includes(",")) {
return arg.split(",").map((v) => this.fractFloat(v));
}
return this.fractFloat(arg);
}
fractFloat(v) {
if (v.includes("/")) {
let [num, den] = v.split("/").map(Number);
return num / den;
}
return parseFloat(v);
}
relMove(state, delta, arg = 1) {
if (Array.isArray(arg)) {
return [state[0] + delta[0] * arg[0], state[1] + delta[1] * arg[1]];
} else {
return [state[0] + delta[0] * arg, state[1] + delta[1] * arg];
}
}
doCommand(start, state, bbox, cmd, arg, path) {
let [x, y, penDown] = state;
if (this.compass[cmd]) {
let [dx, dy] = this.compass[cmd];
let [nx, ny] = this.relMove([x, y], [dx, dy], arg);
if (penDown) path.push([x, y]);
if (penDown) path.push([nx, ny]);
return [nx, ny, penDown];
}
if (cmd === this.special.abs_move) {
if (Array.isArray(arg)) {
return [arg[0], arg[1], penDown];
}
return [arg, arg, penDown];
}
if (cmd === this.special.pen_down) {
return [x, y, true];
}
if (cmd === this.special.start) {
return [start[0], start[1], penDown];
}
if (cmd === this.special.left) {
return [0, y, penDown];
}
if (cmd === this.special.right) {
return [bbox[0] - 1, y, penDown];
}
if (cmd === this.special.top) {
return [x, 0, penDown];
}
if (cmd === this.special.bottom) {
return [x, bbox[1] - 1, penDown];
}
throw new Error("Unknown command: " + cmd);
}
drawSymbol(sym, x, y, sc = 20, th = 1) {
if (!this.symbols[sym]) {
console.warn("Symbol not found:", sym);
return;
}
let symbolObj = this.symbols[sym];
let paths = symbolObj.paths || this.parseRaw(symbolObj);
symbolObj.paths = paths;
push();
translate(x, y);
scale(sc, sc);
strokeWeight(th / sc);
noFill();
for (let path of paths) {
beginShape();
for (let pt of path) {
vertex(pt[0], pt[1]);
}
endShape();
}
pop();
}
drawString(txt, startX, startY, sc = DEFAULT_SCALE, th = 1) {
txt = txt.toLowerCase(); // Hoff only supports lowercase.
let x = startX;
let y = startY;
let xdst = DEFAULT_XDST;
for (let c of txt) {
if (!this.symbols[c]) {
// Handle missing char: advance by 1 grid unit + spacing
x += sc + xdst;
continue;
}
let symbolObj = this.symbols[c];
this.drawSymbol(c, x, y, sc, th);
// Advance by glyph width scaled + fixed spacing
let glyphWidth = symbolObj.w !== undefined ? symbolObj.w : 1;
x += glyphWidth * sc + xdst;
}
}
}