✨
This commit is contained in:
@ -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
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user