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

Skip to content

Commit 200094d

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 200094d

1 file changed

Lines changed: 28 additions & 70 deletions

File tree

lib/matplotlib/backends/backend_ps.py

Lines changed: 28 additions & 70 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
@@ -1262,67 +1263,32 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12621263
commands to convert those tags to text. LaTeX/dvips produces the postscript
12631264
file that includes the actual text.
12641265
"""
1265-
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
1276-
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~")
1317-
1318-
cbook._check_and_log_subprocess(
1319-
["latex", "-interaction=nonstopmode", '"%s"' % latexfile],
1320-
_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)
1325-
shutil.move(psfile, tmpfile)
1266+
with mpl.rc_context({
1267+
"text.latex.preamble":
1268+
rcParams["text.latex.preamble"] +
1269+
r"\usepackage{psfrag,color}"
1270+
r"\usepackage[dvips]{graphicx}"
1271+
r"\PassOptionsToPackage{dvips}{geometry}"}):
1272+
dvifile = TexManager().make_dvi(
1273+
r"\newgeometry{papersize={%(width)sin,%(height)sin},"
1274+
r"body={%(width)sin,%(height)sin}, margin={0in,0in}}""\n"
1275+
r"\begin{figure}"
1276+
r"\centering\leavevmode%(psfrags)s"
1277+
r"\includegraphics*[angle=%(angle)s]{%(epsfile)s}"
1278+
r"\end{figure}"
1279+
% {
1280+
"width": paper_width, "height": paper_height,
1281+
"psfrags": "\n".join(psfrags),
1282+
"angle": 90 if orientation == 'landscape' else 0,
1283+
"epsfile": pathlib.Path(tmpfile).resolve().as_posix(),
1284+
},
1285+
fontsize=10) # tex's default fontsize.
1286+
1287+
with TemporaryDirectory() as tmpdir:
1288+
psfile = os.path.join(tmpdir, "tmp.ps")
1289+
cbook._check_and_log_subprocess(
1290+
['dvips', '-q', '-R0', '-o', psfile, dvifile], _log)
1291+
shutil.move(psfile, tmpfile)
13261292

13271293
# check if the dvips created a ps in landscape paper. Somehow,
13281294
# above latex+dvips results in a ps file in a landscape mode for a
@@ -1332,15 +1298,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13321298
# information. The return value is used in pstoeps step to recover
13331299
# the correct bounding box. 2010-06-05 JJL
13341300
with open(tmpfile) as fh:
1335-
if "Landscape" in fh.read(1000):
1336-
psfrag_rotated = True
1337-
else:
1338-
psfrag_rotated = False
1339-
1340-
if not debugPS:
1341-
for fname in glob.glob(tmpfile+'.*'):
1342-
os.remove(fname)
1343-
1301+
psfrag_rotated = "Landscape" in fh.read(1000)
13441302
return psfrag_rotated
13451303

13461304

0 commit comments

Comments
 (0)