Menu

[r5802]: / branches / jdhunter / matplotlib / text.py  Maximize  Restore  History

Download this file

429 lines (339 with data), 13.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from __future__ import division
import pygtk
pygtk.require('2.0')
import gtk
import gobject
from gtk import gdk
import pango
from colors import ColorDispatcher
from artist import Artist
class AxisText(Artist):
"""
Handle storing and drawing of text in window or data coordinates
"""
fontweights = {'normal' : pango.WEIGHT_NORMAL,
'bold' : pango.WEIGHT_BOLD,
'heavy' : pango.WEIGHT_HEAVY,
'light' : pango.WEIGHT_LIGHT,
'normal' : pango.WEIGHT_NORMAL,
'ultrabold' : pango.WEIGHT_ULTRABOLD,
'ultralight' : pango.WEIGHT_ULTRALIGHT,
}
fontangles = {
'italic': pango.STYLE_ITALIC,
'normal' : pango.STYLE_NORMAL,
'oblique' : pango.STYLE_OBLIQUE,
}
def __init__(self, x=0, y=0, text='',
color='k',
verticalalignment='bottom',
horizontalalignment='left',
fontname='Sans',
fontsize=10,
fontweight='bold',
fontangle='normal',
rotation=None,
):
Artist.__init__(self)
self._x, self._y = x, y
self._color = color
self._text = text
self._verticalalignment = verticalalignment
self._horizontalalignment = horizontalalignment
self._rotation = rotation
self._fontname = fontname
self._fontsize = fontsize
self._fontweight = fontweight
self._fontangle = fontangle
self._reset=1
self._drawn = 0
self._eraseImg = None
self._lastXY = 0,0
def _compute_offsets(self):
"""
Return the (x,y) offsets to comensate for the alignment
specifications
"""
if self._drawingArea is None: return
try: self._width
except AttributeError: self._set_font()
if self._rotation=='vertical':
w, h = self._height, self._width
if self._horizontalalignment=='center': offsetx = -w/2
elif self._horizontalalignment=='right': offsetx = -w
else: offsetx = 0
if self._verticalalignment=='center': offsety = -h/2
elif self._verticalalignment=='top': offsety = 0
else: offsety = -h
else:
if self._horizontalalignment=='center': offsetx = -self._width/2
elif self._horizontalalignment=='right': offsetx = -self._width
else: offsetx = 0
if self._verticalalignment=='center': offsety = -self._height/2
elif self._verticalalignment=='top': offsety = 0
else: offsety = -self._height
return (offsetx, offsety)
def _draw(self, drawable, *args, **kwargs):
"""
Render the text to the drawable (or defaul drawable is drawable is None
"""
if self._text=='': return
if self._drawn: return
if self._reset: self._set_font()
self.erase()
fg = ColorDispatcher().get(self._color)
x, y = self.transform_points_to_win(self._x, self._y)
ox, oy = self._compute_offsets()
if self._rotation=='vertical':
drawn = self.draw_rotated(drawable, fg)
if not drawn: return
else:
inkRect, logicalRect = self._layout.get_pixel_extents()
w, h = logicalRect[2], logicalRect[3]
if y+oy<0 or x+ox<0:
return
img = drawable.get_image(x+ox, y+oy, w, h)
self._eraseImg = ( (x+ox, y+oy, w, h), img)
gc = drawable.new_gc()
self.clip_gc(gc)
gc.foreground = fg
drawable.draw_layout(
gc, x=x+ox, y=y+oy,
layout=self._layout)
self._drawn = 1
self._reset = 0
self._lastXY = x,y
def draw_rotated(self, drawable, fg):
"""
Draw the text rotated 90 degrees
"""
inkRect, logicalRect = self._layout.get_pixel_extents()
x, y, w, h = logicalRect
# get the bacground image
ox, oy = self._compute_offsets()
x, y = self.transform_points_to_win(self._x, self._y)
if y+oy<0 or x+ox<0:
return 0
imgBack = drawable.get_image(x+ox, y+oy, h, w)
rect = (x+ox, y+oy, h, w)
self._eraseImg = ( rect, imgBack)
pixmap = gtk.gdk.Pixmap(self._drawingArea.window, w, h)
# rotate the background image to horizontal to fill the pixmap
# background
imageHoriz = gtk.gdk.Image(type=0,
visual=pixmap.get_visual(),
width=w, height=h)
imageHoriz.set_colormap(imgBack.get_colormap())
for i in range(w):
for j in range(h):
imageHoriz.put_pixel(i, j, imgBack.get_pixel(j,w-i-1) )
gc = drawable.new_gc()
self.clip_gc(gc)
pixmap.draw_image(gc, imageHoriz, 0, 0, 0, 0, w, h)
gc.foreground = fg
pixmap.draw_layout(gc, x=0, y=0, layout=self._layout)
imageIn = pixmap.get_image(x=0, y=0, width=w, height=h)
imageOut = gtk.gdk.Image(type=0,
visual=pixmap.get_visual(),
width=h, height=w)
imageOut.set_colormap(imageIn.get_colormap())
for i in range(w):
for j in range(h):
imageOut.put_pixel(j, i, imageIn.get_pixel(w-i-1,j) )
pixbuf = gtk.gdk.Pixbuf(colorspace=gtk.gdk.COLORSPACE_RGB,
has_alpha=0, bits_per_sample=8,
width=h, height=w)
pixbuf.get_from_image(src=imageOut, cmap=imageIn.get_colormap(),
src_x=0, src_y=0, dest_x=0, dest_y=0,
width=h, height=w)
pixbuf.render_to_drawable(drawable, gc,
src_x=0, src_y=0,
dest_x=x+ox, dest_y=y+oy,
width=h, height=w,
dither=0, x_dither=0, y_dither=0)
return 1
def erase(self):
"Erase the last draw"
if self._eraseImg is None or not self._drawn: return
gc = self._drawable.new_gc()
self.clip_gc(gc)
rect, img = self._eraseImg
if img is None: return
x,y,w,h = rect
self._drawable.draw_image(gc, img, 0, 0, x, y, w, h)
self._drawn = 0
self._reset = 1
self._eraseImg = None
def get_data_extent(self):
'Get the pixel extents left, bottom, width, height'
if self._drawingArea is None:
msg = 'You must first set the drawing area for ' + self._text
raise RuntimeError, msg
try: self._width
except AttributeError: self._set_font()
ox, oy = self._compute_offsets()
inkRect, logicalRect = self._layout.get_pixel_extents()
x, y = self._x, self._y
if self._rotation=='vertical':
return (x+ox, y+oy+logicalRect[2], logicalRect[3], logicalRect[2])
else:
return (x+ox, y+oy+logicalRect[3], logicalRect[2], logicalRect[3])
def get_fontname(self):
"Return the font name as string"
return self._fontname
def get_fontsize(self):
"Return the font size as integer"
return self._fontsize
def get_fontweight(self):
"Get the font weight as string"
return self._fontweight
def get_fontangle(self):
"Get the font angle as string"
return self._fontangle
def get_horizontalalignment(self):
"Return the horizontal alignment as string"
return self._horizontalalignment
def get_text(self):
"Get the text as string"
return self._text
def get_verticalalignment(self):
"Return the vertical alignment as string"
return self._verticalalignment
def get_left_right(self):
"get the left, right boundaries of the text in in win coords"
if self._drawingArea is None:
raise RuntimeError, 'You must first set the drawing area with set_drawing_area'
ox,oy = self._compute_offsets()
return self._x + ox, self._x + ox + self._width
def get_bottom_top(self):
"get the bottom, top boundaries of the text in win coords"
if self._drawingArea is None:
raise RuntimeError, 'You must first set the drawing area with set_drawing_area'
ox,oy = self._compute_offsets()
return self._y + oy + self._height, self._y
def get_position(self):
"Return x, y as tuple"
return self._x, self._y
def wash_brushes(self):
"Flush all state vars and prepare for a clean redraw"
self._reset = 1
self._drawn = 0
self._lastXY = 0,0
self._eraseImg = None
def set_backgroundcolor(self, color):
"Set the background color of the text"
if color == self._backgroundcolor: return
self._state_change()
self._backgroundcolor = color
def set_color(self, color):
"Set the foreground color of the text"
if self._color == color: return
self._state_change()
self._color = color
def set_horizontalalignment(self, align):
"""
Set the horizontal alignment to one of
'center', 'right', or 'left'
"""
legal = ('center', 'right', 'left')
if align not in legal:
raise ValueError,\
'Horizontal alignment must be one of %s' % legal
if self._horizontalalignment == align: return
self._state_change()
self._horizontalalignment = align
def _set_font(self):
"Update the pango layout"
if self._drawingArea is None:
msg = 'You must first call set_drawing_area() for', self._text
raise RuntimeError, msg
self._font = pango.FontDescription(
'%s %d' % (self._fontname, self._fontsize))
self._font.set_weight(self.fontweights[self._fontweight])
self._font.set_style(self.fontangles[self._fontangle])
self._context = self._drawingArea.create_pango_context()
self._layout = self._drawingArea.create_pango_layout(self._text)
self._layout.set_font_description(self._font)
inkRect, logicalRect = self._layout.get_pixel_extents()
self._width = logicalRect[2]
self._height = logicalRect[3]
def set_fontname(self, fontname):
"""
Set the font name, eg, 'Sans', 'Courier', 'Helvetica'
"""
if self._fontname == fontname: return
self._state_change()
self._fontname = fontname
def set_fontsize(self, fontsize):
"""
Set the font size, eg, 8, 10, 12, 14...
"""
if self._fontsize == fontsize: return
self._state_change()
self._fontsize = fontsize
def set_fontweight(self, weight):
"""
Set the font weight, one of:
'normal', 'bold', 'heavy', 'light', 'ultrabold', 'ultralight'
"""
if self._fontweight == weight: return
self._state_change()
self._fontweight = weight
def set_fontangle(self, angle):
"""
Set the font angle, one of 'normal', 'italic', 'oblique'
"""
if self._fontangle == angle: return
self._state_change()
self._fontangle = angle
def set_position(self, x, y):
if (x,y) == (self._x, self._y): return
self._state_change()
self._x = x
self._y = y
def set_rotation(self, s):
"Currently only s='vertical', or s='horizontal' are supported"
if s==self._rotation: return
self._state_change()
self._rotation = s
def set_verticalalignment(self, align):
"""
Set the vertical alignment to one of
'center', 'top', or 'bottom'
"""
legal = ('top', 'bottom', 'center')
if align not in legal:
raise ValueError,\
'Vertical alignment must be one of %s' % legal
if self._verticalalignment == align: return
self._state_change()
self._verticalalignment = align
def set_drawing_area(self, da):
"Update the drawing area the widget renders into"
if self._drawingArea == da: return
#print 'Setting da for', self._text
Artist.set_drawing_area(self, da)
self._state_change()
def set_text(self, text):
"Set the text"
if self._text == text: return
self._state_change()
self._text = text
def _state_change(self):
self._reset = 1
def update_properties(self, d):
"Update the font attributes with the dictionary in d"
#check the keys
legal = ('color', 'verticalalignment', 'horizontalalignment',
'fontname', 'fontsize', 'fontweight',
'fontangle', 'rotation')
for k,v in d.items():
if k not in legal:
raise ValueError, 'Illegal key %s. Legal values are %s' % (
k, legal)
self.__dict__['_' + k] = v
#print self.__dict__
self._state_change()
def __del__(self):
"Bye bye"
self.erase()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.