Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fontTools.afmLib import AFM
- from ufo2fdk.fontInfoData import getFontBounds
- from ufo2fdk.fontInfoData import getAttrWithFallback
- from robofab.interface.all.dialogs import PutFile
- def buildFlatKerningWithGroupsAndKerning(groups, kerning):
- flat = {}
- keys = sorted(kerning.keys())
- for key in keys:
- kern = kerning[key]
- (first, second) = key
- if "@" in first:
- first = groups.get(first, [])
- else:
- first = [first]
- if "@" in second:
- second = groups.get(second, [])
- else:
- second = [second]
- for s in second:
- for f in first:
- flat[(f, s)] = kern
- return flat
- # get the current font
- f = CurrentFont()
- # a new AFM object
- metrics = AFM()
- # add a comment
- metrics.addComment("exported from RoboFont")
- # add some font info
- metrics.FontName = getAttrWithFallback(f.info, "postscriptFontName")
- metrics.FullName = getAttrWithFallback(f.info, "postscriptFullName")
- metrics.FamilyName = f.info.familyName
- metrics.ItalicAngle = f.info.italicAngle
- metrics.CapHeight = f.info.capHeight
- metrics.XHeight = f.info.xHeight
- metrics.Ascender = f.info.ascender
- metrics.Descender = f.info.descender
- metrics.FontBBox = getFontBounds(f)
- metrics.Version = getAttrWithFallback(f.info, "openTypeNameVersion")
- metrics.Notice = getAttrWithFallback(f.info, "copyright")
- metrics.EncodingScheme = "AdobeStandardEncoding"
- # adding all glyphs
- for g in f:
- charnum = g.unicode
- if charnum is None:
- charnum = -1
- metrics[g.name] = (charnum, g.width, g.box)
- # build a flat kerning dict
- flatKerning = buildFlatKerningWithGroupsAndKerning(f.groups, f.kerning)
- # adding all kering pairs from the flat kerning
- for pair, value in flatKerning.items():
- metrics[pair] = value
- # write
- path = PutFile("Save this .afm file as..", fileName="%s-%s.afm" % (f.info.familyName, f.info.styleName))
- if path:
- metrics.write(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement