From f6b0745b17779bdb515d83e56cd3b7220f9aefc0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 11 Jun 2019 11:11:11 +0200 Subject: [PATCH] mathtext style fixes. --- .flake8 | 2 +- lib/matplotlib/mathtext.py | 281 +++++++++++++++++++------------------ 2 files changed, 143 insertions(+), 140 deletions(-) diff --git a/.flake8 b/.flake8 index c5d96e57f370..e50401c6ebfb 100644 --- a/.flake8 +++ b/.flake8 @@ -36,7 +36,7 @@ per-file-ignores = lib/matplotlib/_cm.py: E202, E203, E302 lib/matplotlib/_mathtext_data.py: E203, E261 lib/matplotlib/font_manager.py: E203, E221, E251, E501 - lib/matplotlib/mathtext.py: E201, E202, E203, E211, E221, E222, E225, E251, E301, E402 + lib/matplotlib/mathtext.py: E221, E251 lib/matplotlib/pylab.py: E501 lib/matplotlib/rcsetup.py: E501 lib/matplotlib/tests/test_mathtext.py: E501 diff --git a/lib/matplotlib/mathtext.py b/lib/matplotlib/mathtext.py index aec77c9838c0..5d822fc29969 100644 --- a/lib/matplotlib/mathtext.py +++ b/lib/matplotlib/mathtext.py @@ -24,14 +24,11 @@ import unicodedata import numpy as np - from pyparsing import ( Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore, Optional, ParseBaseException, ParseFatalException, ParserElement, QuotedString, Regex, StringEnd, Suppress, ZeroOrMore) -ParserElement.enablePackrat() - from matplotlib import cbook, colors as mcolors, rcParams from matplotlib.afm import AFM from matplotlib.cbook import get_realpath_and_stat @@ -41,6 +38,7 @@ tex2uni, latex_to_cmex, stix_virtual_fonts) +ParserElement.enablePackrat() _log = logging.getLogger(__name__) @@ -653,14 +651,15 @@ class BakomaFonts(TruetypeFonts): Symbols are strewn about a number of font files, each of which has its own proprietary 8-bit encoding. """ - _fontmap = { 'cal' : 'cmsy10', - 'rm' : 'cmr10', - 'tt' : 'cmtt10', - 'it' : 'cmmi10', - 'bf' : 'cmb10', - 'sf' : 'cmss10', - 'ex' : 'cmex10' - } + _fontmap = { + 'cal': 'cmsy10', + 'rm': 'cmr10', + 'tt': 'cmtt10', + 'it': 'cmmi10', + 'bf': 'cmb10', + 'sf': 'cmss10', + 'ex': 'cmex10', + } def __init__(self, *args, **kwargs): self._stix_fallback = StixFonts(*args, **kwargs) @@ -702,44 +701,44 @@ def _get_glyph(self, fontname, font_class, sym, fontsize, math=True): # delimiters. The AutoSizedChar class will use these alternatives # and select the best (closest sized) glyph. _size_alternatives = { - '(' : [('rm', '('), ('ex', '\xa1'), ('ex', '\xb3'), + '(': [('rm', '('), ('ex', '\xa1'), ('ex', '\xb3'), ('ex', '\xb5'), ('ex', '\xc3')], - ')' : [('rm', ')'), ('ex', '\xa2'), ('ex', '\xb4'), + ')': [('rm', ')'), ('ex', '\xa2'), ('ex', '\xb4'), ('ex', '\xb6'), ('ex', '\x21')], - '{' : [('cal', '{'), ('ex', '\xa9'), ('ex', '\x6e'), + '{': [('cal', '{'), ('ex', '\xa9'), ('ex', '\x6e'), ('ex', '\xbd'), ('ex', '\x28')], - '}' : [('cal', '}'), ('ex', '\xaa'), ('ex', '\x6f'), + '}': [('cal', '}'), ('ex', '\xaa'), ('ex', '\x6f'), ('ex', '\xbe'), ('ex', '\x29')], # The fourth size of '[' is mysteriously missing from the BaKoMa # font, so I've omitted it for both '[' and ']' - '[' : [('rm', '['), ('ex', '\xa3'), ('ex', '\x68'), + '[': [('rm', '['), ('ex', '\xa3'), ('ex', '\x68'), ('ex', '\x22')], - ']' : [('rm', ']'), ('ex', '\xa4'), ('ex', '\x69'), + ']': [('rm', ']'), ('ex', '\xa4'), ('ex', '\x69'), ('ex', '\x23')], - r'\lfloor' : [('ex', '\xa5'), ('ex', '\x6a'), + r'\lfloor': [('ex', '\xa5'), ('ex', '\x6a'), ('ex', '\xb9'), ('ex', '\x24')], - r'\rfloor' : [('ex', '\xa6'), ('ex', '\x6b'), + r'\rfloor': [('ex', '\xa6'), ('ex', '\x6b'), ('ex', '\xba'), ('ex', '\x25')], - r'\lceil' : [('ex', '\xa7'), ('ex', '\x6c'), + r'\lceil': [('ex', '\xa7'), ('ex', '\x6c'), ('ex', '\xbb'), ('ex', '\x26')], - r'\rceil' : [('ex', '\xa8'), ('ex', '\x6d'), + r'\rceil': [('ex', '\xa8'), ('ex', '\x6d'), ('ex', '\xbc'), ('ex', '\x27')], - r'\langle' : [('ex', '\xad'), ('ex', '\x44'), + r'\langle': [('ex', '\xad'), ('ex', '\x44'), ('ex', '\xbf'), ('ex', '\x2a')], - r'\rangle' : [('ex', '\xae'), ('ex', '\x45'), + r'\rangle': [('ex', '\xae'), ('ex', '\x45'), ('ex', '\xc0'), ('ex', '\x2b')], - r'\__sqrt__' : [('ex', '\x70'), ('ex', '\x71'), + r'\__sqrt__': [('ex', '\x70'), ('ex', '\x71'), ('ex', '\x72'), ('ex', '\x73')], r'\backslash': [('ex', '\xb2'), ('ex', '\x2f'), ('ex', '\xc2'), ('ex', '\x2d')], - r'/' : [('rm', '/'), ('ex', '\xb1'), ('ex', '\x2e'), + r'/': [('rm', '/'), ('ex', '\xb1'), ('ex', '\x2e'), ('ex', '\xcb'), ('ex', '\x2c')], - r'\widehat' : [('rm', '\x5e'), ('ex', '\x62'), ('ex', '\x63'), + r'\widehat': [('rm', '\x5e'), ('ex', '\x62'), ('ex', '\x63'), ('ex', '\x64')], r'\widetilde': [('rm', '\x7e'), ('ex', '\x65'), ('ex', '\x66'), ('ex', '\x67')], - r'<' : [('cal', 'h'), ('ex', 'D')], - r'>' : [('cal', 'i'), ('ex', 'E')] + r'<': [('cal', 'h'), ('ex', 'D')], + r'>': [('cal', 'i'), ('ex', 'E')] } for alias, target in [(r'\leftparen', '('), @@ -881,11 +880,12 @@ def __init__(self, *args, **kwargs): self.fontmap = {} # Include Stix sized alternatives for glyphs self._fontmap.update({ - 1 : 'STIXSizeOneSym', - 2 : 'STIXSizeTwoSym', - 3 : 'STIXSizeThreeSym', - 4 : 'STIXSizeFourSym', - 5 : 'STIXSizeFiveSym'}) + 1: 'STIXSizeOneSym', + 2: 'STIXSizeTwoSym', + 3: 'STIXSizeThreeSym', + 4: 'STIXSizeFourSym', + 5: 'STIXSizeFiveSym', + }) for key, name in self._fontmap.items(): fullpath = findfont(name) self.fontmap[key] = fullpath @@ -916,14 +916,15 @@ class DejaVuSerifFonts(DejaVuFonts): If a glyph is not found it will fallback to Stix Serif """ - _fontmap = { 'rm' : 'DejaVu Serif', - 'it' : 'DejaVu Serif:italic', - 'bf' : 'DejaVu Serif:weight=bold', - 'sf' : 'DejaVu Sans', - 'tt' : 'DejaVu Sans Mono', - 'ex' : 'DejaVu Serif Display', - 0 : 'DejaVu Serif', - } + _fontmap = { + 'rm': 'DejaVu Serif', + 'it': 'DejaVu Serif:italic', + 'bf': 'DejaVu Serif:weight=bold', + 'sf': 'DejaVu Sans', + 'tt': 'DejaVu Sans Mono', + 'ex': 'DejaVu Serif Display', + 0: 'DejaVu Serif', + } class DejaVuSansFonts(DejaVuFonts): @@ -932,14 +933,15 @@ class DejaVuSansFonts(DejaVuFonts): If a glyph is not found it will fallback to Stix Sans """ - _fontmap = { 'rm' : 'DejaVu Sans', - 'it' : 'DejaVu Sans:italic', - 'bf' : 'DejaVu Sans:weight=bold', - 'sf' : 'DejaVu Sans', - 'tt' : 'DejaVu Sans Mono', - 'ex' : 'DejaVu Sans Display', - 0 : 'DejaVu Sans', - } + _fontmap = { + 'rm': 'DejaVu Sans', + 'it': 'DejaVu Sans:italic', + 'bf': 'DejaVu Sans:weight=bold', + 'sf': 'DejaVu Sans', + 'tt': 'DejaVu Sans Mono', + 'ex': 'DejaVu Sans Display', + 0: 'DejaVu Sans', + } class StixFonts(UnicodeFonts): @@ -954,20 +956,20 @@ class StixFonts(UnicodeFonts): - handles sized alternative characters for the STIXSizeX fonts. """ - _fontmap = { 'rm' : 'STIXGeneral', - 'it' : 'STIXGeneral:italic', - 'bf' : 'STIXGeneral:weight=bold', - 'nonunirm' : 'STIXNonUnicode', - 'nonuniit' : 'STIXNonUnicode:italic', - 'nonunibf' : 'STIXNonUnicode:weight=bold', - - 0 : 'STIXGeneral', - 1 : 'STIXSizeOneSym', - 2 : 'STIXSizeTwoSym', - 3 : 'STIXSizeThreeSym', - 4 : 'STIXSizeFourSym', - 5 : 'STIXSizeFiveSym' - } + _fontmap = { + 'rm': 'STIXGeneral', + 'it': 'STIXGeneral:italic', + 'bf': 'STIXGeneral:weight=bold', + 'nonunirm': 'STIXNonUnicode', + 'nonuniit': 'STIXNonUnicode:italic', + 'nonunibf': 'STIXNonUnicode:weight=bold', + 0: 'STIXGeneral', + 1: 'STIXSizeOneSym', + 2: 'STIXSizeTwoSym', + 3: 'STIXSizeThreeSym', + 4: 'STIXSizeFourSym', + 5: 'STIXSizeFiveSym', + } use_cmex = False cm_fallback = False _sans = False @@ -1063,14 +1065,15 @@ class StandardPsFonts(Fonts): """ basepath = str(cbook._get_data_path('fonts/afm')) - fontmap = { 'cal' : 'pzcmi8a', # Zapf Chancery - 'rm' : 'pncr8a', # New Century Schoolbook - 'tt' : 'pcrr8a', # Courier - 'it' : 'pncri8a', # New Century Schoolbook Italic - 'sf' : 'phvr8a', # Helvetica - 'bf' : 'pncb8a', # New Century Schoolbook Bold - None : 'psyr' # Symbol - } + fontmap = { + 'cal': 'pzcmi8a', # Zapf Chancery + 'rm': 'pncr8a', # New Century Schoolbook + 'tt': 'pcrr8a', # Courier + 'it': 'pncri8a', # New Century Schoolbook Italic + 'sf': 'phvr8a', # Helvetica + 'bf': 'pncb8a', # New Century Schoolbook Bold + None: 'psyr', # Symbol + } def __init__(self, default_font_prop): Fonts.__init__(self, default_font_prop, MathtextBackendPs()) @@ -2387,7 +2390,7 @@ def __init__(self): | Error(r"Expected \hspace{n}")) ) - unicode_range = "\U00000080-\U0001ffff" + unicode_range = "\U00000080-\U0001ffff" p.single_symbol <<= Regex( r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" % unicode_range) @@ -2416,11 +2419,11 @@ def __init__(self): + oneOf(list(self._function_names)) ) - p.start_group <<= Optional(p.latexfont) + p.lbrace - p.end_group <<= p.rbrace.copy() - p.simple_group <<= Group(p.lbrace + ZeroOrMore(p.token) + p.rbrace) - p.required_group<<= Group(p.lbrace + OneOrMore(p.token) + p.rbrace) - p.group <<= Group( + p.start_group <<= Optional(p.latexfont) + p.lbrace + p.end_group <<= p.rbrace.copy() + p.simple_group <<= Group(p.lbrace + ZeroOrMore(p.token) + p.rbrace) + p.required_group <<= Group(p.lbrace + OneOrMore(p.token) + p.rbrace) + p.group <<= Group( p.start_group + ZeroOrMore(p.token) + p.end_group ) @@ -2459,9 +2462,9 @@ def __init__(self): p.right_delim <<= oneOf(list(self._right_delim)) p.right_delim_safe <<= oneOf([*(self._right_delim - {'}'}), r'\}']) - p.genfrac <<= Group( + p.genfrac <<= Group( Suppress(Literal(r"\genfrac")) - - (( (p.lbrace + - (((p.lbrace + Optional(p.ambi_delim | p.left_delim, default='') + p.rbrace) + (p.lbrace @@ -2473,21 +2476,21 @@ def __init__(self): r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}")) ) - p.sqrt <<= Group( + p.sqrt <<= Group( Suppress(Literal(r"\sqrt")) - ((Optional(p.lbracket + p.int_literal + p.rbracket, default=None) + p.required_group) | Error("Expected \\sqrt{value}")) ) - p.overline <<= Group( + p.overline <<= Group( Suppress(Literal(r"\overline")) - (p.required_group | Error("Expected \\overline{value}")) ) - p.unknown_symbol<<= Combine(p.bslash + Regex("[A-Za-z]*")) + p.unknown_symbol <<= Combine(p.bslash + Regex("[A-Za-z]*")) - p.operatorname <<= Group( + p.operatorname <<= Group( Suppress(Literal(r"\operatorname")) - ((p.lbrace + ZeroOrMore(p.simple | p.unknown_symbol) + p.rbrace) | Error("Expected \\operatorname{value}")) @@ -2679,22 +2682,23 @@ def _make_space(self, percentage): self._em_width_cache[key] = width return Kern(width * percentage) - _space_widths = { r'\,' : 0.16667, # 3/18 em = 3 mu - r'\thinspace' : 0.16667, # 3/18 em = 3 mu - r'\/' : 0.16667, # 3/18 em = 3 mu - r'\>' : 0.22222, # 4/18 em = 4 mu - r'\:' : 0.22222, # 4/18 em = 4 mu - r'\;' : 0.27778, # 5/18 em = 5 mu - r'\ ' : 0.33333, # 6/18 em = 6 mu - r'~' : 0.33333, # 6/18 em = 6 mu, nonbreakable - r'\enspace' : 0.5, # 9/18 em = 9 mu - r'\quad' : 1, # 1 em = 18 mu - r'\qquad' : 2, # 2 em = 36 mu - r'\!' : -0.16667, # -3/18 em = -3 mu - } + _space_widths = { + r'\,': 0.16667, # 3/18 em = 3 mu + r'\thinspace': 0.16667, # 3/18 em = 3 mu + r'\/': 0.16667, # 3/18 em = 3 mu + r'\>': 0.22222, # 4/18 em = 4 mu + r'\:': 0.22222, # 4/18 em = 4 mu + r'\;': 0.27778, # 5/18 em = 5 mu + r'\ ': 0.33333, # 6/18 em = 6 mu + r'~': 0.33333, # 6/18 em = 6 mu, nonbreakable + r'\enspace': 0.5, # 9/18 em = 9 mu + r'\quad': 1, # 1 em = 18 mu + r'\qquad': 2, # 2 em = 36 mu + r'\!': -0.16667, # -3/18 em = -3 mu + } def space(self, s, loc, toks): - assert len(toks)==1 + assert len(toks) == 1 num = self._space_widths[toks[0]] box = self._make_space(num) return [box] @@ -2792,35 +2796,35 @@ def c_over_c(self, s, loc, toks): ]) _accent_map = { - r'hat' : r'\circumflexaccent', - r'breve' : r'\combiningbreve', - r'bar' : r'\combiningoverline', - r'grave' : r'\combininggraveaccent', - r'acute' : r'\combiningacuteaccent', - r'tilde' : r'\combiningtilde', - r'dot' : r'\combiningdotabove', - r'ddot' : r'\combiningdiaeresis', - r'vec' : r'\combiningrightarrowabove', - r'"' : r'\combiningdiaeresis', - r"`" : r'\combininggraveaccent', - r"'" : r'\combiningacuteaccent', - r'~' : r'\combiningtilde', - r'.' : r'\combiningdotabove', - r'^' : r'\circumflexaccent', - r'overrightarrow' : r'\rightarrow', - r'overleftarrow' : r'\leftarrow', - r'mathring' : r'\circ' - } + r'hat': r'\circumflexaccent', + r'breve': r'\combiningbreve', + r'bar': r'\combiningoverline', + r'grave': r'\combininggraveaccent', + r'acute': r'\combiningacuteaccent', + r'tilde': r'\combiningtilde', + r'dot': r'\combiningdotabove', + r'ddot': r'\combiningdiaeresis', + r'vec': r'\combiningrightarrowabove', + r'"': r'\combiningdiaeresis', + r"`": r'\combininggraveaccent', + r"'": r'\combiningacuteaccent', + r'~': r'\combiningtilde', + r'.': r'\combiningdotabove', + r'^': r'\circumflexaccent', + r'overrightarrow': r'\rightarrow', + r'overleftarrow': r'\leftarrow', + r'mathring': r'\circ', + } _wide_accents = set(r"widehat widetilde widebar".split()) # make a lambda and call it to get the namespace right _snowflake = (lambda am: [p for p in tex2uni if - any(p.startswith(a) and a != p for a in am)] - ) (set(_accent_map)) + any(p.startswith(a) and a != p for a in am)])( + set(_accent_map)) def accent(self, s, loc, toks): - assert len(toks)==1 + assert len(toks) == 1 state = self.get_state() thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi) @@ -2881,7 +2885,7 @@ def end_group(self, s, loc, toks): return [] def font(self, s, loc, toks): - assert len(toks)==1 + assert len(toks) == 1 name = toks[0] self.get_state().font = name return [] @@ -2907,7 +2911,7 @@ def is_between_brackets(self, s, loc): return False def subsuper(self, s, loc, toks): - assert len(toks)==1 + assert len(toks) == 1 nucleus = None sub = None @@ -3231,8 +3235,8 @@ def sqrt(self, s, loc, toks): return [hlist] def overline(self, s, loc, toks): - assert len(toks)==1 - assert len(toks[0])==1 + assert len(toks) == 1 + assert len(toks[0]) == 1 body = toks[0][0] @@ -3292,23 +3296,22 @@ class MathTextParser: _backend_mapping = { 'bitmap': MathtextBackendBitmap, - 'agg' : MathtextBackendAgg, - 'ps' : MathtextBackendPs, - 'pdf' : MathtextBackendPdf, - 'svg' : MathtextBackendSvg, - 'path' : MathtextBackendPath, - 'cairo' : MathtextBackendCairo, + 'agg': MathtextBackendAgg, + 'ps': MathtextBackendPs, + 'pdf': MathtextBackendPdf, + 'svg': MathtextBackendSvg, + 'path': MathtextBackendPath, + 'cairo': MathtextBackendCairo, 'macosx': MathtextBackendAgg, - } - + } _font_type_mapping = { - 'cm' : BakomaFonts, - 'dejavuserif' : DejaVuSerifFonts, - 'dejavusans' : DejaVuSansFonts, - 'stix' : StixFonts, - 'stixsans' : StixSansFonts, - 'custom' : UnicodeFonts - } + 'cm': BakomaFonts, + 'dejavuserif': DejaVuSerifFonts, + 'dejavusans': DejaVuSansFonts, + 'stix': StixFonts, + 'stixsans': StixSansFonts, + 'custom': UnicodeFonts, + } def __init__(self, output): """ @@ -3449,7 +3452,7 @@ def get_depth(self, texstr, dpi=120, fontsize=14): depth : int Offset of the baseline from the bottom of the image, in pixels. """ - assert self._output=="bitmap" + assert self._output == "bitmap" prop = FontProperties(size=fontsize) ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop) return depth