|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert plain text to BRF (Braille Ready Format) using liblouis. |
| 4 | +No TTS required — this is pure text-to-braille-dots translation. |
| 5 | +
|
| 6 | +Usage: python3 build-brf.py input.txt output.brf |
| 7 | +""" |
| 8 | +import sys |
| 9 | +import louis |
| 10 | + |
| 11 | +# NABCC (North American Braille Computer Code) lookup table. |
| 12 | +# Maps 6-dot braille cell bitmask (0-63) to ASCII character. |
| 13 | +# bit 0=dot1, bit1=dot2, bit2=dot3, bit3=dot4, bit4=dot5, bit5=dot6 |
| 14 | +NABCC = " A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)=" |
| 15 | + |
| 16 | + |
| 17 | +def unicode_braille_to_brf(text): |
| 18 | + """Convert Unicode braille characters (U+2800-U+283F) to NABCC ASCII.""" |
| 19 | + out = [] |
| 20 | + for ch in text: |
| 21 | + code = ord(ch) |
| 22 | + if 0x2800 <= code <= 0x283F: |
| 23 | + out.append(NABCC[code - 0x2800]) |
| 24 | + elif code < 128: |
| 25 | + out.append(ch) |
| 26 | + else: |
| 27 | + out.append(" ") |
| 28 | + return "".join(out) |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + input_file = sys.argv[1] if len(sys.argv) > 1 else "/tmp/book.txt" |
| 33 | + output_file = sys.argv[2] if len(sys.argv) > 2 else "output.brf" |
| 34 | + |
| 35 | + with open(input_file, encoding="utf-8", errors="replace") as f: |
| 36 | + lines = f.read().split("\n") |
| 37 | + |
| 38 | + brf_lines = [] |
| 39 | + for line in lines: |
| 40 | + if line.strip(): |
| 41 | + try: |
| 42 | + braille = louis.translateString(["en-ueb-g2.ctb"], line) |
| 43 | + brf_lines.append(unicode_braille_to_brf(braille)) |
| 44 | + except Exception: |
| 45 | + brf_lines.append(line[:80]) |
| 46 | + else: |
| 47 | + brf_lines.append("") |
| 48 | + |
| 49 | + output = "\n".join(brf_lines) |
| 50 | + with open(output_file, "w", encoding="ascii", errors="replace") as f: |
| 51 | + f.write(output) |
| 52 | + |
| 53 | + print(f"BRF written: {len(output):,} bytes, {len(brf_lines):,} lines") |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments