diff --git a/README b/README index 45d4296..efa6524 100644 --- a/README +++ b/README @@ -8,7 +8,7 @@ Python 3, pillow Usage ----- -$ gff +$ gff [initial char index] [char range] Example: $ gff "$HOME/.fonts/Kenney Future Narrow" 16 kenney_future_narrow.png diff --git a/gff b/gff index 6906e68..79869ff 100755 --- a/gff +++ b/gff @@ -1,14 +1,23 @@ #!/usr/bin/env python3 +from math import ceil from sys import argv from PIL import Image, ImageDraw, ImageFont -if (len(argv) != 4): - raise BaseException("Three arguments expected: " + \ - " ") +if (len(argv) < 4): + raise BaseException("At least three arguments expected: " + \ + " " + \ + "[initial char index] [char range]") font_path = argv[1] font_size = int(argv[2]) image_out = argv[3] +initial_char_index = int(argv[4]) if len(argv) > 4 else 0x20 +char_range = int(argv[5]) if len(argv) > 5 else 96 +line_count = ceil(char_range / 16) +scan_index = 0x20 if len(argv) > 4 else initial_char_index +scan_range = 0x30ff - 0x20 if len(argv) > 4 else char_range + +assert char_range > 1 mode = '1' # 1-bit depth background_color = (1) # white @@ -20,8 +29,8 @@ font = ImageFont.truetype(font_path, font_size) # find max char size char_width = 0 char_height = 0 -for i in range(95): - bbox = list(font.getbbox(chr(32 + i))) +for i in range(scan_range): + bbox = list(font.getbbox(chr(scan_index + i))) # don't you dare overlap if (bbox[0] < 0): bbox[2] = -bbox[0] @@ -33,7 +42,7 @@ for i in range(95): char_height = bbox[3] image_out_width = char_width * 16 -image_out_height = char_height * 6 +image_out_height = char_height * line_count # fxconv metadata print(f"{image_out.split('/')[-1]}:") @@ -50,9 +59,9 @@ draw.rectangle((0, 0, image_out_width, image_out_height), fill=background_color) x = -char_width y = 0 -for i in range(95): +for i in range(char_range): x += char_width - char = chr(32 + i) + char = chr(initial_char_index + i) bbox = font.getbbox(char) x_mod = 0 y_mod = 0