Skip to content

Commit 3403aaa

Browse files
committed
Reuse TexManager implementation in convert_psfrags.
This allows convert_psfrags to also benefit from improvements to texmanager, such as the recent addition of support for unicode minus.
1 parent 9e7a235 commit 3403aaa

File tree

1 file changed

+24
-59
lines changed

1 file changed

+24
-59
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from matplotlib.mathtext import MathTextParser
3232
from matplotlib._mathtext_data import uni2type1
3333
from matplotlib.path import Path
34+
from matplotlib.texmanager import TexManager
3435
from matplotlib.transforms import Affine2D
3536
from matplotlib.backends.backend_mixed import MixedModeRenderer
3637
from . import _backend_pdf_ps
@@ -1263,65 +1264,32 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12631264
file that includes the actual text.
12641265
"""
12651266
tmpdir = os.path.split(tmpfile)[0]
1266-
epsfile = tmpfile+'.eps'
1267-
shutil.move(tmpfile, epsfile)
1268-
latexfile = tmpfile+'.tex'
1269-
dvifile = tmpfile+'.dvi'
1270-
psfile = tmpfile+'.ps'
1271-
1272-
if orientation == 'landscape':
1273-
angle = 90
1274-
else:
1275-
angle = 0
1267+
psfile = tmpfile + '.ps'
12761268

1277-
if rcParams['text.latex.unicode']:
1278-
unicode_preamble = """\\usepackage{ucs}
1279-
\\usepackage[utf8x]{inputenc}"""
1280-
else:
1281-
unicode_preamble = ''
1282-
1283-
s = r"""\documentclass{article}
1284-
%s
1285-
%s
1286-
%s
1287-
\usepackage[
1288-
dvips, papersize={%sin,%sin}, body={%sin,%sin}, margin={0in,0in}]{geometry}
1289-
\usepackage{psfrag}
1290-
\usepackage[dvips]{graphicx}
1291-
\usepackage{color}
1292-
\pagestyle{empty}
1293-
\begin{document}
1294-
\begin{figure}
1295-
\centering
1296-
\leavevmode
1297-
%s
1298-
\includegraphics*[angle=%s]{%s}
1299-
\end{figure}
1300-
\end{document}
1301-
""" % (font_preamble, unicode_preamble, custom_preamble,
1302-
paper_width, paper_height, paper_width, paper_height,
1303-
'\n'.join(psfrags), angle, os.path.split(epsfile)[-1])
1304-
1305-
try:
1306-
pathlib.Path(latexfile).write_text(
1307-
s, encoding='utf-8' if rcParams['text.latex.unicode'] else 'ascii')
1308-
except UnicodeEncodeError:
1309-
_log.info("You are using unicode and latex, but have not enabled the "
1310-
"Matplotlib 'text.latex.unicode' rcParam.")
1311-
raise
1312-
1313-
# Replace \\ for / so latex does not think there is a function call
1314-
latexfile = latexfile.replace("\\", "/")
1315-
# Replace ~ so Latex does not think it is line break
1316-
latexfile = latexfile.replace("~", "\\string~")
1269+
with mpl.rc_context({
1270+
"text.latex.preamble":
1271+
rcParams["text.latex.preamble"] +
1272+
r"\usepackage{psfrag,color}"
1273+
r"\usepackage[dvips]{graphicx}"
1274+
r"\PassOptionsToPackage{dvips}{geometry}"}):
1275+
dvifile = TexManager().make_dvi(
1276+
r"\newgeometry{papersize={%(width)sin,%(height)sin},"
1277+
r"body={%(width)sin,%(height)sin}, margin={0in,0in}}""\n"
1278+
r"\begin{figure}"
1279+
r"\centering\leavevmode%(psfrags)s"
1280+
r"\includegraphics*[angle=%(angle)s]{%(epsfile)s}"
1281+
r"\end{figure}"
1282+
% {
1283+
"width": paper_width, "height": paper_height,
1284+
"psfrags": "\n".join(psfrags),
1285+
"angle": 90 if orientation == 'landscape' else 0,
1286+
"epsfile": pathlib.Path(tmpfile).resolve().as_posix(),
1287+
},
1288+
fontsize=10) # tex's default fontsize.
13171289

13181290
cbook._check_and_log_subprocess(
1319-
["latex", "-interaction=nonstopmode", '"%s"' % latexfile],
1291+
['dvips', '-q', '-R0', '-o', os.path.basename(psfile), dvifile],
13201292
_log, cwd=tmpdir)
1321-
cbook._check_and_log_subprocess(
1322-
['dvips', '-q', '-R0', '-o', os.path.basename(psfile),
1323-
os.path.basename(dvifile)], _log, cwd=tmpdir)
1324-
os.remove(epsfile)
13251293
shutil.move(psfile, tmpfile)
13261294

13271295
# check if the dvips created a ps in landscape paper. Somehow,
@@ -1332,10 +1300,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13321300
# information. The return value is used in pstoeps step to recover
13331301
# the correct bounding box. 2010-06-05 JJL
13341302
with open(tmpfile) as fh:
1335-
if "Landscape" in fh.read(1000):
1336-
psfrag_rotated = True
1337-
else:
1338-
psfrag_rotated = False
1303+
psfrag_rotated = "Landscape" in fh.read(1000)
13391304

13401305
if not debugPS:
13411306
for fname in glob.glob(tmpfile+'.*'):

0 commit comments

Comments
 (0)