@@ -82,6 +82,14 @@ def get_unicode_index(symbol):
8282TeX/Type1 symbol""" % locals ()
8383 raise ValueError , message
8484
85+ def unichr_safe (index ):
86+ """Return the Unicode character corresponding to the index,
87+ or the replacement character if this is a narrow build of Python
88+ and the requested character is outside the BMP."""
89+ try :
90+ return unichr (index )
91+ except ValueError :
92+ return unichr (0xFFFD )
8593
8694class MathtextBackend (object ):
8795 """
@@ -321,7 +329,8 @@ def __init__(self):
321329
322330 def render_glyph (self , ox , oy , info ):
323331 oy = self .height - oy + info .offset
324- thetext = unichr (info .num )
332+ thetext = unichr_safe (info .num )
333+
325334 self .svg_glyphs .append (
326335 (info .font , info .fontsize , thetext , ox , oy , info .metrics ))
327336
@@ -351,7 +360,7 @@ def __init__(self):
351360
352361 def render_glyph (self , ox , oy , info ):
353362 oy = self .height - oy + info .offset
354- thetext = unichr (info .num )
363+ thetext = unichr_safe (info .num )
355364 self .glyphs .append (
356365 (info .font , info .fontsize , thetext , ox , oy ))
357366
@@ -379,7 +388,7 @@ def __init__(self):
379388
380389 def render_glyph (self , ox , oy , info ):
381390 oy = oy - info .offset - self .height
382- thetext = unichr (info .num )
391+ thetext = unichr_safe (info .num )
383392 self .glyphs .append (
384393 (info .font , info .fontsize , thetext , ox , oy ))
385394
@@ -997,7 +1006,7 @@ def get_sized_alternatives_for_symbol(self, fontname, sym):
9971006 cached_font = self ._get_font (i )
9981007 glyphindex = cached_font .charmap .get (uniindex )
9991008 if glyphindex is not None :
1000- alternatives .append ((i , unichr (uniindex )))
1009+ alternatives .append ((i , unichr_safe (uniindex )))
10011010
10021011 # The largest size of the radical symbol in STIX has incorrect
10031012 # metrics that cause it to be disconnected from the stem.
0 commit comments