Skip to content

Commit cc4a8a3

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 a635671 commit cc4a8a3

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
@@ -114,12 +114,12 @@ def glyph_name_or_index(self):
114114
charmap.
115115
"""
116116
# The last section is only true on luatex since luaotfload 3.23; this
117-
# must be checked by the code generated by texmanager. (luaotfload's
118-
# docs states "No one should rely on the mapping between DVI character
119-
# codes and font glyphs [prior to v3.15] unless they tightly
120-
# control all involved versions and are deeply familiar with the
121-
# implementation", but a further mapping bug was fixed in luaotfload
122-
# commit 8f2dca4, first included in v3.23).
117+
# is checked by the code generated by texmanager. (luaotfload's docs
118+
# states "No one should rely on the mapping between DVI character codes
119+
# and font glyphs [prior to v3.15] unless they tightly control all
120+
# involved versions and are deeply familiar with the implementation",
121+
# but a further mapping bug was fixed in luaotfload commit 8f2dca4,
122+
# first included in v3.23).
123123
entry = self._get_pdftexmap_entry()
124124
return (_parse_enc(entry.encoding)[self.glyph]
125125
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
@@ -1038,6 +1038,7 @@ def _convert_validator_spec(key, conv):
10381038
# text props
10391039
"text.color": validate_color,
10401040
"text.usetex": validate_bool,
1041+
"text.latex.engine": ["latex", "xelatex", "lualatex"],
10411042
"text.latex.preamble": validate_string,
10421043
"text.hinting": ["default", "no_autohint", "force_autohint",
10431044
"no_hinting", "auto", "native", "either", "none"],

lib/matplotlib/texmanager.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,32 @@ def _get_tex_source(cls, tex, fontsize):
204204
font_preamble, fontcmd = cls._get_font_preamble_and_command()
205205
baselineskip = 1.25 * fontsize
206206
return "\n".join([
207+
rf"% !TeX program = {mpl.rcParams['text.latex.engine']}",
207208
r"\RequirePackage{fix-cm}",
208209
r"\documentclass{article}",
209210
r"% Pass-through \mathdefault, which is used in non-usetex mode",
210211
r"% to use the default text font but was historically suppressed",
211212
r"% in usetex mode.",
212213
r"\newcommand{\mathdefault}[1]{#1}",
213-
font_preamble,
214+
r"\usepackage{iftex}",
215+
r"\ifpdftex",
214216
r"\usepackage[utf8]{inputenc}",
215217
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
218+
font_preamble,
219+
r"\fi",
220+
r"\ifluatex",
221+
r"\begingroup\catcode`\%=12\relax\gdef\percent{%}\endgroup",
222+
r"\directlua{",
223+
r" v = luaotfload.version",
224+
r" major, minor = string.match(v, '(\percent d+).(\percent d+)')",
225+
r" major = tonumber(major)",
226+
r" minor = tonumber(minor) - (string.sub(v, -4) == '-dev' and .5 or 0)",
227+
r" if major < 3 or major == 3 and minor < 23 then",
228+
r" tex.error(string.format(",
229+
r" 'luaotfload>=3.23 is required; you have \percent s', v))",
230+
r" end",
231+
r"}",
232+
r"\fi",
216233
r"% geometry is loaded before the custom preamble as ",
217234
r"% convert_psfrags relies on a custom preamble to change the ",
218235
r"% geometry.",
@@ -282,7 +299,9 @@ def make_dvi(cls, tex, fontsize):
282299
Return the file name.
283300
"""
284301
basefile = cls.get_basefile(tex, fontsize)
285-
dvifile = '%s.dvi' % basefile
302+
ext = {"latex": "dvi", "xelatex": "xdv", "lualatex": "dvi"}[
303+
mpl.rcParams["text.latex.engine"]]
304+
dvifile = f"{basefile}.{ext}"
286305
if not os.path.exists(dvifile):
287306
texfile = Path(cls.make_tex(tex, fontsize))
288307
# Generate the dvi in a temporary directory to avoid race
@@ -297,8 +316,13 @@ def make_dvi(cls, tex, fontsize):
297316
cwd = Path(dvifile).parent
298317
with TemporaryDirectory(dir=cwd) as tmpdir:
299318
tmppath = Path(tmpdir)
319+
cmd = {
320+
"latex": ["latex"],
321+
"xelatex": ["xelatex", "-no-pdf"],
322+
"lualatex": ["lualatex", "--output-format=dvi"],
323+
}[mpl.rcParams["text.latex.engine"]]
300324
cls._run_checked_subprocess(
301-
["latex", "-interaction=nonstopmode", "--halt-on-error",
325+
[*cmd, "-interaction=nonstopmode", "--halt-on-error",
302326
f"--output-directory={tmppath.name}",
303327
f"{texfile.name}"], tex, cwd=cwd)
304328
(tmppath / Path(dvifile).name).replace(dvifile)

0 commit comments

Comments
 (0)