-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfonts.py
More file actions
67 lines (51 loc) · 1.7 KB
/
fonts.py
File metadata and controls
67 lines (51 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Font configuration for the Locus GUI.
Handles Chinese / English font resolution and provides font helper functions.
"""
import tkinter.font as tkfont
from i18n import get_lang
# Windows Chinese fonts in priority order
_ZH_FONT_CANDIDATES = [
"Microsoft YaHei UI", # 微软雅黑 UI — best for UI, ships with Win7+
"Microsoft YaHei", # 微软雅黑
"SimHei", # 黑体 — always available on Chinese Windows
"DengXian", # 等线 — Win10+ default
"Source Han Sans SC", # 思源黑体
"Noto Sans CJK SC", # Google Noto
]
_EN_FONT = "Segoe UI"
_MONO_FONT = "Consolas"
_EMOJI_FONT = "Segoe UI Emoji"
_zh_font_cache = None
def _resolve_zh_font(root=None) -> str:
"""Find the best available Chinese font on this system."""
global _zh_font_cache
if _zh_font_cache is not None:
return _zh_font_cache
available = set()
try:
if root:
available = set(tkfont.families(root))
else:
available = set(tkfont.families())
except Exception:
pass
for candidate in _ZH_FONT_CANDIDATES:
if candidate in available:
_zh_font_cache = candidate
return candidate
_zh_font_cache = _EN_FONT
return _zh_font_cache
def ui_font(size: int = 11, bold: bool = False) -> tuple:
"""Return the correct UI font tuple for the current language."""
if get_lang() == "zh":
family = _resolve_zh_font()
else:
family = _EN_FONT
if bold:
return (family, size, "bold")
return (family, size)
def mono_font(size: int = 10) -> tuple:
return (_MONO_FONT, size)
def emoji_font(size: int = 48) -> tuple:
return (_EMOJI_FONT, size)