8000 Changed ft2font.get_charmap so that it now returns a character code->… · matplotlib/matplotlib@cf29210 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf29210

Browse files
committed
Changed ft2font.get_charmap so that it now returns a character code->glyph index dict, instead of the reverse as it used to return; updated all the files where get_charmap is used
svn path=/trunk/matplotlib/; revision=3026
1 parent 587e350 commit cf29210

File tree

11 files changed

+483
-354
lines changed

11 files changed

+483
-354
lines changed

API_CHANGES

+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
ft2font.get_charmap() now returns a dict that maps character codes
2+
to glyph indices (until now it was reversed)
3+
14
Moved data files into lib/matplotlib so that setuptools' develop
25
mode works. Re-organized the mpl-data layout so that this source
36
structure is maintained in the installation. (I.e. the 'fonts' and

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2007-02-17 Changed ft2font.get_charmap, and updated all the files where
2+
get_charmap is mentioned - ES
3+
14
2007-02-13 Added barcode demo- JDH
25

36
2007-02-13 Added binary colormap to cm - JDH

examples/font_indexing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
font.set_charmap(0)
1515

1616
codes = font.get_charmap().items()
17-
dsu = [(ccode, glyphind) for glyphind, ccode in codes]
18-
dsu.sort()
17+
#dsu = [(ccode, glyphind) for ccode, glyphind in codes]
18+
#dsu.sort()
1919
#for ccode, glyphind in dsu:
2020
# try: name = font.get_glyph_name(glyphind)
2121
# except RuntimeError: pass
@@ -26,7 +26,7 @@
2626
# make a charname to charcode and glyphind dictionary
2727
coded = {}
2828
glyphd = {}
29-
for glyphind, ccode in codes:
29+
for ccode, glyphind in codes:
3030
name = font.get_glyph_name(glyphind)
3131
coded[name] = ccode
3232
glyphd[name] = glyphind

examples/font_table_t 6D40 tf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
colors = [ [0.95 for c in range(16)] for r in range(16)]
2929

3030
figure(figsize=(8,4),dpi=120)
31-
for glyphind, ccode in codes:
31+
for ccode, glyphind in codes:
3232
if ccode>=256: continue
3333
r,c = divmod(ccode,16)
3434
s = chr(ccode)

examples/mathtext2_demo.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,57 @@
88
99
BUILD_FT2FONT = True
1010
11-
You have to put the following lines in your matplotlibrc file
11+
You have to put the following lines in your matplotlibrc file if you want to
12+
enable mathtext2 globaly (not needed for running this example):
1213
1314
mathtext.mathtext2: True # Needed to enable the new mathtext
1415
mathtext.rm : FreeSerif.ttf
1516
mathtext.it : FreeSerifItalic.ttf # Text italic
1617
mathtext.tt : FreeMono.ttf # Typewriter (monospaced)
1718
mathtext.mit : FreeSerifItalic.ttf # Math italic
1819
mathtext.cal : FreeSansOblique.ttf # Caligraphic
19-
mathtext.nonascii: FreeSerif.ttf # Used for \sum, \infty etc.
20+
mathtext.nonascii: FreeSerif.ttf # #Used for \sum, \infty etc.
2021
21-
Note that "FreeSerif.ttf" etc. may be replaced by any font. Also, for now
22-
the font files must me in the mpl-data dir.
22+
Note that "FreeSerif.ttf" etc. may be replaced by any font. Font files must be
23+
in your system's font path.
2324
2425
Only the first parameter must be set (mathtext2 uses BaKoMa fonts by
2526
default, and they come packaged with matplotlib, so the above lines
2627
override them) because mathtext2 is disabled by default.
2728
28-
This demo assumes that you have FreeSerif.ttf in the mpl-data dir.
29+
This demo assumes that you have FreeSerif.ttf installed.
2930
You can get FreeSerif.ttf (and other files) from:
3031
http://download.savannah.gnu.org/releases/freefont/
31-
32-
FreeFonts are distributed under GPL
32+
if you are on windows. On linux, they are usually shipped by default.
33+
FreeFonts are distributed under GPL.
3334
3435
"""
3536
# We override the default params
3637
from matplotlib import rcParams
37-
#rcParams['mathtext.mathtext2'] = True
38+
rcParams['mathtext.mathtext2'] = True
3839

3940
# You can put other fonts to override the default ones
40-
#rcParams['mathtext.rm'] = 'FreeSerif.ttf'
41+
rcParams['mathtext.rm'] = 'FreeSerif.ttf'
4142
#rcParams['mathtext.it'] = 'FreeSerifItalic.ttf'
4243
#rcParams['mathtext.tt'] = 'FreeMono.ttf'
4344
#rcParams['mathtext.mit'] = 'FreeSerifItalic.ttf'
4445
#rcParams['mathtext.cal'] = 'FreeSansOblique.ttf'
4546

4647
# This is used by mathtext2 to find chars with ord > 255 (Unicode characters)
47-
#rcParams['mathtext.nonascii'] = 'FreeSerif.ttf'
48-
48+
rcParams['mathtext.nonascii'] = 'FreeSerif.ttf'
4949
from pylab import *
5050
subplot(111, axisbg='y')
5151
plot([1,2,3], 'r')
5252
x = arange(0.0, 3.0, 0.1)
53-
54-
tex = r'$u_{x^2_1}^{y_{-q_u}}$'
55-
text(0.5, 2., tex, fontsize=20)
56-
show()
5753
grid(True)
54+
55+
tex = r'$1+1\ u_{x^2_1}^{y_{-q_u}}$'
56+
#text(0.5, 2., tex, fontsize=20)
57+
#show()
5858
#xlabel(r'$\Delta_i^j$', fontsize=20)
5959
#ylabel(r'$\Delta_{i+1}^j$', fontsize=20)
6060
#tex = r'$\cal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\rm{sin}(2 \pi f x_i)$'
61-
tex = ur"$1^j_3$"
61+
#tex = ur"$1^j_3$"
6262
#tex = ur"$Tj_1j_jj_gT$"
6363
#tex = ur"$F_1^1y_{1_{2_{3_2\sum_1^2{4}^6}}3}1_23$"
6464
#tex = ur"$x_2{\cal TRALALA}\sum_1^2$"
@@ -68,18 +68,22 @@
6868
#tex = ur"$\sin\exp{\rm sin\ exp}$"
6969
#tex = ur"$a^{\sin x}\sin b\sin(x/x), {\rm sin}(x/x){\rm sin\ }(x/x)$"
7070
#tex = ur"$1\frac {\int_{-\infty}^\infty} 22$"
71+
#tex = ur"$\rm a\vtext{Traktor}b$"
72+
tex = ur"$a\vtext{a}$"
7173
#tex = ur"$\frac{\int_{-\infty}^\infty} 2$"
7274
#tex = ur"$1_\frac{\sum^2_{i_{23}=0}} 2678$"
73-
tex = ur"$1_{\frac{\sum^2_{i_{23}=0}}{\sum_{i=\frac94}^\infty} 345}678$"
75+
#tex = ur"$1_{\frac{\sum^2_{i_{23}=0}}{\sum_{i=\frac94}^\infty} 345}678$"
7476
text(0.5, 2., tex, fontsize=20)
7577
tex = r'${\cal R}\prod_{i=\alpha_{i+1}}^\infty a_i\sin\exp(2 \pi f x_i)$'
76-
text(1, 1.9, tex, fontsize=20)
78+
#text(1, 1.9, tex, fontsize=20)
7779
tex = ur"$F_1^1y_{1_{2_{3_2\sum_1^2{4}^6}}3}1_23$"
78-
text(1, 1.7, tex, fontsize=20)
80+
#text(1, 1.7, tex, fontsize=20)
7981
tex = ur"$x = \sin(\sum_{i=0}^\infty y_i)$"
80-
text(1, 1.5, tex, fontsize=20)
82+
#text(1, 1.5, tex, fontsize=20)
8183
#title(r'$\Delta_i^j \hspace{0.4} \rm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20)
82-
savefig('mathtext_demo.png')
84+
85+
#savefig('mathtext_demo.png')
86+
savefig('mathtext_demo.svg')
8387
#savefig('mathtext_demo.ps')
8488

8589

lib/matplotlib/_mathtext_data.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
"""
44

55
# this dict maps symbol names to fontnames, glyphindex. To get the
6-
# glyph index from the character code, you have to use a reverse
7-
# dictionary grom font.get_charmaps, eg,
6+
# glyph index from the character code, you have to use get_charmap
87
"""
98
from matplotlib.ft2font import FT2Font
109
font = FT2Font('/usr/local/share/matplotlib/cmr10.ttf')
11-
codes = font.get_charmap().items()
12-
rd = dict([(charcode, glyphind) for glyphind,charcode in codes])
13-
items = rd.items()
10+
items = font.get_charmap().items()
1411
items.sort()
1512
1613
for charcode, glyphind in items:

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
1515
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
1616
FigureManagerBase, FigureCanvasBase
1717

18-
from matplotlib.cbook import is_string_like, izip, reverse_dict
18+
from matplotlib.cbook import is_string_like, izip
1919
from matplotlib.figure import Figure
2020

2121
from matplotlib.font_manager import fontManager
@@ -753,14 +753,13 @@ def draw_unicode(self, gc, x, y, s, prop, angle):
753753
self.set_font(font.get_sfnt()[(1,0,0,6)], prop.get_size_in_points())
754754

755755
cmap = font.get_charmap()
756-
glyphd = reverse_dict(cmap)
757756
lastgind = None
758757
#print 'text', s
759758
lines = []
760759
thisx, thisy = 0,0
761760
for c in s:
762761
ccode = ord(c)
763-
gind = glyphd.get(ccode)
762+
gind = cmap.get(ccode)
764763
if gind is None:
765764
ccode = ord('?')
766765
name = '.notdef'

lib/matplotlib/mathtext.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,12 @@ class UnicodeFonts(Fonts):
302302
filenamesd should be declared as a class atribute
303303
* glyphdict: a dict used for caching of glyph specific data
304304
* fonts: a dict of facename -> fontface pairs
305-
* charmaps: a dict of facename -> charmap pairs
305+
* charmaps: a dict of facename -> charmap pairs. Charmap maps character
306+
codes to glyph indices
306307
* glyphmaps: a dict of facename -> glyphmap pairs. A glyphmap is an
307308
inverted charmap
308309
* output: a string in ['Agg','SVG','PS'], coresponding to the backends
309-
* index: Fontfile specific index of a glyph/char. Taken from a charmap.
310+
* index: Fontfile specific index of a glyph.
310311
311312
"""
312313

@@ -324,16 +325,16 @@ def __init__(self, output='Agg'):
324325
self.fonts = dict(
325326
[ (facename, font_open(self.filenamesd[facename])) for
326327
facename in self.facenames])
327-
# a dict of glyphindex -> charcode pairs
328+
# a dict of charcode -> glyphindex pairs
328329
self.charmaps = dict(
329330
[ (facename, self.fonts[facename].get_charmap())
330331
for facename in self.facenames])
331-
# a dict of charcode -> glyphindex pairs
332+
# a dict of glyphindex -> charcode pairs
332333
self.glyphmaps = {}
333334
for facename in self.facenames:
334335
charmap = self.charmaps[facename]
335-
self.glyphmaps[facename] = dict([(charcode, glyphind)
336-
for glyphind, charcode in charmap.items()])
336+
self.glyphmaps[facename] = dict([(glyphind, charcode)
337+
for charcode, glyphind in charmap.items()])
337338
for fontface in self.fonts.values():
338339
fontface.clear()
339340
if self.output == 'SVG':
@@ -578,11 +579,11 @@ def __init__(self, useSVG=False):
578579

579580
self.charmaps = dict(
580581
[ (name, self.fonts[name].get_charmap()) for name in self.fnames])
581-
# glyphmaps is a dict names to a dict of charcode -> glyphindex
582+
# glyphmaps is a dict names to a dict of glyphindex -> charcode
582583
self.glyphmaps = {}
583584
for name in self.fnames:
584585
cmap = self.charmaps[name]
585-
self.glyphmaps[name] = dict([(ccode, glyphind) for glyphind, ccode in cmap.items()])
586+
self.glyphmaps[name] = dict([(glyphind, ccode) for ccode, glyphind in cmap.items()])
586587

587588
for font in self.fonts.values():
588589
font.clear()
@@ -607,7 +608,7 @@ def _get_info (self, font, sym, fontsize, dpi):
607608

608609
if latex_to_bakoma.has_key(sym):
609< 10000 code>610
basename, num = latex_to_bakoma[sym]
610-
num = self.charmaps[basename][num]
611+
num = self.glyphmaps[basename][num]
611612
elif len(sym) == 1:
612613
num = ord(sym)
613614
else:
@@ -658,7 +659,7 @@ def render(self, ox, oy, font, sym, fontsize, dpi):
658659
basename = self.fontmap[font]
659660
if latex_to_bakoma.has_key(sym):
660661
basename, num = latex_to_bakoma[sym]
661-
num = self.charmaps[basename][num]
662+
num = self.glyphmaps[basename][num]
662663
elif len(sym) == 1:
663664
num = ord(sym)
664665
else:
@@ -693,7 +694,7 @@ def _get_num(self, font, sym):
693694
basename = self.fontmap[font]
694695
if latex_to_bakoma.has_key(sym):
695696
basename, num = latex_to_bakoma[sym]
696-
num = self.charmaps[basename][num]
697+
num = self.glyphmaps[basename][num]
697698
elif len(sym) == 1:
698699
num = ord(sym)
699700
else:
@@ -705,7 +706,7 @@ class BakomaPSFonts(Fonts):
705706
"""
706707
Use the Bakoma postscript fonts for rendering to backend_ps
707708
"""
708-
fnames = ('cmmi10', 'cmsy10', 'cmex10',
709+
facenames = ('cmmi10', 'cmsy10', 'cmex10',
709710
'cmtt10', 'cmr10')
710711
# allocate a new set of fonts
711712
basepath = os.path.join( get_data_path(), 'fonts', 'ttf' )
@@ -721,10 +722,13 @@ def __init__(self):
721722
self.glyphd = {}
722723
self.fonts = dict(
723724
[ (name, FT2Font(os.path.join(self.basepath, name) + '.ttf'))
724-
for name in self.fnames])
725+
for name in self.facenames])
725726

