|
7 | 7 |
|
8 | 8 | Usage python font_table_ttf.py somefile.ttf
|
9 | 9 | """
|
10 |
| -import sys, os |
| 10 | + |
| 11 | +import sys |
| 12 | +import os |
| 13 | + |
| 14 | +import matplotlib |
11 | 15 | from matplotlib.ft2font import FT2Font
|
12 |
| -from pylab import figure, table, show, axis, title |
13 | 16 | from matplotlib.font_manager import FontProperties
|
| 17 | +from pylab import figure, table, show, axis, title |
| 18 | + |
| 19 | +try: |
| 20 | + unichr |
| 21 | +except NameError: |
| 22 | + # Python 3 |
| 23 | + unichr = chr |
14 | 24 |
|
15 | 25 | # the font table grid
|
16 | 26 |
|
|
19 | 29 | labelr = ['00', '10', '20', '30', '40', '50', '60', '70', '80', '90',
|
20 | 30 | 'A0', 'B0', 'C0', 'D0', 'E0', 'F0']
|
21 | 31 |
|
22 |
| -fontname = sys.argv[1] |
| 32 | +if len(sys.argv) > 1: |
| 33 | + fontname = sys.argv[1] |
| 34 | +else: |
| 35 | + fontname = os.path.join(matplotlib.get_data_path(), |
| 36 | + 'fonts', 'ttf', 'Vera.ttf') |
| 37 | + |
23 | 38 | font = FT2Font(fontname)
|
24 |
| -codes = font.get_charmap().items() |
| 39 | +codes = list(font.get_charmap().items()) |
25 | 40 | codes.sort()
|
26 | 41 |
|
27 | 42 | # a 16,16 array of character strings
|
28 |
| -chars = [ ['' for c in range(16)] for r in range(16)] |
29 |
| -colors = [ [(0.95,0.95,0.95) for c in range(16)] for r in range(16)] |
| 43 | +chars = [['' for c in range(16)] for r in range(16)] |
| 44 | +colors = [[(0.95, 0.95, 0.95) for c in range(16)] for r in range(16)] |
30 | 45 |
|
31 |
| -figure(figsize=(8,4),dpi=120) |
| 46 | +figure(figsize=(8, 4), dpi=120) |
32 | 47 | for ccode, glyphind in codes:
|
33 |
| - if ccode>=256: continue |
34 |
| - r,c = divmod(ccode,16) |
| 48 | + if ccode >= 256: |
| 49 | + continue |
| 50 | + r, c = divmod(ccode, 16) |
35 | 51 | s = unichr(ccode)
|
36 | 52 | chars[r][c] = s
|
37 | 53 |
|
38 |
| - |
39 |
| - |
40 |
| -lightgrn = (0.5,0.8,0.5) |
| 54 | +lightgrn = (0.5, 0.8, 0.5) |
41 | 55 | title(fontname)
|
42 | 56 | tab = table(cellText=chars,
|
43 | 57 | rowLabels=labelr,
|
|
50 | 64 |
|
51 | 65 | for key, cell in tab.get_celld().items():
|
52 | 66 | row, col = key
|
53 |
| - if row>0 and col>0: |
54 |
| - cell.set_text_props(fontproperties=FontProperties(fname=sys.argv[1])) |
| 67 | + if row > 0 and col > 0: |
| 68 | + cell.set_text_props(fontproperties=FontProperties(fname=fontname)) |
55 | 69 | axis('off')
|
56 | 70 | show()
|
0 commit comments