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

Skip to content

Commit 7fc9917

Browse files
committed
Support {lua,xe}tex as alternative usetex engine.
Currently, this PR is mostly a proof of concept; only the svg backend is supported (under rcParams["svg.fonttype"] = "none", the default). However, there is a companion branch on the mplcairo repository, also named "luadvi", which implements support for all output formats. Example (requiring both this PR, and mplcairo installed from its luadvi branch): ``` import matplotlib as mpl; mpl.use("module://mplcairo.qt") from matplotlib import pyplot as plt plt.rcParams["text.latex.engine"] = "lualatex" # or "xelatex" plt.rcParams["text.latex.preamble"] = ( # {lua,xe}tex can use any font installed on the system, spec'd using its # "normal" name. Try e.g. DejaVu Sans instead. r"\usepackage{fontspec}\setmainfont{TeX Gyre Pagella}") plt.figtext(.5, .5, r"\textrm{gff\textwon}", usetex=True) plt.show() ``` Font effects are supported by mplcairo, e.g. `\fontspec{DejaVu Sans}[FakeSlant=0.2] abc`. TODO: - Fix many likely remaining bugs. - Rework font selection in texmanager, which is currently very ad-hoc due to the limited number of fonts supported by latex. - Implement rendering support in the (other) builtin backends. In particular, the Agg (and, if we care, cairo) backend will require significant reworking because dvipng, currently used to rasterize dvi to png, doesn't support luatex-generated dvi; instead we will need to proceed as with the other backends, reading the glyphs one at a time from the dvi file and rasterizing them one at a time to the output buffer. Working on the other backends is not very high on my priority list (as I already have mplcairo as playground...) so it would be nice if others showed some interest for it :-)
1 parent 6a177bd commit 7fc9917

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

lib/matplotlib/dviread.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ def glyph_name_or_index(self):
109109
charmap.
110110
"""
111111
# The last section is only true on luatex since luaotfload 3.23; this
112-
# must be checked by the code generated by texmanager. (luaotfload's
113-
# docs states "No one should rely on the mapping between DVI character
114-
# codes and font glyphs [prior to v3.15] unless they tightly
115-
# control all involved versions and are deeply familiar with the
116-
# implementation", but a further mapping bug was fixed in luaotfload
117-
# commit 8f2dca4, first included in v3.23).
112+
# is checked by the code generated by texmanager. (luaotfload's docs
113+
# states "No one should rely on the mapping between DVI character codes
114+
# and font glyphs [prior to v3.15] unless they tightly control all
115+
# involved versions and are deeply familiar with the implementation",
116+
# but a further mapping bug was fixed in luaotfload commit 8f2dca4,
117+
# first included in v3.23).
118118
entry = self._get_pdftexmap_entry()
119119
return (_parse_enc(entry.encoding)[self.glyph]
120120
if entry.encoding is not None else self.glyph)

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@
327327
# zapf chancery, charter, serif, sans-serif, helvetica,
328328
# avant garde, courier, monospace, computer modern roman,
329329
# computer modern sans serif, computer modern typewriter
330+
#text.latex.engine: latex
330331
#text.latex.preamble: # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
331332
# AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
332333
# IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ def _convert_validator_spec(key, conv):
10511051
# text props
10521052
"text.color": validate_color,
10531053
"text.usetex": validate_bool,
1054+
"text.latex.engine": ["latex", "xelatex", "lualatex"],
10541055
"text.latex.preamble": validate_string,
10551056
"text.hinting": ["default", "no_autohint", "force_autohint",
10561057
"no_hinting", "auto", "native", "either", "none"],

lib/matplotlib/texmanager.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,31 @@ def _get_tex_source(cls, tex, fontsize):
200200
font_preamble, fontcmd = cls._get_font_preamble_and_command()
201201
baselineskip = 1.25 * fontsize
202202
return "\n".join([
203+
rf"% !TeX program = {mpl.rcParams['text.latex.engine']}",
203204
r"\documentclass{article}",
204205
r"% Pass-through \mathdefault, which is used in non-usetex mode",
205206
r"% to use the default text font but was historically suppressed",
206207
r"% in usetex mode.",
207208
r"\newcommand{\mathdefault}[1]{#1}",
208-
font_preamble,
209+
r"\usepackage{iftex}",
210+
r"\ifpdftex",
209211
r"\usepackage[utf8]{inputenc}",
210212
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
213+
font_preamble,
214+
r"\fi",
215+
r"\ifluatex",
216+
r"\begingroup\catcode`\%=12\relax\gdef\percent{%}\endgroup",
217+
r"\directlua{",
218+
r" v = luaotfload.version",
219+
r" major, minor = string.match(v, '(\percent d+).(\percent d+)')",
220+
r" major = tonumber(major)",
221+
r" minor = tonumber(minor) - (string.sub(v, -4) == '-dev' and .5 or 0)",
222+
r" if major < 3 or major == 3 and minor < 23 then",
223+
r" tex.error(string.format(",
224+
r" 'luaotfload>=3.23 is required; you have \percent s', v))",
225+
r" end",
226+
r"}",
227+
r"\fi",
211228
r"% geometry is loaded before the custom preamble as ",
212229
r"% convert_psfrags relies on a custom preamble to change the ",
213230
r"% geometry.",
@@ -277,7 +294,9 @@ def make_dvi(cls, tex, fontsize):
277294
Return the file name.
278295
"""
279296
basefile = cls.get_basefile(tex, fontsize)
280-
dvifile = '%s.dvi' % basefile
297+
ext = {"latex": "dvi", "xelatex": "xdv", "lualatex": "dvi"}[
298+
mpl.rcParams["text.latex.engine"]]
299+
dvifile = f"{basefile}.{ext}"
281300
if not os.path.exists(dvifile):
282301
texfile = Path(cls.make_tex(tex, fontsize))
283302
# Generate the dvi in a temporary directory to avoid race
@@ -292,8 +311,13 @@ def make_dvi(cls, tex, fontsize):
292311
cwd = Path(dvifile).parent
293312
with TemporaryDirectory(dir=cwd) as tmpdir:
294313
tmppath = Path(tmpdir)
314+
cmd = {
315+
"latex": ["latex"],
316+
"xelatex": ["xelatex", "-no-pdf"],
317+
"lualatex": ["lualatex", "--output-format=dvi"],
318+
}[mpl.rcParams["text.latex.engine"]]
295319
cls._run_checked_subprocess(
296-
["latex", "-interaction=nonstopmode", "--halt-on-error",
320+
[*cmd, "-interaction=nonstopmode", "--halt-on-error",
297321
f"--output-directory={tmppath.name}",
298322
f"{texfile.name}"], tex, cwd=cwd)
299323
(tmppath / Path(dvifile).name).replace(dvifile)

0 commit comments

Comments
 (0)