-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.py
executable file
·61 lines (52 loc) · 1.81 KB
/
bundle.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
#!/bin/env python
from wrpg.wfont.pack import pack_font
import xml.etree.ElementTree as ET
import sys
# Littera exports in UTF-8. We want Extended ASCII.
# There is a need to convert the values
# {in: out}
littera_char_translations = {
338: 140,
339: 155,
}
def parse_littera_xml_font(tree):
root = tree.getroot()
common = root.find('common')
baseline = int(common.get('base'))
chars = []
chars_element = root.find('chars')
space_width = 1
for char in chars_element:
xml_id = int(char.get('id'))
char_id = xml_id
if xml_id in littera_char_translations:
new_id = littera_char_translations[xml_id]
print('Correspondant char {} => {}'.format(xml_id, new_id))
char_id = new_id
if char_id > 255:
continue
# If it's a space, collect the x advance variable
if char_id == 32:
space_width = int(char.get('xadvance'))
chars.append({'index': int(char_id),
'x': int(char.get('x')),
'y': int(char.get('y')),
'width': int(char.get('width')),
'height': int(char.get('height')),
'x_offset': int(char.get('xoffset')),
'y_offset': int(char.get('yoffset'))
})
return {'baseline': baseline, 'chars': chars, 'space_width': space_width}
def main():
if len(sys.argv) != 3:
return usage()
tree = ET.parse(sys.argv[1])
char_data = parse_littera_xml_font(tree)
print('- Data found -')
print('Baseline : {}'.format(char_data['baseline']))
with open(sys.argv[2], 'wb+') as f:
f.write(pack_font(char_data))
def usage():
print('Usage : {} file output_file'.format(sys.argv[0]))
if __name__ == '__main__':
main()