@@ -310,6 +310,19 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
310
310
return [fname for fname in fontfiles if os .path .exists (fname )]
311
311
312
312
313
+ class FontPath (str ):
314
+ __match_args__ = ('path' , 'face_index' )
315
+
316
+ def __new__ (cls , path , face_index ):
317
+ ret = super ().__new__ (cls , path )
318
+ ret .face_index = face_index
319
+ return ret
320
+
321
+ @property
322
+ def path (self ):
323
+ return str (self )
324
+
325
+
313
326
@dataclasses .dataclass (frozen = True )
314
327
class FontEntry :
315
328
"""
@@ -1542,7 +1555,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1542
1555
# actually raised.
1543
1556
return cbook ._ExceptionInfo (ValueError , "No valid font could be found" )
1544
1557
1545
- return _cached_realpath (result )
1558
+ return FontPath ( _cached_realpath (result ), best_font . index )
1546
1559
1547
1560
1548
1561
@_api .deprecated ("3.11" )
@@ -1618,8 +1631,9 @@ def get_font(font_filepaths, hinting_factor=None):
1618
1631
1619
1632
Parameters
1620
1633
----------
1621
- font_filepaths : Iterable[str, Path, bytes, tuple[str | Path | bytes, int]], \
1622
- str, Path, bytes, tuple[str | Path | bytes, int]
1634
+ font_filepaths : Iterable[str, Path, bytes, FontPath, \
1635
+ tuple[str | Path | bytes, int, FontPath]], \
1636
+ str, Path, bytes, FontPath, tuple[str | Path | bytes, int]
1623
1637
Relative or absolute paths to the font files to be used.
1624
1638
1625
1639
If a single string, bytes, or `pathlib.Path`, then it will be treated
@@ -1635,14 +1649,17 @@ def get_font(font_filepaths, hinting_factor=None):
1635
1649
1636
1650
"""
1637
1651
match font_filepaths :
1652
+ case FontPath (path , index ):
1653
+ paths = ((_cached_realpath (path ), index ), )
1638
1654
case str () | Path () | bytes () as path :
1639
1655
paths = ((_cached_realpath (path ), 0 ), )
1640
1656
case (str () | Path () | bytes () as path , int () as index ):
1641
1657
paths = ((_cached_realpath (path ), index ), )
1642
1658
case _:
1643
1659
paths = tuple (
1644
1660
(_cached_realpath (fname [0 ]), fname [1 ]) if isinstance (fname , tuple )
1645
- else (_cached_realpath (fname ), 0 )
1661
+ else (_cached_realpath (fname .path ), fname .face_index )
1662
+ if isinstance (fname , FontPath ) else (_cached_realpath (fname ), 0 )
1646
1663
for fname in font_filepaths )
1647
1664
1648
1665
hinting_factor = mpl ._val_or_rc (hinting_factor , 'text.hinting_factor' )
0 commit comments