字体索引

一个小示例,它展示了对字体表的各种索引是如何相互关联的。主要是为MPL开发商.。

输出:

  1. (6, 0, 519, 576)
  2. 36 57 65 86
  3. AV 0
  4. AV 0
  5. AV 0
  6. AV 0
  1. import matplotlib
  2. from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, KERNING_UNFITTED, KERNING_UNSCALED
  3. fname = matplotlib.get_data_path() + '/fonts/ttf/DejaVuSans.ttf'
  4. font = FT2Font(fname)
  5. font.set_charmap(0)
  6. codes = font.get_charmap().items()
  7. #dsu = [(ccode, glyphind) for ccode, glyphind in codes]
  8. #dsu.sort()
  9. #for ccode, glyphind in dsu:
  10. # try: name = font.get_glyph_name(glyphind)
  11. # except RuntimeError: pass
  12. # else: print('% 4d % 4d %s %s' % (glyphind, ccode, hex(int(ccode)), name))
  13. # make a charname to charcode and glyphind dictionary
  14. coded = {}
  15. glyphd = {}
  16. for ccode, glyphind in codes:
  17. name = font.get_glyph_name(glyphind)
  18. coded[name] = ccode
  19. glyphd[name] = glyphind
  20. code = coded['A']
  21. glyph = font.load_char(code)
  22. print(glyph.bbox)
  23. print(glyphd['A'], glyphd['V'], coded['A'], coded['V'])
  24. print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_DEFAULT))
  25. print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNFITTED))
  26. print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNSCALED))
  27. print('AV', font.get_kerning(glyphd['A'], glyphd['T'], KERNING_UNSCALED))

下载这个示例