|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +matplotlib has support for freetype fonts. Here's a little example |
| 4 | +using the 'table' command to build a font table that shows the glyphs |
| 5 | +by character code. |
| 6 | +
|
| 7 | +Usage python font_table_ttf.py somefile.ttf |
| 8 | +""" |
| 9 | +import sys, os |
| 10 | +from matplotlib.ft2font import FT2Font |
| 11 | +from pylab import figure, table, show, axis, title |
| 12 | +from matplotlib.font_manager import FontProperties |
| 13 | + |
| 14 | +# the font table grid |
| 15 | + |
| 16 | +labelc = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 17 | + 'A', 'B', 'C', 'D', 'E', 'F'] |
| 18 | +labelr = ['00', '10', '20', '30', '40', '50', '60', '70', '80', '90', |
| 19 | + 'A0', 'B0', 'C0', 'D0', 'E0', 'F0'] |
| 20 | + |
| 21 | +fontname = sys.argv[1] |
| 22 | +font = FT2Font(fontname) |
| 23 | +codes = font.get_charmap().items() |
| 24 | +codes.sort() |
| 25 | + |
| 26 | +# a 16,16 array of character strings |
| 27 | +chars = [ ['' for c in range(16)] for r in range(16)] |
| 28 | +colors = [ [0.95 for c in range(16)] for r in range(16)] |
| 29 | + |
| 30 | +figure(figsize=(8,4),dpi=120) |
| 31 | +for glyphind, ccode in codes: |
| 32 | + if ccode>=256: continue |
| 33 | + r,c = divmod(ccode,16) |
| 34 | + s = chr(ccode) |
| 35 | + chars[r][c] = s |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +lightgrn = (0.5,0.8,0.5) |
| 40 | +title(fontname) |
| 41 | +tab = table(cellText=chars, |
| 42 | + rowLabels=labelr, |
| 43 | + colLabels=labelc, |
| 44 | + rowColours=[lightgrn]*16, |
| 45 | + colColours=[lightgrn]*16, |
| 46 | + cellColours=colors, |
| 47 | + cellLoc='center', |
| 48 | + loc='upper left') |
| 49 | + |
| 50 | +for key, cell in tab.get_celld().items(): |
| 51 | + row, col = key |
| 52 | + if row>0 and col>0: |
| 53 | + cell.set_text_props(fontproperties=FontProperties(fname=sys.argv[1])) |
| 54 | +axis('off') |
| 55 | +show() |
0 commit comments