rsplotlib 0.1.9__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rsplotlib/__init__.py ADDED
@@ -0,0 +1,23 @@
1
+ """rsplotlib 包顶层导出。建议通过导入自 `rsplotlib.api` 使用公开 API。"""
2
+
3
+ from .core.api import * # noqa: F403, F401
4
+ from .core.api import __all__ as _api_all
5
+ from .rsplotlib import register_sans_serif_font
6
+ from . import pyplot, pylab # noqa: F401
7
+ from .utils import _font_resolver, style # noqa: F401
8
+ from .layout import gridspec # noqa: F401
9
+ from .ticks import ticker # noqa: F401
10
+
11
+ GridSpec = gridspec.GridSpec
12
+ MaxNLocator = ticker.MaxNLocator
13
+ MultipleLocator = ticker.MultipleLocator
14
+ AutoMinorLocator = ticker.AutoMinorLocator
15
+
16
+ __version__ = "0.1.9"
17
+ # 从内部 Rust 模块导出字体注册函数
18
+
19
+ __all__ = list(_api_all) + [
20
+ 'pyplot', 'style', 'gridspec', 'ticker',
21
+ 'GridSpec', 'MaxNLocator', 'MultipleLocator',
22
+ 'AutoMinorLocator', 'register_sans_serif_font',
23
+ ]
@@ -0,0 +1,167 @@
1
+ """rsplotlib._font_resolver - 字体族名 → 字体文件路径解析
2
+
3
+ 将 matplotlib 风格的无衬线字体族名(如 "Arial Unicode MS"、"Helvetica Neue")
4
+ 映射到本地字体文件路径。找不到时返回 None,由调用方决定回退到默认字体。
5
+ """
6
+ import os
7
+ from typing import Optional, List
8
+ import sys
9
+
10
+
11
+ # ====== 字体族名 → 候选文件路径映射(按平台分别维护)======
12
+
13
+ _FONT_NAME_TO_PATHS = {
14
+ # macOS
15
+ "Arial Unicode MS": [
16
+ "/Library/Fonts/Arial Unicode.ttf",
17
+ "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
18
+ ],
19
+ "Arial": [
20
+ "/Library/Fonts/Arial.ttf",
21
+ "/System/Library/Fonts/Supplemental/Arial.ttf",
22
+ ],
23
+ "Helvetica": [
24
+ "/System/Library/Fonts/Helvetica.ttc",
25
+ "/System/Library/Fonts/HelveticaNeue.ttc",
26
+ ],
27
+ "Helvetica Neue": [
28
+ "/System/Library/Fonts/HelveticaNeue.ttc",
29
+ ],
30
+ "PingFang SC": [
31
+ "/System/Library/Fonts/PingFang.ttc",
32
+ ],
33
+ "Heiti SC": [
34
+ "/System/Library/Fonts/STHeiti Light.ttc",
35
+ "/System/Library/Fonts/STHeiti Medium.ttc",
36
+ ],
37
+ "Hiragino Sans GB": [
38
+ "/System/Library/Fonts/Hiragino Sans GB W3.otf",
39
+ "/System/Library/Fonts/Hiragino Sans GB W6.otf",
40
+ ],
41
+ # Linux
42
+ "DejaVu Sans": [
43
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
44
+ "/usr/share/fonts/dejavu/DejaVuSans.ttf",
45
+ ],
46
+ "Liberation Sans": [
47
+ "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
48
+ ],
49
+ "Noto Sans CJK SC": [
50
+ "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
51
+ "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
52
+ "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
53
+ ],
54
+ "WenQuanYi Micro Hei": [
55
+ "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
56
+ ],
57
+ # Windows
58
+ "Microsoft YaHei": [
59
+ "C:/Windows/Fonts/msyh.ttc",
60
+ "C:/Windows/Fonts/msyh.ttf",
61
+ "C:/Windows/Fonts/msyhbd.ttc",
62
+ ],
63
+ "SimHei": [
64
+ "C:/Windows/Fonts/simhei.ttf",
65
+ ],
66
+ "SimSun": [
67
+ "C:/Windows/Fonts/simsun.ttc",
68
+ ],
69
+ }
70
+
71
+
72
+ # 跨平台按系统名归一化的额外兜底字体路径
73
+
74
+ def _system_fallback_paths() -> List[str]:
75
+ """按当前操作系统返回一组"通用全功能字体"候选路径"""
76
+ system = sys.platform
77
+ if system == "darwin":
78
+ return [
79
+ "/Library/Fonts/Arial Unicode.ttf",
80
+ "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
81
+ ]
82
+ elif system == "linux":
83
+ return [
84
+ "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
85
+ "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
86
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
87
+ "/usr/share/fonts/dejavu/DejaVuSans.ttf",
88
+ ]
89
+ elif system == "win32":
90
+ return [
91
+ "C:/Windows/Fonts/msyh.ttc",
92
+ "C:/Windows/Fonts/msyh.ttf",
93
+ "C:/Windows/Fonts/msyhbd.ttc",
94
+ ]
95
+ elif system == "cygwin":
96
+ return [
97
+ "C:/Windows/Fonts/msyh.ttc",
98
+ "C:/Windows/Fonts/arial.ttf",
99
+ ]
100
+ return []
101
+
102
+
103
+ def resolve_font_path(family: str) -> Optional[str]:
104
+ """根据字体族名查找本地的字体文件路径。
105
+
106
+ 找不到时返回 None。
107
+ """
108
+ if not family:
109
+ return None
110
+ # 精确匹配
111
+ candidates = _FONT_NAME_TO_PATHS.get(family, [])
112
+ for path in candidates:
113
+ if os.path.isfile(path):
114
+ return path
115
+ # 大小写不敏感匹配
116
+ lower_map = {k.lower(): v for k, v in _FONT_NAME_TO_PATHS.items()}
117
+ candidates = lower_map.get(family.lower(), [])
118
+ for path in candidates:
119
+ if os.path.isfile(path):
120
+ return path
121
+ # 平台回退
122
+ for path in _system_fallback_paths():
123
+ if os.path.isfile(path):
124
+ return path
125
+ return None
126
+
127
+
128
+ def apply_rcparams_font() -> Optional[str]:
129
+ """读取 rcParams["font.sans-serif"],把第一个能解析到本地文件的字体注册到 plotters。
130
+
131
+ 返回实际注册的字体文件路径,如果没找到任何字体则返回 None。
132
+ """
133
+ try:
134
+ # 延迟导入避免循环依赖
135
+ from . import rsplotlib as _rsplotlib
136
+ from .pylab import mpl
137
+ except Exception:
138
+ return None
139
+
140
+ sans_serif = mpl.rcParams.get("font.sans-serif")
141
+ if not sans_serif:
142
+ return None
143
+
144
+ if isinstance(sans_serif, str):
145
+ candidates = [sans_serif]
146
+ else:
147
+ try:
148
+ candidates = list(sans_serif)
149
+ except TypeError:
150
+ candidates = [str(sans_serif)]
151
+
152
+ # "sans-serif" 关键字跳过:让 plotters 使用内部默认
153
+ candidates = [c for c in candidates if c and c.lower() != "sans-serif"]
154
+
155
+ for family in candidates:
156
+ path = resolve_font_path(family)
157
+ if path is None:
158
+ # 可能是直接的字体文件路径
159
+ if os.path.isfile(family):
160
+ path = family
161
+ if path is not None:
162
+ try:
163
+ _rsplotlib.register_sans_serif_font(path)
164
+ return path
165
+ except Exception:
166
+ continue
167
+ return None
@@ -0,0 +1,2 @@
1
+ """rsplotlib.core - 核心 API 函数"""
2
+ from .api import * # noqa: F403, F401