2017-08-22 10:05:20 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf8
|
|
|
|
#
|
|
|
|
# Grab http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
|
|
|
|
#
|
2018-09-04 02:06:04 +03:00
|
|
|
import os, sys
|
|
|
|
from os.path import dirname, basename, abspath, relpath, join as pjoin
|
|
|
|
sys.path.append(abspath(pjoin(dirname(__file__), 'tools')))
|
|
|
|
from common import BASEDIR, getVersion, getLocalTimeZoneOffset
|
|
|
|
|
|
|
|
import json, re
|
|
|
|
import time
|
2017-08-22 10:05:20 +03:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from collections import OrderedDict
|
2019-01-02 07:47:16 +03:00
|
|
|
from configparser import RawConfigParser
|
2018-09-04 02:06:04 +03:00
|
|
|
from unicode_util import parseUnicodeDataFile
|
|
|
|
from defcon import Font
|
2017-08-22 10:05:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
def rgbaToCSSColor(r=0, g=0, b=0, a=1):
|
|
|
|
R,G,B = int(r * 255), int(g * 255), int(b * 255)
|
|
|
|
if a == 1:
|
|
|
|
return '#%02x%02x%02x' % (R,G,B)
|
|
|
|
else:
|
2018-09-04 02:06:04 +03:00
|
|
|
return 'rgba(%d,%d,%d,%g)' % (R,G,B,a)
|
2017-08-22 10:05:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
def unicodeName(cp):
|
|
|
|
if cp is not None and len(cp.name):
|
|
|
|
if cp.name[0] == '<':
|
|
|
|
return '[' + cp.categoryName + ']'
|
|
|
|
elif len(cp.name):
|
|
|
|
return cp.name
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-09-04 02:06:04 +03:00
|
|
|
# YYYY/MM/DD HH:mm:ss => YYYY-MM-DDTHH:mm:ssZ (local time -> UTC)
|
|
|
|
def localDateTimeToUTCStr(localstr, pattern='%Y/%m/%d %H:%M:%S'):
|
|
|
|
t = time.strptime(localstr, pattern)
|
|
|
|
ts = time.mktime(t) - getLocalTimeZoneOffset()
|
|
|
|
return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(ts))
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-22 10:05:20 +03:00
|
|
|
def main():
|
|
|
|
argparser = ArgumentParser(
|
|
|
|
description='Generate info on name, unicodes and color mark for all glyphs')
|
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
'-ucd', dest='ucdFile', metavar='<file>', type=str,
|
|
|
|
help='UnicodeData.txt file from http://www.unicode.org/')
|
|
|
|
|
|
|
|
argparser.add_argument(
|
2019-05-27 02:38:03 +03:00
|
|
|
'fontPath', metavar='<ufofile>', type=str)
|
2017-08-22 10:05:20 +03:00
|
|
|
|
|
|
|
args = argparser.parse_args()
|
2019-05-27 02:38:03 +03:00
|
|
|
font = Font(args.fontPath)
|
2017-08-22 10:05:20 +03:00
|
|
|
ucd = {}
|
|
|
|
if args.ucdFile:
|
|
|
|
ucd = parseUnicodeDataFile(args.ucdFile)
|
|
|
|
|
2018-09-04 02:06:04 +03:00
|
|
|
glyphs = [] # contains final glyph data printed as JSON
|
2019-05-27 02:38:03 +03:00
|
|
|
|
|
|
|
for name in font.lib['public.glyphOrder']:
|
|
|
|
g = font[name]
|
|
|
|
|
2019-05-27 21:32:39 +03:00
|
|
|
# not exported?
|
|
|
|
if 'com.schriftgestaltung.Glyphs.Export' in g.lib:
|
|
|
|
if not g.lib['com.schriftgestaltung.Glyphs.Export']:
|
|
|
|
continue
|
|
|
|
|
2019-05-27 02:38:03 +03:00
|
|
|
# color
|
|
|
|
color = None
|
|
|
|
if 'public.markColor' in g.lib:
|
|
|
|
rgba = [float(c.strip()) for c in g.lib['public.markColor'].strip().split(',')]
|
|
|
|
color = rgbaToCSSColor(*rgba)
|
|
|
|
|
|
|
|
isEmpty = 0
|
|
|
|
if not g.bounds or g.bounds[3] == 0:
|
|
|
|
isEmpty = 1
|
|
|
|
|
2019-05-27 21:32:39 +03:00
|
|
|
# name, isEmpty, unicode, unicodeName, color
|
2019-05-27 02:38:03 +03:00
|
|
|
glyph = None
|
|
|
|
ucs = g.unicodes
|
|
|
|
if len(ucs):
|
|
|
|
for uc in ucs:
|
|
|
|
ucName = unicodeName(ucd.get(uc))
|
|
|
|
# if not ucName and uc >= 0xE000 and uc <= 0xF8FF:
|
|
|
|
# ucName = '[private use %04X]' % uc
|
|
|
|
|
|
|
|
ucstr = '%04X' % uc
|
2018-09-04 02:06:04 +03:00
|
|
|
if color:
|
2019-05-27 02:38:03 +03:00
|
|
|
glyph = [name, isEmpty, ucstr, ucName, color]
|
|
|
|
elif ucName:
|
|
|
|
glyph = [name, isEmpty, ucstr, ucName]
|
2018-09-04 02:06:04 +03:00
|
|
|
else:
|
2019-05-27 02:38:03 +03:00
|
|
|
glyph = [name, isEmpty, ucstr]
|
|
|
|
else:
|
|
|
|
if color:
|
|
|
|
glyph = [name, isEmpty, None, None, color]
|
|
|
|
else:
|
|
|
|
glyph = [name, isEmpty]
|
2018-09-04 02:06:04 +03:00
|
|
|
|
2019-05-27 02:38:03 +03:00
|
|
|
glyphs.append(glyph)
|
2017-08-22 10:05:20 +03:00
|
|
|
|
|
|
|
print('{"glyphs":[')
|
|
|
|
prefix = ' '
|
|
|
|
for g in glyphs:
|
|
|
|
print(prefix + json.dumps(g))
|
|
|
|
if prefix == ' ':
|
|
|
|
prefix = ', '
|
|
|
|
print(']}')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|