Files
wordcloud/backend/core/fonts.py
T
2026-07-04 02:40:45 +08:00

26 lines
817 B
Python

from matplotlib.font_manager import FontProperties
from PIL import ImageFont
from . import config
_global_font_cache = {}
_font_properties_cache = {}
def get_cached_font(font_path, size):
key = (font_path, size)
if key not in _global_font_cache:
try:
_global_font_cache[key] = ImageFont.truetype(font_path, size)
except IOError as e:
config._warn(f"字体加载失败,回退默认字体: path={font_path}, size={size}, error={e}")
_global_font_cache[key] = ImageFont.load_default()
return _global_font_cache[key]
def get_font_properties(font_path, size):
key = (font_path, size)
if key not in _font_properties_cache:
_font_properties_cache[key] = FontProperties(fname=font_path, size=size)
return _font_properties_cache[key]