summaryrefslogtreecommitdiff
path: root/misc/gen-glyphorder.py
blob: 9707b807b95d0fd4b39df8c20927560e03cec11b (plain)
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
#!/usr/bin/env python
# encoding: utf8
from __future__ import print_function
import os, plistlib
from collections import OrderedDict
from argparse import ArgumentParser


def parseGlyphComposition(composite):
  c = composite.split("=")
  d = c[1].split("/")
  glyphName = d[0]
  if len(d) == 1:
    offset = [0, 0]
  else:
    offset = [int(i) for i in d[1].split(",")]
  accentString = c[0]
  accents = accentString.split("+")
  baseName = accents.pop(0)
  accentNames = [i.split(":") for i in accents]
  return (glyphName, baseName, accentNames, offset)


def loadGlyphCompositions(filename):  # { glyphName => (baseName, accentNames, offset) }
  compositions = OrderedDict()
  with open(filename, 'r') as f:
    for line in f:
      line = line.strip()
      if len(line) > 0 and line[0] != '#':
        glyphName, baseName, accentNames, offset = parseGlyphComposition(line)
        compositions[glyphName] = (baseName, accentNames, offset)
  return compositions


def main():
  argparser = ArgumentParser(description='Generate glyph order list from UFO files')
  argparser.add_argument('fontPaths', metavar='<ufofile>', type=str, nargs='+', help='UFO files')
  args = argparser.parse_args()

  srcdir = os.path.abspath(os.path.join(__file__, '..', '..'))

  nameLists = []
  fontPaths = []

  for fontPath in args.fontPaths:
    if 'regular' or 'Regular' in fontPath:
      fontPaths = [fontPath] + fontPaths
    else:
      fontPaths.append(fontPath)

  for fontPath in fontPaths:
    libPlist = plistlib.readPlist(os.path.join(fontPath, 'lib.plist'))
    if 'public.glyphOrder' in libPlist:
      nameLists.append(libPlist['public.glyphOrder'])

  glyphorderUnion = OrderedDict()

  for names in zip(*nameLists):
    for name in names:
      glyphorderUnion[name] = True

  # add any composed glyphs to the end
  diacriticComps = loadGlyphCompositions(os.path.join(srcdir, 'src', 'diacritics.txt'))
  for name in diacriticComps.keys():
    glyphorderUnion[name] = True

  glyphorderUnionNames = glyphorderUnion.keys()
  print('\n'.join(glyphorderUnionNames))


if __name__ == '__main__':
  main()