Skip to content

Commit c904605

Browse files
committed
added alpha support for unicode
svn path=/trunk/matplotlib/; revision=1102
1 parent 1244c5a commit c904605

14 files changed

+471
-80
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
New entries should be added at the top
22

3+
2005-03-29 Added alpha unicode support for Agg and PS in progress - JDH
4+
35
2005-03-28 Added Jarrod's svg patch for text - JDH
46

57
2005-03-28 Added ludal arrow and quiver patch - JDH

INSTALL

+9-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ WINDOWS
107107
you are interactively generating plots, your best bet is
108108
TkAgg from the standard python shell or ipython.
109109

110-
PACKAGE MANAGERS: (rpms, apt, fink)
110+
DISTROS: (rpms, apt, fink, osx, freebsd, gentoo, etc)
111111

112112
RPMS
113113

@@ -137,6 +137,14 @@ PACKAGE MANAGERS: (rpms, apt, fink)
137137
# apt-get update
138138
# apt-get install python-matplotlib python-matplotlib-doc
139139

140+
FREEBSD
141+
142+
http://www.freshports.org/math/py-matplotlib/
143+
144+
Gentoo
145+
146+
http://www.gentoo-portage.com/dev-python/matplotlib
147+
140148
OS X
141149

142150
All of the backends run on OS X. You will need to install

examples/ftface_props.py

+3
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@
7171
print 'Glyph names :', font.style_flags & FT_FACE_FLAG_GLYPH_NAMES != 0
7272

7373
print dir(font)
74+
75+
cmap = font.get_charmap()
76+
print font.get_kerning

examples/unicode_demo.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
from pylab import *
6+
plot([1,2,4])
7+
title( unicode('Développés et fabriqués', 'latin-1') )
8+
xlabel( unicode("réactivité nous permettent d'être sélectionnés et adoptés", 'latin-1') )
9+
ylabel( unicode('Andr\xc3\xa9 was here!', 'utf-8') )
10+
text( 0.5, 2.5, unicode('Institut für Festkörperphysik', 'latin-1'), rotation=45)
11+
text( 2, 2.5, u'AVA')
12+
savefig('test.ps')
13+
show()

lib/matplotlib/_mathtext_data.py

+185-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,191 @@
253253
r'\spadesuit' : ('cmsy10', 7),
254254
}
255255

