-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild-brf.py
More file actions
57 lines (45 loc) · 1.69 KB
/
Copy pathbuild-brf.py
File metadata and controls
57 lines (45 loc) · 1.69 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
#!/usr/bin/env python3
"""
Convert plain text to BRF (Braille Ready Format) using liblouis.
No TTS required — this is pure text-to-braille-dots translation.
Usage: python3 build-brf.py input.txt output.brf
"""
import sys
import louis
# NABCC (North American Braille Computer Code) lookup table.
# Maps 6-dot braille cell bitmask (0-63) to ASCII character.
# bit 0=dot1, bit1=dot2, bit2=dot3, bit3=dot4, bit4=dot5, bit5=dot6
NABCC = " A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)="
def unicode_braille_to_brf(text):
"""Convert Unicode braille characters (U+2800-U+283F) to NABCC ASCII."""
out = []
for ch in text:
code = ord(ch)
if 0x2800 <= code <= 0x283F:
out.append(NABCC[code - 0x2800])
elif code < 128:
out.append(ch)
else:
out.append(" ")
return "".join(out)
def main():
input_file = sys.argv[1] if len(sys.argv) > 1 else "/tmp/book.txt"
output_file = sys.argv[2] if len(sys.argv) > 2 else "output.brf"
with open(input_file, encoding="utf-8", errors="replace") as f:
lines = f.read().split("\n")
brf_lines = []
for line in lines:
if line.strip():
try:
braille = louis.translateString(["en-ueb-g2.ctb"], line)
brf_lines.append(unicode_braille_to_brf(braille))
except Exception:
brf_lines.append(line[:80])
else:
brf_lines.append("")
output = "\n".join(brf_lines)
with open(output_file, "w", encoding="ascii", errors="replace") as f:
f.write(output)
print(f"BRF written: {len(output):,} bytes, {len(brf_lines):,} lines")
if __name__ == "__main__":
main()