]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/fancytext.py
MinGW fix.
[wxWidgets.git] / wxPython / wx / lib / fancytext.py
1 # 12/02/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for 2.5 compatability.
4 #
5
6 """
7 FancyText -- methods for rendering XML specified text
8
9 This module exports four main methods::
10
11 def GetExtent(str, dc=None, enclose=True)
12 def GetFullExtent(str, dc=None, enclose=True)
13 def RenderToBitmap(str, background=None, enclose=True)
14 def RenderToDC(str, dc, x, y, enclose=True)
15
16 In all cases, 'str' is an XML string. Note that start and end tags are
17 only required if *enclose* is set to False. In this case the text
18 should be wrapped in FancyText tags.
19
20 In addition, the module exports one class::
21
22 class StaticFancyText(self, window, id, text, background, ...)
23
24 This class works similar to StaticText except it interprets its text
25 as FancyText.
26
27 The text can support superscripts and subscripts, text in different
28 sizes, colors, styles, weights and families. It also supports a
29 limited set of symbols, currently *times*, *infinity*, *angle* as well
30 as greek letters in both upper case (*Alpha* *Beta*... *Omega*) and
31 lower case (*alpha* *beta*... *omega*).
32
33 >>> frame = wx.Frame(wx.NULL, -1, "FancyText demo", wx.DefaultPosition)
34 >>> sft = StaticFancyText(frame, -1, testText, wx.Brush("light grey", wx.SOLID))
35 >>> frame.SetClientSize(sft.GetSize())
36 >>> didit = frame.Show()
37 >>> from guitest import PauseTests; PauseTests()
38
39 """
40
41 # Copyright 2001-2003 Timothy Hochberg
42 # Use as you see fit. No warantees, I cannot be held responsible, etc.
43
44 import copy
45 import math
46 import sys
47
48 import wx
49 import xml.parsers.expat
50
51 __all__ = "GetExtent", "GetFullExtent", "RenderToBitmap", "RenderToDC", "StaticFancyText"
52
53 if sys.platform == "win32":
54 _greekEncoding = str(wx.FONTENCODING_CP1253)
55 else:
56 _greekEncoding = str(wx.FONTENCODING_ISO8859_7)
57
58 _families = {"fixed" : wx.FIXED, "default" : wx.DEFAULT, "decorative" : wx.DECORATIVE, "roman" : wx.ROMAN,
59 "script" : wx.SCRIPT, "swiss" : wx.SWISS, "modern" : wx.MODERN}
60 _styles = {"normal" : wx.NORMAL, "slant" : wx.SLANT, "italic" : wx.ITALIC}
61 _weights = {"normal" : wx.NORMAL, "light" : wx.LIGHT, "bold" : wx.BOLD}
62
63 # The next three classes: Renderer, SizeRenderer and DCRenderer are
64 # what you will need to override to extend the XML language. All of
65 # the font stuff as well as the subscript and superscript stuff are in
66 # Renderer.
67
68 _greek_letters = ("alpha", "beta", "gamma", "delta", "epsilon", "zeta",
69 "eta", "theta", "iota", "kappa", "lambda", "mu", "nu",
70 "xi", "omnikron", "pi", "rho", "altsigma", "sigma", "tau", "upsilon",
71 "phi", "chi", "psi", "omega")
72
73 def iround(number):
74 return int(round(number))
75
76 def iceil(number):
77 return int(math.ceil(number))
78
79 class Renderer:
80 """Class for rendering XML marked up text.
81
82 See the module docstring for a description of the markup.
83
84 This class must be subclassed and the methods the methods that do
85 the drawing overridden for a particular output device.
86
87 """
88 defaultSize = None
89 defaultFamily = wx.DEFAULT
90 defaultStyle = wx.NORMAL
91 defaultWeight = wx.NORMAL
92 defaultEncoding = None
93 defaultColor = "black"
94
95 def __init__(self, dc=None, x=0, y=None):
96 if dc == None:
97 dc = wx.MemoryDC()
98 self.dc = dc
99 self.offsets = [0]
100 self.fonts = [{}]
101 self.width = self.height = 0
102 self.x = x
103 self.minY = self.maxY = self._y = y
104 if Renderer.defaultSize is None:
105 Renderer.defaultSize = wx.NORMAL_FONT.GetPointSize()
106 if Renderer.defaultEncoding is None:
107 Renderer.defaultEncoding = wx.Font_GetDefaultEncoding()
108
109 def getY(self):
110 if self._y is None:
111 self.minY = self.maxY = self._y = self.dc.GetTextExtent("M")[1]
112 return self._y
113 def setY(self, value):
114 self._y = y
115 y = property(getY, setY)
116
117 def startElement(self, name, attrs):
118 method = "start_" + name
119 if not hasattr(self, method):
120 raise ValueError("XML tag '%s' not supported" % name)
121 getattr(self, method)(attrs)
122
123 def endElement(self, name):
124 methname = "end_" + name
125 if hasattr(self, methname):
126 getattr(self, methname)()
127 elif hasattr(self, "start_" + name):
128 pass
129 else:
130 raise ValueError("XML tag '%s' not supported" % methname)
131
132 def characterData(self, data):
133 self.dc.SetFont(self.getCurrentFont())
134 for i, chunk in enumerate(data.split('\n')):
135 if i:
136 self.x = 0
137 self.y = self.mayY = self.maxY + self.dc.GetTextExtent("M")[1]
138 if chunk:
139 width, height, descent, extl = self.dc.GetFullTextExtent(chunk)
140 self.renderCharacterData(data, iround(self.x), iround(self.y + self.offsets[-1] - height + descent))
141 else:
142 width = height = descent = extl = 0
143 self.updateDims(width, height, descent, extl)
144
145 def updateDims(self, width, height, descent, externalLeading):
146 self.x += width
147 self.width = max(self.x, self.width)
148 self.minY = min(self.minY, self.y+self.offsets[-1]-height+descent)
149 self.maxY = max(self.maxY, self.y+self.offsets[-1]+descent)
150 self.height = self.maxY - self.minY
151
152 def start_FancyText(self, attrs):
153 pass
154 start_wxFancyText = start_FancyText # For backward compatibility
155
156 def start_font(self, attrs):
157 for key, value in attrs.items():
158 if key == "size":
159 value = int(value)
160 elif key == "family":
161 value = _families[value]
162 elif key == "style":
163 value = _styles[value]
164 elif key == "weight":
165 value = _weights[value]
166 elif key == "encoding":
167 value = int(value)
168 elif key == "color":
169 pass
170 else:
171 raise ValueError("unknown font attribute '%s'" % key)
172 attrs[key] = value
173 font = copy.copy(self.fonts[-1])
174 font.update(attrs)
175 self.fonts.append(font)
176
177 def end_font(self):
178 self.fonts.pop()
179
180 def start_sub(self, attrs):
181 if attrs.keys():
182 raise ValueError("<sub> does not take attributes")
183 font = self.getCurrentFont()
184 self.offsets.append(self.offsets[-1] + self.dc.GetFullTextExtent("M", font)[1]*0.5)
185 self.start_font({"size" : font.GetPointSize() * 0.8})
186
187 def end_sub(self):
188 self.fonts.pop()
189 self.offsets.pop()
190
191 def start_sup(self, attrs):
192 if attrs.keys():
193 raise ValueError("<sup> does not take attributes")
194 font = self.getCurrentFont()
195 self.offsets.append(self.offsets[-1] - self.dc.GetFullTextExtent("M", font)[1]*0.3)
196 self.start_font({"size" : font.GetPointSize() * 0.8})
197
198 def end_sup(self):
199 self.fonts.pop()
200 self.offsets.pop()
201
202 def getCurrentFont(self):
203 font = self.fonts[-1]
204 return wx.Font(font.get("size", self.defaultSize),
205 font.get("family", self.defaultFamily),
206 font.get("style", self.defaultStyle),
207 font.get("weight",self.defaultWeight),
208 False, "",
209 font.get("encoding", self.defaultEncoding))
210
211 def getCurrentColor(self):
212 font = self.fonts[-1]
213 return wx.TheColourDatabase.FindColour(font.get("color", self.defaultColor))
214
215 def getCurrentPen(self):
216 return wx.Pen(self.getCurrentColor(), 1, wx.SOLID)
217
218 def renderCharacterData(self, data, x, y):
219 raise NotImplementedError()
220
221
222 def _addGreek():
223 alpha = 0xE1
224 Alpha = 0xC1
225 def end(self):
226 pass
227 for i, name in enumerate(_greek_letters):
228 def start(self, attrs, code=chr(alpha+i)):
229 self.start_font({"encoding" : _greekEncoding})
230 self.characterData(code)
231 self.end_font()
232 setattr(Renderer, "start_%s" % name, start)
233 setattr(Renderer, "end_%s" % name, end)
234 if name == "altsigma":
235 continue # There is no capital for altsigma
236 def start(self, attrs, code=chr(Alpha+i)):
237 self.start_font({"encoding" : _greekEncoding})
238 self.characterData(code)
239 self.end_font()
240 setattr(Renderer, "start_%s" % name.capitalize(), start)
241 setattr(Renderer, "end_%s" % name.capitalize(), end)
242 _addGreek()
243
244
245
246 class SizeRenderer(Renderer):
247 """Processes text as if rendering it, but just computes the size."""
248
249 def __init__(self, dc=None):
250 Renderer.__init__(self, dc, 0, 0)
251
252 def renderCharacterData(self, data, x, y):
253 pass
254
255 def start_angle(self, attrs):
256 self.characterData("M")
257
258 def start_infinity(self, attrs):
259 width, height = self.dc.GetTextExtent("M")
260 width = max(width, 10)
261 height = max(height, width / 2)
262 self.updateDims(width, height, 0, 0)
263
264 def start_times(self, attrs):
265 self.characterData("M")
266
267 def start_in(self, attrs):
268 self.characterData("M")
269
270 def start_times(self, attrs):
271 self.characterData("M")
272
273
274 class DCRenderer(Renderer):
275 """Renders text to a wxPython device context DC."""
276
277 def renderCharacterData(self, data, x, y):
278 self.dc.SetTextForeground(self.getCurrentColor())
279 self.dc.DrawText(data, x, y)
280
281 def start_angle(self, attrs):
282 self.dc.SetFont(self.getCurrentFont())
283 self.dc.SetPen(self.getCurrentPen())
284 width, height, descent, leading = self.dc.GetFullTextExtent("M")
285 y = self.y + self.offsets[-1]
286 self.dc.DrawLine(iround(self.x), iround(y), iround( self.x+width), iround(y))
287 self.dc.DrawLine(iround(self.x), iround(y), iround(self.x+width), iround(y-width))
288 self.updateDims(width, height, descent, leading)
289
290
291 def start_infinity(self, attrs):
292 self.dc.SetFont(self.getCurrentFont())
293 self.dc.SetPen(self.getCurrentPen())
294 width, height, descent, leading = self.dc.GetFullTextExtent("M")
295 width = max(width, 10)
296 height = max(height, width / 2)
297 self.dc.SetPen(wx.Pen(self.getCurrentColor(), max(1, width/10)))
298 self.dc.SetBrush(wx.TRANSPARENT_BRUSH)
299 y = self.y + self.offsets[-1]
300 r = iround( 0.95 * width / 4)
301 xc = (2*self.x + width) / 2
302 yc = iround(y-1.5*r)
303 self.dc.DrawCircle(xc - r, yc, r)
304 self.dc.DrawCircle(xc + r, yc, r)
305 self.updateDims(width, height, 0, 0)
306
307 def start_times(self, attrs):
308 self.dc.SetFont(self.getCurrentFont())
309 self.dc.SetPen(self.getCurrentPen())
310 width, height, descent, leading = self.dc.GetFullTextExtent("M")
311 y = self.y + self.offsets[-1]
312 width *= 0.8
313 width = iround(width+.5)
314 self.dc.SetPen(wx.Pen(self.getCurrentColor(), 1))
315 self.dc.DrawLine(iround(self.x), iround(y-width), iround(self.x+width-1), iround(y-1))
316 self.dc.DrawLine(iround(self.x), iround(y-2), iround(self.x+width-1), iround(y-width-1))
317 self.updateDims(width, height, 0, 0)
318
319
320 def RenderToRenderer(str, renderer, enclose=True):
321 try:
322 if enclose:
323 str = '<?xml version="1.0"?><FancyText>%s</FancyText>' % str
324 p = xml.parsers.expat.ParserCreate()
325 p.returns_unicode = 0
326 p.StartElementHandler = renderer.startElement
327 p.EndElementHandler = renderer.endElement
328 p.CharacterDataHandler = renderer.characterData
329 p.Parse(str, 1)
330 except xml.parsers.expat.error, err:
331 raise ValueError('error parsing text text "%s": %s' % (str, err))
332
333
334 # Public interface
335
336
337 def GetExtent(str, dc=None, enclose=True):
338 "Return the extent of str"
339 renderer = SizeRenderer(dc)
340 RenderToRenderer(str, renderer, enclose)
341 return iceil(renderer.width), iceil(renderer.height) # XXX round up
342
343
344 def GetFullExtent(str, dc=None, enclose=True):
345 renderer = SizeRenderer(dc)
346 RenderToRenderer(str, renderer, enclose)
347 return iceil(renderer.width), iceil(renderer.height), -iceil(renderer.minY) # XXX round up
348
349
350 def RenderToBitmap(str, background=None, enclose=1):
351 "Return str rendered on a minumum size bitmap"
352 dc = wx.MemoryDC()
353 width, height, dy = GetFullExtent(str, dc, enclose)
354 bmp = wx.EmptyBitmap(width, height)
355 dc.SelectObject(bmp)
356 if background is None:
357 dc.SetBackground(wx.WHITE_BRUSH)
358 else:
359 dc.SetBackground(background)
360 dc.Clear()
361 renderer = DCRenderer(dc, y=dy)
362 dc.BeginDrawing()
363 RenderToRenderer(str, renderer, enclose)
364 dc.EndDrawing()
365 dc.SelectObject(wx.NullBitmap)
366 if background is None:
367 img = wx.ImageFromBitmap(bmp)
368 bg = dc.GetBackground().GetColour()
369 img.SetMaskColour(bg.Red(), bg.Green(), bg.Blue())
370 bmp = img.ConvertToBitmap()
371 return bmp
372
373
374 def RenderToDC(str, dc, x, y, enclose=1):
375 "Render str onto a wxDC at (x,y)"
376 width, height, dy = GetFullExtent(str, dc)
377 renderer = DCRenderer(dc, x, y+dy)
378 RenderToRenderer(str, renderer, enclose)
379
380
381 class StaticFancyText(wx.StaticBitmap):
382 def __init__(self, window, id, text, *args, **kargs):
383 args = list(args)
384 kargs.setdefault('name', 'staticFancyText')
385 if 'background' in kargs:
386 background = kargs.pop('background')
387 elif args:
388 background = args.pop(0)
389 else:
390 background = wx.Brush(window.GetBackgroundColour(), wx.SOLID)
391
392 bmp = RenderToBitmap(text, background)
393 wx.StaticBitmap.__init__(self, window, id, bmp, *args, **kargs)
394
395
396 # Old names for backward compatibiliry
397 getExtent = GetExtent
398 renderToBitmap = RenderToBitmap
399 renderToDC = RenderToDC
400
401
402 # Test Driver
403
404 def test():
405 testText = \
406 """<font weight="bold" size="16">FancyText</font> -- <font style="italic" size="16">methods for rendering XML specified text</font>
407 <font family="swiss" size="12">
408 This module exports four main methods::
409 <font family="fixed" style="slant">
410 def GetExtent(str, dc=None, enclose=True)
411 def GetFullExtent(str, dc=None, enclose=True)
412 def RenderToBitmap(str, background=None, enclose=True)
413 def RenderToDC(str, dc, x, y, enclose=True)
414 </font>
415 In all cases, 'str' is an XML string. Note that start and end tags
416 are only required if *enclose* is set to False. In this case the
417 text should be wrapped in FancyText tags.
418
419 In addition, the module exports one class::
420 <font family="fixed" style="slant">
421 class StaticFancyText(self, window, id, text, background, ...)
422 </font>
423 This class works similar to StaticText except it interprets its text
424 as FancyText.
425
426 The text can support<sup>superscripts</sup> and <sub>subscripts</sub>, text
427 in different <font size="20">sizes</font>, <font color="blue">colors</font>, <font style="italic">styles</font>, <font weight="bold">weights</font> and
428 <font family="script">families</font>. It also supports a limited set of symbols,
429 currently <times/>, <infinity/>, <angle/> as well as greek letters in both
430 upper case (<Alpha/><Beta/>...<Omega/>) and lower case (<alpha/><beta/>...<omega/>).
431
432 We can use doctest/guitest to display this string in all its marked up glory.
433 <font family="fixed">
434 >>> frame = wx.Frame(wx.NULL, -1, "FancyText demo", wx.DefaultPosition)
435 >>> sft = StaticFancyText(frame, -1, __doc__, wx.Brush("light grey", wx.SOLID))
436 >>> frame.SetClientSize(sft.GetSize())
437 >>> didit = frame.Show()
438 >>> from guitest import PauseTests; PauseTests()
439
440 </font></font>
441 The End"""
442
443 app = wx.PySimpleApp()
444 box = wx.BoxSizer(wx.VERTICAL)
445 frame = wx.Frame(None, -1, "FancyText demo", wx.DefaultPosition)
446 frame.SetBackgroundColour("light grey")
447 sft = StaticFancyText(frame, -1, testText)
448 box.Add(sft, 1, wx.EXPAND)
449 frame.SetSizer(box)
450 frame.SetAutoLayout(True)
451 box.Fit(frame)
452 box.SetSizeHints(frame)
453 frame.Show()
454 app.MainLoop()
455
456 if __name__ == "__main__":
457 test()
458
459