Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
matplotlib/lib/matplotlib/mathtext.py at v3.8.1 · matplotlib/matplotlib · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
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.2k
Code
Issues
1.1k
Pull requests
423
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
v3.8.1
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
mathtext.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
140 lines (112 loc) · 4.83 KB
v3.8.1
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
mathtext.py
Copy path
Top
File metadata and controls
Code
Blame
140 lines (112 loc) · 4.83 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
LOAD_NO_HINTING
from
matplotlib
.
font_manager
import
FontProperties
from
.
_mathtext
import
(
# noqa: 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
.
check_getitem
(
{
"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; key the cache using an internal copy (see
# text._get_text_metrics_with_cache for a similar case).
prop
=
prop
.
copy
()
if
prop
is
not
None
else
None
antialiased
=
mpl
.
_val_or_rc
(
antialiased
,
'text.antialiased'
)
return
self
.
_parse_cached
(
s
,
dpi
,
prop
,
antialiased
)
@
functools
.
lru_cache
(
50
)
def
_parse_cached
(
self
,
s
,
dpi
,
prop
,
antialiased
):
from
matplotlib
.
backends
import
backend_agg
if
prop
is
None
:
prop
=
FontProperties
()
fontset_class
=
_api
.
check_getitem
(
self
.
_font_type_mapping
,
fontset
=
prop
.
get_math_fontfamily
())
load_glyph_flags
=
{
"vector"
:
LOAD_NO_HINTING
,
"raster"
:
backend_agg
.
get_hinting_flag
(),
}[
self
.
_output_type
]
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
You can’t perform that action at this time.