| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Script to generate gcc/text-art/box-drawing-chars.inc |
| 4 |
# |
| 5 |
# This file is part of GCC. |
| 6 |
# |
| 7 |
# GCC is free software; you can redistribute it and/or modify it under |
| 8 |
# the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation; either version 3, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
# for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with GCC; see the file COPYING3. If not see |
| 19 |
# <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
import unicodedata |
| 22 |
|
| 23 |
def get_box_drawing_char_name(up: bool, |
| 24 |
down: bool, |
| 25 |
left: bool, |
| 26 |
right: bool) -> str: |
| 27 |
if 0: |
| 28 |
print(f'{locals()=}') |
| 29 |
if up and down: |
| 30 |
vertical = True |
| 31 |
up = False |
| 32 |
down = False |
| 33 |
else: |
| 34 |
vertical = False |
| 35 |
|
| 36 |
if left and right: |
| 37 |
horizontal = True |
| 38 |
left = False |
| 39 |
right = False |
| 40 |
else: |
| 41 |
horizontal = False |
| 42 |
|
| 43 |
weights = [] |
| 44 |
heavy = [] |
| 45 |
light = [] |
| 46 |
dirs = [] |
| 47 |
for dir_name in ('up', 'down', 'vertical', 'left', 'right', 'horizontal'): |
| 48 |
val = locals()[dir_name] |
| 49 |
if val: |
| 50 |
dirs.append(dir_name.upper()) |
| 51 |
|
| 52 |
if not dirs: |
| 53 |
return 'SPACE' |
| 54 |
|
| 55 |
name = 'BOX DRAWINGS' |
| 56 |
#print(f'{light=} {heavy=}') |
| 57 |
|
| 58 |
if 0: |
| 59 |
print(dirs) |
| 60 |
|
| 61 |
def weights_frag(weight: str, dirs: list, prefix: bool): |
| 62 |
""" |
| 63 |
Generate a fragment where all directions share the same weight, e.g.: |
| 64 |
'HEAVY HORIZONTAL' |
| 65 |
'DOWN LIGHT' |
| 66 |
'LEFT DOWN HEAVY' |
| 67 |
'HEAVY DOWN AND RIGHT' |
| 68 |
""" |
| 69 |
assert len(dirs) >= 1 |
| 70 |
assert len(dirs) <= 2 |
| 71 |
if prefix: |
| 72 |
return f' {weight} ' + (' AND '.join(dirs)) |
| 73 |
else: |
| 74 |
return ' ' + (' '.join(dirs)) + f' {weight}' |
| 75 |
|
| 76 |
assert(len(dirs) >= 1 and len(dirs) <= 2) |
| 77 |
name += weights_frag('LIGHT', dirs, True) |
| 78 |
|
| 79 |
return name |
| 80 |
|
| 81 |
print('/* Generated by contrib/unicode/gen-box-drawing-chars.py. */') |
| 82 |
print() |
| 83 |
for i in range(16): |
| 84 |
up = (i & 8) |
| 85 |
down = (i & 4) |
| 86 |
left = (i & 2) |
| 87 |
right = (i & 1) |
| 88 |
name = get_box_drawing_char_name(up, down, left, right) |
| 89 |
if i < 15: |
| 90 |
trailing_comma = ',' |
| 91 |
else: |
| 92 |
trailing_comma = ' ' |
| 93 |
unichar = unicodedata.lookup(name) |
| 94 |
print(f'0x{ord(unichar):04X}{trailing_comma} /* "{unichar}": U+{ord(unichar):04X}: {name} */') |