From 440ff992da83b7beb613131ace931cc0ce456866 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Mon, 4 Mar 2024 21:02:25 +0100 Subject: [PATCH] Remove unused files --- cli_ui.py | 161 ---------------------------------------------------- exporter.py | 69 ---------------------- 2 files changed, 230 deletions(-) delete mode 100644 cli_ui.py delete mode 100644 exporter.py diff --git a/cli_ui.py b/cli_ui.py deleted file mode 100644 index 43e6bc3..0000000 --- a/cli_ui.py +++ /dev/null @@ -1,161 +0,0 @@ -import os -from tkinter import filedialog -import json -import base64 -import numpy as np - - -def get_choice(text, choices): - line = "=" * len(text) - print(line) - print(text) - print(line) - - choices_len = len(choices) - - for i, choice in enumerate(choices): - print(f"{i+1}. {choice}") - - while True: - - try: - choice = int(input(f"> ")) - except ValueError: - print("\nInvalid input! Input must be a number.") - continue - - if choice < 1 or choice > choices_len: - print(f"\nInvalid input! Choose number from the list.") - continue - - return choice - 1 - - -def get_int_input(text, min_val=None, max_val=None): - while True: - try: - value = int(input(text)) - except ValueError: - print("\nInvalid input! Input must be a number.") - continue - - if (min_val is not None and value < min_val) or (max_val is not None and value > max_val): - print(f"\nInvalid input! Input must be between {min_val} and {max_val}.") - continue - - return value - - - -def key_tips(): - print("\n======== CONTROLS ========") - - print("Scroll - Select character") - print("Mouse click/drag - Draw") - print("Delete - Remove character") - print("G - Toggle grid") - print("E - Export font") - print("P - Reprint this controls info") - - print("\nBlank characters are automatically removed.") - #print("Type any character into the console and press enter at any time to jump to it.") - - print("============================\n") - - - -def cli_main(): - while True: - - choices = ["Create new font", "Open font project"] - - # if last_path_cache.txt exists, read it - try: - with open("last_path_cache.txt", "r") as file: - last_path = file.read() - choices.append(f"Open recent project ({os.path.basename(last_path)})") - except FileNotFoundError: - last_path = None - - - - choice = get_choice( - "What do you want to do?" + (" No recently opened project found." if last_path is None else ""), - choices - ) - - #choice = 2 - - - # create new font project - if choice == 0: - print("\n" + "=" * 30) - - project_data = { - "char_width": get_int_input("Enter character width: ", min_val=2), - "char_height": get_int_input("Enter character height: ", min_val=2), - "chars": {} - } - - # show file save dialog - file_path = filedialog.asksaveasfilename( - title="Save font project", - filetypes=[("Font project (json)", "*.fontproj")], - defaultextension=".fontproj" - ) - - if file_path == "": - print("\nCanceled.\n") - continue - - # save project data to file - with open(file_path, "w") as f: - json.dump(project_data, f, indent=4) - - # save last path to cache - with open("last_path_cache.txt", "w") as f: - f.write(file_path) - - - # open existing font project - elif choice == 1: - pass - - # open last project - elif choice == 2: - file_path = last_path - - try: - with open(file_path, "r") as f: - project_data = json.load(f) - except FileNotFoundError: - print("\nCouldn't open last project. File not found.\n") - continue - - - # process project data if opened existing project - if choice != 0: - # reverse the packed characters - unpacked_chars = {} - for key, value in project_data["chars"].items(): - # decode from base64 - decoded_data = base64.b64decode(value.encode("utf-8")) - # unpackbits - unpacked_data = np.unpackbits(np.frombuffer(decoded_data, dtype=np.uint8)) - # remove padding - unpacked_data = unpacked_data[:project_data["char_width"] * project_data["char_height"]] - # reshape into original shape - unpacked_data = unpacked_data.reshape(project_data["char_width"], project_data["char_height"]) - # store unpacked character - unpacked_chars[key] = unpacked_data.astype(bool) - - project_data["chars"] = unpacked_chars - - - - print("\n" + "=" * 30) - print(f"Font resolution: {project_data['char_width']}x{project_data['char_height']}") - - key_tips() - - return project_data, file_path diff --git a/exporter.py b/exporter.py deleted file mode 100644 index a5c9665..0000000 --- a/exporter.py +++ /dev/null @@ -1,69 +0,0 @@ -from tkinter import filedialog -from PIL import Image -import numpy as np - - -def export(char_res, chars): - - export_path = filedialog.askdirectory( - title="Choose folder where exported pages will be saved." - ) - - if export_path == "": - print("\nExport canceled.\n") - return - - - char_res = np.array(char_res, dtype=np.uint16) - chars = tuple(chars.items()) - - - # there will be 256 characters per page - - last_char_idx = len(chars) - 1 - - page_res = char_res * 16 - - page = 0 - page_char_x = 0 - page_char_y = 0 - for char_idx, char in enumerate(chars): - if page_char_x == 0 and page_char_y == 0: - page_arr = np.zeros(page_res, dtype=bool) - - char_map_string = "" - - char_string, char_bitmap = char - - - # put char_bitmap onto page_img at correct position - page_arr[ - page_char_x * char_res[0] : (page_char_x + 1) * char_res[0], - page_char_y * char_res[1] : (page_char_y + 1) * char_res[1] - ] = char_bitmap - - char_map_string += char_string - - page_char_x += 1 - - if page_char_x == 16: - page_char_x = 0 - page_char_y += 1 - - if page_char_y == 16 or char_idx == last_char_idx: - ## save page - - # numpy array to 1 bit image - page_img = Image.fromarray(page_arr.T * 255).convert("1") - - # save page - page_img.save(f"{export_path}/page_{page}.png") - - # save char map - with open(f"{export_path}/page_{page}.txt", "w", encoding="utf-32") as f: - f.write(char_map_string) - - page += 1 - page_char_y = 0 - - print("\nExport finished.\n") \ No newline at end of file