17
17
import weakref
18
18
19
19
import matplotlib as mpl
20
- from matplotlib import _png , rcParams
20
+ from matplotlib import _png , font_manager as fm , rcParams
21
21
from matplotlib .backend_bases import (
22
22
_Backend , FigureCanvasBase , FigureManagerBase , GraphicsContextBase ,
23
23
RendererBase )
28
28
29
29
###############################################################################
30
30
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
-
52
31
53
32
def get_texcommand ():
54
33
"""Get chosen TeX system from rc."""
@@ -66,17 +45,17 @@ def get_fontspec():
66
45
latex_fontspec .append ("\\ usepackage{fontspec}" )
67
46
68
47
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 ))
80
59
81
60
return "\n " .join (latex_fontspec )
82
61
0 commit comments