-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
521 lines (481 loc) ยท 14.8 KB
/
build.rs
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use std::{env, fmt::Write, fs, path::Path};
#[path = "src/int.rs"]
mod int;
use self::int::BinInteger as _;
fn main() {
let mut glyphsets_rs = String::new();
for &glyph_set_in in GLYPH_SETS {
process_one_glyph_set(&mut glyphsets_rs, glyph_set_in);
}
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("glyphsets.rs");
fs::write(&dest_path, glyphsets_rs).unwrap();
println!("cargo:rerun-if-changed=build.rs");
}
fn process_one_glyph_set(glyphsets_rs: &mut String, gs: &GlyphSetIn) {
macro_rules! wl {
($($tt:tt)*) => { writeln!(glyphsets_rs, $($tt)*).unwrap() };
}
let mask_dims = gs.mask_dims;
#[derive(Clone, Copy)]
struct IndexEnt {
glyph_i: usize,
distance: usize,
}
let mut index = vec![None; 1 << (mask_dims[0] * mask_dims[1])];
// Put the exact matches
for (glyph_i, &(_, mask)) in gs.glyphs.iter().enumerate() {
let mask = decode_mask(mask, mask_dims);
if index[mask as usize].is_some() {
// If there are two glyphs with identical masks, the first one
// takes precedence
continue;
}
index[mask as usize] = Some(IndexEnt {
glyph_i,
distance: 0,
});
}
// Mutate known patterns to create lesser matches
let mut last_distance = 0;
for base_distance in 0.. {
let mut should_continue = false;
for mask in 0..index.len() {
if let Some(IndexEnt { distance, glyph_i }) = index[mask] {
if distance != base_distance {
// Already processed by a previous iteration
continue;
}
mutate_fragment_by_dilation_and_erosion(mask_dims, mask as Fragment, |new_mask| {
if index[new_mask as usize].is_some() {
return;
}
index[new_mask as usize] = Some(IndexEnt {
glyph_i,
distance: base_distance + 1,
});
eprintln!("{:09b} -> {:09b}, {}", mask, new_mask, base_distance + 1);
should_continue = true;
});
}
}
if !should_continue {
break;
}
last_distance = base_distance;
}
// The above mutation technique doesn't cover the entire space. Now bring a
// bigger gun
for base_distance in 0.. {
let mut should_continue = false;
for mask in 0..index.len() {
if let Some(IndexEnt { distance, glyph_i }) = index[mask] {
if distance != base_distance {
// Already processed by a previous iteration
continue;
}
mutate_fragment_unconditional(mask_dims, mask as Fragment, |new_mask| {
if index[new_mask as usize].is_some() {
return;
}
index[new_mask as usize] = Some(IndexEnt {
glyph_i,
distance: base_distance + 1,
});
eprintln!("{:09b} -> {:09b}, {}", mask, new_mask, base_distance + 1);
should_continue = true;
});
}
}
if !should_continue && base_distance > last_distance {
break;
}
}
let max_glyph_len = gs.glyphs.iter().map(|x| x.0.len()).max().unwrap();
wl!("pub const {}: &dyn GlyphSet =", gs.const_name);
wl!(" &IndexedGlyphSet {{");
wl!(
" mask_dims: [{}, {}],",
gs.mask_dims[0],
gs.mask_dims[1]
);
wl!(
" mask_overlap: [{}, {}],",
gs.mask_overlap[0],
gs.mask_overlap[1]
);
wl!(" max_glyph_len: {},", max_glyph_len);
wl!(" index: &[");
for ent in index.iter() {
wl!(
" r##\"{}\"##,",
gs.glyphs[ent.unwrap().glyph_i].0
);
}
wl!(" ],");
wl!(" }};");
}
/// Mutate a fragment.
fn mutate_fragment_by_dilation_and_erosion(
[w, h]: [usize; 2],
frag: Fragment,
mut cb: impl FnMut(Fragment),
) {
let mut i = 0;
for y in 0..h {
for x in 0..w {
let cur = frag.get_bit(i);
// Deny dilation (the outcome is opposite - the result image will
// be dilated)
if cur {
continue;
}
// try copying this pixel to a neighboring one
for &(sx, sy) in &[(-1isize, 0), (1, 0), (0, -1isize), (0, 1)] {
let nx = x.wrapping_add(sx as usize);
let ny = y.wrapping_add(sy as usize);
if nx >= w || ny >= h {
continue; // OOB
}
let ni = (nx + ny * w) as u32;
let nei = frag.get_bit(ni);
if cur == nei {
continue; // no change
}
// mutate
let new_frag = frag ^ (1 << ni);
cb(new_frag);
}
i += 1;
}
}
}
/// Mutate a fragment differently.
fn mutate_fragment_unconditional([w, h]: [usize; 2], frag: Fragment, mut cb: impl FnMut(Fragment)) {
for i in 0..w * h {
cb(frag & !(1 << i));
}
}
const GLYPH_SETS: &[&GlyphSetIn] = &[
&GLYPH_SET_SLC,
&GLYPH_SET_MS_2X3,
&GLYPH_SET_1X1,
&GLYPH_SET_1X2,
&GLYPH_SET_2X2,
&GLYPH_SET_2X3,
];
/// A small bitmap image, whose dimensions are specified implciitly (e.g., by
/// `GlyphSetIn::mask_dims`).
type Fragment = u32;
struct GlyphSetIn {
const_name: &'static str,
mask_dims: [usize; 2],
mask_overlap: [usize; 2],
glyphs: &'static [(&'static str, Fragment)],
}
/// The masks in `GlyphSetIn` are in reverse order so that their writing
/// direction matches the visual order. This function converts a mask to
/// the standard order (LSB = upper left corner, MSB = lower right corner).
fn decode_mask(mask: Fragment, mask_dims: [usize; 2]) -> Fragment {
mask.reverse_bits() >> (32 - (mask_dims[0] * mask_dims[1]))
}
const GLYPH_SET_SLC: GlyphSetIn = GlyphSetIn {
const_name: "GLYPH_SET_SLC",
mask_dims: [3, 3],
mask_overlap: [0, 0],
glyphs: &[
(" ", 0b000_000_000),
("โ", 0b010_111_010),
("๐ฌ", 0b000_110_000),
("๐ฌ", 0b000_100_000),
("โน", 0b010_010_000),
("๐ฌ", 0b000_011_000),
("๐ฌ", 0b000_001_000),
("โป", 0b000_010_010),
("โ", 0b111_111_111),
("โ", 0b110_110_110),
("โ", 0b100_100_100),
("๐ฌ", 0b110_000_000),
("๐ฌ", 0b011_000_000),
("๐ฌ", 0b111_000_000),
("๐ฌ", 0b000_111_000),
("๐ฌ", 0b111_111_000),
("๐ฌ", 0b000_000_110),
("๐ฌ", 0b000_000_011),
("๐ฌญ", 0b000_000_111),
("๐ฌฐ", 0b111_000_111),
("๐ฌน", 0b000_111_111),
("๐ฌผ", 0b000_000_100),
("๐ฌพ", 0b000_100_100),
("๐ฌฟ", 0b000_100_111),
("๐ฌฟ", 0b000_110_111),
("๐ญ", 0b100_100_110),
("๐ญ", 0b100_110_110),
("๐ญ", 0b011_111_111),
("๐ญ", 0b001_111_111),
("๐ญ", 0b001_011_111),
("๐ญ
", 0b011_011_111),
("๐ญ", 0b000_001_111),
("๐ญ", 0b000_011_111),
("๐ญ", 0b000_000_001),
("๐ญ", 0b000_001_001),
("๐ญ", 0b110_111_111),
("๐ญ", 0b100_111_111),
("๐ญ", 0b110_110_111),
("๐ญ", 0b111_111_011),
("๐ญ", 0b111_011_001),
("๐ญ", 0b111_011_011),
("๐ญ", 0b100_000_000),
("๐ญ", 0b100_100_000),
("๐ญ", 0b111_100_000),
("๐ญ", 0b111_110_000),
("๐ญ", 0b111_111_110),
("๐ญ", 0b111_111_100),
("๐ญ ", 0b111_110_100),
("๐ญก", 0b111_110_110),
("๐ญข", 0b001_000_000),
("๐ญค", 0b001_001_000),
("๐ญง", 0b111_001_000),
("๐ญง", 0b111_011_000),
("๐ญจ", 0b111_011_111),
("๐ญฉ", 0b101_111_111),
("๐ญช", 0b111_110_111),
("๐ญซ", 0b111_111_101),
("๐ญฌ", 0b100_110_100),
("๐ญญ", 0b111_010_000),
("๐ญฎ", 0b001_011_001),
("๐ญฏ", 0b000_010_111),
("๐ฎ", 0b111_010_111),
("๐ฎ", 0b101_111_101),
("๐ญฒ", 0b010_010_010),
("๐ฎ", 0b001_001_001),
("๐ฎ", 0b011_011_011),
],
};
/// Marching square approximation using Symbols for Legacy Computing.
///
/// The following exterior verticies are permitted:
///
/// โโโโXโโโโ
/// โ โ
/// X X
/// โ โ
/// โโโโโโโโโค
/// โ โ
/// X X
/// โ โ
/// โโโโXโโโโ
///
/// The glyph outline is unambigous, so glyphs will connect to others seemlessly.
const GLYPH_SET_MS_2X3: GlyphSetIn = GlyphSetIn {
const_name: "GLYPH_SET_MS_2X3",
mask_dims: [2, 3],
mask_overlap: [1, 1],
glyphs: &[
(" ", 0b00_00_00),
("โ", 0b11_11_11),
("๐ฌ", 0b11_00_00),
("๐ฌ", 0b00_11_00),
("๐ฌ", 0b11_11_00),
("๐ฌญ", 0b00_00_11),
("๐ฌฐ", 0b11_00_11),
("๐ฌน", 0b00_11_11),
// -----
("๐ญ", 0b01_11_11),
("๐ญ", 0b01_01_11),
("๐ญ", 0b00_01_11),
("๐ญ", 0b00_00_01),
("๐ญ", 0b00_01_01),
// -----
("๐ฌผ", 0b00_00_10),
("๐ญ", 0b10_11_11),
("๐ญ", 0b10_10_11),
("๐ญ", 0b00_10_11),
("๐ฌพ", 0b00_10_10),
// -----
("๐ญ", 0b10_00_00),
("๐ญ", 0b10_10_00),
("๐ญ", 0b11_10_00),
("๐ญ", 0b11_10_10),
("๐ญ", 0b11_11_10),
// -----
("๐ญข", 0b01_00_00),
("๐ญค", 0b01_01_00),
("๐ญง", 0b11_01_00),
("๐ญ", 0b11_01_01),
("๐ญ", 0b11_11_01),
// -----
("โ", 0b10_10_10),
("โ", 0b01_01_01),
// -----
// last resort; introduces extra interior verices
("๐ฌ", 0b10_00_00),
("๐ฌ", 0b01_00_00),
("๐ฌ", 0b11_00_00),
("๐ฌ", 0b00_10_00),
("๐ฌ", 0b10_10_00),
("๐ฌ
", 0b01_10_00),
("๐ฌ", 0b11_10_00),
("๐ฌ", 0b00_01_00),
("๐ฌ", 0b10_01_00),
("๐ฌ", 0b01_01_00),
("๐ฌ", 0b11_01_00),
("๐ฌ", 0b00_11_00),
("๐ฌ", 0b10_11_00),
("๐ฌ", 0b01_11_00),
("๐ฌ", 0b11_11_00),
("๐ฌ", 0b00_00_10),
("๐ฌ", 0b10_00_10),
("๐ฌ", 0b01_00_10),
("๐ฌ", 0b11_00_10),
("๐ฌ", 0b00_10_10),
("โ", 0b10_10_10),
("๐ฌ", 0b01_10_10),
("๐ฌ", 0b11_10_10),
("๐ฌ", 0b00_01_10),
("๐ฌ", 0b10_01_10),
("๐ฌ", 0b01_01_10),
("๐ฌ", 0b11_01_10),
("๐ฌ", 0b00_11_10),
("๐ฌ", 0b10_11_10),
("๐ฌ", 0b01_11_10),
("๐ฌ", 0b11_11_10),
("๐ฌ", 0b00_00_01),
("๐ฌ", 0b10_00_01),
("๐ฌ ", 0b01_00_01),
("๐ฌก", 0b11_00_01),
("๐ฌข", 0b00_10_01),
("๐ฌฃ", 0b10_10_01),
("๐ฌค", 0b01_10_01),
("๐ฌฅ", 0b11_10_01),
("๐ฌฆ", 0b00_01_01),
("โ", 0b01_01_01),
("๐ฌง", 0b10_01_01),
("๐ฌจ", 0b11_01_01),
("๐ฌฉ", 0b00_11_01),
("๐ฌช", 0b10_11_01),
("๐ฌซ", 0b01_11_01),
("๐ฌฌ", 0b11_11_01),
("๐ฌญ", 0b00_00_11),
("๐ฌฎ", 0b10_00_11),
("๐ฌฏ", 0b01_00_11),
("๐ฌฐ", 0b11_00_11),
("๐ฌฑ", 0b00_10_11),
("๐ฌฒ", 0b10_10_11),
("๐ฌณ", 0b01_10_11),
("๐ฌด", 0b11_10_11),
("๐ฌต", 0b00_01_11),
("๐ฌถ", 0b10_01_11),
("๐ฌท", 0b01_01_11),
("๐ฌธ", 0b11_01_11),
("๐ฌน", 0b00_11_11),
("๐ฌบ", 0b10_11_11),
("๐ฌป", 0b01_11_11),
],
};
const GLYPH_SET_1X1: GlyphSetIn = GlyphSetIn {
const_name: "GLYPH_SET_1X1",
mask_dims: [1, 1],
mask_overlap: [0, 0],
glyphs: &[("โ", 0b1), (" ", 0b0)],
};
const GLYPH_SET_1X2: GlyphSetIn = GlyphSetIn {
const_name: "GLYPH_SET_1X2",
mask_dims: [1, 2],
mask_overlap: [0, 0],
glyphs: &[("โ", 0b1_1), (" ", 0b0_0), ("โ", 0b1_0), ("โ", 0b0_1)],
};
const GLYPH_SET_2X2: GlyphSetIn = GlyphSetIn {
const_name: "GLYPH_SET_2X2",
mask_dims: [2, 2],
mask_overlap: [0, 0],
glyphs: &[
("โ", 0b11_11),
("โ", 0b00_10),
("โ", 0b00_01),
("โ", 0b10_00),
("โ", 0b10_11),
("โ", 0b10_01),
("โ", 0b11_10),
("โ", 0b11_01),
("โ", 0b01_00),
("โ", 0b01_10),
("โ", 0b01_11),
(" ", 0b00_00),
("๐ฌ", 0b11_00),
("๐ฌน", 0b00_11),
("โ", 0b10_10),
("โ", 0b01_01),
],
};
const GLYPH_SET_2X3: GlyphSetIn = GlyphSetIn {
const_name: "GLYPH_SET_2X3",
mask_dims: [2, 3],
mask_overlap: [0, 0],
glyphs: &[
("โ", 0b11_11_11),
(" ", 0b00_00_00),
("๐ฌ", 0b10_00_00),
("๐ฌ", 0b01_00_00),
("๐ฌ", 0b11_00_00),
("๐ฌ", 0b00_10_00),
("๐ฌ", 0b10_10_00),
("๐ฌ
", 0b01_10_00),
("๐ฌ", 0b11_10_00),
("๐ฌ", 0b00_01_00),
("๐ฌ", 0b10_01_00),
("๐ฌ", 0b01_01_00),
("๐ฌ", 0b11_01_00),
("๐ฌ", 0b00_11_00),
("๐ฌ", 0b10_11_00),
("๐ฌ", 0b01_11_00),
("๐ฌ", 0b11_11_00),
("๐ฌ", 0b00_00_10),
("๐ฌ", 0b10_00_10),
("๐ฌ", 0b01_00_10),
("๐ฌ", 0b11_00_10),
("๐ฌ", 0b00_10_10),
("โ", 0b10_10_10),
("๐ฌ", 0b01_10_10),
("๐ฌ", 0b11_10_10),
("๐ฌ", 0b00_01_10),
("๐ฌ", 0b10_01_10),
("๐ฌ", 0b01_01_10),
("๐ฌ", 0b11_01_10),
("๐ฌ", 0b00_11_10),
("๐ฌ", 0b10_11_10),
("๐ฌ", 0b01_11_10),
("๐ฌ", 0b11_11_10),
("๐ฌ", 0b00_00_01),
("๐ฌ", 0b10_00_01),
("๐ฌ ", 0b01_00_01),
("๐ฌก", 0b11_00_01),
("๐ฌข", 0b00_10_01),
("๐ฌฃ", 0b10_10_01),
("๐ฌค", 0b01_10_01),
("๐ฌฅ", 0b11_10_01),
("๐ฌฆ", 0b00_01_01),
("โ", 0b01_01_01),
("๐ฌง", 0b10_01_01),
("๐ฌจ", 0b11_01_01),
("๐ฌฉ", 0b00_11_01),
("๐ฌช", 0b10_11_01),
("๐ฌซ", 0b01_11_01),
("๐ฌฌ", 0b11_11_01),
("๐ฌญ", 0b00_00_11),
("๐ฌฎ", 0b10_00_11),
("๐ฌฏ", 0b01_00_11),
("๐ฌฐ", 0b11_00_11),
("๐ฌฑ", 0b00_10_11),
("๐ฌฒ", 0b10_10_11),
("๐ฌณ", 0b01_10_11),
("๐ฌด", 0b11_10_11),
("๐ฌต", 0b00_01_11),
("๐ฌถ", 0b10_01_11),
("๐ฌท", 0b01_01_11),
("๐ฌธ", 0b11_01_11),
("๐ฌน", 0b00_11_11),
("๐ฌบ", 0b10_11_11),
("๐ฌป", 0b01_11_11),
],
};