diff --git a/font-list.txt b/font-list.txt index 8d3e82a..17f22b0 100644 --- a/font-list.txt +++ b/font-list.txt @@ -1,29 +1,33 @@ +## Sans Serif Akzidenz Grotesk Arial -#Baskerville -#Bembo -#Bodoni -#Cambria Calibri -#Clarendon -#Dax Pro -#Didot -#Franklin Gothic -#Frutiger Futura -#Garamond -#Georgia -#Gill Sans -#Gotham + +## Serif +Cambria + +Baskerville +Bembo +Bodoni +Clarendon +Dax Pro +Didot +Franklin Gothic +Frutiger +Garamond +Georgia +Gill Sans +Gotham Helvetica -#Minion -#Mrs Eaves -#Myriad -#News Gothic +Minion +Mrs Eaves +Myriad +News Gothic Roboto -#Roboto Slab -#Rockwell -#Sabon -#Segoe UI -#Times New Roman -#Verdana \ No newline at end of file +Roboto Slab +Rockwell +Sabon +Segoe UI +Times New Roman +Verdana \ No newline at end of file diff --git a/font_game/util.py b/font_game/util.py index fa0f4d1..0881774 100644 --- a/font_game/util.py +++ b/font_game/util.py @@ -1,7 +1,7 @@ import string import pathlib import time -from PIL import ImageFont +from PIL import ImageFont, Image, ImageDraw BASE64_DIGITS = string.digits + string.ascii_letters + "+_" @@ -12,6 +12,9 @@ IMAGE_SIZE = (1800, 1200) MARGINS = 60 +MAKE_TESTS = True +TEST_TEXT = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + def read_file(path: str) -> dict: with open(path, "r", encoding="utf-8") as file: game_dict = dict( @@ -43,13 +46,40 @@ def gen_id() -> str: micro_seconds = int(time.time() * 1000000) return base64((micro_seconds * 9876534021204356789) % 0xfffffffffffffff) +def make_test_pages(fonts: dict[str, ImageFont.FreeTypeFont]): + image_width = FONT_SIZE*8 + image_height = FONT_SIZE*8 + image_margin = FONT_SIZE//3 + writable_area = image_width - image_margin*2 + + for font_name, font in fonts.items(): + final_text = [] + current_text = "" + for char in TEST_TEXT: + if font.getsize(current_text + char)[0] > (writable_area): + final_text.append(current_text) + current_text = char + else: + current_text += char + + final_text.append(current_text) + im = Image.new("RGB", (image_width, image_height), (255, 255, 255)) + drawer = ImageDraw.ImageDraw(im) + drawer.text( + (image_margin, image_margin), + '\n'.join(final_text), + (0, 0, 0), + font + ) + im.save(f"fonts/test_pages/{font_name}.png") + def make_fonts(): with open("font-list.txt", "r", encoding="utf-8") as file_pointer: fonts = file_pointer.read().split("\n") image_fonts = {} for font_name in fonts: - if font_name[0] == "#": + if font_name == "" or font_name[0] == "#": continue font_name = font_name.replace(" ","-") if pathlib.Path(f"./fonts/{font_name}.TTF").is_file(): @@ -61,10 +91,17 @@ def make_fonts(): continue font_path = f"./fonts/{font_name}.{file_type}" - new_font = ImageFont.truetype(font_path, FONT_SIZE) + tmp_font = ImageFont.truetype(font_path, FONT_SIZE) + letter_height = tmp_font.getsize("A")[1] + new_size = (FONT_SIZE**2)//letter_height + new_font = ImageFont.truetype(font_path, new_size) image_fonts[font_name] = new_font print(f"Successfully loaded font \033[0;32m{font_name}\033[0m") + if MAKE_TESTS: + make_test_pages(image_fonts) + print("Successfully created test pages") + return image_fonts diff --git a/fonts/test_pages/Akzidenz-Grotesk.png b/fonts/test_pages/Akzidenz-Grotesk.png new file mode 100644 index 0000000..85a6b10 Binary files /dev/null and b/fonts/test_pages/Akzidenz-Grotesk.png differ diff --git a/fonts/test_pages/Arial.png b/fonts/test_pages/Arial.png new file mode 100644 index 0000000..82019d3 Binary files /dev/null and b/fonts/test_pages/Arial.png differ diff --git a/fonts/test_pages/Baskerville.png b/fonts/test_pages/Baskerville.png new file mode 100644 index 0000000..53bc6a6 Binary files /dev/null and b/fonts/test_pages/Baskerville.png differ diff --git a/fonts/test_pages/Bembo.png b/fonts/test_pages/Bembo.png new file mode 100644 index 0000000..233b1ef Binary files /dev/null and b/fonts/test_pages/Bembo.png differ diff --git a/fonts/test_pages/Bodoni.png b/fonts/test_pages/Bodoni.png new file mode 100644 index 0000000..32f234d Binary files /dev/null and b/fonts/test_pages/Bodoni.png differ diff --git a/fonts/test_pages/Calibri.png b/fonts/test_pages/Calibri.png new file mode 100644 index 0000000..86b136f Binary files /dev/null and b/fonts/test_pages/Calibri.png differ diff --git a/fonts/test_pages/Cambria.png b/fonts/test_pages/Cambria.png new file mode 100644 index 0000000..cf47d59 Binary files /dev/null and b/fonts/test_pages/Cambria.png differ diff --git a/fonts/test_pages/Futura.png b/fonts/test_pages/Futura.png new file mode 100644 index 0000000..00f1a04 Binary files /dev/null and b/fonts/test_pages/Futura.png differ diff --git a/fonts/test_pages/Garamond.png b/fonts/test_pages/Garamond.png new file mode 100644 index 0000000..3eb05a1 Binary files /dev/null and b/fonts/test_pages/Garamond.png differ diff --git a/fonts/test_pages/Georgia.png b/fonts/test_pages/Georgia.png new file mode 100644 index 0000000..9b6b521 Binary files /dev/null and b/fonts/test_pages/Georgia.png differ diff --git a/fonts/test_pages/Helvetica.png b/fonts/test_pages/Helvetica.png new file mode 100644 index 0000000..83bcc65 Binary files /dev/null and b/fonts/test_pages/Helvetica.png differ diff --git a/fonts/test_pages/Roboto-Slab.png b/fonts/test_pages/Roboto-Slab.png new file mode 100644 index 0000000..51ecf1a Binary files /dev/null and b/fonts/test_pages/Roboto-Slab.png differ diff --git a/fonts/test_pages/Roboto.png b/fonts/test_pages/Roboto.png new file mode 100644 index 0000000..02818a8 Binary files /dev/null and b/fonts/test_pages/Roboto.png differ diff --git a/fonts/test_pages/Segoe-UI.png b/fonts/test_pages/Segoe-UI.png new file mode 100644 index 0000000..92c42fd Binary files /dev/null and b/fonts/test_pages/Segoe-UI.png differ diff --git a/fonts/test_pages/Times-New-Roman.png b/fonts/test_pages/Times-New-Roman.png new file mode 100644 index 0000000..f216d01 Binary files /dev/null and b/fonts/test_pages/Times-New-Roman.png differ diff --git a/fonts/test_pages/Verdana.png b/fonts/test_pages/Verdana.png new file mode 100644 index 0000000..333c947 Binary files /dev/null and b/fonts/test_pages/Verdana.png differ