r""" :mod:`~matplotlib.mathtext` is a module for parsing a subset of the TeX math syntax and drawing them to a matplotlib backend.
For a tutorial of its usage see :doc:`/tutorials/text/mathtext`. This document is primarily concerned with implementation details.
The module uses pyparsing_ to parse the TeX expression.
.. _pyparsing: http://pyparsing.wikispaces.com/
The Bakoma distribution of the TeX Computer Modern fonts, and STIX fonts are supported. There is experimental support for using arbitrary fonts, but results may vary without proper tweaking and metrics for those fonts. """
Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore, Optional, ParseBaseException, ParseFatalException, ParserElement, QuotedString, Regex, StringEnd, Suppress, ZeroOrMore)
tex2uni, latex_to_cmex, stix_virtual_fonts)
####################
############################################################################## # FONTS
"""get_unicode_index(symbol, [bool]) -> integer
Return the integer index (from the Unicode table) of symbol. *symbol* can be a single unicode character, a TeX command (i.e. r'\\pi'), or a Type1 symbol name (i.e. 'phi'). If math is False, the current symbol should be treated as a non-math symbol. """ # for a non-math symbol, simply return its unicode index # From UTF #25: U+2212 minus sign is the preferred # representation of the unary and binary minus sign rather than # the ASCII-derived U+002D hyphen-minus, because minus sign is # unambiguous and because it is rendered with a more desirable # length, usually longer than a hyphen. except KeyError: raise ValueError( "'{}' is not a valid Unicode character or TeX/Type1 symbol" .format(symbol))
""" The base class for the mathtext backend-specific code. The purpose of :class:`MathtextBackend` subclasses is to interface between mathtext and a specific matplotlib graphics backend.
Subclasses need to override the following:
- :meth:`render_glyph` - :meth:`render_rect_filled` - :meth:`get_results`
And optionally, if you need to use a FreeType hinting style:
- :meth:`get_hinting_type` """
'Dimension the drawing canvas'
""" Draw a glyph described by *info* to the reference point (*ox*, *oy*). """ raise NotImplementedError()
""" Draw a filled black rectangle from (*x1*, *y1*) to (*x2*, *y2*). """ raise NotImplementedError()
""" Return a backend-specific tuple to return to the backend after all processing is done. """ raise NotImplementedError()
""" Get the FreeType hinting type to use with this particular backend. """ return LOAD_NO_HINTING
""" Render glyphs and rectangles to an FTImage buffer, which is later transferred to the Agg image by the Agg backend. """
min(self.bbox[1], y1), max(self.bbox[2], x2), max(self.bbox[3], y2)]
oy - info.metrics.ymax, ox + info.metrics.xmax, oy - info.metrics.ymin) else: self.image, ox, oy - info.metrics.iceberg, info.glyph, antialiased=rcParams['text.antialiased'])
if self.mode == 'bbox': self._update_bbox(x1, y1, x2, y2) else: height = max(int(y2 - y1) - 1, 0) if height == 0: center = (y2 + y1) / 2.0 y = int(center - (height + 1) / 2.0) else: y = int(y1) self.image.draw_rect_filled(int(x1), y, np.ceil(x2), y + height)
bbox[2] - bbox[0], (bbox[3] - bbox[1]) - orig_depth, (bbox[3] - bbox[1]) - orig_height) self.oy, self.width, self.height + self.depth, self.depth, self.image, used_characters)
ox, oy, width, height, depth, image, characters = \ MathtextBackendAgg.get_results(self, box, used_characters) return image, depth
""" Store information to write a mathtext rendering to the PostScript backend. """ self.pswriter = StringIO() self.lastfont = None
oy = self.height - oy + info.offset postscript_name = info.postscript_name fontsize = info.fontsize symbol_name = info.symbol_name
if (postscript_name, fontsize) != self.lastfont: ps = """/%(postscript_name)s findfont %(fontsize)s scalefont setfont """ % locals() self.lastfont = postscript_name, fontsize self.pswriter.write(ps)
ps = """%(ox)f %(oy)f moveto /%(symbol_name)s glyphshow\n """ % locals() self.pswriter.write(ps)
ps = "%f %f %f %f rectfill\n" % (x1, self.height - y2, x2 - x1, y2 - y1) self.pswriter.write(ps)
ship(0, 0, box) return (self.width, self.height + self.depth, self.depth, self.pswriter, used_characters)
""" Store information to write a mathtext rendering to the PDF backend. """ self.glyphs = [] self.rects = []
filename = info.font.fname oy = self.height - oy + info.offset self.glyphs.append( (ox, oy, filename, info.fontsize, info.num, info.symbol_name))
self.rects.append((x1, self.height - y2, x2 - x1, y2 - y1))
ship(0, 0, box) return (self.width, self.height + self.depth, self.depth, self.glyphs, self.rects, used_characters)
""" Store information to write a mathtext rendering to the SVG backend. """ self.svg_glyphs = [] self.svg_rects = []
oy = self.height - oy + info.offset
self.svg_glyphs.append( (info.font, info.fontsize, info.num, ox, oy, info.metrics))
self.svg_rects.append( (x1, self.height - y1 + 1, x2 - x1, y2 - y1))
ship(0, 0, box) svg_elements = types.SimpleNamespace(svg_glyphs=self.svg_glyphs, svg_rects=self.svg_rects) return (self.width, self.height + self.depth, self.depth, svg_elements, used_characters)
""" Store information to write a mathtext rendering to the text path machinery. """
self.glyphs = [] self.rects = []
oy = self.height - oy + info.offset thetext = info.num self.glyphs.append( (info.font, info.fontsize, thetext, ox, oy))
self.rects.append( (x1, self.height-y2 , x2 - x1, y2 - y1))
ship(0, 0, box) return (self.width, self.height + self.depth, self.depth, self.glyphs, self.rects)
""" Store information to write a mathtext rendering to the Cairo backend. """
self.glyphs = [] self.rects = []
oy = oy - info.offset - self.height thetext = chr(info.num) self.glyphs.append( (info.font, info.fontsize, thetext, ox, oy))
self.rects.append( (x1, y1 - self.height, x2 - x1, y2 - y1))
ship(0, 0, box) return (self.width, self.height + self.depth, self.depth, self.glyphs, self.rects)
""" An abstract base class for a system of fonts to use for mathtext.
The class must be able to take symbol keys and font file names and return the character metrics. It also delegates to a backend class to do the actual drawing. """
""" *default_font_prop*: A :class:`~matplotlib.font_manager.FontProperties` object to use for the default non-math font, or the base font for Unicode (generic) font rendering.
*mathtext_backend*: A subclass of :class:`MathTextBackend` used to delegate the actual rendering. """
""" Fix any cyclical references before the object is about to be destroyed. """
font2, fontclass2, sym2, fontsize2, dpi): """ Get the kerning distance for font between *sym1* and *sym2*.
*fontX*: one of the TeX font names::
tt, it, rm, cal, sf, bf or default/regular (non-math)
*fontclassX*: TODO
*symX*: a symbol in raw TeX form. e.g., '1', 'x' or '\\sigma'
*fontsizeX*: the fontsize in points
*dpi*: the current dots-per-inch """ return 0.
""" *font*: one of the TeX font names::
tt, it, rm, cal, sf, bf or default/regular (non-math)
*font_class*: TODO
*sym*: a symbol in raw TeX form. e.g., '1', 'x' or '\\sigma'
*fontsize*: font size in points
*dpi*: current dots-per-inch
*math*: whether sym is a math character
Returns an object with the following attributes:
- *advance*: The advance distance (in points) of the glyph.
- *height*: The height of the glyph in points.
- *width*: The width of the glyph in points.
- *xmin*, *xmax*, *ymin*, *ymax* - the ink rectangle of the glyph
- *iceberg* - the distance from the baseline to the top of the glyph. This corresponds to TeX's definition of "height". """
""" Set the size of the buffer used to render the math expression. Only really necessary for the bitmap backends. """ self.width, self.height, self.depth)
""" Draw a glyph at
- *ox*, *oy*: position
- *facename*: One of the TeX face names
- *font_class*:
- *sym*: TeX symbol name or single character
- *fontsize*: fontsize in points
- *dpi*: The dpi to draw at. """ stat_key, (realpath, set()))
""" Draw a filled rectangle from (*x1*, *y1*) to (*x2*, *y2*). """ self.mathtext_backend.render_rect_filled(x1, y1, x2, y2)
""" Get the xheight for the given *font* and *fontsize*. """ raise NotImplementedError()
""" Get the line thickness that matches the given font. Used as a base unit for drawing lines such as in a fraction or radical. """ raise NotImplementedError()
""" Get the set of characters that were used in the math expression. Used by backends that need to subset fonts so they know which glyphs to include. """
""" Get the data needed by the backend to render the math expression. The return value is backend-specific. """
""" Override if your font provides multiple sizes of the same symbol. Should return a list of symbols matching *sym* in various sizes. The expression renderer will select the most appropriate size for a given situation from this list. """ return [(fontname, sym)]
""" A generic base class for all font setups that use Truetype fonts (through FT2Font). """
else:
return ((glyph.height/64.0/2.0) + (fontsize/3.0 * dpi/72.0))
self._get_glyph(fontname, font_class, sym, fontsize, math)
num, flags=self.mathtext_backend.get_hinting_type())
advance = glyph.linearHoriAdvance/65536.0, height = glyph.height/64.0, width = glyph.width/64.0, xmin = xmin, xmax = xmax, ymin = ymin+offset, ymax = ymax+offset, # iceberg is the equivalent of TeX's "height" iceberg = glyph.horiBearingY/64.0 + offset, slanted = slanted )
font = font, fontsize = fontsize, postscript_name = font.postscript_name, metrics = metrics, symbol_name = symbol_name, num = num, glyph = glyph, offset = offset )
# Some fonts don't store the xHeight, so we do a poor man's xHeight xHeight = (pclt['xHeight'] / 64.0) * (fontsize / 12.0) * (dpi / 100.0) return xHeight
# This function used to grab underline thickness from the font # metrics, but that information is just too un-reliable, so it # is now hardcoded.
font2, fontclass2, sym2, fontsize2, dpi): return Fonts.get_kern(self, font1, fontclass1, sym1, fontsize1, font2, fontclass2, sym2, fontsize2, dpi)
""" Use the Bakoma TrueType fonts for rendering.
Symbols are strewn about a number of font files, each of which has its own proprietary 8-bit encoding. """ 'rm' : 'cmr10', 'tt' : 'cmtt10', 'it' : 'cmmi10', 'bf' : 'cmb10', 'sf' : 'cmss10', 'ex' : 'cmex10' }
symbol_name = None font = None if fontname in self.fontmap and sym in latex_to_bakoma: basename, num = latex_to_bakoma[sym] slanted = (basename == "cmmi10") or sym in self._slanted_symbols font = self._get_font(basename) elif len(sym) == 1: slanted = (fontname == "it") font = self._get_font(fontname) if font is not None: num = ord(sym)
if font is not None: gid = font.get_char_index(num) if gid != 0: symbol_name = font.get_glyph_name(gid)
if symbol_name is None: return self._stix_fallback._get_glyph( fontname, font_class, sym, fontsize, math)
return font, num, symbol_name, fontsize, slanted
# The Bakoma fonts contain many pre-sized alternatives for the # delimiters. The AutoSizedChar class will use these alternatives # and select the best (closest sized) glyph. '(' : [('rm', '('), ('ex', '\xa1'), ('ex', '\xb3'), ('ex', '\xb5'), ('ex', '\xc3')], ')' : [('rm', ')'), ('ex', '\xa2'), ('ex', '\xb4'), ('ex', '\xb6'), ('ex', '\x21')], '{' : [('cal', '{'), ('ex', '\xa9'), ('ex', '\x6e'), ('ex', '\xbd'), ('ex', '\x28')], '}' : [('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'), ('ex', '\x22')], ']' : [('rm', ']'), ('ex', '\xa4'), ('ex', '\x69'), ('ex', '\x23')], r'\lfloor' : [('ex', '\xa5'), ('ex', '\x6a'), ('ex', '\xb9'), ('ex', '\x24')], r'\rfloor' : [('ex', '\xa6'), ('ex', '\x6b'), ('ex', '\xba'), ('ex', '\x25')], r'\lceil' : [('ex', '\xa7'), ('ex', '\x6c'), ('ex', '\xbb'), ('ex', '\x26')], r'\rceil' : [('ex', '\xa8'), ('ex', '\x6d'), ('ex', '\xbc'), ('ex', '\x27')], r'\langle' : [('ex', '\xad'), ('ex', '\x44'), ('ex', '\xbf'), ('ex', '\x2a')], r'\rangle' : [('ex', '\xae'), ('ex', '\x45'), ('ex', '\xc0'), ('ex', '\x2b')], 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'), ('ex', '\xcb'), ('ex', '\x2c')], 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'\rightparent', ')'), (r'\leftbrace', '{'), (r'\rightbrace', '}'), (r'\leftbracket', '['), (r'\rightbracket', ']'), (r'\{', '{'), (r'\}', '}'), (r'\[', '['), (r'\]', ']')]:
return self._size_alternatives.get(sym, [(fontname, sym)])
""" An abstract base class for handling Unicode fonts.
While some reasonably complete Unicode fonts (such as DejaVu) may work in some situations, the only Unicode font I'm aware of with a complete set of math symbols is STIX.
This class will "fallback" on the Bakoma fonts when a required symbol can not be found in the font. """
# This must come first so the backend's owner is set correctly if rcParams['mathtext.fallback_to_cm']: self.cm_fallback = BakomaFonts(*args, **kwargs) else: self.cm_fallback = None TruetypeFonts.__init__(self, *args, **kwargs) self.fontmap = {} for texfont in "cal rm tt it bf sf".split(): prop = rcParams['mathtext.' + texfont] font = findfont(prop) self.fontmap[texfont] = font prop = FontProperties('cmex10') font = findfont(prop) self.fontmap['ex'] = font
uniindex = latex_to_cmex.get(sym) if uniindex is not None: fontname = 'ex' found_symbol = True
except ValueError: uniindex = ord('?') warnings.warn( "No TeX to unicode mapping for {!a}.".format(sym), MathTextWarning)
fontname, font_class, uniindex)
# Only characters in the "Letter" class should be italicized in 'it' # mode. Greek capital letters should be Roman. or unicodedata.name(unistring).startswith("GREEK CAPITAL")):
if self.cm_fallback: if isinstance(self.cm_fallback, BakomaFonts): warnings.warn( "Substituting with a symbol from Computer Modern.", MathTextWarning) if (fontname in ('it', 'regular') and isinstance(self.cm_fallback, StixFonts)): return self.cm_fallback._get_glyph( 'rm', font_class, sym, fontsize) else: return self.cm_fallback._get_glyph( fontname, font_class, sym, fontsize) else: if (fontname in ('it', 'regular') and isinstance(self, StixFonts)): return self._get_glyph('rm', font_class, sym, fontsize) warnings.warn( "Font {!r} does not have a glyph for {!a} [U+{:x}], " "substituting with a dummy symbol.".format( new_fontname, sym, uniindex), MathTextWarning) fontname = 'rm' new_fontname = fontname font = self._get_font(fontname) uniindex = 0xA4 # currency character, for lack of anything better glyphindex = font.get_char_index(uniindex) slanted = False
if self.cm_fallback: return self.cm_fallback.get_sized_alternatives_for_symbol( fontname, sym) return [(fontname, sym)]
# This must come first so the backend's owner is set correctly self.cm_fallback = StixFonts(*args, **kwargs) else: # Include Stix sized alternatives for glyphs 1 : 'STIXSizeOneSym', 2 : 'STIXSizeTwoSym', 3 : 'STIXSizeThreeSym', 4 : 'STIXSizeFourSym', 5 : 'STIXSizeFiveSym'})
""" Override prime symbol to use Bakoma """ return self.bakoma._get_glyph( fontname, font_class, sym, fontsize, math) else: # check whether the glyph is available in the display font return super()._get_glyph( 'ex', font_class, sym, fontsize, math) # otherwise return regular glyph fontname, font_class, sym, fontsize, math)
""" A font handling class for the DejaVu Serif fonts
If a glyph is not found it will fallback to Stix Serif """ 'it' : 'DejaVu Serif:italic', 'bf' : 'DejaVu Serif:weight=bold', 'sf' : 'DejaVu Sans', 'tt' : 'DejaVu Sans Mono', 'ex' : 'DejaVu Serif Display', 0 : 'DejaVu Serif', }
""" A font handling class for the DejaVu Sans fonts
If a glyph is not found it will fallback to Stix Sans """ 'it' : 'DejaVu Sans:italic', 'bf' : 'DejaVu Sans:weight=bold', 'sf' : 'DejaVu Sans', 'tt' : 'DejaVu Sans Mono', 'ex' : 'DejaVu Sans Display', 0 : 'DejaVu Sans', }
""" A font handling class for the STIX fonts.
In addition to what UnicodeFonts provides, this class:
- supports "virtual fonts" which are complete alpha numeric character sets with different font styles at special Unicode code points, such as "Blackboard".
- handles sized alternative characters for the STIXSizeX fonts. """ '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' }
# Handle these "fonts" that are actually embedded in # other fonts. mapping = stix_virtual_fonts.get(fontname) if (self._sans and mapping is None and fontname not in ('regular', 'default')): mapping = stix_virtual_fonts['sf'] doing_sans_conversion = True else: doing_sans_conversion = False
if mapping is not None: if isinstance(mapping, dict): try: mapping = mapping[font_class] except KeyError: mapping = mapping['rm']
# Binary search for the source glyph lo = 0 hi = len(mapping) while lo < hi: mid = (lo+hi)//2 range = mapping[mid] if uniindex < range[0]: hi = mid elif uniindex <= range[1]: break else: lo = mid + 1
if range[0] <= uniindex <= range[1]: uniindex = uniindex - range[0] + range[3] fontname = range[2] elif not doing_sans_conversion: # This will generate a dummy character uniindex = 0x1 fontname = rcParams['mathtext.default']
# Handle private use area glyphs if (fontname in ('it', 'rm', 'bf') and uniindex >= 0xe000 and uniindex <= 0xf8ff): fontname = 'nonuni' + fontname
return fontname, uniindex
fixes = {'\\{': '{', '\\}': '}', '\\[': '[', '\\]': ']'} sym = fixes.get(sym, sym)
alternatives = self._size_alternatives.get(sym) if alternatives: return alternatives
alternatives = [] try: uniindex = get_unicode_index(sym) except ValueError: return [(fontname, sym)]
fix_ups = { ord('<'): 0x27e8, ord('>'): 0x27e9 }
uniindex = fix_ups.get(uniindex, uniindex)
for i in range(6): font = self._get_font(i) glyphindex = font.get_char_index(uniindex) if glyphindex != 0: alternatives.append((i, chr(uniindex)))
# The largest size of the radical symbol in STIX has incorrect # metrics that cause it to be disconnected from the stem. if sym == r'\__sqrt__': alternatives = alternatives[:-1]
self._size_alternatives[sym] = alternatives return alternatives
""" A font handling class for the STIX fonts (that uses sans-serif characters by default). """
""" Use the standard postscript fonts for rendering to backend_ps
Unlike the other font classes, BakomaFont and UnicodeFont, this one requires the Ps backend. """
'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 }
Fonts.__init__(self, default_font_prop, MathtextBackendPs()) self.glyphd = {} self.fonts = {}
filename = findfont(default_font_prop, fontext='afm', directory=self.basepath) if filename is None: filename = findfont('Helvetica', fontext='afm', directory=self.basepath) with open(filename, 'rb') as fd: default_font = AFM(fd) default_font.fname = filename
self.fonts['default'] = default_font self.fonts['regular'] = default_font self.pswriter = StringIO()
if font in self.fontmap: basename = self.fontmap[font] else: basename = font
cached_font = self.fonts.get(basename) if cached_font is None: fname = os.path.join(self.basepath, basename + ".afm") with open(fname, 'rb') as fd: cached_font = AFM(fd) cached_font.fname = fname self.fonts[basename] = cached_font self.fonts[cached_font.get_fontname()] = cached_font return cached_font
'load the cmfont, metrics and glyph with caching' key = fontname, sym, fontsize, dpi tup = self.glyphd.get(key)
if tup is not None: return tup
# Only characters in the "Letter" class should really be italicized. # This class includes greek letters, so we're ok if (fontname == 'it' and (len(sym) > 1 or not unicodedata.category(sym).startswith("L"))): fontname = 'rm'
found_symbol = False
if sym in latex_to_standard: fontname, num = latex_to_standard[sym] glyph = chr(num) found_symbol = True elif len(sym) == 1: glyph = sym num = ord(glyph) found_symbol = True else: warnings.warn( "No TeX to built-in Postscript mapping for {!r}".format(sym), MathTextWarning)
slanted = (fontname == 'it') font = self._get_font(fontname)
if found_symbol: try: symbol_name = font.get_name_char(glyph) except KeyError: warnings.warn( "No glyph in standard Postscript font {!r} for {!r}" .format(font.get_fontname(), sym), MathTextWarning) found_symbol = False
if not found_symbol: glyph = sym = '?' num = ord(glyph) symbol_name = font.get_name_char(glyph)
offset = 0
scale = 0.001 * fontsize
xmin, ymin, xmax, ymax = [val * scale for val in font.get_bbox_char(glyph)] metrics = types.SimpleNamespace( advance = font.get_width_char(glyph) * scale, width = font.get_width_char(glyph) * scale, height = font.get_height_char(glyph) * scale, xmin = xmin, xmax = xmax, ymin = ymin+offset, ymax = ymax+offset, # iceberg is the equivalent of TeX's "height" iceberg = ymax + offset, slanted = slanted )
self.glyphd[key] = types.SimpleNamespace( font = font, fontsize = fontsize, postscript_name = font.get_fontname(), metrics = metrics, symbol_name = symbol_name, num = num, glyph = glyph, offset = offset )
return self.glyphd[key]
font2, fontclass2, sym2, fontsize2, dpi): if font1 == font2 and fontsize1 == fontsize2: info1 = self._get_info(font1, fontclass1, sym1, fontsize1, dpi) info2 = self._get_info(font2, fontclass2, sym2, fontsize2, dpi) font = info1.font return (font.get_kern_dist(info1.glyph, info2.glyph) * 0.001 * fontsize1) return Fonts.get_kern(self, font1, fontclass1, sym1, fontsize1, font2, fontclass2, sym2, fontsize2, dpi)
font = self._get_font(font) return font.get_xheight() * 0.001 * fontsize
font = self._get_font(font) return font.get_underline_thickness() * 0.001 * fontsize
############################################################################## # TeX-LIKE BOX MODEL
# The following is based directly on the document 'woven' from the # TeX82 source code. This information is also available in printed # form: # # Knuth, Donald E.. 1986. Computers and Typesetting, Volume B: # TeX: The Program. Addison-Wesley Professional. # # The most relevant "chapters" are: # Data structures for boxes and their friends # Shipping pages out (Ship class) # Packaging (hpack and vpack) # Data structures for math mode # Subroutines for math mode # Typesetting math formulas # # Many of the docstrings below refer to a numbered "node" in that # book, e.g., node123 # # Note that (as TeX) y increases downward, unlike many other parts of # matplotlib.
# How much text shrinks when going to the next-smallest level. GROW_FACTOR # must be the inverse of SHRINK_FACTOR. # The number of different sizes of chars to use, beyond which they will not # get any smaller
""" A set of constants that controls how certain things, such as sub- and superscripts are laid out. These are all metrics that can't be reliably retrieved from the font metrics in the font itself. """ # Percentage of x-height of additional horiz. space after sub/superscripts
# Percentage of x-height that sub/superscripts drop below the baseline
# Percentage of x-height that superscripts are raised from the baseline
# Percentage of x-height that subscripts drop below the baseline
# Percentage of x-height that subscripts drop below the baseline when a # superscript is present
# Percentage of x-height that sub/supercripts are offset relative to the # nucleus edge for non-slanted nuclei
# Additional percentage of last character height above 2/3 of the # x-height that supercripts are offset relative to the subscript # for slanted nuclei
# Percentage of x-height that supercripts and subscripts are offset for # integrals
# Maps font family names to the FontConstantBase subclass to use 'DejaVu Sans': DejaVuSansFontConstants, 'DejaVu Sans Mono': DejaVuSansFontConstants, 'DejaVu Serif': DejaVuSerifFontConstants, 'cmb10': ComputerModernFontConstants, 'cmex10': ComputerModernFontConstants, 'cmmi10': ComputerModernFontConstants, 'cmr10': ComputerModernFontConstants, 'cmss10': ComputerModernFontConstants, 'cmsy10': ComputerModernFontConstants, 'cmtt10': ComputerModernFontConstants, 'STIXGeneral': STIXFontConstants, 'STIXNonUnicode': STIXFontConstants, 'STIXSizeFiveSym': STIXFontConstants, 'STIXSizeFourSym': STIXFontConstants, 'STIXSizeThreeSym': STIXFontConstants, 'STIXSizeTwoSym': STIXFontConstants, 'STIXSizeOneSym': STIXFontConstants, # Map the fonts we used to ship, just for good measure 'Bitstream Vera Sans': DejaVuSansFontConstants, 'Bitstream Vera': DejaVuSansFontConstants, }
state.font_output._get_font(state.font).family_name, FontConstantsBase) # STIX sans isn't really its own fonts, just different code points # in the STIX fonts, so we have to detect this one separately. isinstance(state.font_output, StixSansFonts)): return STIXSansFontConstants
""" A node in the TeX box model """
def __repr__(self): return self.__class__.__name__
""" Shrinks one level smaller. There are only three levels of sizes, after which things will no longer get smaller. """
""" Grows one level larger. There is no limit to how big something can get. """ self.size -= 1
pass
""" Represents any node with a physical location. """
Node.grow(self) self.width *= GROW_FACTOR self.height *= GROW_FACTOR self.depth *= GROW_FACTOR
pass
""" A box with only height (zero width). """ Box.__init__(self, 0., height, depth)
""" A box with only width (zero height and depth). """ Box.__init__(self, width, 0., 0.)
""" Represents a single character. Unlike TeX, the font information and metrics are stored with each :class:`Char` to make it easier to lookup the font metrics when needed. Note that TeX boxes have a width, height, and depth, unlike Type1 and Truetype which use a full bounding box and an advance in the x-direction. The metrics must be converted to the TeX way, and the advance (if different from width) must be converted into a :class:`Kern` node when the :class:`Char` is added to its parent :class:`Hlist`. """ # The real width, height and depth will be set during the # pack phase, after we know the real fontsize
def __repr__(self): return '`%s`' % self.c
self.font, self.font_class, self.c, self.fontsize, self.dpi, self.math) else:
""" Return the amount of kerning between this and the given character. Called when characters are strung together into :class:`Hlist` to create :class:`Kern` nodes. """ self.font, self.font_class, self.c, self.fontsize, next.font, next.font_class, next.c, next.fontsize, self.dpi)
""" Render the character to the canvas """ x, y, self.font, self.font_class, self.c, self.fontsize, self.dpi)
Node.grow(self) self.fontsize *= GROW_FACTOR self.width *= GROW_FACTOR self.height *= GROW_FACTOR self.depth *= GROW_FACTOR
""" The font metrics need to be dealt with differently for accents, since they are already offset correctly from the baseline in TrueType fonts. """ metrics = self._metrics = self.font_output.get_metrics( self.font, self.font_class, self.c, self.fontsize, self.dpi) self.width = metrics.xmax - metrics.xmin self.height = metrics.ymax - metrics.ymin self.depth = 0
Char.shrink(self) self._update_metrics()
Char.grow(self) self._update_metrics()
""" Render the character to the canvas. """ self.font_output.render_glyph( x - self._metrics.xmin, y + self._metrics.ymin, self.font, self.font_class, self.c, self.fontsize, self.dpi)
""" A list of nodes (either horizontal or vertical). """ # The following parameters are set in the vpack and hpack functions
def __repr__(self): return '[%s <%.02f %.02f %.02f %.02f> %s]' % ( super().__repr__(), self.width, self.height, self.depth, self.shift_amount, ' '.join([repr(x) for x in self.children]))
def _determine_order(totals): """ Determine the highest order of glue used by the members of this list.
Helper function used by vpack and hpack. """ for i in range(len(totals))[::-1]: if totals[i] != 0: return i return 0
o = self._determine_order(totals) self.glue_order = o self.glue_sign = sign if totals[o] != 0.: self.glue_set = x / totals[o] else: self.glue_sign = 0 self.glue_ratio = 0. if o == 0: if len(self.children): warnings.warn( "%s %s: %r" % (error_type, self.__class__.__name__, self), MathTextWarning)
for child in self.children: child.grow() Box.grow(self) self.shift_amount *= GROW_FACTOR self.glue_set *= GROW_FACTOR
""" A horizontal list of boxes. """
""" Insert :class:`Kern` nodes between :class:`Char` nodes to set kerning. The :class:`Char` nodes themselves determine the amount of kerning they need (in :meth:`~Char.get_kerning`), and this function just creates the linked list in the correct way. """ else:
# This is a failed experiment to fake cross-font kerning. # def get_kerning(self, next): # if len(self.children) >= 2 and isinstance(self.children[-2], Char): # if isinstance(next, Char): # print "CASE A" # return self.children[-2].get_kerning(next) # elif isinstance(next, Hlist) and len(next.children) and isinstance(next.children[0], Char): # print "CASE B" # result = self.children[-2].get_kerning(next.children[0]) # print result # return result # return 0.0
""" The main duty of :meth:`hpack` is to compute the dimensions of the resulting boxes, and to adjust the glue if one of those dimensions is pre-specified. The computed sizes normally enclose all of the material inside the new box; but some items may stick out if negative glue is used, if the box is overfull, or if a ``\\vbox`` includes other boxes that have been shifted left.
- *w*: specifies a width
- *m*: is either 'exactly' or 'additional'.
Thus, ``hpack(w, 'exactly')`` produces a box whose width is exactly *w*, while ``hpack(w, 'additional')`` yields a box whose width is the natural width plus *w*. The default values produce a box with the natural width. """ # I don't know why these get reset in TeX. Shift_amount is pretty # much useless if we do. #self.shift_amount = 0. glue_spec = p.glue_spec x += glue_spec.width total_stretch[glue_spec.stretch_order] += glue_spec.stretch total_shrink[glue_spec.shrink_order] += glue_spec.shrink
if x > 0.: self._set_glue(x, 1, total_stretch, "Overfull") else: self._set_glue(x, -1, total_shrink, "Underfull")
""" A vertical list of boxes. """
""" The main duty of :meth:`vpack` is to compute the dimensions of the resulting boxes, and to adjust the glue if one of those dimensions is pre-specified.
- *h*: specifies a height - *m*: is either 'exactly' or 'additional'. - *l*: a maximum height
Thus, ``vpack(h, 'exactly')`` produces a box whose height is exactly *h*, while ``vpack(h, 'additional')`` yields a box whose height is the natural height plus *h*. The default values produce a box with the natural width. """ # I don't know why these get reset in TeX. Shift_amount is pretty # much useless if we do. # self.shift_amount = 0. x += d d = 0. glue_spec = p.glue_spec x += glue_spec.width total_stretch[glue_spec.stretch_order] += glue_spec.stretch total_shrink[glue_spec.shrink_order] += glue_spec.shrink elif isinstance(p, Char): raise RuntimeError("Internal mathtext error: Char node found in Vlist.")
x += d - l self.depth = l else:
if x > 0.: self._set_glue(x, 1, total_stretch, "Overfull") else: self._set_glue(x, -1, total_shrink, "Underfull")
""" A :class:`Rule` node stands for a solid black rectangle; it has *width*, *depth*, and *height* fields just as in an :class:`Hlist`. However, if any of these dimensions is inf, the actual value will be determined by running the rule up to the boundary of the innermost enclosing box. This is called a "running dimension." The width is never running in an :class:`Hlist`; the height and depth are never running in a :class:`Vlist`. """ Box.__init__(self, width, height, depth) self.font_output = state.font_output
self.font_output.render_rect_filled(x, y, x + w, y + h)
""" Convenience class to create a horizontal rule. """ if thickness is None: thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi) height = depth = thickness * 0.5 Rule.__init__(self, np.inf, height, depth, state)
""" Convenience class to create a vertical rule. """ thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi) Rule.__init__(self, thickness, np.inf, np.inf, state)
""" Most of the information in this object is stored in the underlying :class:`GlueSpec` class, which is shared between multiple glue objects. (This is a memory optimization which probably doesn't matter anymore, but it's easier to stick to what TeX does.) """ Node.__init__(self) self.glue_subtype = 'normal' if isinstance(glue_type, str): glue_spec = GlueSpec.factory(glue_type) elif isinstance(glue_type, GlueSpec): glue_spec = glue_type else: raise ValueError("glue_type must be a glue spec name or instance") if copy: glue_spec = glue_spec.copy() self.glue_spec = glue_spec
Node.shrink(self) if self.size < NUM_SIZE_LEVELS: if self.glue_spec.width != 0.: self.glue_spec = self.glue_spec.copy() self.glue_spec.width *= SHRINK_FACTOR
Node.grow(self) if self.glue_spec.width != 0.: self.glue_spec = self.glue_spec.copy() self.glue_spec.width *= GROW_FACTOR
""" See :class:`Glue`. """
return GlueSpec( self.width, self.stretch, self.stretch_order, self.shrink, self.shrink_order)
return cls._types[glue_type]
'fil': GlueSpec(0., 1., 1, 0., 0), 'fill': GlueSpec(0., 1., 2, 0., 0), 'filll': GlueSpec(0., 1., 3, 0., 0), 'neg_fil': GlueSpec(0., 0., 0, 1., 1), 'neg_fill': GlueSpec(0., 0., 0, 1., 2), 'neg_filll': GlueSpec(0., 0., 0, 1., 3), 'empty': GlueSpec(0., 0., 0, 0., 0), 'ss': GlueSpec(0., 1., 1, -1., 1) }
# Some convenient ways to get common kinds of glue
Glue.__init__(self, 'fil')
Glue.__init__(self, 'fill')
Glue.__init__(self, 'filll')
Glue.__init__(self, 'neg_fil')
Glue.__init__(self, 'neg_fill')
Glue.__init__(self, 'neg_filll')
Glue.__init__(self, 'ss')
""" A convenience class to create an :class:`Hlist` whose contents are centered within its enclosing box. """ Hlist.__init__(self, [SsGlue()] + elements + [SsGlue()], do_kern=False)
""" A convenience class to create a :class:`Vlist` whose contents are centered within its enclosing box. """ Vlist.__init__(self, [SsGlue()] + elements + [SsGlue()])
""" A :class:`Kern` node has a width field to specify a (normally negative) amount of spacing. This spacing correction appears in horizontal lists between letters like A and V when the font designer said that it looks better to move them closer together or further apart. A kern node can also appear in a vertical list, when its *width* denotes additional spacing in the vertical direction. """
def __repr__(self): return "k%.02f" % self.width
Node.grow(self) self.width *= GROW_FACTOR
""" :class:`SubSuperCluster` is a sort of hack to get around that fact that this code do a two-pass parse like TeX. This lets us store enough information in the hlist itself, namely the nucleus, sub- and super-script, such that if another script follows that needs to be attached, it can be reconfigured on the fly. """ self.nucleus = None self.sub = None self.super = None Hlist.__init__(self, [])
""" :class:`AutoHeightChar` will create a character as close to the given height and depth as possible. When using a font with multiple height versions of some characters (such as the BaKoMa fonts), the correct glyph will be selected, otherwise this will always just return a scaled version of the glyph. """ alternatives = state.font_output.get_sized_alternatives_for_symbol( state.font, c)
xHeight = state.font_output.get_xheight( state.font, state.fontsize, state.dpi)
state = state.copy() target_total = height + depth for fontname, sym in alternatives: state.font = fontname char = Char(sym, state) # Ensure that size 0 is chosen when the text is regular sized but # with descender glyphs by subtracting 0.2 * xHeight if char.height + char.depth >= target_total - 0.2 * xHeight: break
shift = 0 if state.font != 0: if factor is None: factor = (target_total) / (char.height + char.depth) state.fontsize *= factor char = Char(sym, state)
shift = (depth - char.depth)
Hlist.__init__(self, [char]) self.shift_amount = shift
""" :class:`AutoWidthChar` will create a character as close to the given width as possible. When using a font with multiple width versions of some characters (such as the BaKoMa fonts), the correct glyph will be selected, otherwise this will always just return a scaled version of the glyph. """ alternatives = state.font_output.get_sized_alternatives_for_symbol( state.font, c)
state = state.copy() for fontname, sym in alternatives: state.font = fontname char = char_class(sym, state) if char.width >= width: break
factor = width / char.width state.fontsize *= factor char = char_class(sym, state)
Hlist.__init__(self, [char]) self.width = char.width
""" Once the boxes have been set up, this sends them to output. Since boxes can be inside of boxes inside of boxes, the main work of :class:`Ship` is done by two mutually recursive routines, :meth:`hlist_out` and :meth:`vlist_out`, which traverse the :class:`Hlist` nodes and :class:`Vlist` nodes inside of horizontal and vertical boxes. The global variables used in TeX to store state as it processes have become member variables here. """
if value < -1000000000.: return -1000000000. if value > 1000000000.: return 1000000000. return value
# node623 else: else: # p.vpack(box.height + box.depth, 'exactly') elif isinstance(p, Box): # node624 rule_height = p.height rule_depth = p.depth rule_width = p.width if np.isinf(rule_height): rule_height = box.height if np.isinf(rule_depth): rule_depth = box.depth if rule_height > 0 and rule_width > 0: self.cur_v = base_line + rule_depth p.render(self.cur_h + self.off_h, self.cur_v + self.off_v, rule_width, rule_height) self.cur_v = base_line self.cur_h += rule_width elif isinstance(p, Glue): # node625 glue_spec = p.glue_spec rule_width = glue_spec.width - cur_g if glue_sign != 0: # normal if glue_sign == 1: # stretching if glue_spec.stretch_order == glue_order: cur_glue += glue_spec.stretch cur_g = np.round(clamp(float(box.glue_set) * cur_glue)) elif glue_spec.shrink_order == glue_order: cur_glue += glue_spec.shrink cur_g = np.round(clamp(float(box.glue_set) * cur_glue)) rule_width += cur_g self.cur_h += rule_width
self.cur_v += p.height + p.depth else: else: self.vlist_out(p) elif isinstance(p, Box): rule_height = p.height rule_depth = p.depth rule_width = p.width if np.isinf(rule_width): rule_width = box.width rule_height += rule_depth if rule_height > 0 and rule_depth > 0: self.cur_v += rule_height p.render(self.cur_h + self.off_h, self.cur_v + self.off_v, rule_width, rule_height) elif isinstance(p, Glue): glue_spec = p.glue_spec rule_height = glue_spec.width - cur_g if glue_sign != 0: # normal if glue_sign == 1: # stretching if glue_spec.stretch_order == glue_order: cur_glue += glue_spec.stretch cur_g = np.round(clamp(float(box.glue_set) * cur_glue)) elif glue_spec.shrink_order == glue_order: # shrinking cur_glue += glue_spec.shrink cur_g = np.round(clamp(float(box.glue_set) * cur_glue)) rule_height += cur_g self.cur_v += rule_height elif isinstance(p, Char): raise RuntimeError("Internal mathtext error: Char node found in vlist")
############################################################################## # PARSER
""" Helper class to raise parser errors. """ def raise_error(s, loc, toks): raise ParseFatalException(s, loc, msg)
empty.setParseAction(raise_error)
""" This is the pyparsing-based parser for math expressions. It actually parses full strings *containing* math expressions, in that raw text may also appear outside of pairs of ``$``.
The grammar is based directly on that in TeX, though it cuts a few corners. """
scriptstyle=2, scriptscriptstyle=3)
+ * - \\pm \\sqcap \\rhd \\mp \\sqcup \\unlhd \\times \\vee \\unrhd \\div \\wedge \\oplus \\ast \\setminus \\ominus \\star \\wr \\otimes \\circ \\diamond \\oslash \\bullet \\bigtriangleup \\odot \\cdot \\bigtriangledown \\bigcirc \\cap \\triangleleft \\dagger \\cup \\triangleright \\ddagger \\uplus \\lhd \\amalg'''.split())
= < > : \\leq \\geq \\equiv \\models \\prec \\succ \\sim \\perp \\preceq \\succeq \\simeq \\mid \\ll \\gg \\asymp \\parallel \\subset \\supset \\approx \\bowtie \\subseteq \\supseteq \\cong \\Join \\sqsubset \\sqsupset \\neq \\smile \\sqsubseteq \\sqsupseteq \\doteq \\frown \\in \\ni \\propto \\vdash \\dashv \\dots \\dotplus \\doteqdot'''.split())
\\leftarrow \\longleftarrow \\uparrow \\Leftarrow \\Longleftarrow \\Uparrow \\rightarrow \\longrightarrow \\downarrow \\Rightarrow \\Longrightarrow \\Downarrow \\leftrightarrow \\longleftrightarrow \\updownarrow \\Leftrightarrow \\Longleftrightarrow \\Updownarrow \\mapsto \\longmapsto \\nearrow \\hookleftarrow \\hookrightarrow \\searrow \\leftharpoonup \\rightharpoonup \\swarrow \\leftharpoondown \\rightharpoondown \\nwarrow \\rightleftharpoons \\leadsto'''.split())
\sum \prod \coprod \bigcap \bigcup \bigsqcup \bigvee \bigwedge \bigodot \bigotimes \bigoplus \biguplus '''.split())
r"lim liminf limsup sup max min".split())
arccos csc ker min arcsin deg lg Pr arctan det lim sec arg dim liminf sin cos exp limsup sinh cosh gcd ln sup cot hom log tan coth inf max tanh""".split())
| \\| / \\backslash \\uparrow \\downarrow \\updownarrow \\Uparrow \\Downarrow \\Updownarrow . \\vert \\Vert \\\\|""".split())
# All forward declarations are here
# Set names on everything -- very useful for debugging
- ((p.lbrace + p.float_literal + p.rbrace) | Error(r"Expected \hspace{n}")))
unicode_range) FollowedBy(Regex("[^A-Za-z]").leaveWhitespace() | StringEnd()))
Suppress(p.bslash) + oneOf([*self._accent_map, *self._wide_accents]) - p.placeable )
Suppress(Literal(r"\frac")) - ((p.required_group + p.required_group) | Error(r"Expected \frac{num}{den}")) )
Suppress(Literal(r"\dfrac")) - ((p.required_group + p.required_group) | Error(r"Expected \dfrac{num}{den}")) )
Suppress(Literal(r"\stackrel")) - ((p.required_group + p.required_group) | Error(r"Expected \stackrel{num}{den}")) )
Suppress(Literal(r"\binom")) - ((p.required_group + p.required_group) | Error(r"Expected \binom{num}{den}")) )
Suppress(Literal(r"\genfrac")) - (((p.lbrace + Optional(p.ambi_delim | p.left_delim, default='') + p.rbrace) + (p.lbrace + Optional(p.ambi_delim | p.right_delim_safe, default='') + p.rbrace) + (p.lbrace + p.float_literal + p.rbrace) + p.simple_group + p.required_group + p.required_group) | Error(r"Expected \genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}")) )
Suppress(Literal(r"\sqrt")) - ((Optional(p.lbracket + p.int_literal + p.rbracket, default=None) + p.required_group) | Error("Expected \\sqrt{value}")) )
Suppress(Literal(r"\overline")) - (p.required_group | Error("Expected \\overline{value}")) )
Suppress(Literal(r"\operatorname")) - ((p.lbrace + ZeroOrMore(p.simple | p.unknown_symbol) + p.rbrace) | Error("Expected \\operatorname{value}")) )
# that are prefixed with an accent name work | p.accent # Must be before symbol as all accents are symbols | p.symbol # Must be third to catch all named symbols and single chars not in a group | p.c_over_c | p.function | p.group | p.frac | p.dfrac | p.stackrel | p.binom | p.genfrac | p.sqrt | p.overline | p.operatorname )
| p.customspace | p.font | p.subsuper )
(Optional(p.placeable) + OneOrMore(p.subsuperop - p.placeable) + Optional(p.apostrophe)) | (p.placeable + Optional(p.apostrophe)) | p.apostrophe )
| p.auto_delim | p.unknown_symbol # Must be last )
- ((p.left_delim | p.ambi_delim) | Error("Expected a delimiter")) + Group(ZeroOrMore(p.simple | p.auto_delim)) + Suppress(Literal(r"\right")) - ((p.right_delim | p.ambi_delim) | Error("Expected a delimiter")) )
# Set actions
""" Parse expression *s* using the given *fonts_object* for output, at the given *fontsize* and *dpi*.
Returns the parse tree of :class:`Node` instances. """ except ParseBaseException as err: raise ValueError("\n".join(["", err.line, " " * (err.column - 1) + "^", str(err)]))
# The state of the parser is maintained in a stack. Upon # entering and leaving a group { } or math/non-math, the stack # is pushed and popped accordingly. The current state always # exists in the top element of the stack. """ Stores the state of the parser.
States are pushed and popped from a stack as necessary, and the "current" state is always at the top of the stack. """
self.font_output, self.font, self.font_class, self.fontsize, self.dpi)
""" Get the current :class:`State` of the parser. """
""" Pop a :class:`State` off of the stack. """
""" Push a new :class:`State` onto the stack which is just a copy of the current state. """
# We're going into math now, so set font to 'it'
# All spaces are relative to em width state.font, rcParams['mathtext.default'], 'm', state.fontsize, state.dpi)
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'\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 }
return [self._make_space(float(toks[0]))]
except ValueError: raise ParseFatalException(s, loc, "Unknown symbol: %s" % c)
# iterate until we find previous character, needed for cases # such as ${ -2}$, $ -2$, or $ -2$. prev_char = next((c for c in s[:loc][::-1] if c != ' '), '') # Binary operators at start of string should not be spaced if (c in self._binary_operators and (len(s[:loc].split()) == 0 or prev_char == '{' or prev_char in self._left_delim)): return [char] else: return [Hlist([self._make_space(0.2), char, self._make_space(0.2)] , do_kern = True)]
# Do not space commas between brackets if c == ',': prev_char = next((c for c in s[:loc][::-1] if c != ' '), '') next_char = next((c for c in s[loc + 1:] if c != ' '), '') if prev_char == '{' and next_char == '}': return [char]
# Do not space dots as decimal separators if c == '.' and s[loc - 1].isdigit() and s[loc + 1].isdigit(): return [char] else: return [Hlist([char, self._make_space(0.2)], do_kern = True)]
c = toks[0] raise ParseFatalException(s, loc, "Unknown symbol: %s" % c)
# The first 2 entries in the tuple are (font, char, sizescale) for # the two symbols under and over. The third element is the space # (in multiples of underline height) r'AA': (('it', 'A', 1.0), (None, '\\circ', 0.5), 0.0), }
sym = toks[0] state = self.get_state() thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi)
under_desc, over_desc, space = \ self._char_over_chars.get(sym, (None, None, 0.0)) if under_desc is None: raise ParseFatalException("Error parsing symbol")
over_state = state.copy() if over_desc[0] is not None: over_state.font = over_desc[0] over_state.fontsize *= over_desc[2] over = Accent(over_desc[1], over_state)
under_state = state.copy() if under_desc[0] is not None: under_state.font = under_desc[0] under_state.fontsize *= under_desc[2] under = Char(under_desc[1], under_state)
width = max(over.width, under.width)
over_centered = HCentered([over]) over_centered.hpack(width, 'exactly')
under_centered = HCentered([under]) under_centered.hpack(width, 'exactly')
return Vlist([ over_centered, Vbox(0., thickness * space), under_centered ])
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' }
# make a lambda and call it to get the namespace right any(p.startswith(a) and a != p for a in am)] ) (set(_accent_map))
assert len(toks)==1 state = self.get_state() thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi) if len(toks[0]) != 2: raise ParseFatalException("Error parsing accent") accent, sym = toks[0] if accent in self._wide_accents: accent_box = AutoWidthChar( '\\' + accent, sym.width, state, char_class=Accent) else: accent_box = Accent(self._accent_map[accent], state) if accent == 'mathring': accent_box.shrink() accent_box.shrink() centered = HCentered([Hbox(sym.width / 4.0), accent_box]) centered.hpack(sym.width, 'exactly') return Vlist([ centered, Vbox(0., thickness * 2.0), Hlist([sym]) ])
self.push_state() state = self.get_state() state.font = 'rm' hlist = Hlist([Char(c, state) for c in toks[0]]) self.pop_state() hlist.function_name = toks[0] return hlist
self.push_state() state = self.get_state() state.font = 'rm' # Change the font of Chars, but leave Kerns alone for c in toks[0]: if isinstance(c, Char): c.font = 'rm' c._update_metrics() self.pop_state() return Hlist(toks[0])
# Deal with LaTeX-style font tokens self.get_state().font = toks[0][4:]
assert len(toks)==1 name = toks[0] self.get_state().font = name return []
elif isinstance(nucleus, Hlist) and hasattr(nucleus, 'function_name'): return nucleus.function_name in self._overunder_functions return False
return False
return False
return False
# Pick all of the apostrophes out, including first apostrophes that have # been parsed as characters napostrophes += len(tok) napostrophes += 1 else:
assert napostrophes nucleus = Hbox(0.0) else: nucleus = toks[0] # single subscript or superscript else: super = next # subscript and superscript if op1 == '_': raise ParseFatalException("Double subscript") else: raise ParseFatalException("Double superscript") else: super = next1 sub = next2 else: raise ParseFatalException( "Subscript/superscript sequence is too long. " "Use braces { } to remove ambiguity.")
state.font, state.fontsize, state.dpi) state.font, state.fontsize, state.dpi)
if super is None: super = Hlist([]) for i in range(napostrophes): super.children.extend(self.symbol(s, loc, ['\\prime'])) # kern() and hpack() needed to get the metrics right after extending super.kern() super.hpack()
# Handle over/under symbols, such as sum or integral vlist = [] shift = 0. width = nucleus.width if super is not None: super.shrink() width = max(width, super.width) if sub is not None: sub.shrink() width = max(width, sub.width)
if super is not None: hlist = HCentered([super]) hlist.hpack(width, 'exactly') vlist.extend([hlist, Kern(rule_thickness * 3.0)]) hlist = HCentered([nucleus]) hlist.hpack(width, 'exactly') vlist.append(hlist) if sub is not None: hlist = HCentered([sub]) hlist.hpack(width, 'exactly') vlist.extend([Kern(rule_thickness * 3.0), hlist]) shift = hlist.height vlist = Vlist(vlist) vlist.shift_amount = shift + nucleus.depth result = Hlist([vlist]) return [result]
# We remove kerning on the last character for consistency (otherwise it # will compute kerning based on non-shrinked characters and may put them # too close together when superscripted) # We change the width of the last character to match the advance to # consider some fonts with weird metrics: e.g. stix's f has a width of # 7.75 and a kerning of -4.0 for an advance of 3.72, and we want to put # the superscript at the advance new_children = nucleus.children if len(new_children): # remove last kern if (isinstance(new_children[-1],Kern) and hasattr(new_children[-2], '_metrics')): new_children = new_children[:-1] last_char = new_children[-1] if hasattr(last_char, '_metrics'): last_char.width = last_char._metrics.advance # create new Hlist without kerning nucleus = Hlist(new_children, do_kern=False) else:
# Handle regular sub/superscripts lc_baseline = last_char.depth
# Compute kerning for sub and super (lc_height - xHeight * 2. / 3.)) subkern = (3 * constants.delta - constants.delta_integral) * lc_height superkern = (3 * constants.delta + constants.delta_integral) * lc_height else:
# node757 shift_down = lc_baseline + constants.subdrop * xHeight else: else: shift_up = lc_height - constants.subdrop * xHeight else: x.shift_amount = -shift_up else: # Both sub and superscript shift_down = lc_baseline + constants.subdrop * xHeight else: # If sub and superscript collide, move super up ((shift_up - x.depth) - (y.height - shift_down))) shift_up += clr Kern((shift_up - x.depth) - (y.height - shift_down)), y])
state = self.get_state() thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi)
rule = float(rule)
# If style != displaystyle == 0, shrink the num and den if style != self._math_style_dict['displaystyle']: num.shrink() den.shrink() cnum = HCentered([num]) cden = HCentered([den]) width = max(num.width, den.width) cnum.hpack(width, 'exactly') cden.hpack(width, 'exactly') vlist = Vlist([cnum, # numerator Vbox(0, thickness * 2.0), # space Hrule(state, rule), # rule Vbox(0, thickness * 2.0), # space cden # denominator ])
# Shift so the fraction line sits in the middle of the # equals sign metrics = state.font_output.get_metrics( state.font, rcParams['mathtext.default'], '=', state.fontsize, state.dpi) shift = (cden.height - ((metrics.ymax + metrics.ymin) / 2 - thickness * 3.0)) vlist.shift_amount = shift
result = [Hlist([vlist, Hbox(thickness * 2.)])] if ldelim or rdelim: if ldelim == '': ldelim = '.' if rdelim == '': rdelim = '.' return self._auto_sized_delimiter(ldelim, result, rdelim) return result
assert len(toks) == 1 assert len(toks[0]) == 6
return self._genfrac(*tuple(toks[0]))
assert len(toks) == 1 assert len(toks[0]) == 2 state = self.get_state()
thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi) num, den = toks[0]
return self._genfrac('', '', thickness, self._math_style_dict['textstyle'], num, den)
assert len(toks) == 1 assert len(toks[0]) == 2 state = self.get_state()
thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi) num, den = toks[0]
return self._genfrac('', '', thickness, self._math_style_dict['displaystyle'], num, den)
assert len(toks) == 1 assert len(toks[0]) == 2 num, den = toks[0]
return self._genfrac('', '', 0.0, self._math_style_dict['textstyle'], num, den)
assert len(toks) == 1 assert len(toks[0]) == 2 num, den = toks[0]
return self._genfrac('(', ')', 0.0, self._math_style_dict['textstyle'], num, den)
root, body = toks[0] state = self.get_state() thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi)
# Determine the height of the body, and add a little extra to # the height so it doesn't seem cramped height = body.height - body.shift_amount + thickness * 5.0 depth = body.depth + body.shift_amount check = AutoHeightChar(r'\__sqrt__', height, depth, state, always=True) height = check.height - check.shift_amount depth = check.depth + check.shift_amount
# Put a little extra space to the left and right of the body padded_body = Hlist([Hbox(thickness * 2.0), body, Hbox(thickness * 2.0)]) rightside = Vlist([Hrule(state), Fill(), padded_body]) # Stretch the glue between the hrule and the body rightside.vpack(height + (state.fontsize * state.dpi) / (100.0 * 12.0), 'exactly', depth)
# Add the root and shift it upward so it is above the tick. # The value of 0.6 is a hard-coded hack ;) if root is None: root = Box(check.width * 0.5, 0., 0.) else: root = Hlist([Char(x, state) for x in root]) root.shrink() root.shrink()
root_vlist = Vlist([Hlist([root])]) root_vlist.shift_amount = -height * 0.6
hlist = Hlist([root_vlist, # Root # Negative kerning to put root over tick Kern(-check.width * 0.5), check, # Check rightside]) # Body return [hlist]
assert len(toks)==1 assert len(toks[0])==1
body = toks[0][0]
state = self.get_state() thickness = state.font_output.get_underline_thickness( state.font, state.fontsize, state.dpi)
height = body.height - body.shift_amount + thickness * 3.0 depth = body.depth + body.shift_amount
# Place overline above body rightside = Vlist([Hrule(state), Fill(), Hlist([body])])
# Stretch the glue between the hrule and the body rightside.vpack(height + (state.fontsize * state.dpi) / (100.0 * 12.0), 'exactly', depth)
hlist = Hlist([rightside]) return [hlist]
state = self.get_state() if len(middle): height = max(x.height for x in middle) depth = max(x.depth for x in middle) factor = None else: height = 0 depth = 0 factor = 1.0 parts = [] # \left. and \right. aren't supposed to produce any symbols if front != '.': parts.append(AutoHeightChar(front, height, depth, state, factor=factor)) parts.extend(middle) if back != '.': parts.append(AutoHeightChar(back, height, depth, state, factor=factor)) hlist = Hlist(parts) return hlist
front, middle, back = toks
return self._auto_sized_delimiter(front, middle.asList(), back)
###
############################################################################## # MAIN
'bitmap': MathtextBackendBitmap, 'agg' : MathtextBackendAgg, 'ps' : MathtextBackendPs, 'pdf' : MathtextBackendPdf, 'svg' : MathtextBackendSvg, 'path' : MathtextBackendPath, 'cairo' : MathtextBackendCairo, 'macosx': MathtextBackendAgg, }
'cm' : BakomaFonts, 'dejavuserif' : DejaVuSerifFonts, 'dejavusans' : DejaVuSansFonts, 'stix' : StixFonts, 'stixsans' : StixSansFonts, 'custom' : UnicodeFonts }
""" Create a MathTextParser for the given backend *output*. """
""" Parse the given math expression *s* at the given *dpi*. If *prop* is provided, it is a :class:`~matplotlib.font_manager.FontProperties` object specifying the "default" font to use in the math expression, used for all non-math text.
The results are cached, so multiple calls to :meth:`parse` with the same expression should be fast. """
prop = FontProperties()
font_output = StandardPsFonts(prop) else: else: raise ValueError( "mathtext.fontset must be either 'cm', 'dejavuserif', " "'dejavusans', 'stix', 'stixsans', or 'custom'")
# This is a class variable so we don't rebuild the parser # with each request.
""" *texstr* A valid mathtext string, e.g., r'IQ: $\\sigma_i=15$'
*dpi* The dots-per-inch to render the text
*fontsize* The font size in points
Returns a tuple (*array*, *depth*)
- *array* is an NxM uint8 alpha ubyte mask array of rasterized tex.
- depth is the offset of the baseline from the bottom of the image in pixels. """ assert self._output == "bitmap" prop = FontProperties(size=fontsize) ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
x = ftimage.as_array() return x, depth
""" *texstr* A valid mathtext string, e.g., r'IQ: $\\sigma_i=15$'
*color* Any matplotlib color argument
*dpi* The dots-per-inch to render the text
*fontsize* The font size in points
Returns a tuple (*array*, *depth*)
- *array* is an NxM uint8 alpha ubyte mask array of rasterized tex.
- depth is the offset of the baseline from the bottom of the image in pixels. """ x, depth = self.to_mask(texstr, dpi=dpi, fontsize=fontsize)
r, g, b, a = mcolors.to_rgba(color) RGBA = np.zeros((x.shape[0], x.shape[1], 4), dtype=np.uint8) RGBA[:, :, 0] = 255 * r RGBA[:, :, 1] = 255 * g RGBA[:, :, 2] = 255 * b RGBA[:, :, 3] = x return RGBA, depth
""" Writes a tex expression to a PNG file.
Returns the offset of the baseline from the bottom of the image in pixels.
*filename* A writable filename or fileobject
*texstr* A valid mathtext string, e.g., r'IQ: $\\sigma_i=15$'
*color* A valid matplotlib color argument
*dpi* The dots-per-inch to render the text
*fontsize* The font size in points
Returns the offset of the baseline from the bottom of the image in pixels. """ rgba, depth = self.to_rgba(texstr, color=color, dpi=dpi, fontsize=fontsize) _png.write_png(rgba, filename) return depth
""" Returns the offset of the baseline from the bottom of the image in pixels.
*texstr* A valid mathtext string, e.g., r'IQ: $\\sigma_i=15$'
*dpi* The dots-per-inch to render the text
*fontsize* The font size in points """ assert self._output=="bitmap" prop = FontProperties(size=fontsize) ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop) return depth
""" Given a math expression, renders it in a closely-clipped bounding box to an image file.
*s* A math expression. The math portion should be enclosed in dollar signs.
*filename_or_obj* A filepath or writable file-like object to write the image data to.
*prop* If provided, a FontProperties() object describing the size and style of the text.
*dpi* Override the output dpi, otherwise use the default associated with the output format.
*format* The output format, e.g., 'svg', 'pdf', 'ps' or 'png'. If not provided, will be deduced from the filename. """ from matplotlib import figure # backend_agg supports all of the core output formats from matplotlib.backends import backend_agg
if prop is None: prop = FontProperties()
parser = MathTextParser('path') width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop)
fig = figure.Figure(figsize=(width / 72.0, height / 72.0)) fig.text(0, depth/height, s, fontproperties=prop) backend_agg.FigureCanvasAgg(fig) fig.savefig(filename_or_obj, dpi=dpi, format=format)
return depth |