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

Skip to content

Commit 76ecd02

Browse files
committed
Pass explicit font paths to fontspec in backend_pgf.
1 parent 74b6913 commit 76ecd02

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import weakref
1818

1919
import matplotlib as mpl
20-
from matplotlib import _png, rcParams
20+
from matplotlib import _png, font_manager as fm, rcParams
2121
from matplotlib.backend_bases import (
2222
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
2323
RendererBase)
@@ -28,27 +28,6 @@
2828

2929
###############################################################################
3030

31-
# create a list of system fonts, all of these should work with xe/lua-latex
32-
system_fonts = []
33-
if sys.platform.startswith('win'):
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-
5231

5332
def get_texcommand():
5433
"""Get chosen TeX system from rc."""
@@ -66,17 +45,17 @@ def get_fontspec():
6645
latex_fontspec.append("\\usepackage{fontspec}")
6746

6847
if texcommand != "pdflatex" and rcParams["pgf.rcfonts"]:
69-
# try to find fonts from rc parameters
70-
families = ["serif", "sans-serif", "monospace"]
71-
fontspecs = [r"\setmainfont{%s}", r"\setsansfont{%s}",
72-
r"\setmonofont{%s}"]
73-
for family, fontspec in zip(families, fontspecs):
74-
matches = [f for f in rcParams["font." + family]
75-
if f in system_fonts]
76-
if matches:
77-
latex_fontspec.append(fontspec % matches[0])
78-
else:
79-
pass # no fonts found, fallback to LaTeX defaule
48+
families = ["serif", "sans\\-serif", "monospace"]
49+
commands = ["setmainfont", "setsansfont", "setmonofont"]
50+
for family, command in zip(families, commands):
51+
dirname, basename = os.path.split(fm.findfont(family))
52+
# 1) Forward slashes also work on Windows, so don't mess with
53+
# backslashes. 2) The dirname needs to include a separator.
54+
if sys.platform == "win32":
55+
dirname = dirname.replace("\\", "/")
56+
dirname += "/"
57+
latex_fontspec.append(
58+
r"\%s{%s}[Path=%s]" % (command, basename, dirname))
8059

8160
return "\n".join(latex_fontspec)
8261

0 commit comments

Comments
 (0)