Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ def glyph_name_or_index(self):
charmap.
"""
# The last section is only true on luatex since luaotfload 3.23; this
# must be checked by the code generated by texmanager. (luaotfload's
# docs states "No one should rely on the mapping between DVI character
# codes and font glyphs [prior to v3.15] unless they tightly
# control all involved versions and are deeply familiar with the
# implementation", but a further mapping bug was fixed in luaotfload
# commit 8f2dca4, first included in v3.23).
# is checked by the code generated by texmanager. (luaotfload's docs
# states "No one should rely on the mapping between DVI character codes
# and font glyphs [prior to v3.15] unless they tightly control all
# involved versions and are deeply familiar with the implementation",
# but a further mapping bug was fixed in luaotfload commit 8f2dca4,
# first included in v3.23).
entry = self._get_pdftexmap_entry()
return (_parse_enc(entry.encoding)[self.glyph]
if entry.encoding is not None else self.glyph)
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@
# zapf chancery, charter, serif, sans-serif, helvetica,
# avant garde, courier, monospace, computer modern roman,
# computer modern sans serif, computer modern typewriter
#text.latex.engine: latex
#text.latex.preamble: # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
# AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
# IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ def _convert_validator_spec(key, conv):
# text props
"text.color": validate_color,
"text.usetex": validate_bool,
"text.latex.engine": ["latex", "xelatex", "lualatex"],
"text.latex.preamble": validate_string,
"text.hinting": ["default", "no_autohint", "force_autohint",
"no_hinting", "auto", "native", "either", "none"],
Expand Down
32 changes: 28 additions & 4 deletions lib/matplotlib/texmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,32 @@ def _get_tex_source(cls, tex, fontsize):
font_preamble, fontcmd = cls._get_font_preamble_and_command()
baselineskip = 1.25 * fontsize
return "\n".join([
rf"% !TeX program = {mpl.rcParams['text.latex.engine']}",
r"\RequirePackage{fix-cm}",
r"\documentclass{article}",
r"% Pass-through \mathdefault, which is used in non-usetex mode",
r"% to use the default text font but was historically suppressed",
r"% in usetex mode.",
r"\newcommand{\mathdefault}[1]{#1}",
font_preamble,
r"\usepackage{iftex}",
r"\ifpdftex",
r"\usepackage[utf8]{inputenc}",
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
font_preamble,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this would be a behaviour change, wouldn't it? I don't recall which usepackage wins though. Also, do we not want to allow a preamble for other engines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think moving the font preamble after inputenc & DeclareUnicodeCharacter would really make a difference for latex, and there's no behaviour change for xe/lualatex as these weren't supported before.

However, it is correct that there's a bit of behavior design that needs to be decided here, which is what is the default font for xe/lualatex? Right now the behavior for latex is: pick one of the few supported fonts (PSNFSS) that more or less matches the rcParams settings, defaulting to computer modern serif (as the default rcParam, DejaVu Sans, is not supported). Note that the Text object's FontProperties is not used (except for the fontsize).

For xel/lualatex, right now this PR does the same thing, and we could very well just document "set rcParams["text.latex.preamble"] = r"\usepackage{fontspec}\setmainfont{...}" to pick whatever font with xe/lualatex". Alternatively, a perhaps better option would be to actually use the Text object's FontProperties (which would need to be threaded down to TexManager, but that seems likely doable) and draw text using the standardly configured font (similarly to rcParams["pgf.rcfonts"] = True for the pgf backend). In that case, usetex with xe/lualatex would not use computer modern, unless the Text's FontProperties say so.

My preference would definitely be for the latter case (just use whatever fonts are configured using the normal mechanism), but I deferred this discussion (and implementation) until the preliminary parts of the work (dvi parsing) got in. Thoughts?

r"\fi",
r"\ifluatex",
r"\begingroup\catcode`\%=12\relax\gdef\percent{%}\endgroup",
r"\directlua{",
r" v = luaotfload.version",
r" major, minor = string.match(v, '(\percent d+).(\percent d+)')",
r" major = tonumber(major)",
r" minor = tonumber(minor) - (string.sub(v, -4) == '-dev' and .5 or 0)",
r" if major < 3 or major == 3 and minor < 23 then",
r" tex.error(string.format(",
r" 'luaotfload>=3.23 is required; you have \percent s', v))",
r" end",
r"}",
r"\fi",
r"% geometry is loaded before the custom preamble as ",
r"% convert_psfrags relies on a custom preamble to change the ",
r"% geometry.",
Expand Down Expand Up @@ -284,7 +301,9 @@ def make_dvi(cls, tex, fontsize):

Return the file name.
"""
dvipath = cls._get_base_path(tex, fontsize).with_suffix(".dvi")
ext = {"latex": "dvi", "xelatex": "xdv", "lualatex": "dvi"}[
mpl.rcParams["text.latex.engine"]]
dvipath = cls._get_base_path(tex, fontsize).with_suffix(f".{ext}")
if not dvipath.exists():
# Generate the tex and dvi in a temporary directory to avoid race
# conditions e.g. if multiple processes try to process the same tex
Expand All @@ -298,10 +317,15 @@ def make_dvi(cls, tex, fontsize):
with TemporaryDirectory(dir=dvipath.parent) as tmpdir:
Path(tmpdir, "file.tex").write_text(
cls._get_tex_source(tex, fontsize), encoding='utf-8')
cmd = {
"latex": ["latex"],
"xelatex": ["xelatex", "-no-pdf"],
"lualatex": ["lualatex", "--output-format=dvi"],
}[mpl.rcParams["text.latex.engine"]]
cls._run_checked_subprocess(
["latex", "-interaction=nonstopmode", "--halt-on-error",
[*cmd, "-interaction=nonstopmode", "--halt-on-error",
"file.tex"], tex, cwd=tmpdir)
Path(tmpdir, "file.dvi").replace(dvipath)
Path(tmpdir, f"file.{ext}").replace(dvipath)
# Also move the tex source to the main cache directory, but
# only for backcompat.
Path(tmpdir, "file.tex").replace(dvipath.with_suffix(".tex"))
Expand Down
Loading