Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a52a350

Browse files
committed
Merge pull request #975 from cgohlke/patch-11
Enhance font_table_ttf.py
2 parents 461807e + 59c2e0c commit a52a350

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

examples/pylab_examples/font_table_ttf.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@
77
88
Usage python font_table_ttf.py somefile.ttf
99
"""
10-
import sys, os
10+
11+
import sys
12+
import os
13+
14+
import matplotlib
1115
from matplotlib.ft2font import FT2Font
12-
from pylab import figure, table, show, axis, title
1316
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
1424

1525
# the font table grid
1626

@@ -19,25 +29,29 @@
1929
labelr = ['00', '10', '20', '30', '40', '50', '60', '70', '80', '90',
2030
'A0', 'B0', 'C0', 'D0', 'E0', 'F0']
2131

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+
2338
font = FT2Font(fontname)
24-
codes = font.get_charmap().items()
39+
codes = list(font.get_charmap().items())
2540
codes.sort()
2641

2742
# 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)]
3045

31-
figure(figsize=(8,4),dpi=120)
46+
figure(figsize=(8, 4), dpi=120)
3247
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)
3551
s = unichr(ccode)
3652
chars[r][c] = s
3753

38-
39-
40-
lightgrn = (0.5,0.8,0.5)
54+
lightgrn = (0.5, 0.8, 0.5)
4155
title(fontname)
4256
tab = table(cellText=chars,
4357
rowLabels=labelr,
@@ -50,7 +64,7 @@
5064

5165
for key, cell in tab.get_celld().items():
5266
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))
5569
axis('off')
5670
show()

0 commit comments

Comments
 (0)