25
25
# - setWeights function needs improvement
26
26
# - 'light' is an invalid weight value, remove it.
27
27
28
+ from __future__ import annotations
29
+
28
30
from base64 import b64encode
29
31
from collections import namedtuple
30
32
import copy
41
43
import subprocess
42
44
import sys
43
45
import threading
44
- from typing import Union
45
46
46
47
import matplotlib as mpl
47
48
from matplotlib import _api , _afm , cbook , ft2font
@@ -304,42 +305,35 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
304
305
return [fname for fname in fontfiles if os .path .exists (fname )]
305
306
306
307
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
+ """
335
311
A class for storing Font properties.
336
312
337
313
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 ()
343
337
344
338
345
339
def ttfFontProperty (font ):
@@ -926,8 +920,7 @@ def default(self, o):
926
920
try :
927
921
# Cache paths of fonts shipped with Matplotlib relative to the
928
922
# 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 ()))
931
924
except ValueError :
932
925
pass
933
926
return d
@@ -944,10 +937,9 @@ def _json_decode(o):
944
937
r .__dict__ .update (o )
945
938
return r
946
939
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 )
951
943
return r
952
944
else :
953
945
raise ValueError ("Don't know how to deserialize __class__=%s" % cls )
0 commit comments