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

Skip to content

Commit 7eb0043

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 364c027 commit 7eb0043

4 files changed

Lines changed: 38 additions & 14 deletions

File tree

lib/matplotlib/dviread.py

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

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@
333333
# computer modern sans serif, computer modern typewriter
334334

335335
## The TeX engine/format to use. The following values are supported:
336-
## - "latex": The classic TeX engine (the current default). All backends render
337-
## TeX's output by parsing the DVI output into glyphs and boxes and emitting
338-
## those one by one.
336+
## - "latex", "xelatex", "lualatex": The corresponding TeX engines. All
337+
## backends render TeX's output by parsing the DVI output into glyphs and
338+
## boxes and emitting those one by one.
339339
## - "latex+dvipng": The same as "latex", with the exception that Agg-based
340340
## backends rely on dvipng to rasterize TeX's output. This value was the
341341
## default up to Matplotlib 3.10.

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ def _convert_validator_spec(key, conv):
10741074
# text props
10751075
"text.color": validate_color,
10761076
"text.usetex": validate_bool,
1077-
"text.latex.engine": ["latex", "latex+dvipng"],
1077+
"text.latex.engine": ["latex", "latex+dvipng", "xelatex", "lualatex"],
10781078
"text.latex.preamble": validate_string,
10791079
"text.hinting": ["default", "no_autohint", "force_autohint",
10801080
"no_hinting", "auto", "native", "either", "none"],

lib/matplotlib/texmanager.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,32 @@ def _get_tex_source(cls, tex, fontsize):
210210
font_preamble, fontcmd = cls._get_font_preamble_and_command()
211211
baselineskip = 1.25 * fontsize
212212
return "\n".join([
213+
rf"% !TeX program = {mpl.rcParams['text.latex.engine']}",
213214
r"\RequirePackage{fix-cm}",
214215
r"\documentclass{article}",
215216
r"% Pass-through \mathdefault, which is used in non-usetex mode",
216217
r"% to use the default text font but was historically suppressed",
217218
r"% in usetex mode.",
218219
r"\newcommand{\mathdefault}[1]{#1}",
219-
font_preamble,
220+
r"\usepackage{iftex}",
221+
r"\ifpdftex",
220222
r"\usepackage[utf8]{inputenc}",
221223
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
224+
font_preamble,
225+
r"\fi",
226+
r"\ifluatex",
227+
r"\begingroup\catcode`\%=12\relax\gdef\percent{%}\endgroup",
228+
r"\directlua{",
229+
r" v = luaotfload.version",
230+
r" major, minor = string.match(v, '(\percent d+).(\percent d+)')",
231+
r" major = tonumber(major)",
232+
r" minor = tonumber(minor) - (string.sub(v, -4) == '-dev' and .5 or 0)",
233+
r" if major < 3 or major == 3 and minor < 23 then",
234+
r" tex.error(string.format(",
235+
r" 'luaotfload>=3.23 is required; you have \percent s', v))",
236+
r" end",
237+
r"}",
238+
r"\fi",
222239
r"% geometry is loaded before the custom preamble as ",
223240
r"% convert_psfrags relies on a custom preamble to change the ",
224241
r"% geometry.",
@@ -284,7 +301,9 @@ def make_dvi(cls, tex, fontsize):
284301
285302
Return the file name.
286303
"""
287-
dvipath = cls._get_base_path(tex, fontsize).with_suffix(".dvi")
304+
ext = {"latex": "dvi", "xelatex": "xdv", "lualatex": "dvi"}[
305+
mpl.rcParams["text.latex.engine"]]
306+
dvipath = cls._get_base_path(tex, fontsize).with_suffix(f".{ext}")
288307
if not dvipath.exists():
289308
# Generate the tex and dvi in a temporary directory to avoid race
290309
# conditions e.g. if multiple processes try to process the same tex
@@ -298,10 +317,15 @@ def make_dvi(cls, tex, fontsize):
298317
with TemporaryDirectory(dir=dvipath.parent) as tmpdir:
299318
Path(tmpdir, "file.tex").write_text(
300319
cls._get_tex_source(tex, fontsize), encoding='utf-8')
320+
cmd = {
321+
"latex": ["latex"],
322+
"xelatex": ["xelatex", "-no-pdf"],
323+
"lualatex": ["lualatex", "--output-format=dvi"],
324+
}[mpl.rcParams["text.latex.engine"]]
301325
cls._run_checked_subprocess(
302-
["latex", "-interaction=nonstopmode", "--halt-on-error",
326+
[*cmd, "-interaction=nonstopmode", "--halt-on-error",
303327
"file.tex"], tex, cwd=tmpdir)
304-
Path(tmpdir, "file.dvi").replace(dvipath)
328+
Path(tmpdir, f"file.{ext}").replace(dvipath)
305329
# Also move the tex source to the main cache directory, but
306330
# only for backcompat.
307331
Path(tmpdir, "file.tex").replace(dvipath.with_suffix(".tex"))

0 commit comments

Comments
 (0)