Skip to content

Commit 1149215

Browse files
committed
textpath.get_glyph_tex tries set the font charmap
svn path=/trunk/matplotlib/; revision=7948
1 parent 42c640e commit 1149215

File tree

3 files changed

+53
-35
lines changed

3 files changed

+53
-35
lines changed

lib/matplotlib/textpath.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TextToPath(object):
1919

2020
FONT_SCALE = 50.
2121
DPI = 72
22-
22+
2323
def __init__(self):
2424
"""
2525
Initialization
@@ -44,7 +44,7 @@ def _get_font(self, prop):
4444

4545
def _get_hinting_flag(self):
4646
return LOAD_NO_HINTING
47-
47+
4848
def _get_char_id(self, font, ccode):
4949
"""
5050
Return a unique id for the given font and character-code set.
@@ -64,7 +64,7 @@ def _get_char_id_ps(self, font, ccode):
6464

6565
def glyph_to_path(self, glyph, currx=0.):
6666
"""
67-
convert the ft2font glyph to vertices and codes.
67+
convert the ft2font glyph to vertices and codes.
6868
"""
6969
#Mostly copied from backend_svg.py.
7070

@@ -102,14 +102,14 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
102102
103103
*s*
104104
text to be converted
105-
105+
106106
*usetex*
107107
If True, use matplotlib usetex mode.
108108
109109
*ismath*
110110
If True, use mathtext parser. Effective only if usetex == False.
111111
112-
112+
113113
"""
114114
if usetex==False:
115115
if ismath == False:
@@ -121,26 +121,26 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
121121
glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s)
122122

123123
verts, codes = [], []
124-
124+
125125
for glyph_id, xposition, yposition, scale in glyph_info:
126126
verts1, codes1 = glyph_map[glyph_id]
127127
if verts1:
128-
verts1 = np.array(verts1)*scale + [xposition, yposition]
128+
verts1 = np.array(verts1)*scale + [xposition, yposition]
129129
verts.extend(verts1)
130130
codes.extend(codes1)
131131

132132
for verts1, codes1 in rects:
133133
verts.extend(verts1)
134134
codes.extend(codes1)
135-
135+
136136
return verts, codes
137137

138-
138+
139139
def get_glyphs_with_font(self, font, s, glyph_map=None,
140140
return_new_glyphs_only=False):
141141
"""
142142
convert the string *s* to vertices and codes using the
143-
provided ttf font.
143+
provided ttf font.
144144
"""
145145

146146
# Mostly copied from backend_svg.py.
@@ -151,7 +151,7 @@ def get_glyphs_with_font(self, font, s, glyph_map=None,
151151
currx = 0
152152
xpositions = []
153153
glyph_ids = []
154-
154+
155155
if glyph_map is None:
156156
glyph_map = dict()
157157

@@ -190,14 +190,14 @@ def get_glyphs_with_font(self, font, s, glyph_map=None,
190190
glyph_ids.append(char_id)
191191

192192
currx += horiz_advance
193-
193+
194194
lastgind = gind
195195

196196
ypositions = [0] * len(xpositions)
197197
sizes = [1.] * len(xpositions)
198198

199199
rects = []
200-
200+
201201
return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map_new, rects
202202

203203

@@ -275,14 +275,14 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
275275
"""
276276

277277
# codes are modstly borrowed from pdf backend.
278-
278+
279279
texmanager = self.get_texmanager()
280280

281281
if self.tex_font_map is None:
282282
self.tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
283283

284284
fontsize = prop.get_size_in_points()
285-
if hasattr(texmanager, "get_dvi"): #
285+
if hasattr(texmanager, "get_dvi"): #
286286
dvifilelike = texmanager.get_dvi(s, self.FONT_SCALE)
287287
dvi = dviread.DviFromFileLike(dvifilelike, self.DPI)
288288
else:
@@ -312,6 +312,10 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
312312
if font_and_encoding is None:
313313
font_bunch = self.tex_font_map[dvifont.texname]
314314
font = FT2Font(font_bunch.filename)
315+
try:
316+
font.select_charmap(1094992451) # select ADOBE_CUSTOM
317+
except ValueError:
318+
font.set_charmap(0)
315319
if font_bunch.encoding:
316320
enc = dviread.Encoding(font_bunch.encoding)
317321
else:
@@ -322,25 +326,15 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
322326
font, enc = font_and_encoding
323327

324328
ft2font_flag = LOAD_TARGET_LIGHT
325-
if enc:
326-
ng = font.get_name_index(enc.encoding[glyph])
327-
else:
328-
ng = glyph
329329

330-
char_id = self._get_char_id_ps(font, ng)
330+
char_id = self._get_char_id_ps(font, glyph)
331331

332332
if not char_id in glyph_map:
333333
font.clear()
334334
font.set_size(self.FONT_SCALE, self.DPI)
335335

336-
if ng == 0:
337-
# While 0 is a valid index (e.g., "-", "\Gamma"),
338-
# font.load_glyph(0) does not seem to work. This
339-
# may not be a general solution.
340-
glyph0 = font.load_glyph(128, flags=ft2font_flag)
341-
else:
342-
glyph0 = font.load_glyph(ng, flags=ft2font_flag)
343-
336+
glyph0 = font.load_char(glyph, flags=ft2font_flag)
337+
344338
glyph_map_new[char_id] = self.glyph_to_path(glyph0)
345339

