8000 added some more font examples · matplotlib/matplotlib@3dc9527 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3dc9527

Browse files
committed
added some more font examples
svn path=/trunk/matplotlib/; revision=1110
1 parent 9c19d00 commit 3dc9527

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

examples/font_indexing.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
A little example that shows how the various indexing into the font
3+
tables relate to one another. Mainly for mpl developers....
4+
5+
"""
6+
import matplotlib
7+
from matplotlib.ft2font import FT2Font
8+
9+
10+
11+
fname = matplotlib.get_data_path() + '/cmr10.ttf'
12+
font = FT2Font(fname)
13+
14+
codes = font.get_charmap().items()
15+
dsu = [(ccode, glyphind) for glyphind, ccode in codes]
16+
dsu.sort()
17+
for ccode, glyphind in dsu:
18+
try: name = font.get_glyph_name(glyphind)
19+
except RuntimeError: pass
20+
else: print '% 4d % 4d %s %s'%(glyphind, ccode, hex(int(ccode)), name)
21+
22+
23+
24+
# make a charname to charcode and glyphind dictionary
25+
coded = {}
26+
glyphd = {}
27+
for glyphind, ccode in codes:
28+
name = font.get_glyph_name(glyphind)
29+
coded[name] = ccode
30+
glyphd[name] = glyphind
31+
32+
code = coded['A']
33+
glyph = font.load_char(code)
34+
print glyph.bbox
35+
36+
print 'AV', font.get_kerning(glyphd['A'], glyphd['V'])/64.0
37+
print 'AA', font.get_kerning(glyphd['A'], glyphd['A'])/64.0

examples/font_table_ttf.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)
0