From c2452dee3a25f6b4710348bc7a5b585c31037bf3 Mon Sep 17 00:00:00 2001 From: Rasmus Andersson Date: Tue, 26 Sep 2023 17:09:24 -0700 Subject: [PATCH] adjusted tools for v4 website --- misc/tools/gen-glyphinfo.py | 67 +++++++++++++++++------------- misc/tools/gen-metrics-and-svgs.py | 23 +++++----- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/misc/tools/gen-glyphinfo.py b/misc/tools/gen-glyphinfo.py index 94cdb8927..877bbbdd4 100755 --- a/misc/tools/gen-glyphinfo.py +++ b/misc/tools/gen-glyphinfo.py @@ -41,16 +41,18 @@ def localDateTimeToUTCStr(localstr, pattern='%Y/%m/%d %H:%M:%S'): return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(ts)) -def processGlyph(g, ucd, seenGlyphnames): - name = g.name - if name in seenGlyphnames: - return None - seenGlyphnames.add(name) - - # not exported? +def include_glyph(g): if 'com.schriftgestaltung.Glyphs.Export' in g.lib: if not g.lib['com.schriftgestaltung.Glyphs.Export']: - return None + return False + return True + + +NAME_TO_ID_RE = re.compile(r'([A-Z_])') + + +def processGlyph(g, ucd): + name = g.name # color color = None @@ -62,6 +64,8 @@ def processGlyph(g, ucd, seenGlyphnames): if not g.bounds or g.bounds[3] == 0: isEmpty = 1 + id = NAME_TO_ID_RE.sub(r'\1_', name).lower() + # name, isEmpty, unicode, unicodeName, color glyph = None ucs = g.unicodes @@ -72,27 +76,30 @@ def processGlyph(g, ucd, seenGlyphnames): # ucName = '[private use %04X]' % uc ucstr = '%04X' % uc if color: - glyph = [name, isEmpty, ucstr, ucName, color] + glyph = [id, name, isEmpty, ucstr, ucName, color] elif ucName: - glyph = [name, isEmpty, ucstr, ucName] + glyph = [id, name, isEmpty, ucstr, ucName] else: - glyph = [name, isEmpty, ucstr] + glyph = [id, name, isEmpty, ucstr] + break else: if color: - glyph = [name, isEmpty, None, None, color] + glyph = [id, name, isEmpty, None, None, color] else: - glyph = [name, isEmpty] + glyph = [id, name, isEmpty] return glyph + def glyphSortFun(g): - if len(g) > 2 and g[2] is not None: - return g[2] + if len(g) > 3 and g[3] is not None: + return g[3] elif len(g) > 0: - return g[0] + return g[1] else: return "" + def main(): argparser = ArgumentParser( description='Generate info on name, unicodes and color mark for all glyphs') @@ -111,23 +118,23 @@ def main(): ucd = parseUnicodeDataFile(args.ucdFile) glyphs = [] # contains final glyph data printed as JSON - seenGlyphnames = set() - - for name in font.lib['public.glyphOrder']: - g = font[name] - glyph = processGlyph(g, ucd, seenGlyphnames) - if glyph is not None: - glyphs.append(glyph) - unorderedGlyphs = [] + seenGlyphs = set() + for name in font.lib['public.glyphOrder']: + if name not in font: + print(f'warning: glyph "{name}" (in public.glyphOrder) does not exist', + file=sys.stderr) + continue + g = font[name] + seenGlyphs.add(g) + if include_glyph(g): + glyphs.append(processGlyph(g, ucd)) for g in font: - glyph = processGlyph(g, ucd, seenGlyphnames) - if glyph is not None: - unorderedGlyphs.append(glyph) + if include_glyph(g) and g not in seenGlyphs: + unorderedGlyphs.append(processGlyph(g, ucd)) - if unorderedGlyphs: - # sort by unicode - glyphs = glyphs + sorted(unorderedGlyphs, key=glyphSortFun) + # append unorderedGlyphs, sorted by unicode + glyphs = glyphs + sorted(unorderedGlyphs, key=glyphSortFun) print('{"glyphs":[') prefix = ' ' diff --git a/misc/tools/gen-metrics-and-svgs.py b/misc/tools/gen-metrics-and-svgs.py index 8d38ce958..b9d32726e 100755 --- a/misc/tools/gen-metrics-and-svgs.py +++ b/misc/tools/gen-metrics-and-svgs.py @@ -30,7 +30,6 @@ def num(s): return int(s) if s.find('.') == -1 else float(s) - def glyphToSVGPath(g, yMul): pen = SVGPathPen(g.getParent(), yMul) g.draw(pen) @@ -233,6 +232,11 @@ argparser.add_argument('-scale', dest='scale', metavar='', type=str, argparser.add_argument('ufopath', metavar='', type=str, help='Path to UFO packages') +argparser.add_argument('jsonfile', metavar='', type=str, + help='Output JSON file') +argparser.add_argument('htmlfile', metavar='', type=str, + help='Path to HTML file to patch') + argparser.add_argument('glyphs', metavar='', type=str, nargs='*', help='Only generate specific glyphs.') @@ -249,10 +253,6 @@ ufopath = args.ufopath.rstrip('/') ufo = Font(ufopath) effectiveAscender = max(ufo.info.ascender, ufo.info.unitsPerEm) - -deleteNames.add('.notdef') -deleteNames.add('.null') - glyphnames = args.glyphs if len(args.glyphs) else ufo.keys() glyphnameSet = set(glyphnames) @@ -296,10 +296,8 @@ for glyphname in glyphnames: svgtext = '\n'.join(svgLines) # print(svgtext) -glyphsHtmlFilename = os.path.join(BASEDIR, 'docs', 'glyphs', 'index.html') - html = u'' -with open(glyphsHtmlFilename, 'r', encoding="utf-8") as f: +with open(args.htmlfile, 'r', encoding="utf-8") as f: html = f.read() startMarker = u'
' @@ -308,7 +306,7 @@ startPos = html.find(startMarker) endMarker = u'