-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
68 lines (53 loc) · 2.29 KB
/
main.py
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
#### View settings.py for options before running
###############################################################################
import os
import shutil
from unittest.mock import Base
from svgpathtools import parse_path, wsvg
from fontTools.pens.svgPathPen import SVGPathPen
from fontTools.ttLib import TTFont
from settings import *
from settings_helper import *
from prompt import confirm
template = open('template.svg', 'r').read().replace('[[]]', f'{char_height * 2}')
if os.path.exists(costumes_path):
if not confirm(f"'{costumes_path}/' exists. Overwrite? "):
print("Cancelled.")
exit()
shutil.rmtree(costumes_path)
if os.path.exists(charwidths_path):
if not confirm(f"'{charwidths_path}' exists. Overwrite? "):
print("Cancelled.")
exit()
os.remove(charwidths_path)
os.mkdir(costumes_path)
charwidths = open(charwidths_path, 'w')
def create_costume(svg_char_path, costume_path, units_per_em, step):
transform_size = char_height / units_per_em
with open(costume_path, 'w') as file:
path = parse_path(svg_char_path).scaled(transform_size, -transform_size, 240 + char_height / 2 + (step + 1) / steps - 0.5 + (180 + char_height) * 1j)
file.write(template.replace('{{}}', path.d()))
file.close()
def get_glyph(char, glyph_set):
key = glyph_set.font['cmap'].getBestCmap()[ord(char)]
return glyph_set[key]
def get_svg_char_path(glyph):
pen = SVGPathPen(glyph.glyphSet)
glyph.draw(pen)
return pen.getCommands()
def get_costume_path(char_type, char_number, font_number, step):
char_alias = char_aliases[char_type][char_number]
return os.path.join(costumes_path, char_type, f'{f"{font_number}_" if len(font_paths) > 1 else ""}{char_alias}{step if steps > 1 else ""}.svg')
for char_type in chars:
os.mkdir(os.path.join(costumes_path, char_type))
for font_number, font_path in enumerate(font_paths):
ttfont = TTFont(font_path)
units_per_em = ttfont['head'].unitsPerEm # type: ignore
glyph_set = ttfont.getGlyphSet()
for char_number, char in enumerate(chars[char_type]):
for step in range(steps):
glyph = get_glyph(char, glyph_set)
costume_path = get_costume_path(char_type, char_number, font_number, step)
svg_char_path = get_svg_char_path(glyph)
create_costume(svg_char_path, costume_path, units_per_em, step)
charwidths.write(f'{glyph.width / units_per_em * char_height}\n')