726-
self.charmaps = dict(
727-
[ (name, self.fonts[name].get_charmap()) for name in self.fnames])
727+
self.glyphmaps = {}
728+
for facename in self.facenames:
729+
charmap = self.fonts[facename].get_charmap()
730+
self.glyphmaps[facename] = dict([(glyphind, charcode)
731+
for charcode, glyphind in charmap.items()])
728732
for font in self.fonts.values():
729733
font.clear()
730734

@@ -741,7 +745,7 @@ def _get_info (self, font, sym, fontsize, dpi):
741745
if latex_to_bakoma.has_key(sym):
742746
basename, num = latex_to_bakoma[sym]
743747
sym = self.fonts[basename].get_glyph_name(num)
744-
num = self.charmaps[basename][num]
748+
num = self.glyphmaps[basename][num]
745749
elif len(sym) == 1:
746750
num = ord(sym)
747751
else:
@@ -812,7 +816,7 @@ def _get_filename_and_num (self, font, sym, fontsize, dpi):
812816
if latex_to_bakoma.has_key(sym):
813817
basename, num = latex_to_bakoma[sym]
814818
sym = self.fonts[basename].get_glyph_name(num)
815-
num = self.charmaps[basename][num]
819+
num = self.glyphmaps[basename][num]
816820
elif len(sym) == 1:
817821
num = ord(sym)
818822
else:

0 commit comments

Comments
 (0)
0