FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/lib/matplotlib/mathtext.py at v3.11.2 · matplotlib/matplotlib · GitHub
matplotlib
/
matplotlib
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
8.5k
Star
23.3k
Code
Issues
1.1k
Pull requests
430
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
mathtext.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
140 lines (114 loc) · 4.99 KB
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
mathtext.py
Copy path
File metadata and controls
140 lines (114 loc) · 4.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
r"""
A module for parsing a subset of the TeX math syntax and rendering it to a
Matplotlib backend.
For a tutorial of its usage, see :ref:`mathtext`. This
document is primarily concerned with implementation details.
The module uses pyparsing_ to parse the TeX expression.
.. _pyparsing: https://pypi.org/project/pyparsing/
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.
"""
import
functools
import
logging
import
matplotlib
as
mpl
from
matplotlib
import
_api
,
_mathtext
from
matplotlib
.
ft2font
import
LoadFlags
from
matplotlib
.
font_manager
import
FontProperties
from
.
_mathtext
import
(
# noqa: F401, reexported API
RasterParse
,
VectorParse
,
get_unicode_index
)
_log
=
logging
.
getLogger
(
__name__
)
get_unicode_index
.
__module__
=
__name__
##############################################################################
# MAIN
class
MathTextParser
:
_parser
=
None
_font_type_mapping
=
{
'cm'
:
_mathtext
.
BakomaFonts
,
'dejavuserif'
:
_mathtext
.
DejaVuSerifFonts
,
'dejavusans'
:
_mathtext
.
DejaVuSansFonts
,
'stix'
:
_mathtext
.
StixFonts
,
'stixsans'
:
_mathtext
.
StixSansFonts
,
'custom'
:
_mathtext
.
UnicodeFonts
,
}
def
__init__
(
self
,
output
):
"""
Create a MathTextParser for the given backend *output*.
Parameters
----------
output : {"path", "agg"}
Whether to return a `VectorParse` ("path") or a
`RasterParse` ("agg", or its synonym "macosx").
"""
self
.
_output_type
=
_api
.
getitem_checked
(
{
"path"
:
"vector"
,
"agg"
:
"raster"
,
"macosx"
:
"raster"
},
output
=
output
.
lower
())
def
parse
(
self
,
s
,
dpi
=
72
,
prop
=
None
,
*
,
antialiased
=
None
):
"""
Parse the given math expression *s* at the given *dpi*. If *prop* is
provided, it is a `.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 `parse`
with the same expression should be fast.
Depending on the *output* type, this returns either a `VectorParse` or
a `RasterParse`.
"""
# lru_cache can't decorate parse() directly because prop is
# mutable, so we key the cache using an internal copy (see
# Text._get_text_metrics_with_cache for a similar case); likewise,
# we need to check the mutable state of the text.antialiased and
# text.hinting rcParams.
prop
=
prop
.
copy
()
if
prop
is
not
None
else
None
antialiased
=
mpl
.
_val_or_rc
(
antialiased
,
'text.antialiased'
)
from
matplotlib
.
backends
import
backend_agg
load_glyph_flags
=
{
"vector"
:
LoadFlags
.
NO_HINTING
,
"raster"
:
backend_agg
.
get_hinting_flag
(),
}[
self
.
_output_type
]
return
self
.
_parse_cached
(
s
,
dpi
,
prop
,
antialiased
,
load_glyph_flags
)
@
functools
.
lru_cache
(
50
)
def
_parse_cached
(
self
,
s
,
dpi
,
prop
,
antialiased
,
load_glyph_flags
):
if
prop
is
None
:
prop
=
FontProperties
()
fontset_class
=
_api
.
getitem_checked
(
self
.
_font_type_mapping
,
fontset
=
prop
.
get_math_fontfamily
())
fontset
=
fontset_class
(
prop
,
load_glyph_flags
)
fontsize
=
prop
.
get_size_in_points
()
if
self
.
_parser
is
None
:
# Cache the parser globally.
self
.
__class__
.
_parser
=
_mathtext
.
Parser
()
box
=
self
.
_parser
.
parse
(
s
,
fontset
,
fontsize
,
dpi
)
output
=
_mathtext
.
ship
(
box
)
if
self
.
_output_type
==
"vector"
:
return
output
.
to_vector
()
elif
self
.
_output_type
==
"raster"
:
return
output
.
to_raster
(
antialiased
=
antialiased
)
def
math_to_image
(
s
,
filename_or_obj
,
prop
=
None
,
dpi
=
None
,
format
=
None
,
*
,
color
=
None
):
"""
Given a math expression, renders it in a closely-clipped bounding
box to an image file.
Parameters
----------
s : str
A math expression. The math portion must be enclosed in dollar signs.
filename_or_obj : str or path-like or file-like
Where to write the image data.
prop : `.FontProperties`, optional
The size and style of the text.
dpi : float, optional
The output dpi. If not set, the dpi is determined as for
`.Figure.savefig`.
format : str, optional
The output format, e.g., 'svg', 'pdf', 'ps' or 'png'. If not set, the
format is determined as for `.Figure.savefig`.
color : str, optional
Foreground color, defaults to :rc:`text.color`.
"""
from
matplotlib
import
figure
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
,
color
=
color
)
fig
.
savefig
(
filename_or_obj
,
dpi
=
dpi
,
format
=
format
)
return
depth
Back
|
FazBrowse Home
|
New Git URL