Maze generation with OpenType
Turning text into a maze with calting
Round 109 of code guessing asked participants to "make a maze". My submission renders a maze through a font, using calting rather than runtime RNG. The source is on GitHub.

The problem
A basic font maps each character to a fixed glyph. If every a maps to /, repeated input such as aaaa produces a stripe rather than a varied maze.
The demonstration compares the font with calting disabled and enabled:
calt
OpenType’s calt feature (or contextual alternates) supports substitutions based on neighboring glyphs. I used these lookup rules to implement a deterministic state machine.
The initial design assigned each character 16 states (u0061.s0 through u0061.s15 for a). Eight variants render / and eight render \. A seeded shuffle assigned the shapes reproducibly during the build.
calt processes the line from left to right, so the previous glyph has an assigned state when the next substitution is evaluated. Each rule maps the previous state to the current glyph’s next state:
out += [f"sub @g{g}s{s} @any' lookup set{NEXT[g][s]};" for g in range(GROUPS) for s in range(STATES)]
This generates feature definitions such as:
feature calt {
sub @g0s0 @any' lookup set1;
sub @g0s1 @any' lookup set15;
sub @g0s2 @any' lookup set7;
...
} calt;
set1 maps a glyph to its state 1 variant. The transition table NEXT has 7 groups selected by the previous character’s codepoint mod 7. 7 groups and 16 states require 112 rules, allowing different characters to influence the resulting pattern.
Enter text to inspect its state transitions:
The first character uses a fixed initial state assigned during the font build. Spaces remain visually blank while carrying a state that maintains the transition chain.
Derangements were not enough
The first implementation generated each transition table as a derangement: a permutation in which no state maps to itself.
def derangement(n):
while True:
p = list(range(n))
random.shuffle(p)
if all(p[i] != i for i in range(n)):
return p
Preventing self-transitions was intended to stop repeated characters from remaining on a single slash indefinitely.
Derangements can still contain short cycles between multiple states. Only 8 letters traversed all 16 states in the initial version. The letters c, q, and x formed 2 state cycles, while g and j formed cycles in which both states rendered /. Repeated input therefore still produced stripes.
The following chart shows each letter’s cycle in the initial implementation:
Solution: Using a single transition cycle
I shuffled the 16 states into a sequence, linked each state to the next, and linked the final state back to the first:
def cycle(n):
order = list(range(n))
random.shuffle(order)
p = [0] * n
for i in range(n):
p[order[i]] = order[(i + 1) % n]
return p
This creates one cycle containing all 16 states. Repeated characters now visit 8 / states and 8 \ states before repeating. The algorithm also avoids retrying permutations until a valid derangement is found.
The updated cycles produce the following patterns:
Size
The initial design required 205 characters × 16 states or 3,280 glyphs. Each glyph referenced one of two outlines (_slash and _backslash), producing a font of approximately 230 KB.
I then investigated how to reduce the file size while preserving identical rendered output.
Sharing glyphs across character groups
The substitution rules depend only on the previous glyph’s group (codepoint mod 7) and state. They do not require its original character identity. For example, a and h both belong to group 6, so their state 3 variants can share a glyph.
The revised design assigns glyphs by group and state:
def family(cp):
return (cp % GROUPS, cp in SPACES)
FAMILIES = sorted({family(cp) for cp in CHARS})
def name(fam, s):
g, blank = fam
return f"g{g}{'b' if blank else ''}.s{s}"
Spaces use separate blank families. The two supported space characters (0x20 and 0xA0) occupy different groups, giving 9 families × 16 states (144 glyphs in total).
The character map (cmap) retains each character’s initial state and points to the corresponding family glyph. Sharing glyphs therefore preserves the visible output.
Each substitution class was reduced from 205 glyphs to 9.
Glyph names
The original font stored names such as u0061.s0 for all 3,280 glyphs. These names supported tooling rather than rendering and occupied 37 KB. I removed them.
Results
I compared the original and optimized fonts using 3,003 randomly generated strings. Every rendered slash matched. The optimized font was 12 KB compared with approximately 230 KB for the original.
The font retains the name information required for Windows installation and the ascent and descent values needed to prevent clipping.
The output is deterministic: identical input produces an identical maze. Copying the rendered text preserves the original characters typed because the maze exists only in the glyph rendering.