summaryrefslogtreecommitdiff
path: root/misc/tools/gen-glyphorder.py
blob: 3d44fdd16d8cc93cae6a5b018ece4f338584e390 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# encoding: utf8
from __future__ import print_function
import os, plistlib, sys
from collections import OrderedDict
from argparse import ArgumentParser
from ConfigParser import RawConfigParser


BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))


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.join(BASEDIR, 'src')

  # load fontbuild config
  config = RawConfigParser(dict_type=OrderedDict)
  config.read(os.path.join(srcDir, 'fontbuild.cfg'))
  deleteNames = set(config.get('glyphs', 'delete').split())

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

  fontPath0 = args.fontPaths[0]
  libPlist = plistlib.readPlist(os.path.join(fontPath, 'lib.plist'))
  glyphOrder = libPlist['public.glyphOrder']
  glyphNameSet = set(glyphOrder)

  nameLists = []
  indexOffset = 0
  index = -1

  for fontPath in fontPaths[1:]:
    libPlist = plistlib.readPlist(os.path.join(fontPath, 'lib.plist'))
    if 'public.glyphOrder' in libPlist:
      names = libPlist['public.glyphOrder']
      numInserted = 0
      for i in range(len(names)):
        name = names[i]
        if name not in glyphNameSet:
          if i > 0 and names[i-1] in glyphNameSet:
            # find position of prev glyph
            index = glyphOrder.index(names[i-1]) + 1
          elif index != -1:
            index += 1
          else:
            index = min(len(glyphOrder), i - indexOffset)

          glyphOrder.insert(index, name)
          numInserted += 1
          glyphNameSet.add(name)

      indexOffset += numInserted

  # add any composed glyphs to the end
  diacriticComps = loadGlyphCompositions(os.path.join(srcDir, 'diacritics.txt'))
  for name in diacriticComps.keys():
    if name not in glyphNameSet:
      glyphOrder.append(name)

  # filter out deleted glyphs
  glyphOrder = [n for n in glyphOrder if n not in deleteNames]

  print('\n'.join(glyphOrder))


if __name__ == '__main__':
  main()