好的,经过仔细审视后,发现这是一个错误。使用以下测试:
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
font_cache = [i for i in
fm.findSystemFonts(fontpaths=None, fontext='ttf')
if 'umor' in i]
for i in font_cache:
print(i)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1],[1],'o')
ax.set_title('My Title', fontname='Humor Sans')
fig.savefig('tmp.png')
我已经比较了Humor Sans
和Homemade Apple
(一个被我打包成AUR软件包的免费谷歌字体)的行为。问题在于matplotlib
会对fontname=
中指定的字体名称进行匹配,该匹配不仅使用名称,还使用字体的多个属性。您可以在/home/grochmal/mat3/lib/python3.5/site-packages/matplotlib/font_manager.py
中看到匹配过程。
for font in fontlist:
if (directory is not None and
os.path.commonprefix([font.fname, directory]) != directory):
continue
score = \
self.score_family(prop.get_family(), font.name) * 10.0 + \
self.score_style(prop.get_style(), font.style) + \
self.score_variant(prop.get_variant(), font.variant) + \
self.score_weight(prop.get_weight(), font.weight) + \
self.score_stretch(prop.get_stretch(), font.stretch) + \
self.score_size(prop.get_size(), font.size)
if score < best_score:
best_score = score
best_font = font
if score == 0:
break
很遗憾,Humor Sans
从未达到匹配阶段,因为无法填充所有的 prop.get_...
。实质上它从未被包括在 fontlist
中。而 Homemade Apple
被包括是因为它可以填充所有属性。
字体属性的差异可以如下所示:
[me@haps aur]
Family: Homemade Apple
Subfamily: Regular
Full name: Homemade Apple
PostScript name: HomemadeApple
Preferred family: Homemade Apple
Preferred subfamily: Regular
Mac font menu name: Homemade Apple
Version: Version 1.000
Unique ID: FontDiner,Inc: Homemade Apple: 2010
Description: Copyright (c) 2010 by Font Diner, Inc. All rights reserved.
Designer: Font Diner, Inc
Designer URL: http://www.fontdiner.com
Manufacturer: Font Diner, Inc
Vendor URL: http://www.fontdiner.com
Trademark: Homemade Apple is a trademark of Font Diner, Inc.
Copyright: Copyright (c) 2010 by Font Diner, Inc. All rights reserved.
License URL: http://www.apache.org/licenses/LICENSE-2.0
License Description: Licensed under the Apache License, Version 2.0
Vendor ID: DINR
[me@haps aur]
Family: Humor Sans
Subfamily: Regular
Full name: Humor Sans
PostScript name: HumorSans
Version: Version 2.9 28/3/09
Unique ID: Fontifier 2.9 (172) www.fontifier.com Humor Sans
Copyright: Copyright (c) Randall Munroe's biggest fan 2009. Created by www.fontifier.com. usa-1lip-4fvu15
Vendor ID: Alts
Humor Sans
字体中缺少的字段并非必需,公正地说,TTF中描述字体方式存在几个不一致之处(例如google
italic与
oblique),因此这也不是
Humor Sans
的问题。你面临的问题是文件格式的不一致性和缺乏统一的代码来处理它们的组合。
我建议找一个外观类似的不同字体。编辑TTF或matplotlib代码都很棘手,可能会导致其他问题。
fc-list | grep -i humor
命令是否有输出?如果没有,尝试运行fc-cache -s
命令(重新构建字体缓存)后再次检查。 - grochmal