256-
256+
# the kerning dictionary in design space units
257+
cmkern = {
258+
'cmr10' :
259+
{
260+
('A', 't') : -0.027779,
261+
('A', 'C') : -0.027779,
262+
('A', 'O') : -0.027779,
263+
('A', 'G') : -0.027779,
264+
('A', 'U') : -0.027779,
265+
('A', 'Q') : -0.027779,
266+
('A', 'T') : -0.083334,
267+
('A', 'Y') : -0.083334,
268+
('A', 'V') : -0.111112,
269+
('A', 'W') : -0.111112,
270+
('D', 'X') : -0.027779,
271+
('D', 'W') : -0.027779,
272+
('D', 'A') : -0.027779,
273+
('D', 'V') : -0.027779,
274+
('D', 'Y') : -0.027779,
275+
('F', 'o') : -0.083334,
276+
('F', 'e') : -0.083334,
277+
('F', 'u') : -0.083334,
278+
('F', 'r') : -0.083334,
279+
('F', 'a') : -0.083334,
280+
('F', 'A') : -0.111112,
281+
('F', 'O') : -0.027779,
282+
('F', 'C') : -0.027779,
283+
('F', 'G') : -0.027779,
284+
('F', 'Q') : -0.027779,
285+
('I', 'I') : 0.027779,
286+
('K', 'O') : -0.027779,
287+
('K', 'C') : -0.027779,
288+
('K', 'G') : -0.027779,
289+
('K', 'Q') : -0.027779,
290+
('L', 'T') : -0.083334,
291+
('L', 'Y') : -0.083334,
292+
('L', 'V') : -0.111112,
293+
('L', 'W') : -0.111112,
294+
('O', 'X') : -0.027779,
295+
('O', 'W') : -0.027779,
296+
('O', 'A') : -0.027779,
297+
('O', 'V') : -0.027779,
298+
('O', 'Y') : -0.027779,
299+
('P', 'A') : -0.083334,
300+
('P', 'o') : -0.027779,
301+
('P', 'e') : -0.027779,
302+
('P', 'a') : -0.027779,
303+
('R', 't') : -0.027779,
304+
('R', 'C') : -0.027779,
305+
('R', 'O') : -0.027779,
306+
('R', 'G') : -0.027779,
307+
('R', 'U') : -0.027779,
308+
('R', 'Q') : -0.027779,
309+
('R', 'T') : -0.083334,
310+
('R', 'Y') : -0.083334,
311+
('R', 'V') : -0.111112,
312+
('R', 'W') : -0.111112,
313+
('T', 'y') : -0.027779,
314+
('T', 'e') : -0.083334,
315+
('T', 'o') : -0.083334,
316+
('T', 'r') : -0.083334,
317+
('T', 'a') : -0.083334,
318+
('T', 'A') : -0.083334,
319+
('T', 'u') : -0.083334,
320+
('V', 'o') : -0.083334,
321+
('V', 'e') : -0.083334,
322+
('V', 'u') : -0.083334,
323+
('V', 'r') : -0.083334,
324+
('V', 'a') : -0.083334,
325+
('V', 'A') : -0.111112,
326+
('V', 'O') : -0.027779,
327+
('V', 'C') : -0.027779,
328+
('V', 'G') : -0.027779,
329+
('V', 'Q') : -0.027779,
330+
('W', 'o') : -0.083334,
331+
('W', 'e') : -0.083334,
332+
('W', 'u') : -0.083334,
333+
('W', 'r') : -0.083334,
334+
('W', 'a') : -0.083334,
335+
('W', 'A') : -0.111112,
336+
('W', 'O') : -0.027779,
337+
('W', 'C') : -0.027779,
338+
('W', 'G') : -0.027779,
339+
('W', 'Q') : -0.027779,
340+
('X', 'O') : -0.027779,
341+
('X', 'C') : -0.027779,
342+
('X', 'G') : -0.027779,
343+
('X', 'Q') : -0.027779,
344+
('Y', 'e') : -0.083334,
345+
('Y', 'o') : -0.083334,
346+
('Y', 'r') : -0.083334,
347+
('Y', 'a') : -0.083334,
348+
('Y', 'A') : -0.083334,
349+
('Y', 'u') : -0.083334,
350+
('a', 'v') : -0.027779,
351+
('a', 'j') : 0.055555,
352+
('a', 'y') : -0.027779,
353+
('a', 'w') : -0.027779,
354+
('b', 'e') : 0.027779,
355+
('b', 'o') : 0.027779,
356+
('b', 'x') : -0.027779,
357+
('b', 'd') : 0.027779,
358+
('b', 'c') : 0.027779,
359+
('b', 'q') : 0.027779,
360+
('b', 'v') : -0.027779,
361+
('b', 'j') : 0.055555,
362+
('b', 'y') : -0.027779,
363+
('b', 'w') : -0.027779,
364+
('c', 'h') : -0.027779,
365+
('c', 'k') : -0.027779,
366+
('g', 'j') : 0.027779,
367+
('h', 't') : -0.027779,
368+
('h', 'u') : -0.027779,
369+
('h', 'b') : -0.027779,
370+
('h', 'y') : -0.027779,
371+
('h', 'v') : -0.027779,
372+
('h', 'w') : -0.027779,
373+
('k', 'a') : -0.055555,
374+
('k', 'e') : -0.027779,
375+
('k', 'a') : -0.027779,
376+
('k', 'o') : -0.027779,
377+
('k', 'c') : -0.027779,
378+
('m', 't') : -0.027779,
379+
('m', 'u') : -0.027779,
380+
('m', 'b') : -0.027779,
381+
('m', 'y') : -0.027779,
382+
('m', 'v') : -0.027779,
383+
('m', 'w') : -0.027779,
384+
('n', 't') : -0.027779,
385+
('n', 'u') : -0.027779,
386+
('n', 'b') : -0.027779,
387+
('n', 'y') : -0.027779,
388+
('n', 'v') : -0.027779,
389+
('n', 'w') : -0.027779,
390+
('o', 'e') : 0.027779,
391+
('o', 'o') : 0.027779,
392+
('o', 'x') : -0.027779,
393+
('o', 'd') : 0.027779,
394+
('o', 'c') : 0.027779,
395+
('o', 'q') : 0.027779,
396+
('o', 'v') : -0.027779,
397+
('o', 'j') : 0.055555,
398+
('o', 'y') : -0.027779,
399+
('o', 'w') : -0.027779,
400+
('p', 'e') : 0.027779,
401+
('p', 'o') : 0.027779,
402+
('p', 'x') : -0.027779,
403+
('p', 'd') : 0.027779,
404+
('p', 'c') : 0.027779,
405+
('p', 'q') : 0.027779,
406+
('p', 'v') : -0.027779,
407+
('p', 'j') : 0.055555,
408+
('p', 'y') : -0.027779,
409+
('p', 'w') : -0.027779,
410+
('t', 'y') : -0.027779,
411+
('t', 'w') : -0.027779,
412+
('u', 'w') : -0.027779,
413+
('v', 'a') : -0.055555,
414+
('v', 'e') : -0.027779,
415+
('v', 'a') : -0.027779,
416+
('v', 'o') : -0.027779,
417+
('v', 'c') : -0.027779,
418+
('w', 'e') : -0.027779,
419+
('w', 'a') : -0.027779,
420+
('w', 'o') : -0.027779,
421+
('w', 'c') : -0.027779,
422+
('y', 'o') : -0.027779,
423+
('y', 'e') : -0.027779,
424+
('y', 'a') : -0.027779,
425+
},
426+
'cmex10' : {},
427+
'cmsy10' : {},
428+
'cmmi10' :
429+
{
430+
('9', 'A') : -0.055555,
431+
('9', 'M') : -0.055555,
432+
('9', 'N') : -0.055555,
433+
('9', 'Y') : 0.055555,
434+
('9', 'Z') : -0.055555,
435+
('d', 'Y') : 0.055555,
436+
('d', 'Z') : -0.055555,
437+
('d', 'j') : -0.111112,
438+
('d', 'f') : -0.166667,
439+
},
440+
}
257441
ams_type1 = {
258442
r'\Leftbracket' : ( 'cmex10', '02'),
259443
r'\Rightbracket' : ( 'cmex10', '03'),

lib/matplotlib/axes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ def quiver(self, U, V, *args, **kwargs ):
891891
antialiaseds = (0,),
892892
linewidths = (width,),
893893
)
894-
if C:
894+
if C is not None:
895895
collection.set_array( C )
896896
else:
897897
collection.set_facecolor( (color,) )
@@ -2532,7 +2532,7 @@ def psd(self, x, NFFT=256, Fs=2, detrend=detrend_none,
25322532
logi = int(log10(intv))
25332533
if logi==0: logi=.1
25342534
step = 10*logi
2535-
print vmin, vmax, step, intv, math.floor(vmin), math.ceil(vmax)+1
2535+
#print vmin, vmax, step, intv, math.floor(vmin), math.ceil(vmax)+1
25362536
ticks = arange(math.floor(vmin), math.ceil(vmax)+1, step)
25372537
self.set_yticks(ticks)
25382538

lib/matplotlib/backends/backend_agg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
205205

206206
font.load_char(ord(s))
207207
else:
208-
font.set_text(str(s), angle)
208+
font.set_text(s, angle)
209209
font.draw_glyphs_to_bitmap()
210210

211211
self._renderer.draw_text(font, int(x), int(y), gc)
@@ -222,7 +222,7 @@ def get_text_width_height(self, s, prop, ismath):
222222
s, self.dpi.get(), prop.get_size_in_points())
223223
return width, height
224224
font = self._get_agg_font(prop)
225-
font.set_text(str(s), 0.0) # the width and height of unrotated string
225+
font.set_text(s, 0.0) # the width and height of unrotated string
226226
w, h = font.get_width_height()
227227
w /= 64.0 # convert from subpixels
228228
h /= 64.0

