| 35 | |
| 36 | |
| 37 | class MathTextParser: |
| 38 | _parser = None |
| 39 | _font_type_mapping = { |
| 40 | 'cm': _mathtext.BakomaFonts, |
| 41 | 'dejavuserif': _mathtext.DejaVuSerifFonts, |
| 42 | 'dejavusans': _mathtext.DejaVuSansFonts, |
| 43 | 'stix': _mathtext.StixFonts, |
| 44 | 'stixsans': _mathtext.StixSansFonts, |
| 45 | 'custom': _mathtext.UnicodeFonts, |
| 46 | } |
| 47 | |
| 48 | def __init__(self, output): |
| 49 | """ |
| 50 | Create a MathTextParser for the given backend *output*. |
| 51 | |
| 52 | Parameters |
| 53 | ---------- |
| 54 | output : {"path", "agg"} |
| 55 | Whether to return a `VectorParse` ("path") or a |
| 56 | `RasterParse` ("agg", or its synonym "macosx"). |
| 57 | """ |
| 58 | self._output_type = _api.getitem_checked( |
| 59 | {"path": "vector", "agg": "raster", "macosx": "raster"}, |
| 60 | output=output.lower()) |
| 61 | |
| 62 | def parse(self, s, dpi=72, prop=None, *, antialiased=None): |
| 63 | """ |
| 64 | Parse the given math expression *s* at the given *dpi*. If *prop* is |
| 65 | provided, it is a `.FontProperties` object specifying the "default" |
| 66 | font to use in the math expression, used for all non-math text. |
| 67 | |
| 68 | The results are cached, so multiple calls to `parse` |
| 69 | with the same expression should be fast. |
| 70 | |
| 71 | Depending on the *output* type, this returns either a `VectorParse` or |
| 72 | a `RasterParse`. |
| 73 | """ |
| 74 | # lru_cache can't decorate parse() directly because prop is |
| 75 | # mutable, so we key the cache using an internal copy (see |
| 76 | # Text._get_text_metrics_with_cache for a similar case); likewise, |
| 77 | # we need to check the mutable state of the text.antialiased and |
| 78 | # text.hinting rcParams. |
| 79 | prop = prop.copy() if prop is not None else None |
| 80 | antialiased = mpl._val_or_rc(antialiased, 'text.antialiased') |
| 81 | from matplotlib.backends import backend_agg |
| 82 | load_glyph_flags = { |
| 83 | "vector": LoadFlags.NO_HINTING, |
| 84 | "raster": backend_agg.get_hinting_flag(), |
| 85 | }[self._output_type] |
| 86 | return self._parse_cached(s, dpi, prop, antialiased, load_glyph_flags) |
| 87 | |
| 88 | @functools.lru_cache(50) |
| 89 | def _parse_cached(self, s, dpi, prop, antialiased, load_glyph_flags): |
| 90 | if prop is None: |
| 91 | prop = FontProperties() |
| 92 | fontset_class = _api.getitem_checked( |
| 93 | self._font_type_mapping, fontset=prop.get_math_fontfamily()) |
| 94 | fontset = fontset_class(prop, load_glyph_flags) |
no outgoing calls
no test coverage detected
searching dependent graphs…