Skip to content

Commit 3893cf2

Browse files
committed
Parse data from all fonts within a collection
This should allow listing the metadata from the whole collection, which will also pick the right one if specified, though it will not load the specific index yet.
1 parent 4899954 commit 3893cf2

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/matplotlib/font_manager.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ class FontEntry:
319319
"""
320320

321321
fname: str = ''
322+
index: int = 0
322323
name: str = ''
323324
style: str = 'normal'
324325
variant: str = 'normal'
@@ -465,7 +466,8 @@ def get_weight(): # From fontconfig's FcFreeTypeQueryFaceInternal.
465466
raise NotImplementedError("Non-scalable fonts are not supported")
466467
size = 'scalable'
467468

468-
return FontEntry(font.fname, name, style, variant, weight, stretch, size)
469+
return FontEntry(font.fname, font.face_index, name,
470+
style, variant, weight, stretch, size)
469471

470472

471473
def afmFontProperty(fontpath, font):
@@ -535,7 +537,7 @@ def afmFontProperty(fontpath, font):
535537

536538
size = 'scalable'
537539

538-
return FontEntry(fontpath, name, style, variant, weight, stretch, size)
540+
return FontEntry(fontpath, 0, name, style, variant, weight, stretch, size)
539541

540542

541543
def _cleanup_fontproperties_init(init_method):
@@ -1069,7 +1071,7 @@ class FontManager:
10691071
# Increment this version number whenever the font cache data
10701072
# format or behavior has changed and requires an existing font
10711073
# cache files to be rebuilt.
1072-
__version__ = '3.11.0a1'
1074+
__version__ = '3.11.0a2'
10731075

10741076
def __init__(self, size=None, weight='normal'):
10751077
self._version = self.__version__
@@ -1134,6 +1136,10 @@ def addfont(self, path):
11341136
font = ft2font.FT2Font(path)
11351137
prop = ttfFontProperty(font)
11361138
self.ttflist.append(prop)
1139+
for face_index in range(1, font.num_faces):
1140+
subfont = ft2font.FT2Font(path, face_index=face_index)
1141+
prop = ttfFontProperty(subfont)
1142+
self.ttflist.append(prop)
11371143
self._findfont_cached.cache_clear()
11381144

11391145
@property

lib/matplotlib/tests/test_font_manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,13 @@ def test_utf16m_sfnt():
117117

118118
def test_find_ttc():
119119
fp = FontProperties(family=["WenQuanYi Zen Hei"])
120-
if Path(findfont(fp)).name != "wqy-zenhei.ttc":
120+
fontpath = findfont(fp)
121+
if Path(fontpath).name != "wqy-zenhei.ttc":
121122
pytest.skip("Font wqy-zenhei.ttc may be missing")
123+
# All fonts from this collection should have loaded as well.
124+
for name in ["WenQuanYi Zen Hei Mono", "WenQuanYi Zen Hei Sharp"]:
125+
assert findfont(FontProperties(family=[name]),
126+
fallback_to_default=False) == fontpath
122127
fig, ax = plt.subplots()
123128
ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp)
124129
for fmt in ["raw", "svg", "pdf", "ps"]:

0 commit comments

Comments
 (0)