summaryrefslogtreecommitdiff
path: root/scripts/lib/fontbuild/generateGlyph.py
blob: 4f9f0268d89666055dd94a07974b3ee3c7f8d368 (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
from anchors import alignComponentsToAnchors


def parseComposite(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 generateGlyph(f,gname):
    glyphName, baseName, accentNames, offset = parseComposite(gname)
    if baseName.find("_") != -1:
        g = f.newGlyph(glyphName)
        lastWidth = 0
        for componentName in baseName.split("_"):
            g.appendComponent(componentName, (lastWidth, 0))
            lastWidth = f[componentName].width
            g.width += lastWidth
    else: 
        if not f.has_key(glyphName):
            try:
                f.compileGlyph(glyphName, baseName, accentNames)
            except KeyError as e:
                print ("KeyError raised for composition rule '%s', likely %s "
                    "anchor not found in glyph '%s'" % (gname, e, baseName))
                return
            g = f[glyphName]
            if len(accentNames) > 0:
                alignComponentsToAnchors(f, glyphName, baseName, accentNames)
            if offset[0] != 0 or offset[1] != 0:
                g.width += offset[1] + offset[0]
                g.move((offset[0], 0))
        else:
            print ("Existing glyph '%s' found in font, ignoring composition "
                "rule '%s'" % (glyphName, gname))