removeKerningForPair() is really slow

I’m using “Remove Small Kerning Pairs” from @mekkablue and it’s seems that removeKerningForPair() method is really slow.

Maybe something could be optimized in the method.

With this script, it took less than 1 second, whereas removing small kerning pairs took several minutes.

# Initialize the selected font master and threshold for kerning adjustment
selected_master_id = Font.selectedFontMaster.id
kerning_threshold = 5

adjusted_kerning = {}
pairs_deleted = 0

# Iterate through the kerning pairs of the selected font master
for left_glyph, right_pairs in Font.kerning[selected_master_id].items():
    
    # Filter kerning pairs that exceed the specified threshold
    valid_pairs = {pair: value for pair, value in right_pairs.items() if abs(value) >= kerning_threshold}
    
    if valid_pairs:
        adjusted_kerning[left_glyph] = valid_pairs
    else:
        pairs_deleted += len(right_pairs)

# Update the font's kerning with the adjusted values
Font.kerning[selected_master_id] = adjusted_kerning

# Output the result
print(f"{pairs_deleted} pairs deleted in {Font.selectedFontMaster.name}")

I suppose that pressing the [-] button in the Kerning Panel also triggers removeKerningForPair(), because when I select a lot of pairs, it’s also very slow to delete them.

I noticed the same, but this is only the case in 3234+ (maybe later)

On my machine running 3234, the script is really fast, but on my machine running 3240, the script simply doesn’t finish.

I updated the script, but needed to fix something in the Glyphs code, too. So it will be quicker with the next update.

2 Likes

Has this really been fixed? I am using removeKerningForPair() a lot in an export workflow and it is still extremely slow. 3245

In 3234, it works fine.

The script needs Glyphs.app Python Scripting API Documentation — Glyphs.app Python Scripting API 3.2 documentation

I have that, of course.

I do, however, make several copies of the font object after disabling interface updates. Do I need to disable interface updates again for every font object copy?

font.disableUpdateInterface()
export_font = font.copy()
export_font.removeKerningForPair(...)

You only need to do it for the font object where you change the kerning.

Well, it doesn’t work in any case. It’s still extremely slow in 3234+.

Can you send me the script and a test file?

Sent, on Telegram (but you have repo access anyway)