346340
glyph_ids.append(char_id)
@@ -349,7 +343,7 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
349343
sizes.append(dvifont.size/self.FONT_SCALE)
350344

351345
myrects = []
352-
346+
353347
for ox, oy, h, w in page.boxes:
354348
vert1=[(ox, oy), (ox+w, oy), (ox+w, oy+h), (ox, oy+h), (ox, oy), (0,0)]
355349
code1 = [Path.MOVETO,
@@ -358,12 +352,13 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
358352
myrects.append((vert1, code1))
359353

360354

361-
return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map, myrects
355+
return zip(glyph_ids, xpositions, ypositions, sizes), \
356+
glyph_map_new, myrects
357+
362358

363359

364360

365361

366-
367362

368363
from matplotlib.font_manager import FontProperties
369364
from matplotlib import rcParams
@@ -384,7 +379,7 @@ def __init__(self, xy, s, size=None, prop=None,
384379
it simply is a path, not an artist. You need to use the
385380
PathPatch (or other artists) to draw this path onto the
386381
canvas.
387-
382+
388383
xy : position of the text.
389384
s : text
390385
size : font size
@@ -437,7 +432,7 @@ def _get_codes(self):
437432
Return the codes
438433
"""
439434
return self._codes
440-
435+
441436
vertices = property(_get_vertices)
442437
codes = property(_get_codes)
443438

@@ -463,7 +458,7 @@ def is_math_text(self, s):
463458
Returns True if the given string *s* contains any mathtext.
464459
"""
465460
# copied from Text.is_math_text -JJL
466-
461+
467462
# Did we find an even number of non-escaped dollar signs?
468463
# If so, treat is as math text.
469464
dollar_count = s.count(r'$') - s.count(r'\$')
@@ -489,7 +484,7 @@ def text_get_vertices_codes(self, prop, s, usetex):
489484
else:
490485
clean_line, ismath = self.is_math_text(s)
491486
verts, codes = text_to_path.get_text_path(prop, clean_line, ismath=ismath)
492-
487+
493488
return verts, codes
494489

495490

src/ft2font.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,24 @@ FT2Font::set_charmap(const Py::Tuple & args) {
885885
return Py::Object();
886886
}
887887

888+
char FT2Font::select_charmap__doc__[] =
889+
"select_charmap(i)\n"
890+
"\n"
891+
"select charmap i where i is one of the FT_Encoding number\n"
892+
;
893+
894+
Py::Object
895+
FT2Font::select_charmap(const Py::Tuple & args) {
896+
_VERBOSE("FT2Font::set_charmap");
897+
args.verify_length(1);
898+
899+
unsigned long i = Py::Long(args[0]);
900+
//if (FT_Select_Charmap( face, FT_ENCODING_ADOBE_CUSTOM ))
901+
if (FT_Select_Charmap( face, (FT_Encoding) i ))
902+
throw Py::ValueError("Could not set the charmap");
903+
return Py::Object();
904+
}
905+
888906
FT_BBox
889907
FT2Font::compute_string_bbox( ) {
890908
_VERBOSE("FT2Font::compute_string_bbox");
@@ -1368,6 +1386,7 @@ FT2Font::get_charmap(const Py::Tuple & args) {
13681386
FT_UInt index;
13691387
Py::Dict charmap;
13701388

1389+
//std::cout << "asd" << face->charmaps[1]->encoding << std::endl;
13711390
FT_ULong code = FT_Get_First_Char(face, &index);
13721391
while (index != 0) {
13731392
charmap[Py::Long((long) code)] = Py::Int((int) index);
@@ -1841,6 +1860,8 @@ FT2Font::init_type() {
18411860
FT2Font::set_size__doc__);
18421861
add_varargs_method("set_charmap", &FT2Font::set_charmap,
18431862
FT2Font::set_charmap__doc__);
1863+
add_varargs_method("select_charmap", &FT2Font::select_charmap,
1864+
FT2Font::select_charmap__doc__);
18441865

18451866
add_varargs_method("get_width_height", &FT2Font::get_width_height,
18461867
FT2Font::get_width_height__doc__);

src/ft2font.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class FT2Font : public Py::PythonExtension<FT2Font> {
9595
Py::Object clear(const Py::Tuple & args);
9696
Py::Object set_size(const Py::Tuple & args);
9797
Py::Object set_charmap(const Py::Tuple & args);
98+
Py::Object select_charmap(const Py::Tuple & args);
9899
Py::Object set_text(const Py::Tuple & args, const Py::Dict & kwargs);
99100
Py::Object get_kerning(const Py::Tuple & args);
100101
Py::Object get_num_glyphs(const Py::Tuple & args);
@@ -137,6 +138,7 @@ class FT2Font : public Py::PythonExtension<FT2Font> {
137138
static char clear__doc__ [];
138139
static char set_size__doc__ [];
139140
static char set_charmap__doc__ [];
141+
static char select_charmap__doc__ [];
140142
static char set_text__doc__ [];
141143
static char get_glyph__doc__ [];
142144
static char get_num_glyphs__doc__ [];

0 commit comments

Comments
 (0)