Advertisement
Guest User

Untitled

a guest
May 24th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. from fontTools.afmLib import AFM
  2.  
  3. from ufo2fdk.fontInfoData import getFontBounds
  4. from ufo2fdk.fontInfoData import getAttrWithFallback
  5.  
  6. from robofab.interface.all.dialogs import PutFile
  7.  
  8. def buildFlatKerningWithGroupsAndKerning(groups, kerning):
  9. flat = {}
  10. keys = sorted(kerning.keys())
  11. for key in keys:
  12. kern = kerning[key]
  13. (first, second) = key
  14. if "@" in first:
  15. first = groups.get(first, [])
  16. else:
  17. first = [first]
  18. if "@" in second:
  19. second = groups.get(second, [])
  20. else:
  21. second = [second]
  22. for s in second:
  23. for f in first:
  24. flat[(f, s)] = kern
  25. return flat
  26.  
  27. # get the current font
  28. f = CurrentFont()
  29.  
  30. # a new AFM object
  31. metrics = AFM()
  32. # add a comment
  33. metrics.addComment("exported from RoboFont")
  34. # add some font info
  35. metrics.FontName = getAttrWithFallback(f.info, "postscriptFontName")
  36. metrics.FullName = getAttrWithFallback(f.info, "postscriptFullName")
  37. metrics.FamilyName = f.info.familyName
  38. metrics.ItalicAngle = f.info.italicAngle
  39. metrics.CapHeight = f.info.capHeight
  40. metrics.XHeight = f.info.xHeight
  41. metrics.Ascender = f.info.ascender
  42. metrics.Descender = f.info.descender
  43. metrics.FontBBox = getFontBounds(f)
  44. metrics.Version = getAttrWithFallback(f.info, "openTypeNameVersion")
  45. metrics.Notice = getAttrWithFallback(f.info, "copyright")
  46. metrics.EncodingScheme = "AdobeStandardEncoding"
  47. # adding all glyphs
  48. for g in f:
  49. charnum = g.unicode
  50. if charnum is None:
  51. charnum = -1
  52. metrics[g.name] = (charnum, g.width, g.box)
  53. # build a flat kerning dict
  54. flatKerning = buildFlatKerningWithGroupsAndKerning(f.groups, f.kerning)
  55. # adding all kering pairs from the flat kerning
  56. for pair, value in flatKerning.items():
  57. metrics[pair] = value
  58. # write
  59. path = PutFile("Save this .afm file as..", fileName="%s-%s.afm" % (f.info.familyName, f.info.styleName))
  60. if path:
  61. metrics.write(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement