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

Skip to content

Commit b002924

Browse files
committed
Pass explicit font paths to fontspec in backend_pgf.
1 parent e22a16a commit b002924

File tree

2 files changed

+17
-38
lines changed

2 files changed

+17
-38
lines changed

doc/api/next_api_changes/2018-02-15-AL-deprecations.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following modules are deprecated:
1111
The following classes, methods, functions, and attributes are deprecated:
1212

1313
- ``afm.parse_afm``,
14+
- ``backend_pgf.get_texcommand``,
1415
- ``backend_qt5.error_msg_qt``, ``backend_qt5.exception_handler``,
1516
- ``backend_wx.FigureCanvasWx.macros``,
1617
- ``cbook.GetRealpathAndStat``, ``cbook.Locked``,

lib/matplotlib/backends/backend_pgf.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import math
66
import os
7+
from pathlib import Path
78
import re
89
import shutil
910
import subprocess
@@ -13,7 +14,7 @@
1314
import weakref
1415

1516
import matplotlib as mpl
16-
from matplotlib import _png, rcParams, __version__
17+
from matplotlib import _png, cbook, font_manager as fm, __version__, rcParams
1718
from matplotlib.backend_bases import (
1819
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
1920
RendererBase)
@@ -28,33 +29,13 @@
2829

2930
###############################################################################
3031

31-
# create a list of system fonts, all of these should work with xe/lua-latex
32-
system_fonts = []
33-
if sys.platform == 'win32':
34-
from matplotlib import font_manager
35-
for f in font_manager.win32InstalledFonts():
36-
try:
37-
system_fonts.append(font_manager.get_font(str(f)).family_name)
38-
except:
39-
pass # unknown error, skip this font
40-
else:
41-
# assuming fontconfig is installed and the command 'fc-list' exists
42-
try:
43-
# list scalable (non-bitmap) fonts
44-
fc_list = subprocess.check_output(
45-
['fc-list', ':outline,scalable', 'family'])
46-
fc_list = fc_list.decode('utf8')
47-
system_fonts = [f.split(',')[0] for f in fc_list.splitlines()]
48-
system_fonts = list(set(system_fonts))
49-
except:
50-
warnings.warn('error getting fonts from fc-list', UserWarning)
51-
5232

5333
_luatex_version_re = re.compile(
5434
r'This is LuaTeX, Version (?:beta-)?([0-9]+)\.([0-9]+)\.([0-9]+)'
5535
)
5636

5737

38+
@cbook.deprecated("3.0")
5839
def get_texcommand():
5940
"""Get chosen TeX system from rc."""
6041
texsystem_options = ["xelatex", "lualatex", "pdflatex"]
@@ -77,23 +58,20 @@ def _parse_lualatex_version(output):
7758
def get_fontspec():
7859
"""Build fontspec preamble from rc."""
7960
latex_fontspec = []
80-
texcommand = get_texcommand()
61+
texcommand = rcParams["pgf.texsystem"]
8162

8263
if texcommand != "pdflatex":
8364
latex_fontspec.append("\\usepackage{fontspec}")
8465

8566
if texcommand != "pdflatex" and rcParams["pgf.rcfonts"]:
86-
# try to find fonts from rc parameters
87-
families = ["serif", "sans-serif", "monospace"]
88-
fontspecs = [r"\setmainfont{%s}", r"\setsansfont{%s}",
89-
r"\setmonofont{%s}"]
90-
for family, fontspec in zip(families, fontspecs):
91-
matches = [f for f in rcParams["font." + family]
92-
if f in system_fonts]
93-
if matches:
94-
latex_fontspec.append(fontspec % matches[0])
95-
else:
96-
pass # no fonts found, fallback to LaTeX defaule
67+
families = ["serif", "sans\\-serif", "monospace"]
68+
commands = ["setmainfont", "setsansfont", "setmonofont"]
69+
for family, command in zip(families, commands):
70+
# 1) Forward slashes also work on Windows, so don't mess with
71+
# backslashes. 2) The dirname needs to include a separator.
72+
dirname = Path(fm.findfont(family)).parent.as_posix() + "/"
73+
latex_fontspec.append(
74+
r"\%s{%s}[Path=%s]" % (command, basename, dirname))
9775

9876
return "\n".join(latex_fontspec)
9977

@@ -163,7 +141,7 @@ def _font_properties_str(prop):
163141
family = prop.get_family()[0]
164142
if family in families:
165143
commands.append(families[family])
166-
elif family in system_fonts and get_texcommand() != "pdflatex":
144+
elif family in system_fonts and rcParams["pgf.texsystem"] != "pdflatex":
167145
commands.append(r"\setmainfont{%s}\rmfamily" % family)
168146
else:
169147
pass # print warning?
@@ -232,7 +210,7 @@ class LatexManagerFactory:
232210

233211
@staticmethod
234212
def get_latex_manager():
235-
texcommand = get_texcommand()
213+
texcommand = rcParams["pgf.texsystem"]
236214
latex_header = LatexManager._build_latex_header()
237215
prev = LatexManagerFactory.previous_instance
238216

@@ -307,7 +285,7 @@ def __init__(self):
307285
LatexManager._unclean_instances.add(self)
308286

309287
# test the LaTeX setup to ensure a clean startup of the subprocess
310-
self.texcommand = get_texcommand()
288+
self.texcommand = rcParams["pgf.texsystem"]
311289
self.latex_header = LatexManager._build_latex_header()
312290
latex_end = "\n\\makeatletter\n\\@@end\n"
313291
try:
@@ -909,7 +887,7 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
909887
with codecs.open(fname_tex, "w", "utf-8") as fh_tex:
910888
fh_tex.write(latexcode)
911889

912-
texcommand = get_texcommand()
890+
texcommand = rcParams["pgf.texsystem"]
913891
cmdargs = [texcommand, "-interaction=nonstopmode",
914892
"-halt-on-error", "figure.tex"]
915893
try:

0 commit comments

Comments
 (0)