Revision: 4110 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4110&view=rev Author: mdboom Date: 2007-11-05 09:30:08 -0800 (Mon, 05 Nov 2007)
Log Message: ----------- Make STIX fonts work. Modified Paths: -------------- trunk/matplotlib/examples/mathtext_examples.py trunk/matplotlib/lib/matplotlib/_mathtext_data.py trunk/matplotlib/lib/matplotlib/config/mplconfig.py trunk/matplotlib/lib/matplotlib/config/rcsetup.py trunk/matplotlib/lib/matplotlib/mathtext.py trunk/matplotlib/lib/matplotlib/rcsetup.py trunk/matplotlib/matplotlibrc.template Modified: trunk/matplotlib/examples/mathtext_examples.py =================================================================== --- trunk/matplotlib/examples/mathtext_examples.py 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/examples/mathtext_examples.py 2007-11-05 17:30:08 UTC (rev 4110) @@ -48,7 +48,8 @@ r'$\mathcal{H} = \int d \tau \left(\epsilon E^2 + \mu H^2\right)$', r'$\widehat{abc}\widetilde{def}$', r'$\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega$', - r'$\alpha \beta \gamma \delta \epsilon \zeta \eta \theta \iota \lambda \mu \nu \xi \pi \kappa \rho \sigma \tau \upsilon \phi \chi \psi$' + r'$\alpha \beta \gamma \delta \epsilon \zeta \eta \theta \iota \lambda \mu \nu \xi \pi \kappa \rho \sigma \tau \upsilon \phi \chi \psi$', + ur'Generic symbol: $\u23ce \mathrm{\ue0f2}$' ] from pylab import * Modified: trunk/matplotlib/lib/matplotlib/_mathtext_data.py =================================================================== --- trunk/matplotlib/lib/matplotlib/_mathtext_data.py 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/lib/matplotlib/_mathtext_data.py 2007-11-05 17:30:08 UTC (rev 4110) @@ -1755,7 +1755,10 @@ uni2type1 = dict([(v,k) for k,v in type12uni.items()]) -tex2uni = {'doteq': 8784, +tex2uni = { +'widehat': 0x0302, +'widetilde': 0x0303, +'doteq': 8784, 'partial': 8706, 'gg': 8811, 'asymp': 8781, Modified: trunk/matplotlib/lib/matplotlib/config/mplconfig.py =================================================================== --- trunk/matplotlib/lib/matplotlib/config/mplconfig.py 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/lib/matplotlib/config/mplconfig.py 2007-11-05 17:30:08 UTC (rev 4110) @@ -166,7 +166,7 @@ it = T.Trait('serif:oblique' , mplT.FontconfigPatternHandler()) bf = T.Trait('serif:bold' , mplT.FontconfigPatternHandler()) sf = T.Trait('sans' , mplT.FontconfigPatternHandler()) - use_cm = T.true + fontset = T.Trait('cm', 'cm', 'stix', 'custom') fallback_to_cm = T.true class axes(TConfig): @@ -344,7 +344,7 @@ 'mathtext.it' : (self.tconfig.mathtext, 'it'), 'mathtext.bf' : (self.tconfig.mathtext, 'bf'), 'mathtext.sf' : (self.tconfig.mathtext, 'sf'), - 'mathtext.use_cm' : (self.tconfig.mathtext, 'use_cm'), + 'mathtext.fontset' : (self.tconfig.mathtext, 'fontset'), 'mathtext.fallback_to_cm' : (self.tconfig.mathtext, 'fallback_to_cm'), 'image.aspect' : (self.tconfig.image, 'aspect'), Modified: trunk/matplotlib/lib/matplotlib/config/rcsetup.py =================================================================== --- trunk/matplotlib/lib/matplotlib/config/rcsetup.py 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/lib/matplotlib/config/rcsetup.py 2007-11-05 17:30:08 UTC (rev 4110) @@ -203,6 +203,8 @@ parse_fontconfig_pattern(s) return s +validate_fontset = ValidateInStrings('fontset', ['cm', 'stix', 'custom']) + validate_verbose = ValidateInStrings('verbose',[ 'silent', 'helpful', 'debug', 'debug-annoying', ]) @@ -365,7 +367,7 @@ 'mathtext.it' : ['serif:italic', validate_font_properties], 'mathtext.bf' : ['serif:bold', validate_font_properties], 'mathtext.sf' : ['sans\-serif', validate_font_properties], - 'mathtext.use_cm' : [True, validate_bool], + 'mathtext.fontset' : ['cm', validate_fontset], 'mathtext.fallback_to_cm' : [True, validate_bool], 'image.aspect' : ['equal', validate_aspect], # equal, auto, a number Modified: trunk/matplotlib/lib/matplotlib/mathtext.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mathtext.py 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/lib/matplotlib/mathtext.py 2007-11-05 17:30:08 UTC (rev 4110) @@ -528,7 +528,7 @@ A generic base class for all font setups that use Truetype fonts (through ft2font) """ - basepath = os.path.join( get_data_path(), 'fonts', 'ttf' ) + basepath = os.path.join( get_data_path(), 'fonts' ) class CachedFont: def __init__(self, font): @@ -665,7 +665,7 @@ TruetypeFonts.__init__(self, *args, **kwargs) if not len(self.fontmap): for key, val in self._fontmap.iteritems(): - fullpath = os.path.join(self.basepath, val + ".ttf") + fullpath = os.path.join(self.basepath, 'ttf', val + ".ttf") self.fontmap[key] = fullpath self.fontmap[val] = fullpath @@ -750,7 +750,8 @@ """ fontmap = {} - + use_cmex = True + def __init__(self, *args, **kwargs): # This must come first so the backend's owner is set correctly if rcParams['mathtext.fallback_to_cm']: @@ -772,18 +773,20 @@ def _get_glyph(self, fontname, sym, fontsize): found_symbol = False - uniindex = latex_to_cmex.get(sym) - if uniindex is not None: - fontname = 'ex' - found_symbol = True - else: + if self.use_cmex: + uniindex = latex_to_cmex.get(sym) + if uniindex is not None: + fontname = 'ex' + found_symbol = True + + if not found_symbol: try: uniindex = get_unicode_index(sym) found_symbol = True except ValueError: uniindex = ord('?') warn("No TeX to unicode mapping for '%s'" % - sym.encode('ascii', 'replace'), + sym.encode('ascii', 'backslashreplace'), MathTextWarning) # Only characters in the "Letter" class should be italicized in 'it' @@ -804,7 +807,7 @@ except KeyError: warn("Font '%s' does not have a glyph for '%s'" % (cached_font.font.postscript_name, - sym.encode('ascii', 'replace')), + sym.encode('ascii', 'backslashreplace')), MathTextWarning) found_symbol = False @@ -815,6 +818,7 @@ return self.cm_fallback._get_glyph(fontname, sym, fontsize) else: warn("Substituting with a dummy symbol.", MathTextWarning) + fontname = 'rm' new_fontname = fontname cached_font = self._get_font(fontname) uniindex = 0xA4 # currency character, for lack of anything better @@ -829,6 +833,71 @@ return self.cm_fallback.get_sized_alternatives_for_symbol( fontname, sym) return [(fontname, sym)] + +class StixFonts(UnicodeFonts): + _fontmap = { 'rm' : ('STIXGeneral', 'otf'), + 'tt' : ('VeraMono', 'ttf'), + 'it' : ('STIXGeneralItalic', 'otf'), + 'bf' : ('STIXGeneralBol', 'otf'), + 'sf' : ('Vera', 'ttf'), + 'nonunirm' : ('STIXNonUni', 'otf'), + 'nonuniit' : ('STIXNonUniIta', 'otf'), + 'nonunibf' : ('STIXNonUniBol', 'otf'), + + 0 : ('STIXGeneral', 'otf'), + 1 : ('STIXSiz1Sym', 'otf'), + 2 : ('STIXSiz2Sym', 'otf'), + 3 : ('STIXSiz3Sym', 'otf'), + 4 : ('STIXSiz4Sym', 'otf'), + 5 : ('STIXSiz5Sym', 'otf') + } + fontmap = {} + use_cmex = False + cm_fallback = False + + def __init__(self, *args, **kwargs): + TruetypeFonts.__init__(self, *args, **kwargs) + if not len(self.fontmap): + for key, (name, ext) in self._fontmap.iteritems(): + fullpath = os.path.join(self.basepath, ext, name + "." + ext) + self.fontmap[key] = fullpath + self.fontmap[name] = fullpath + + def _get_glyph(self, fontname, sym, fontsize): + # Handle calligraphic letters + if fontname == 'cal': + if len(sym) != 1 or ord(sym) < ord('A') or ord(sym) > ord('Z'): + raise ValueError(r"Sym '%s' is not available in \mathcal font" % sym) + fontname = 'nonuniit' + sym = unichr(ord(sym) + 0xe22d - ord('A')) + + # Handle private use area glyphs + if (fontname in ('it', 'rm', 'bf') and + len(sym) == 1 and ord(sym) >= 0xe000 and ord(sym) <= 0xf8ff): + fontname = 'nonuni' + fontname + + return UnicodeFonts._get_glyph(self, fontname, sym, fontsize) + + _size_alternatives = {} + def get_sized_alternatives_for_symbol(self, fontname, sym): + alternatives = self._size_alternatives.get(sym) + if alternatives: + return alternatives + + alternatives = [] + try: + uniindex = get_unicode_index(sym) + except ValueError: + return [(fontname, sym)] + + for i in range(6): + cached_font = self._get_font(i) + glyphindex = cached_font.charmap.get(uniindex) + if glyphindex is not None: + alternatives.append((i, unichr(uniindex))) + + self._size_alternatives[sym] = alternatives + return alternatives class StandardPsFonts(Fonts): """ @@ -1091,7 +1160,7 @@ Node.__init__(self) self.c = c self.font_output = state.font_output - assert isinstance(state.font, str) + assert isinstance(state.font, (str, unicode, int)) self.font = state.font self.fontsize = state.fontsize self.dpi = state.dpi @@ -1876,7 +1945,7 @@ ) | Error(r"Expected \hspace{n}")) ).setParseAction(self.customspace).setName('customspace') - symbol =(Regex(r"([a-zA-Z0-9 +\-*/<>=:,.;!'@()])|(\\[%${}\[\]])") + symbol =(Regex(ur"([a-zA-Z0-9 +\-*/<>=:,.;!'@()\u0080-\uffff])|(\\[%${}\[\]])") | Combine( bslash + oneOf(tex2uni.keys()) @@ -2508,10 +2577,15 @@ font_output = StandardPsFonts(prop) else: backend = self._backend_mapping[self._output]() - if rcParams['mathtext.use_cm']: + fontset = rcParams['mathtext.fontset'] + if fontset == 'cm': font_output = BakomaFonts(prop, backend) + elif fontset == 'stix': + font_output = StixFonts(prop, backend) + elif fontset == 'custom': + font_output = UnicodeFonts(prop, backend) else: - font_output = UnicodeFonts(prop, backend) + raise ValueError("mathtext.fontset must be either 'cm', 'stix', or 'custom'") fontsize = prop.get_size_in_points() Modified: trunk/matplotlib/lib/matplotlib/rcsetup.py =================================================================== --- trunk/matplotlib/lib/matplotlib/rcsetup.py 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/lib/matplotlib/rcsetup.py 2007-11-05 17:30:08 UTC (rev 4110) @@ -202,7 +202,9 @@ def validate_font_properties(s): parse_fontconfig_pattern(s) return s - + +validate_fontset = ValidateInStrings('fontset', ['cm', 'stix', 'custom']) + validate_verbose = ValidateInStrings('verbose',[ 'silent', 'helpful', 'debug', 'debug-annoying', ]) @@ -365,7 +367,7 @@ 'mathtext.it' : ['serif:italic', validate_font_properties], 'mathtext.bf' : ['serif:bold', validate_font_properties], 'mathtext.sf' : ['sans\-serif', validate_font_properties], - 'mathtext.use_cm' : [True, validate_bool], + 'mathtext.fontset' : [True, validate_fontset], 'mathtext.fallback_to_cm' : [True, validate_bool], 'image.aspect' : ['equal', validate_aspect], # equal, auto, a number Modified: trunk/matplotlib/matplotlibrc.template =================================================================== --- trunk/matplotlib/matplotlibrc.template 2007-11-05 16:50:11 UTC (rev 4109) +++ trunk/matplotlib/matplotlibrc.template 2007-11-05 17:30:08 UTC (rev 4110) @@ -160,20 +160,18 @@ # processing. # The following settings allow you to select the fonts in math mode. -# They map from a TeX font name to a set of arguments for the FontProperties constructor. -# (See FontProperties for more details). -# These settings are only used if mathtext.use_cm is False, otherwise, the -# Bakoma TeX Computer Modern fonts are used. +# They map from a TeX font name to a fontconfig font pattern. +# These settings are only used if mathtext.fontset is 'custom'. #mathtext.cal : cursive #mathtext.rm : serif #mathtext.tt : monospace #mathtext.it : serif:italic #mathtext.bf : serif:bold #mathtext.sf : sans -#mathtext.use_cm : True +#mathtext.fontset : cm # Should be 'cm' (Computer Modern), 'stix' or 'custom' #mathtext.fallback_to_cm : True # When True, use symbols from the Computer Modern # fonts when a symbol can not be found in one of - # the user-specified math fonts. + # the custom math fonts. ### AXES # default face and edge color, default tick sizes, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ Matplotlib-checkins mailing list Matplotlib-checkins@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins