Question

I'm experimenting with the fontforge Python library/wrapper thing and I don't seem to get multiple character substitution to work. Maybe you gals and guys could help to point me at what I'm doing wrong?

Basically, I'm trying to replace "ABC" with the character representation of "a", but I'm going to extend that with proper subsitutions once this is working.

from random import randint
from datetime import datetime

def add_pixel(pen, x, y, scale = 100):
    pen.moveTo(((x + 0) * scale, (y + 0) * scale))
    pen.lineTo(((x + 0) * scale, (y + 1) * scale))
    pen.lineTo(((x + 1) * scale, (y + 1) * scale))
    pen.lineTo(((x + 1) * scale, (y + 0) * scale))
    pen.closePath()

def add_character(font, code, name):
    if not name in list(font):
        font.createChar(code, name)

    pen = font[code].glyphPen()
    for i in range(1, 15):
        add_pixel(pen, randint(0, 15), randint(0, 15), 100 * 10 / 15)

try:
    import fontforge
except Exception, e:
    raise
else:
    font = fontforge.font()
    font.familyname = "font"
    font.fontname = "font x15"
    font.fullname = "font x15"
    font.version = datetime.now().strftime("%Y-%m-%d %H:%M")

    # lower
    for c in range(0x61, 0x61 + 26):
        add_character(font, c, unichr(c))

    # upper
    for c in range(0x41, 0x41 + 26):
        add_character(font, c, unichr(c))

    font.addLookup("gsub", "gsub_multiple", (), (("dlig",(("latn",("dflt")),)),))
    font.addLookupSubtable("gsub", "gsub_n")

    glyph = font["a"]
    glyph.addPosSub("gsub_n", ("A", "B", "C"))

    # font.save("font.x15.sfd")
    font.generate("font.x15.otf", flags=("PfEd-lookups", "opentype"))
finally:
    pass
Was it helpful?

Solution

I think your lookup type doens't match the feature.

# Try "gsub_ligature" as type.
font.addLookup("gsub", "gsub_ligature", (), (("dlig",(("latn",("dflt")),)),))

Tip: You can inspect your features by generating a feature file:

font.generateFeatureFile("features.fea")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top