@@ -310,6 +310,19 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
310310 return [fname for fname in fontfiles if os .path .exists (fname )]
311311
312312
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+
313326@dataclasses .dataclass (frozen = True )
314327class FontEntry :
315328 """
@@ -1542,7 +1555,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
15421555 # actually raised.
15431556 return cbook ._ExceptionInfo (ValueError , "No valid font could be found" )
15441557
1545- return _cached_realpath (result )
1558+ return FontPath ( _cached_realpath (result ), best_font . index )
15461559
15471560
15481561@_api .deprecated ("3.11" )
@@ -1618,8 +1631,9 @@ def get_font(font_filepaths, hinting_factor=None):
16181631
16191632 Parameters
16201633 ----------
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]
16231637 Relative or absolute paths to the font files to be used.
16241638
16251639 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):
16351649
16361650 """
16371651 match font_filepaths :
1652+ case FontPath (path , index ):
1653+ paths = ((_cached_realpath (path ), index ), )
16381654 case str () | Path () | bytes () as path :
16391655 paths = ((_cached_realpath (path ), 0 ), )
16401656 case (str () | Path () | bytes () as path , int () as index ):
16411657 paths = ((_cached_realpath (path ), index ), )
16421658 case _:
16431659 paths = tuple (
16441660 (_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 )
16461663 for fname in font_filepaths )
16471664
16481665 hinting_factor = mpl ._val_or_rc (hinting_factor , 'text.hinting_factor' )
0 commit comments