2525# - setWeights function needs improvement
2626# - 'light' is an invalid weight value, remove it.
2727
28+ from __future__ import annotations
29+
2830from base64 import b64encode
2931from collections import namedtuple
3032import copy
4143import subprocess
4244import sys
4345import threading
44- from typing import Union
4546
4647import matplotlib as mpl
4748from matplotlib import _api , _afm , cbook , ft2font
@@ -304,42 +305,35 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
304305 return [fname for fname in fontfiles if os .path .exists (fname )]
305306
306307
307- def _fontentry_helper_repr_png (fontent ):
308- from matplotlib .figure import Figure # Circular import.
309- fig = Figure ()
310- font_path = Path (fontent .fname ) if fontent .fname != '' else None
311- fig .text (0 , 0 , fontent .name , font = font_path )
312- with BytesIO () as buf :
313- fig .savefig (buf , bbox_inches = 'tight' , transparent = True )
314- return buf .getvalue ()
315-
316-
317- def _fontentry_helper_repr_html (fontent ):
318- png_stream = _fontentry_helper_repr_png (fontent )
319- png_b64 = b64encode (png_stream ).decode ()
320- return f"<img src=\" data:image/png;base64, { png_b64 } \" />"
321-
322-
323- FontEntry = dataclasses .make_dataclass (
324- 'FontEntry' , [
325- ('fname' , str , dataclasses .field (default = '' )),
326- ('name' , str , dataclasses .field (default = '' )),
327- ('style' , str , dataclasses .field (default = 'normal' )),
328- ('variant' , str , dataclasses .field (default = 'normal' )),
329- ('weight' , Union [str , int ], dataclasses .field (default = 'normal' )),
330- ('stretch' , str , dataclasses .field (default = 'normal' )),
331- ('size' , str , dataclasses .field (default = 'medium' )),
332- ],
333- namespace = {
334- '__doc__' : """
308+ @dataclasses .dataclass (frozen = True )
309+ class FontEntry :
310+ """
335311 A class for storing Font properties.
336312
337313 It is used when populating the font lookup dictionary.
338- """ ,
339- '_repr_html_' : lambda self : _fontentry_helper_repr_html (self ),
340- '_repr_png_' : lambda self : _fontentry_helper_repr_png (self ),
341- }
342- )
314+ """
315+
316+ fname : str = ''
317+ name : str = ''
318+ style : str = 'normal'
319+ variant : str = 'normal'
320+ weight : str | int = 'normal'
321+ stretch : str = 'normal'
322+ size : str = 'medium'
323+
324+ def _repr_html_ (self ) -> str :
325+ png_stream = self ._repr_png_ ()
326+ png_b64 = b64encode (png_stream ).decode ()
327+ return f"<img src=\" data:image/png;base64, { png_b64 } \" />"
328+
329+ def _repr_png_ (self ) -> bytes :
330+ from matplotlib .figure import Figure # Circular import.
331+ fig = Figure ()
332+ font_path = Path (self .fname ) if self .fname != '' else None
333+ fig .text (0 , 0 , self .name , font = font_path )
334+ with BytesIO () as buf :
335+ fig .savefig (buf , bbox_inches = 'tight' , transparent = True )
336+ return buf .getvalue ()
343337
344338
345339def ttfFontProperty (font ):
@@ -926,8 +920,7 @@ def default(self, o):
926920 try :
927921 # Cache paths of fonts shipped with Matplotlib relative to the
928922 # Matplotlib data path, which helps in the presence of venvs.
929- d ["fname" ] = str (
930- Path (d ["fname" ]).relative_to (mpl .get_data_path ()))
923+ d ["fname" ] = str (Path (d ["fname" ]).relative_to (mpl .get_data_path ()))
931924 except ValueError :
932925 pass
933926 return d
@@ -944,10 +937,9 @@ def _json_decode(o):
944937 r .__dict__ .update (o )
945938 return r
946939 elif cls == 'FontEntry' :
947- r = FontEntry .__new__ (FontEntry )
948- r .__dict__ .update (o )
949- if not os .path .isabs (r .fname ):
950- r .fname = os .path .join (mpl .get_data_path (), r .fname )
940+ if not os .path .isabs (o ['fname' ]):
941+ o ['fname' ] = os .path .join (mpl .get_data_path (), o ['fname' ])
942+ r = FontEntry (** o )
951943 return r
952944 else :
953945 raise ValueError ("Don't know how to deserialize __class__=%s" % cls )
0 commit comments