-
Notifications
You must be signed in to change notification settings - Fork 7
/
extractor.py
48 lines (44 loc) · 1.77 KB
/
extractor.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
import binascii
import io
import pathlib
import sys
import xml.etree.ElementTree as ET
from emjc import decode_emjc
from PIL import Image
# python3 extractor.py apple common/AppleColorEmoji@2x_00._s_b_i_x.ttx
# input: output folder, font sbix ttx
data = ET.parse(sys.argv[2]).getroot()
supported_types = ['png ', 'emjc']
strike = data.find('sbix/strike')
for strike in data.iter('strike'):
ppem = int(strike.find('ppem').attrib['value'])
print(f'Reading strike of size {ppem}x{ppem}')
pathlib.Path(f'{sys.argv[1]}/{ppem}').mkdir(parents=True, exist_ok=True)
for glyph in strike.findall('glyph'):
type = glyph.get('graphicType')
name = glyph.get('name')
flipped = type == 'flip'
if flipped:
ref = glyph.find('ref')
glyphname = ref.get('glyphname')
glyph = strike.find(f'glyph[@name="{glyphname}"]')
type = glyph.get('graphicType')
if type in supported_types:
# print(f'Exporting {name} ({ppem}x{ppem})')
hexdata = glyph.find('hexdata').text.strip()
data = binascii.unhexlify(''.join(hexdata.split()))
out_path = f'{sys.argv[1]}/{ppem}/{name}.png'
if type == 'emjc':
decoded = decode_emjc(data)
img = Image.frombuffer('RGBA', (ppem, ppem), decoded, 'raw', 'BGRA', 0, 1)
if flipped:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
img.save(out_path)
else:
if flipped:
img = Image.open(io.BytesIO(data))
img = img.transpose(Image.FLIP_LEFT_RIGHT)
img.save(out_path)
else:
with open(out_path, 'wb') as fout:
fout.write(data)