On Wednesday 14 February 2007 21:53, Nicolas Grilly wrote:
> On 2/14/07, Evgeniy Stepanov <[EMAIL PROTECTED]> wrote:
> > FT2Font.get_charmap() returns a mapping from glyph index to character
> > code. This looks like a very bad design decision to me, because several
> > character codes can correspond to one glyph. For example, in Times New
> > Roman, both 0x32 (space) and 0xA0 (nbsp) are mapped to glyph index 3. Of
> > course, the first one gets lost in get_charmap().
> >
> > I think, get_charmap should be fixed to return mapping from character
> > codes to glyph indices. Alternatively, get_charmap() could be left as it
> > is, and get_rcharmap() added.
>
> I agree with you. I've already posted something about this issue some
> time ago: http://sourceforge.net/mailarchive/message.php?msg_id=37418828
>
> > I'm willing to implement either one. Which do you prefer ?
>
> I think we should prefer the first alternative: I've made a quick grep
> through matplotlib's code and I've observed that each time get_charmap
> is called, the returned dict is never used as is, but immediately
> reversed.
I also prefer the first way. Here is the patch. Please re-check at least the
changes to mathtext.py, I could miss something. mathtext_demo.py still works,
but it obviously does not test all the changes.
diff --git a/examples/font_indexing.py b/examples/font_indexing.py
index 2167745..40dbc09 100644
--- a/examples/font_indexing.py
+++ b/examples/font_indexing.py
@@ -13,8 +13,7 @@ fname = matplotlib.get_data_path() + '/Vera.ttf'
font = FT2Font(fname)
font.set_charmap(0)
-codes = font.get_charmap().items()
-dsu = [(ccode, glyphind) for glyphind, ccode in codes]
+dsu = font.get_charmap().items()
dsu.sort()
#for ccode, glyphind in dsu:
# try: name = font.get_glyph_name(glyphind)
diff --git a/examples/font_table_ttf.py b/examples/font_table_ttf.py
index 14a2be3..bbf977f 100644
--- a/examples/font_table_ttf.py
+++ b/examples/font_table_ttf.py
@@ -20,15 +20,15 @@ labelr = ['00', '10', '20', '30', '40', '50', '60', '70', '80', '90',
fontname = sys.argv[1]
font = FT2Font(fontname)
-codes = font.get_charmap().items()
-codes.sort()
+indices = font.get_charmap().items()
+indices.sort()
# a 16,16 array of character strings
chars = [ ['' for c in range(16)] for r in range(16)]
colors = [ [0.95 for c in range(16)] for r in range(16)]
figure(figsize=(8,4),dpi=120)
-for glyphind, ccode in codes:
+for ccode, glyphind in indices:
if ccode>=256: continue
r,c = divmod(ccode,16)
s = chr(ccode)
diff --git a/lib/matplotlib/_mathtext_data.py b/lib/matplotlib/_mathtext_data.py
index 114d74e..5ea9790 100644
--- a/lib/matplotlib/_mathtext_data.py
+++ b/lib/matplotlib/_mathtext_data.py
@@ -8,10 +8,7 @@ font data tables for truetype and afm computer modern fonts
"""
from matplotlib.ft2font import FT2Font
font = FT2Font('/usr/local/share/matplotlib/cmr10.ttf')
-codes = font.get_charmap().items()
-rd = dict([(charcode, glyphind) for glyphind,charcode in codes])
-items = rd.items()
-items.sort()
+items = font.get_charmap().items().sort()
for charcode, glyphind in items:
print charcode, glyphind
diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py
index 8cdb104..8a4391f 100644
--- a/lib/matplotlib/backends/backend_ps.py
+++ b/lib/matplotlib/backends/backend_ps.py
@@ -753,14 +753,13 @@ grestore
self.set_font(font.get_sfnt()[(1,0,0,6)], prop.get_size_in_points())
cmap = font.get_charmap()
- glyphd = reverse_dict(cmap)
lastgind = None
#print 'text', s
lines = []
thisx, thisy = 0,0
for c in s:
ccode = ord(c)
- gind = glyphd.get(ccode)
+ gind = cmap.get(ccode)
if gind is None:
ccode = ord('?')
name = '.notdef'
diff --git a/lib/matplotlib/mathtext.py b/lib/matplotlib/mathtext.py
index 00cffe1..297e378 100644
--- a/lib/matplotlib/mathtext.py
+++ b/lib/matplotlib/mathtext.py
@@ -404,7 +404,7 @@ Returns the name of the glyph directly from the font object.
"""
font = self.fonts[facename]
- glyphindex = self.glyphmaps[facename][uniindex]
+ glyphindex = self.charmaps[facename][uniindex]
return font.get_glyph_name(glyphindex)
def _get_info(self, facename, symbol, fontsize, dpi):
@@ -425,7 +425,7 @@ Returns the name of the glyph directly from the font object.
fontface.set_size(fontsize, dpi)
head = fontface.get_sfnt_table('head')
uniindex = self._get_unicode_index(symbol)
- glyphindex = self.glyphmaps[facename][uniindex]
+ glyphindex = self.charmaps[facename][uniindex]
glyph = fontface.load_char(uniindex)
xmin, ymin, xmax, ymax = [val/64.0 for val in glyph.bbox]
# This is black magic to me (Edin)
@@ -498,7 +498,7 @@ Because BaKoma fonts don't support Unicode, 'uniindex' is misleading
"""
font = self.fonts[facename]
- glyphindex = self.glyphmaps[facename][uniindex]
+ glyphindex = self.charmaps[facename][uniindex]
return font.get_glyph_name(glyphindex)
def _get_info(self, facename, symbol, fontsize, dpi):
@@ -518,7 +518,7 @@ Because BaKoma fonts don't support Unicode, 'uniindex' is misleading
fontface.set_size(fontsize, dpi)
head = fontface.get_sfnt_table('head')
uniindex = self._get_unicode_index(symbol)
- glyphindex = self.glyphmaps[facename][uniindex]
+ glyphindex = self.charmaps[facename][uniindex]
glyph = fontface.load_char(uniindex)
xmin, ymin, xmax, ymax = [val/64.0 for val in glyph.bbox]
# This is black magic to me (Edin)
@@ -578,7 +578,7 @@ class BakomaTrueTypeFonts(Fonts):
self.charmaps = dict(
[ (name, self.fonts[name].get_charmap()) for name in self.fnames])
- # glyphmaps is a dict names to a dict of charcode -> glyphindex
+ # glyphmaps is a dict names to a dict of glyphindex -> charcode
self.glyphmaps = {}
for name in self.fnames:
cmap = self.charmaps[name]
@@ -607,7 +607,7 @@ class BakomaTrueTypeFonts(Fonts):
if latex_to_bakoma.has_key(sym):
basename, num = latex_to_bakoma[sym]
- num = self.charmaps[basename][num]
+ num = self.glyphmaps[basename][num]
elif len(sym) == 1:
num = ord(sym)
else:
@@ -658,7 +658,7 @@ class BakomaTrueTypeFonts(Fonts):
basename = self.fontmap[font]
if latex_to_bakoma.has_key(sym):
basename, num = latex_to_bakoma[sym]
- num = self.charmaps[basename][num]
+ num = self.glyphmaps[basename][num]
elif len(sym) == 1:
num = ord(sym)
else:
@@ -693,7 +693,7 @@ class BakomaTrueTypeFonts(Fonts):
basename = self.fontmap[font]
if latex_to_bakoma.has_key(sym):
basename, num = latex_to_bakoma[sym]
- num = self.charmaps[basename][num]
+ num = self.glyphmaps[basename][num]
elif len(sym) == 1:
num = ord(sym)
else:
@@ -725,6 +725,11 @@ class BakomaPSFonts(Fonts):
self.charmaps = dict(
[ (name, self.fonts[name].get_charmap()) for name in self.fnames])
+ # glyphmaps is a dict names to a dict of glyphindex -> charcode
+ self.glyphmaps = {}
+ for name in self.fnames:
+ cmap = self.charmaps[name]
+ self.glyphmaps[name] = dict([(ccode, glyphind) for glyphind, ccode in cmap.items()])
for font in self.fonts.values():
font.clear()
@@ -741,7 +746,7 @@ class BakomaPSFonts(Fonts):
if latex_to_bakoma.has_key(sym):
basename, num = latex_to_bakoma[sym]
sym = self.fonts[basename].get_glyph_name(num)
- num = self.charmaps[basename][num]
+ num = self.glyphmaps[basename][num]
elif len(sym) == 1:
num = ord(sym)
else:
@@ -812,7 +817,7 @@ class BakomaPDFFonts(BakomaPSFonts):
if latex_to_bakoma.has_key(sym):
basename, num = latex_to_bakoma[sym]
sym = self.fonts[basename].get_glyph_name(num)
- num = self.charmaps[basename][num]
+ num = self.glyphmaps[basename][num]
elif len(sym) == 1:
num = ord(sym)
else:
diff --git a/src/ft2font.cpp b/src/ft2font.cpp
index cd3c9a8..b7f6a94 100644
--- a/src/ft2font.cpp
+++ b/src/ft2font.cpp
@@ -1215,7 +1215,7 @@ FT2Font::get_charmap(const Py::Tuple & args) {
FT_ULong code = FT_Get_First_Char(face, &index);
while (index != 0) {
- charmap[Py::Int((int) index)] = Py::Long((long) code);
+ charmap[Py::Long((long) code)] = Py::Int((int) index);
code = FT_Get_Next_Char(face, code, &index);
}
return charmap;
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel