-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
121 lines (105 loc) · 3.47 KB
/
generator.py
File metadata and controls
121 lines (105 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from os import listdir,mkdir,makedirs
from os.path import join,dirname,abspath,getmtime
from shutil import rmtree, copyfile
from math import floor,ceil,sqrt,log
from PIL import Image
BASE = abspath(dirname(__file__))
IMAGE_SIZE = 8
CHARACTER_START = 0xE000
FLAG_DIR = join(BASE,"./flags")
GENERATE_ITEM_MODELS = False # not yet implemented
try:
rmtree(join(BASE,"./assets"))
except:
pass
try:
rmtree(join(BASE,"./src"))
except:
pass
makedirs(join(BASE,"./assets/flagfont"))
makedirs(join(BASE,"./assets/flagfont/textures/font"))
makedirs(join(BASE,"./src/main/java/dev/spnr/flagfont/"))
mkdir(join(BASE,"./assets/flagfont/font"))
filenames = listdir(FLAG_DIR)
filenames.sort(key=lambda x: getmtime(join(FLAG_DIR,x)))
country = [f for f in filenames if f.startswith("flag_")]
pride = [f for f in filenames if not f in country and not f=="blank.png"]
def get_atlas_size(images):
area = 1
while area < len(images):
area *= 4
return floor(sqrt(area))*IMAGE_SIZE
def get_image_position(n,atlas_size):
x = n*IMAGE_SIZE % (atlas_size)
y = floor(n*IMAGE_SIZE / (atlas_size))*IMAGE_SIZE
return (x,y)
def create_atlas(images,name):
atlas_size = get_atlas_size(country)
atlas = Image.new('RGBA',(atlas_size,atlas_size))
n = 0
for filename in images:
img = Image.open(join(FLAG_DIR,filename))
pos = get_image_position(n,atlas_size)
atlas.paste(img,pos)
img.close()
n+=1
atlas.save(join(BASE,f"assets/flagfont/textures/font/{name}.png"))
return atlas_size
def create_font_json(images,name,atlas_size):
num_rows = int(atlas_size/IMAGE_SIZE)
chars = []
for _ in range(num_rows):
chars.append([" "]*num_rows)
n = 0
flag_char_map = {}
for image in images:
row = floor(n*IMAGE_SIZE/atlas_size)
column = int((n*IMAGE_SIZE%atlas_size)/IMAGE_SIZE)
char = hex(CHARACTER_START+n).replace('0x','\\u')
chars[row][column] = char
flag_char_map[image] = char
n+=1
chars = [''.join(row) for row in chars]
charsstr = str(chars).replace("'",'"').replace("\\\\","\\")
s = f"""{{
"providers": [
{{
"type":"bitmap",
"file":"flagfont:font/{name}.png",
"ascent":{IMAGE_SIZE},
"chars":{charsstr}
}}
]
}}"""
with open(join(BASE,f"assets/flagfont/font/{name}.json"),"w+") as f:
f.write(s)
return flag_char_map
def create_nested_text_class(flag_char_map,name):
head = f" public static final class {name} {{\n"
foot = " }\n"
body = ""
for flag,char in flag_char_map.items():
java_name = flag.replace('.png','').upper()
body+=f" public static final char {java_name} = '{char}';\n"
return head+body+foot
def create_java_class(text_classes):
head = f"""package dev.spnr.flagfont;
public final class FlagFont {{
"""
foot = "\n}"
body = ""
for text_class in text_classes:
body += text_class
with open(join(BASE,"./src/main/java/dev/spnr/flagfont/FlagFont.java"),"w+") as f:
f.write(head+body+foot)
def generate_one(images,name):
atlas_size = create_atlas(images,name)
flag_char_map = create_font_json(images,name,atlas_size)
text_class = create_nested_text_class(flag_char_map,name.title())
return text_class
def generate_all():
classes = []
classes.append(generate_one(country,"country"))
classes.append(generate_one(pride,"pride"))
create_java_class(classes)
generate_all()