Iterating through kerning pairs


#1

Hello!

Just a quick question: is there a way to iterate through all of the exiting kern pairs in a font (a list like in the kerning table)? I know that in the documentation there’s a kerning property in font level but it is a dictionary that doesn’t return one by one all of the pairs. I want to code something that goes in previous/next kerning pairs in a font.

Thank you so much!


#2

You can click on pair after the other in the kerning panel. That will activate the pair you clicked at…


#3

Thanks Georg! I know, but since I am adapting Andy Climer’s kerntroller, which is a controller used to kern, I actually need to assign a button to go to next pair and another to go to the previous so that’s why I am asking if there’s a possibility to iterate through all of the list in the kerning table.

Find here Andy’s kerntroller image from his website: https://github.com/andyclymer/kerntroller/tree/master/Hardware

Thank you so much again!


#4

What do you mean? It is a nested dict. The first key is the master ID, the second the left side, and the third key is the right side:

for masterID in Font.kerning:
	print "Kerning for master ID %s:"%masterID
	for leftSide in Font.kerning[masterID]:
		for rightSide in Font.kerning[masterID][leftSide]:
			print leftSide, rightSide, Font.kerning[masterID][leftSide][rightSide]

#5

This can be done. You are one the right track with the font.kerning. It is a two level dictionary, the first key is the group/glyphID of the first glyph, the value is a dict that where the keys are the second group/glyphID. then the values are the actually kern values.

The problem I see is to find the current pair first. Because you can’t just store what you the function did the last time as the whole context could have been changed.


#6

Thank you @mekkablue and @GeorgSeifert. While working on my own during this afternoon I noticed that nested dicts but with your input makes a lot more of sense.

Georg, about what you are mentioning it makes sense. I was considering maybe that I could open a new tab with the first pair in the kern table, do you think this would be something that could be done?

Again, thank you so much for for time and information.


#7

I would try to find the current kern pair from the glyphs next to the cursor. But it can be a bit tricky with exceptions and classes.


#8

I see, this makes sense. Let’s see what I can do. How can I then open a new tab with a given kern pair in this nested dictionary?


#9

I wound not open a new tab.

To open a new tab, have a looks at @mekkablue scripts, he has several that do this.


#10

You are right, I’ve been trying to open a new tab with a given pair but it actually opens the name as a string instead of the pair itself but I’m going to figure it out. Thanks!


#11

@Ricard_Garcia you might also want to have a little peek into the Kernkraft Plugin. It sems to me you’re about to scratch a lot things that are to be found in there.


#12

Thank you so much, @Mark ! As you might noticed I’m as interested in scripting and code as a newbie so sometimes I know where to search but some things look pretty unknown to me. Would you recommend to look into the KernKraftModule.py that is inside your plugin or is there any other part that might help me?

Again, thank you so much!


#13

Yes, the KernKraftModule.py is where most of the stuff happens. I have some methods there to check for kerning pairs (e.g. where the plugin “skipps already kerned pairs”) and such. Also making a sting from the glyphs and opening tabs are things you can find. I know it is a tiny bit messy. There are certain things that could be put into methods because they have almost entirely repetitive code. But I commented those bits, because back then I was too stupid to figure it out properly. I still tired to document most things as good as possible. Some things might be easier and shorter nowadays, because Georg implemented lots of things into the python wrapper since then. So bear with me about the code uglyness :slight_smile:


#14

The code is super helpful once I’ve spent a bit of time analysing it. Thank you so much again, let’s see what I can do with it! If I would have any question I might write a new post or send you a message if you are up for it.

Thanks also to @GeorgSeifert and @mekkablue

:slight_smile:


#15

Good luck :+1: