MCPcopy Index your code
hub / github.com/BasicProtein/AugmentCode-Free / FontManager

Class FontManager

gui_qt6/font_manager.py:13–100  ·  view source on GitHub ↗

跨平台字体管理器

Source from the content-addressed store, hash-verified

11
12
13class FontManager:
14 """跨平台字体管理器"""
15
16 def __init__(self):
17 self.system = platform.system().lower()
18 # PyQt6中QFontDatabase不需要参数
19 self._font_database = QFontDatabase
20
21 # 系统字体映射 - 解决macOS字体显示异常
22 self.system_fonts = {
23 'windows': {
24 'default': ['Microsoft YaHei', 'SimHei', 'Arial', 'Segoe UI'],
25 'monospace': ['Consolas', 'Courier New', 'monospace']
26 },
27 'darwin': { # macOS
28 'default': ['PingFang SC', 'Helvetica Neue', 'SF Pro Display', 'Arial'],
29 'monospace': ['SF Mono', 'Monaco', 'Menlo', 'monospace']
30 },
31 'linux': {
32 'default': ['Noto Sans CJK SC', 'DejaVu Sans', 'Ubuntu', 'Arial'],
33 'monospace': ['Ubuntu Mono', 'DejaVu Sans Mono', 'monospace']
34 }
35 }
36
37 # 缓存选中的字体
38 self._default_font = None
39 self._monospace_font = None
40
41 # 初始化字体
42 self._initialize_fonts()
43
44 def _initialize_fonts(self):
45 """初始化系统字体"""
46 self._default_font = self._get_best_font('default')
47 self._monospace_font = self._get_best_font('monospace')
48
49 def _get_best_font(self, font_type: str) -> str:
50 """获取最佳可用字体"""
51 font_list = self.system_fonts.get(self.system, {}).get(font_type, ['Arial'])
52
53 for font_name in font_list:
54 if self._is_font_available(font_name):
55 return font_name
56
57 # 如果都不可用,返回系统默认
58 return 'Arial' if font_type == 'default' else 'monospace'
59
60 def _is_font_available(self, font_name: str) -> bool:
61 """检查字体是否可用"""
62 try:
63 # PyQt6中QFontDatabase.families()是静态方法
64 families = QFontDatabase.families()
65 return font_name in families
66 except:
67 return False
68
69 def get_default_font(self, size: int = 10, bold: bool = False) -> QFont:
70 """获取默认字体"""

Callers 1

get_font_managerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected