Duplicating kerning classes in Fontlab via script

butterick's picture

So I see that a FL font object has a "classes" attribute that's a list of the classes in the font.

1) Is there a way to filter this list to only see kerning classes?

2) When I try to copy kerning classes via script, e.g. — 

fl.font2.classes = fl.font1.classes

This will copy the class members properly, but it loses the state of the left glyph / right glyph checkboxes in the class panel. Where does that data live?

gargoyle's picture

Filter for kerning classes by matching the first character of each class with an underscore:

classes = fl.font.classes
for c in range( len(classes) ):
  class_text = classes[c]
  if class_text[0] == "_":
    print class_text
dezcom's picture

You can also save a copy of your classes as a text file from the "Classes" menu. In the upper left corner of the window is a tiny icon which when clicked reveals the option to "Save classes". You can either import this file completely as a replacement classes file, append some parts of it, or copy paste from a text editor the things you want.

eigi's picture

The FontLab Font object offers the folowing methods to access class attributes:

SetClassFlags(int class_index, bool left, bool right)
allows to set 'left' and 'right' properties of the kerning class
SetClassFlags(int class_index, bool lsb, bool rsb, bool width)
allows to set 'lsb', 'rsb' and 'width' properties of the metrics class
GetClassLeft(int class_index)
returns the 'left' property of the class
GetClassRight(int class_index)
returns the 'right' property of the class
GetClassMetricsFlags(int class_index)
returns the tuple containing LSB, RSB and Width flags of the metrics class
butterick's picture

Thanks Eigi. Mission accomplished

# copy kern class information from source
kernClasses = []
kernClassesToBooleans = {}
for index, glyphClass in enumerate(sourceFLFont.classes):
    if glyphClass.startswith("_"):
        kernClasses.append(glyphClass)
        kernClassesToBooleans[glyphClass] = (sourceFLFont.GetClassLeft(index), sourceFLFont.GetClassRight(index))

# clear target; copy classes
targetFLFont.classes = []
targetFLFont.classes = kernClasses

# copy booleans into target
for index, kernClass in enumerate(targetFLFont.classes):
    leftBool, rightBool = kernClassesToBooleans[kernClass]
    targetFLFont.SetClassFlags(index, leftBool, rightBool)
Syndicate content Syndicate content