Commit c6a3e747 authored by russhughes's avatar russhughes

minor refactoring

parent 19613e69
...@@ -28,7 +28,7 @@ def convert_font(file_in, file_out, width, height, first=0x0, last=0xff): ...@@ -28,7 +28,7 @@ def convert_font(file_in, file_out, width, height, first=0x0, last=0xff):
print(f'HEIGHT = {height}', file=font_file) print(f'HEIGHT = {height}', file=font_file)
print(f'FIRST = 0x{first:02x}', file=font_file) print(f'FIRST = 0x{first:02x}', file=font_file)
print(f'LAST = 0x{last:02x}', file=font_file) print(f'LAST = 0x{last:02x}', file=font_file)
print(f'_FONT =\\\n', sep='', end='', file=font_file) print('_FONT =\\\n', sep='', end='', file=font_file)
for chunk in iter(lambda: bin_file.read(chunk_size), b''): for chunk in iter(lambda: bin_file.read(chunk_size), b''):
print('b\'', sep='', end='', file=font_file) print('b\'', sep='', end='', file=font_file)
for data in chunk: for data in chunk:
...@@ -45,9 +45,8 @@ def auto_int(x): ...@@ -45,9 +45,8 @@ def auto_int(x):
return int(x, 0) return int(x, 0)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Convert fomfont.bin font files in input to python in font_directory.') description='Convert Romfont.bin font files in input to python in font_directory.')
parser.add_argument('input', help='file or directory containing binary font file(s).') parser.add_argument('input', help='file or directory containing binary font file(s).')
parser.add_argument('output', help='file or directory to contain python font file(s).') parser.add_argument('output', help='file or directory to contain python font file(s).')
parser.add_argument('-f', '--first-char', type=auto_int, default=0x20) parser.add_argument('-f', '--first-char', type=auto_int, default=0x20)
...@@ -57,11 +56,7 @@ def main(): ...@@ -57,11 +56,7 @@ def main():
file_re = re.compile(r'^(.*)(\d+)x(\d+)\.bin$') file_re = re.compile(r'^(.*)(\d+)x(\d+)\.bin$')
is_dir = os.path.isdir(args.input) is_dir = os.path.isdir(args.input)
if is_dir: bin_files = os.listdir(args.input) if is_dir else [args.input]
bin_files = os.listdir(args.input)
else:
bin_files = [args.input]
for bin_file_name in bin_files: for bin_file_name in bin_files:
match = file_re.match(bin_file_name) match = file_re.match(bin_file_name)
if match: if match:
...@@ -88,4 +83,5 @@ def main(): ...@@ -88,4 +83,5 @@ def main():
font_height, font_height,
args.first_char, args.first_char,
args.last_char) args.last_char)
main() main()
...@@ -40,9 +40,9 @@ def main(): ...@@ -40,9 +40,9 @@ def main():
# get rgb values and convert to 565 # get rgb values and convert to 565
color565 = ( color565 = (
((palette[color*3] & 0xF8) << 8) ((palette[color*3] & 0xF8) << 8) |
| ((palette[color*3+1] & 0xFC) << 3) ((palette[color*3+1] & 0xFC) << 3) |
| ((palette[color*3+2] & 0xF8) >> 3)) ((palette[color*3+2] & 0xF8) >> 3))
# swap bytes in 565 # swap bytes in 565
color = ((color565 & 0xff) << 8) + ((color565 & 0xff00) >> 8) color = ((color565 & 0xff) << 8) + ((color565 & 0xff00) >> 8)
...@@ -59,9 +59,11 @@ def main(): ...@@ -59,9 +59,11 @@ def main():
for x in range(img.width): for x in range(img.width):
pixel = img.getpixel((x, y)) pixel = img.getpixel((x, y))
color = pixel color = pixel
bstring = '' bstring = ''.join(
for bit in range(bits, 0, -1): '1' if (color & (1 << bit - 1)) else '0'
bstring += '1' if (color & (1 << bit-1)) else '0' for bit in range(bits, 0, -1)
)
image_bitstring += bstring image_bitstring += bstring
bitmap_bits = len(image_bitstring) bitmap_bits = len(image_bitstring)
......
...@@ -45,12 +45,10 @@ def to_int(str): ...@@ -45,12 +45,10 @@ def to_int(str):
def get_characters(str): def get_characters(str):
return ''.join([chr(b) for a in [ return ''.join(chr(b) for a in [
(lambda sub: range(sub[0], sub[-1] + 1)) (lambda sub: range(sub[0], sub[-1] + 1))
(list(map(to_int, ele.split('-')))) (list(map(to_int, ele.split('-'))))
for ele in str.split(',')] for ele in str.split(',')] for b in a)
for b in a])
def process_char(img, bits): def process_char(img, bits):
global image_bitstring global image_bitstring
...@@ -62,9 +60,11 @@ def process_char(img, bits): ...@@ -62,9 +60,11 @@ def process_char(img, bits):
for x in range(img.width): for x in range(img.width):
pixel = img.getpixel((x, y)) pixel = img.getpixel((x, y))
color = pixel color = pixel
bit_string = '' bit_string = ''.join(
for bit in range(bits, 0, -1): '1' if (color & (1 << bit - 1)) else '0'
bit_string += '1' if (color & (1 << bit-1)) else '0' for bit in range(bits, 0, -1)
)
image_bitstring += bit_string image_bitstring += bit_string
......
...@@ -27,7 +27,6 @@ def create_png(font_file_name, png_file_name): ...@@ -27,7 +27,6 @@ def create_png(font_file_name, png_file_name):
with open(png_file_name, 'wb') as png_file: with open(png_file_name, 'wb') as png_file:
image = png.Writer((16+2) * font.WIDTH, (row_count+3) * font.HEIGHT, bitdepth=1) image = png.Writer((16+2) * font.WIDTH, (row_count+3) * font.HEIGHT, bitdepth=1)
image_data = [[0 for j in range((16+2) * font.WIDTH)] for i in range((row_count+3)* font.HEIGHT)] image_data = [[0 for j in range((16+2) * font.WIDTH)] for i in range((row_count+3)* font.HEIGHT)]
font_count = len(font.FONT)+1
for chart_row in range(row_count+2): for chart_row in range(row_count+2):
for chart_col in range(16): for chart_col in range(16):
chart_idx = chart_row * 16 + chart_col chart_idx = chart_row * 16 + chart_col
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment