Fontforge python: add kerning classes

295 Views Asked by At

I have been trying for too much time already to do something like this with a fontforge script: *kern with a value of 400 (positive) any glyph except "space" followed by an "s".

How do I do that?

This is how it would be done via the interface:

gif example

1

There are 1 best solutions below

2
On

After not finding how to use wildcards (like starts with "*" and ends with "s"), I ended up with this code, which iterates through every single possible glyph combination and sets the kerning accordingly.

# characters = [...] # Filled with character codes like "a", "b", "c", etc.
def add_kerning(font):
    # Index of character "s"
    s_index = characters.index("s")

    # Fill a matrix with 0's. It's a flat matrix, but you can view it as a 2d square matrix.
    offsets = [0] * len(characters) ** 2

    # Fill the offsets where the second char is s (this fills a column)
    for index in range(len(characters)):
        offsets[index * len(characters) + s_index] = 400

    offsets_tuple = tuple(offsets)

    font.addLookup("kern", "gpos_pair", None, [["kern", [["latn", ["dflt"]]]]])
    font.addKerningClass("kern", "kern-1", tuple(characters), tuple(characters), tuple(offsets_tuple))