lib/matplotlib/backends/backend_ps.py

+53-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
1313
FigureManagerBase, FigureCanvasBase
1414

15-
from matplotlib.cbook import is_string_like
15+
from matplotlib.cbook import is_string_like, reverse_dict
1616
from matplotlib.figure import Figure
1717

1818
from matplotlib.font_manager import fontManager
@@ -49,6 +49,8 @@ def _nums_to_str(*args):
4949

5050
def quote_ps_string(s):
5151
"Quote dangerous characters of S for use in a PostScript string constant."
52+
if isinstance(s, unicode):
53+
s = s.encode('ascii', 'replace') # unicode not supported yet
5254
s=s.replace("\\", "\\\\")
5355
s=s.replace("(", "\\(")
5456
s=s.replace(")", "\\)")
@@ -99,6 +101,7 @@ def __init__(self, width, height, pswriter):
99101
self.fontsize = None
100102

101103
def set_color(self, r, g, b, store=1):
104+
# why is this comparison failing??
102105
if (r,g,b) != self.color:
103106
if r==g and r==b:
104107
self._pswriter.write("%1.3f setgray\n"%r)
@@ -192,7 +195,6 @@ def _get_font_ttf(self, prop):
192195
font = _fontd.get(key)
193196
if font is None:
194197
fname = fontManager.findfont(prop)
195-
196198
font = FT2Font(str(fname))
197199
_fontd[key] = font
198200
if fname not in _type42:
@@ -404,6 +406,11 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
404406
draw a Text instance
405407
"""
406408
# local to avoid repeated attribute lookups
409+
410+
if isinstance(s, unicode):
411+
self.draw_unicode(gc, x, y, s, prop, angle)
412+
return
413+
407414
write = self._pswriter.write
408415
if debugPS:
409416
write("% text\n")
@@ -450,6 +457,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
450457
font = self._get_font_ttf(prop)
451458
font.set_text(s,0)
452459

460+
453461
self.set_color(*gc.get_rgb())
454462
self.set_font(font.get_sfnt()[(1,0,0,6)], prop.get_size_in_points())
455463
write("%s m\n"%_nums_to_str(x,y))
@@ -466,7 +474,49 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
466474
def new_gc(self):
467475
return GraphicsContextPS()
468476

469-
def draw_mathtext(self, gc, x, y, s, prop, angle):
477+
def draw_unicode(self, gc, x, y, s, prop, angle):
478+
"""draw a unicode string. ps doesn't have unicode support, so
479+
we have to do this the hard way
480+
"""
481+
thisx, thisy = x, y
482+
font = self._get_font_ttf(prop)
483+
self.set_font(font.get_sfnt()[(1,0,0,6)], prop.get_size_in_points())
484+
xys = font.set_text(s, 0);
485+
486+
cmap = font.get_charmap()
487+
glyphd = reverse_dict(cmap)
488+
lastcode = None
489+
490+
#print len(s), len(xys)
491+
manual = True
492+
for c, xy in zip(s, xys):
493+
# use the freetype positions
494+
penx, peny = xy
495+
#print '\t', c, penx, peny
496+
if not manual: # use freetype positions
497+
thisx = x + penx/64.
498+
499+
ccode = ord(c)
500+
gind = glyphd[ccode]
501+
name = font.get_glyph_name(gind)
502+
self._pswriter.write('%f %f moveto /%s glyphshow\n'%(thisx, thisy, name))
503+
504+
if manual: #manually lay them out using glyph metrics and kern
505+
glyph = font.load_char(ccode)
506+
#print c, ccode, gind, name, xy[0]/64.
507+
if lastcode is not None:
508+
kern = font.get_kerning(lastcode, ccode)
509+
#print lastcode, ccode, kern
510+
else:
511+
kern = 0
512+
lastcode = ccode
513+
thisx += (glyph.horiAdvance+kern)/64.0
514+
515+
516+
517+
518+
def draw_mathtext(self, gc,
519+
x, y, s, prop, angle):
470520
"""
471521
Draw the math text using matplotlib.mathtext
472522
"""

lib/matplotlib/cbook.py

+4
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,7 @@ def finddir(o, match, case=False):
562562
names = [(name.lower(), name) for name in dir(o) if is_string_like(name)]
563563
match = match.lower()
564564
return [orig for name, orig in names if name.find(match)>=0]
565+
566+
def reverse_dict(d):
567+
'reverse the dictionary -- may lose data if values are not uniq!'
568+
return dict([(v,k) for k,v in d.items()])

0 commit comments

Comments
 (0)