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

Skip to content

Pass explicit font paths to fontspec in backend_pgf. #10339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/next_api_changes/2018-02-15-AL-deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The following modules are deprecated:
The following classes, methods, functions, and attributes are deprecated:

- ``afm.parse_afm``,
- ``backend_pgf.get_texcommand``,
- ``backend_qt5.error_msg_qt``, ``backend_qt5.exception_handler``,
- ``backend_wx.FigureCanvasWx.macros``,
- ``cbook.GetRealpathAndStat``, ``cbook.Locked``,
Expand Down
54 changes: 16 additions & 38 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import math
import os
from pathlib import Path
import re
import shutil
import subprocess
Expand All @@ -13,7 +14,7 @@
import weakref

import matplotlib as mpl
from matplotlib import _png, rcParams, __version__
from matplotlib import _png, cbook, font_manager as fm, __version__, rcParams
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
RendererBase)
Expand All @@ -28,33 +29,13 @@

###############################################################################

# create a list of system fonts, all of these should work with xe/lua-latex
system_fonts = []
if sys.platform == 'win32':
from matplotlib import font_manager
for f in font_manager.win32InstalledFonts():
try:
system_fonts.append(font_manager.get_font(str(f)).family_name)
except:
pass # unknown error, skip this font
else:
# assuming fontconfig is installed and the command 'fc-list' exists
try:
# list scalable (non-bitmap) fonts
fc_list = subprocess.check_output(
['fc-list', ':outline,scalable', 'family'])
fc_list = fc_list.decode('utf8')
system_fonts = [f.split(',')[0] for f in fc_list.splitlines()]
system_fonts = list(set(system_fonts))
except:
warnings.warn('error getting fonts from fc-list', UserWarning)


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


@cbook.deprecated("3.0")
def get_texcommand():
"""Get chosen TeX system from rc."""
texsystem_options = ["xelatex", "lualatex", "pdflatex"]
Expand All @@ -77,23 +58,20 @@ def _parse_lualatex_version(output):
def get_fontspec():
"""Build fontspec preamble from rc."""
latex_fontspec = []
texcommand = get_texcommand()
texcommand = rcParams["pgf.texsystem"]
Copy link
Member

@timhoffm timhoffm May 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we explicitly not want the check and fallback mechanism to xelatex, which was implemented in get_texcommand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does already get checked during standard rcparam validation.


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

if texcommand != "pdflatex" and rcParams["pgf.rcfonts"]:
# try to find fonts from rc parameters
families = ["serif", "sans-serif", "monospace"]
fontspecs = [r"\setmainfont{%s}", r"\setsansfont{%s}",
r"\setmonofont{%s}"]
for family, fontspec in zip(families, fontspecs):
matches = [f for f in rcParams["font." + family]
if f in system_fonts]
if matches:
latex_fontspec.append(fontspec % matches[0])
else:
pass # no fonts found, fallback to LaTeX defaule
families = ["serif", "sans\\-serif", "monospace"]
commands = ["setmainfont", "setsansfont", "setmonofont"]
for family, command in zip(families, commands):
# 1) Forward slashes also work on Windows, so don't mess with
# backslashes. 2) The dirname needs to include a separator.
path = Path(fm.findfont(family))
latex_fontspec.append(r"\%s{%s}[Path=%s]" % (
command, path.name, path.parent.as_posix() + "/"))

return "\n".join(latex_fontspec)

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

@staticmethod
def get_latex_manager():
texcommand = get_texcommand()
texcommand = rcParams["pgf.texsystem"]
latex_header = LatexManager._build_latex_header()
prev = LatexManagerFactory.previous_instance

Expand Down Expand Up @@ -307,7 +285,7 @@ def __init__(self):
LatexManager._unclean_instances.add(self)

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

texcommand = get_texcommand()
texcommand = rcParams["pgf.texsystem"]
cmdargs = [texcommand, "-interaction=nonstopmode",
"-halt-on-error", "figure.tex"]
try:
Expand Down