Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9fcdb97

Browse files
committed
Support {lua,xe}tex as alternative usetex engine.
Currently, this PR is mostly a proof of concept; only the svg backendis supported (under rcParams["svg.fonttype"] = "none", the default).However, there is a companion branch on the mplcairo repository, alsonamed "luadvi", which implements support for all output formats.Example (requiring both this PR, and mplcairo installed from its luadvibranch):```import matplotlib as mpl; mpl.use("module://mplcairo.qt")from matplotlib import pyplot as pltplt.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 parent9b4233b commit9fcdb97

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
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 bechecked by the code generated by texmanager. (luaotfload's
118-
#docsstates "No one should rely on the mapping between DVI character
119-
#codesand font glyphs [prior to v3.15] unless they tightly
120-
#control allinvolved 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+
#ischecked 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
ifentry.encodingisnotNoneelseself.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: 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
ifnotdvipath.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
withTemporaryDirectory(dir=dvipath.parent)astmpdir:
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)

[8]ページ先頭

©2009-2025 Movatter.jp