-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_choco_kanji.py
84 lines (59 loc) · 2.27 KB
/
generate_choco_kanji.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import svgpathtools
from unittest.mock import patch
import xml.etree.ElementTree as ET
import alternative_d
x_advance = 109
header = f"""<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<metadata>
</metadata>
<defs>
<font id="ChocoKanji SVG" horiz-adv-x="{x_advance}" >
<font-face
font-family="ChocoKanji SVG"
units-per-em="{x_advance}"
ascent="100"
descent="20"
cap-height="90"
x-height="30"
/>
<missing-glyph horiz-adv-x="{x_advance}" d="M 0,0 0,80 80,80 80,0 0,0 M 0,0 80,80 M 20,0 80,60 M 40,0 80,40 M 0,20 60,80"/>
<glyph unicode=" " glyph-name="space" horiz-adv-x="50" />
"""
footer = """
</font>
</defs>
</svg>"""
KANJI_VG_FOLDER = 'kanjivg location not set'
def fix_path_string(d):
path = svgpathtools.parse_path(d)
vertical_flip = svgpathtools.parser.parse_transform('scale(1, -1)')
shift_up = svgpathtools.parser.parse_transform('translate(0, 49)')
flipped_path = svgpathtools.path.transform(path, vertical_flip)
shifted_path = svgpathtools.path.transform(flipped_path, shift_up)
# Monkeypatch svgtools.path.d function
with patch('svgpathtools.path.Path.d', new=alternative_d.alternative_d):
return shifted_path.d()
def convert_kanji(svg):
root = ET.fromstring(svg)
character = root[0][0].attrib['{http://kanjivg.tagaini.net}element']
svg_paths = root.findall(".//*[@d]")
paths = [fix_path_string(path.attrib['d']) for path in svg_paths]
combined_strokes = " ".join(paths)
return f'<glyph unicode="{character}" glyph-name="{character}" horiz-adv-x="{x_advance}" d="{combined_strokes}" />\n'
def write_kanji_file(open_file, kanji_filename):
with open(kanji_filename, 'r') as example_kanji:
example_kanji_svg: str = example_kanji.read()
entry = convert_kanji(example_kanji_svg)
open_file.write(entry)
def main():
with open('choco-kanji.svg', 'w') as f:
f.write(header)
kanji_files = os.listdir(KANJI_VG_FOLDER)
simple_kanji_files = [filename for filename in kanji_files if len(filename) == 9]
for kanji_filename in simple_kanji_files:
write_kanji_file(f, KANJI_VG_FOLDER + kanji_filename)
f.write(footer)
main()