parent
02a3dcbaa2
commit
0df9743c9a
112
fonttool.py
112
fonttool.py
@ -1,22 +1,31 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import argparse
|
||||
|
||||
CP_MAX = 0x10FFFF
|
||||
FONT_CPP = "data/font.cpp"
|
||||
FONT_HEIGHT = 12
|
||||
PTRS_PER_LINE = 8
|
||||
|
||||
|
||||
class ReadBDFError(RuntimeError):
|
||||
def __init__(self, line_number, message):
|
||||
super().__init__(self, 'line %i: %s' % (line_number, message))
|
||||
|
||||
|
||||
class FontTool:
|
||||
def __init__(self):
|
||||
with open(FONT_CPP) as font_cpp:
|
||||
self.font_cpp_data = font_cpp.read()
|
||||
font_data = ([ int(s, 16) for s in re.findall(r'\w+', re.search(r'font_data[^{]*{([^;]+);', self.font_cpp_data, re.MULTILINE | re.DOTALL)[1]) ])
|
||||
font_ptrs = ([ int(s, 16) for s in re.findall(r'\w+', re.search(r'font_ptrs[^{]*{([^;]+);', self.font_cpp_data, re.MULTILINE | re.DOTALL)[1]) ])
|
||||
font_ranges = ([ int(s, 16) for s in re.findall(r'\w+', re.search(r'font_ranges[^{]*{([^;]+);', self.font_cpp_data, re.MULTILINE | re.DOTALL)[1]) ])
|
||||
font_data = ([int(s, 16) for s in re.findall(r'\w+', re.search(r'font_data[^{]*{([^;]+);', self.font_cpp_data,
|
||||
re.MULTILINE | re.DOTALL)[1])])
|
||||
font_ptrs = ([int(s, 16) for s in re.findall(r'\w+', re.search(r'font_ptrs[^{]*{([^;]+);', self.font_cpp_data,
|
||||
re.MULTILINE | re.DOTALL)[1])])
|
||||
font_ranges = ([int(s, 16) for s in re.findall(r'\w+',
|
||||
re.search(r'font_ranges[^{]*{([^;]+);', self.font_cpp_data,
|
||||
re.MULTILINE | re.DOTALL)[1])])
|
||||
self.code_points = [False for _ in range(CP_MAX + 2)]
|
||||
ptrs_ptr = 0
|
||||
for i in range(len(font_ranges) // 2 - 1):
|
||||
@ -36,8 +45,10 @@ class FontTool:
|
||||
in_range = True
|
||||
new_ranges.append([i])
|
||||
font_data_lines_hex = [['0x%02X' % v for v in d] for d in filter(lambda x: x, self.code_points)]
|
||||
font_data_lines = [ len(h) > 1 and h[0] + ', ' + ', '.join(h[1 :]) + ',' or '0x00, ' for h in font_data_lines_hex ]
|
||||
font_cpp_data = re.sub(r'font_data[^{]*{([^;]+);', 'font_data[] = {\n ' + '\n '.join(font_data_lines) + '\n};', self.font_cpp_data)
|
||||
font_data_lines = [len(h) > 1 and h[0] + ', ' + ', '.join(h[1:]) + ',' or '0x00, ' for h in
|
||||
font_data_lines_hex]
|
||||
font_cpp_data = re.sub(r'font_data[^{]*{([^;]+);',
|
||||
'font_data[] = {\n ' + '\n '.join(font_data_lines) + '\n};', self.font_cpp_data)
|
||||
font_ptrs_blocks = []
|
||||
data_ptr = 0
|
||||
for ran in new_ranges:
|
||||
@ -49,9 +60,12 @@ class FontTool:
|
||||
for i in range(0, len(block), PTRS_PER_LINE):
|
||||
font_ptrs_wrapped.append(', '.join(['0x%08X' % v for v in block[i: (i + PTRS_PER_LINE)]]))
|
||||
font_ptrs_blocks.append(',\n '.join(font_ptrs_wrapped))
|
||||
font_cpp_data = re.sub(r'font_ptrs[^{]*{([^;]+);', 'font_ptrs[] = {\n ' + ',\n\n '.join(font_ptrs_blocks) + ',\n};', font_cpp_data)
|
||||
font_cpp_data = re.sub(r'font_ptrs[^{]*{([^;]+);',
|
||||
'font_ptrs[] = {\n ' + ',\n\n '.join(font_ptrs_blocks) + ',\n};', font_cpp_data)
|
||||
font_ranges_lines = ['{ 0x%06X, 0x%06X },' % (r[0], r[1]) for r in new_ranges]
|
||||
font_cpp_data = re.sub(r'font_ranges[^{]*{([^;]+);', 'font_ranges[][2] = {\n ' + '\n '.join(font_ranges_lines) + '\n { 0, 0 },\n};', font_cpp_data)
|
||||
font_cpp_data = re.sub(r'font_ranges[^{]*{([^;]+);',
|
||||
'font_ranges[][2] = {\n ' + '\n '.join(font_ranges_lines) + '\n { 0, 0 },\n};',
|
||||
font_cpp_data)
|
||||
with open(FONT_CPP, 'w') as font_cpp:
|
||||
font_cpp.write(font_cpp_data)
|
||||
|
||||
@ -89,6 +103,7 @@ class FontTool:
|
||||
bits -= 2
|
||||
return cp_matrix
|
||||
|
||||
|
||||
class RawReader:
|
||||
def __init__(self, path):
|
||||
self.code_points = [False for _ in range(CP_MAX + 2)]
|
||||
@ -105,10 +120,8 @@ class RawReader:
|
||||
ptr += width * FONT_HEIGHT
|
||||
self.code_points[cp] = FontTool.pack(matrix)
|
||||
|
||||
|
||||
class BDFReader:
|
||||
class ReadBDFError:
|
||||
def __init__(line_number, message):
|
||||
super(RuntimeError, self).__init__('line %i: %s' % ( line_number, message ))
|
||||
|
||||
def __init__(self, path, xoffs, yoffs):
|
||||
self.code_points = [False for _ in range(CP_MAX + 2)]
|
||||
@ -136,7 +149,8 @@ class BDFReader:
|
||||
cv = 0
|
||||
xx = x + xoffs
|
||||
yy = FONT_HEIGHT - 1 - y + yoffs
|
||||
if xx >= char_bbx[2] and xx < char_bbx[0] + char_bbx[2] and yy >= char_bbx[3] and yy < char_bbx[1] + char_bbx[3]:
|
||||
if char_bbx[2] <= xx < char_bbx[0] + char_bbx[2] and char_bbx[3] <= yy < char_bbx[1] + \
|
||||
char_bbx[3]:
|
||||
cv = bitmap[char_bbx[1] - 1 - (yy - char_bbx[3])][xx - char_bbx[2]] * 3
|
||||
cp_matrix[-1].append(cv)
|
||||
self.code_points[char_cp] = FontTool.pack(cp_matrix)
|
||||
@ -190,69 +204,73 @@ class BDFReader:
|
||||
if not startchar:
|
||||
global_dw = char_dw
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
def print_usage_and_exit():
|
||||
print("""Usage:
|
||||
* fonttool.py addbdf FIRST LAST BDFFILE [XOFFS YOFFS]
|
||||
* fonttool.py addraw FIRST LAST RAWFILE
|
||||
* fonttool.py remove FIRST [LAST]
|
||||
* fonttool.py inspect FIRST [LAST]
|
||||
|
||||
LAST defaults to FIRST, XOFFS and YOFFS default to 0. BDF is an
|
||||
archaic bitmap font format; look it up.
|
||||
parser = argparse.ArgumentParser("fonttool.py", description="font tools for managing fonts, this script can be"
|
||||
" imported as a module",
|
||||
fromfile_prefix_chars="@")
|
||||
command = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
"Raw" files are simply ASCII-encoded white-space delimited lists
|
||||
addbdf = command.add_parser("addbdf", help="Adds BDF Formated Font")
|
||||
addbdf.add_argument("first", metavar="FIRST", type=int)
|
||||
addbdf.add_argument("last", metavar="LAST", type=int)
|
||||
addbdf.add_argument("bdffile", metavar="BDFFILE", help="BDF is an archaic bitmap font format")
|
||||
addbdf.add_argument("xoffs", metavar="XOFFS", nargs="?", default=0, type=int, help="Defaults to 0")
|
||||
addbdf.add_argument("yoffs", metavar="YOFFS", nargs="?", default=0, type=int, help="Defaults to 0")
|
||||
|
||||
addraw = command.add_parser("addraw", help="Adds a Raw Formated Font")
|
||||
addraw.add_argument("first", metavar="FIRST", type=int)
|
||||
addraw.add_argument("last", metavar="LAST", type=int)
|
||||
addraw.add_argument("rawfile", metavar="RAWFILE", help=""""Raw" files are simply ASCII-encoded white-space delimited \
|
||||
lists
|
||||
of decimal integer constants. These lists of integers encode
|
||||
characters as any number of consecutive character description
|
||||
structures laid out as follows:
|
||||
* the code point corresponding to the character being described;
|
||||
* the width in pixels of the character being described;
|
||||
* width times %i brightness levels between 0 and 3, a row-major matrix.
|
||||
* width times %i brightness levels between 0 and 3, a row-major matrix.""")
|
||||
|
||||
This script is also an importable module.""" % FONT_HEIGHT)
|
||||
exit(1)
|
||||
remove = command.add_parser("remove", help="Remove")
|
||||
remove.add_argument("first", metavar="FIRST", type=int)
|
||||
remove.add_argument("last", metavar="LAST", type=int, default=None, nargs="?", help="Defaults to FIRST")
|
||||
|
||||
if len(sys.argv) < 3:
|
||||
print_usage_and_exit()
|
||||
inspect = command.add_parser("inspect", help="Inspect")
|
||||
inspect.add_argument("first", metavar="FIRST", type=int)
|
||||
inspect.add_argument("last", metavar="LAST", type=int, default=None, nargs="?", help="Defaults to FIRST")
|
||||
|
||||
cp_first = int(sys.argv[2])
|
||||
if len(sys.argv) < 4:
|
||||
args = parser.parse_args()
|
||||
|
||||
cp_first = args.first
|
||||
if args.last is None:
|
||||
cp_last = cp_first
|
||||
else:
|
||||
cp_last = int(sys.argv[3])
|
||||
cp_last = args.last
|
||||
if cp_first < 0 or cp_last > CP_MAX or cp_first > cp_last:
|
||||
print('invalid range')
|
||||
exit(1)
|
||||
|
||||
ft = FontTool()
|
||||
|
||||
if sys.argv[1] == 'addbdf':
|
||||
if len(sys.argv) < 5:
|
||||
print_usage_and_exit()
|
||||
xoffs = 0
|
||||
yoffs = 0
|
||||
if len(sys.argv) >= 6:
|
||||
xoffs = int(sys.argv[5])
|
||||
if len(sys.argv) >= 7:
|
||||
yoffs = int(sys.argv[6])
|
||||
bdfr = BDFReader(sys.argv[4], xoffs, yoffs)
|
||||
if args.command == "addbdf":
|
||||
xoffs = args.xoffs
|
||||
yoffs = args.yoffs
|
||||
bdfr = BDFReader(args.bdffile, xoffs, yoffs)
|
||||
for i in range(cp_first, cp_last + 1):
|
||||
if bdfr.code_points[i] and not ft.code_points[i]:
|
||||
ft.code_points[i] = bdfr.code_points[i]
|
||||
ft.commit()
|
||||
elif sys.argv[1] == 'addraw':
|
||||
if len(sys.argv) < 5:
|
||||
print_usage_and_exit()
|
||||
rr = RawReader(sys.argv[4])
|
||||
elif args.command == 'addraw':
|
||||
rr = RawReader(args.rawfile)
|
||||
for i in range(cp_first, cp_last + 1):
|
||||
if rr.code_points[i] and not ft.code_points[i]:
|
||||
ft.code_points[i] = rr.code_points[i]
|
||||
ft.commit()
|
||||
elif sys.argv[1] == 'remove':
|
||||
elif args.command == 'remove':
|
||||
for i in range(cp_first, cp_last + 1):
|
||||
ft.code_points[i] = False
|
||||
ft.commit()
|
||||
elif sys.argv[1] == 'inspect':
|
||||
elif args.command == 'inspect':
|
||||
lut = [' ', '░░', '▒▒', '▓▓']
|
||||
for i in range(cp_first, cp_last + 1):
|
||||
if ft.code_points[i]:
|
||||
@ -262,5 +280,3 @@ This script is also an importable module.""" % FONT_HEIGHT)
|
||||
print('')
|
||||
else:
|
||||
print('code point %i (%c) is not available' % (i, i))
|
||||
else:
|
||||
print_usage_and_exit()
|
||||
|
Reference in New Issue
Block a user