Merge remote-tracking branch 'refs/remotes/origin/addRender' into HJD

This commit is contained in:
Hector 2025-12-04 01:47:14 +08:00
commit 32a6cb65e1

View File

@ -512,16 +512,105 @@ class CoreWorld(Meta3DWorld):
# def _loadFont(self):
# """加载中文字体"""
# try:
# self.chinese_font = self.loader.loadFont('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc')
# if not self.chinese_font:
# print("警告: 无法加载中文字体,将使用默认字体")
# else:
# print("✓ 中文字体加载成功")
# except:
# print("警告: 无法加载中文字体,将使用默认字体")
# self.chinese_font = None
def _loadFont(self):
"""加载中文字体"""
"""加载中文字体 - 跨平台Panda3D 友好路径"""
try:
self.chinese_font = self.loader.loadFont('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc')
import os
import platform
from pathlib import Path
from panda3d.core import Filename
self.chinese_font = None # 初始化
# --- 平台与项目根 ---
system = platform.system().lower()
project_root = Path(__file__).resolve().parent.parent # 上两级
# --- 候选字体路径(按平台) ---
if system == "windows":
win_dir = os.environ.get("WINDIR") or r"C:\Windows"
win_fonts_dir = Path(win_dir) / "Fonts"
font_candidates = [
project_root / "RenderPipelineFile" / "data" / "font" / "msyh.ttc",
win_fonts_dir / "msyh.ttc",
win_fonts_dir / "msyh.ttf",
win_fonts_dir / "simhei.ttf",
win_fonts_dir / "simsun.ttc",
]
elif system == "darwin": # macOS
font_candidates = [
Path("/System/Library/Fonts/PingFang.ttc"),
Path("/System/Library/Fonts/STHeiti.ttc"),
Path("/Library/Fonts/Songti.ttc"),
]
else: # Linux / 其他
font_candidates = [
Path("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"),
Path("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc"),
Path("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"),
]
# --- 逐个尝试加载 ---
for os_path in font_candidates:
os_path_str = str(os_path)
if not os.path.exists(os_path_str):
# 文件确实不存在就跳过
continue
# 1) 先把 OS 路径转换成 Panda 内部路径(/d/... 或 /usr/...
panda_fn = Filename.fromOsSpecific(os_path_str)
panda_fn.makeTrueCase() # Windows 上修正大小写(更稳)
panda_internal = panda_fn.getFullpath() # 取“内部样式”的字符串
# 2) 打印便于对照 Panda 的日志
try:
print(f"[FontLoader] 尝试加载: {panda_internal}")
except Exception:
pass
try:
# 3) 传“字符串”给 loader.loadFont关键修复点
font = self.loader.loadFont(panda_internal)
if font:
self.chinese_font = font
print("✓ 中文字体加载成功:", os_path_str)
break
else:
print("[FontLoader] 返回空字体对象,继续尝试下一个候选...")
except TypeError as te:
# 某些旧版绑定只接受 str不接受 Filename此处已传 str若仍报错再兜底一次
try:
font = self.loader.loadFont(str(panda_fn)) # 退一步str(Filename)
if font:
self.chinese_font = font
print("✓ 中文字体加载成功(备用API):", os_path_str)
break
else:
print("[FontLoader] 备用API仍返回空字体对象继续…")
except Exception as te2:
print(f"[FontLoader] 加载失败(TypeError),继续下一个: {os_path},原因: {te2}")
except Exception as e:
print(f"[FontLoader] 加载失败,继续下一个: {os_path},原因: {e}")
# --- 兜底 ---
if not self.chinese_font:
print("警告: 无法加载中文字体,将使用默认字体")
else:
print("✓ 中文字体加载成功")
except:
print("警告: 无法加载中文字体,将使用默认字体")
print("警告: 无法加载中文字体,将使用默认字体(可能导致中文显示不全)")
self.chinese_font = None
except Exception as e:
print(f"警告: 加载中文字体时发生错误: {e}")
self.chinese_font = None
def setQtWidget(self, widget):