Compare side-bearing of two fonts

ttmt's picture

Hi

Is it possible to compare the side-bearings of two separate fonts and mark any differences.

I have been trying to do it with a macro. I can open the two fonts but how do I loop through the glyphs of the fonts. It would be nice it it skips any glyphs that aren't present in both fonts as well.


from robofab.world import OpenFont
fontOne = OpenFont(None)
fontTwo = OpenFont(None)
print fontOne.info.fullName,"/",fontTwo.info.fullName
print " "
for g in fontOne:
if fontOne[g].leftMargin != fontTwo[g].leftMargin or fontOne[g].rightMargin != fontTwo[g].rightMargin:
print fontOne[g].leftMargin,"-",g,"-",fontOne[g].rightMargin
print fontTwo[g].leftMargin,"-",g,"-",fontTwo[g].rightMargin
print fontOne[g].leftMargin - fontTwo[g].leftMargin,"----",fontOne[g].rightMargin - fontTwo[g].rightMargin
print "------------------"
print " "
fontOne[g].mark += 10

jasonc's picture

You just have some syntax issues, mostly.
- just 'cause Python is loosely typed doesn't mean you can ignore variable type. In order to print the left margin, which is an integer, you need to cast it as a string.
- In order to specify a glyph, you need to pass it the glyph name, not the glyph itself, like: font[glyphname"]
- to add things to a print line, use the plus sign, not comma.

So try this:

from robofab.world import OpenFont
fontOne = OpenFont(None)
fontTwo = OpenFont(None)
print fontOne.info.postscriptFullName + " / " + fontTwo.info.postscriptFullName
print " "
for g in fontOne:
if fontOne[g.name].leftMargin != fontTwo[g.name].leftMargin or fontOne[g.name].rightMargin != fontTwo[g.name].rightMargin:
print str(fontOne[g.name].leftMargin) + "-" + g.name + "-" + str(fontOne[g.name].rightMargin)
print str(fontTwo[g.name].leftMargin) + "-" + g.name + "-" + str(fontTwo[g.name].rightMargin)
print str(fontOne[g.name].leftMargin - fontTwo[g.name].leftMargin) + "----" + str(fontOne[g.name].rightMargin - fontTwo[g.name].rightMargin)
print "------------------"
print " "
fontOne[g.name].mark += 10

jasonc's picture

[duplicate post]

ttmt's picture

Thanks for that jasonc

It starts to work but then stops with no error output. I think it might be because the fonts might have slightly different glyphs included.

They are both variants of the same font but one has been updated slightly and I wanted to make sure the spacing hand't been changed.

Is there a log i can check to see why it stops?

jasonc's picture

Really? IN my testing it works when the glyph sets are not the same, it simply reports zeros for both sidebearings. Not an elegant solution but it works.

Did you adjust the indents before running it? Typophile edits out the indents, so you need to do that in order for it to work

ttmt's picture

Yes I replaced the indents.

It starts so I'm assuming it works but then stops.

Is there a log somewhere I could check?

jasonc's picture

Not really. You can try adding print statements at different places so you can see where it's at. If you're not seeing any print lines, then you know it's stuck at the beginning, of course.

Syndicate content Syndicate content