|
| 1 | +# ============================================================================= |
| 2 | +# Copyright (C) 2026 Du Yifeng |
| 3 | +# |
| 4 | +# This file is part of pyfa. |
| 5 | +# |
| 6 | +# pyfa is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# pyfa is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with pyfa. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# ============================================================================= |
| 19 | +""" |
| 20 | +Configures Matplotlib font fallback stack to include available system CJK fonts. |
| 21 | +This ensures Chinese, Japanese, and Korean characters render correctly in graphs. |
| 22 | +""" |
| 23 | + |
| 24 | +import platform |
| 25 | + |
| 26 | + |
| 27 | +def configure_matplotlib_font(): |
| 28 | + """ |
| 29 | + Configure matplotlib to use fonts that can render CJK text. |
| 30 | + """ |
| 31 | + try: |
| 32 | + import matplotlib as mpl |
| 33 | + from matplotlib import font_manager |
| 34 | + except ImportError: |
| 35 | + return |
| 36 | + |
| 37 | + candidates = [] |
| 38 | + |
| 39 | + try: |
| 40 | + # noinspection PyPackageRequirements |
| 41 | + import wx |
| 42 | + gui_face = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT).GetFaceName() |
| 43 | + if gui_face: |
| 44 | + candidates.append(gui_face) |
| 45 | + except Exception: |
| 46 | + pass |
| 47 | + |
| 48 | + candidates.extend([ |
| 49 | + 'Noto Sans CJK KR', 'Noto Sans CJK JP', 'Noto Sans CJK SC', 'Noto Sans CJK TC', |
| 50 | + 'Source Han Sans KR', 'Source Han Sans JP', 'Source Han Sans CN', 'Source Han Sans TW', |
| 51 | + ]) |
| 52 | + |
| 53 | + system = platform.system() |
| 54 | + if system == 'Windows': |
| 55 | + candidates.extend([ |
| 56 | + 'Microsoft YaHei', 'Microsoft JhengHei', 'SimHei', 'SimSun', |
| 57 | + 'Malgun Gothic', 'Gulim', 'Dotum', 'Batang', |
| 58 | + 'Yu Gothic', 'Meiryo', 'Arial Unicode MS', |
| 59 | + ]) |
| 60 | + elif system == 'Darwin': |
| 61 | + candidates.extend([ |
| 62 | + 'Apple SD Gothic Neo', 'AppleGothic', 'PingFang SC', 'PingFang TC', |
| 63 | + 'Hiragino Sans GB', 'Hiragino Sans', 'Heiti SC', 'Arial Unicode MS', |
| 64 | + ]) |
| 65 | + else: |
| 66 | + candidates.extend([ |
| 67 | + 'NanumGothic', 'UnDotum', 'WenQuanYi Micro Hei', 'WenQuanYi Zen Hei', |
| 68 | + 'Droid Sans Fallback', |
| 69 | + ]) |
| 70 | + |
| 71 | + candidates.append('DejaVu Sans') |
| 72 | + |
| 73 | + # Filter out unavailable fonts to prevent Matplotlib warnings |
| 74 | + available = {f.name for f in font_manager.fontManager.ttflist} |
| 75 | + |
| 76 | + resolved = [] |
| 77 | + for name in candidates: |
| 78 | + if name in available and name not in resolved: |
| 79 | + resolved.append(name) |
| 80 | + |
| 81 | + if resolved: |
| 82 | + mpl.rcParams['font.family'] = resolved |
| 83 | + |
| 84 | + mpl.rcParams['axes.unicode_minus'] = False |
0 commit comments