1 前言
笔者最近发现在windows中安装的仿宋GB2312字体加粗的时候会显示异常,结合大模型解决后记录如下:
2 安装官方版本仿宋GB2312字体
访问如下链接下载后右键选择为所有用户安装即可:https://gwgl.bjzx.gov.cn/bjzx/help.html
安装后笔者电脑实测问题如图所示:
3 解决方案
3.1 WSL环境下
需要python环境,直接运行如下命令即可:
echo "[START] GB2312 font repair"
bash <<'BASH'
set -Eeuo pipefail
trap 'echo "[ERROR] 执行失败,行号:$LINENO" >&2' ERR
echo "[1/5] 查找已安装字体"
INFO=$(powershell.exe -NoProfile -NonInteractive -Command '
[Console]::OutputEncoding=[Text.Encoding]::UTF8
foreach ($h in @("HKCU:", "HKLM:")) {
$k = Get-Item "$h\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" -ErrorAction SilentlyContinue
if (!$k) { continue }
foreach ($n in $k.GetValueNames()) {
if ($n -match "^(FangSong|\u4eff\u5b8b)_GB2312\s*(\(.*\))?$") {
$p = $k.GetValue($n)
if (![IO.Path]::IsPathRooted($p)) {
$p = Join-Path "$env:WINDIR\Fonts" $p
}
if (Test-Path -LiteralPath $p) {
Write-Output $p
Write-Output ([Environment]::GetFolderPath("Desktop"))
exit 0
}
}
}
}
throw "GB2312 font not found"
' </dev/null | tr -d '\r')
FONT=$(wslpath -u "$(echo "$INFO" | sed -n '1p')")
DESKTOP=$(wslpath -u "$(echo "$INFO" | sed -n '2p')")
echo "字体路径:$FONT"
test -f "$FONT"
echo "[2/5] 创建 Python 环境"
VENV="$HOME/.venvs/gb2312-fix"
python3 -m venv "$VENV"
echo "[3/5] 安装依赖"
"$VENV/bin/python" -m pip install fonttools skia-pathops
echo "[4/5] 开始修复字体"
"$VENV/bin/python" -u - "$FONT" "$DESKTOP" <<'PY'
import sys
from pathlib import Path
from shutil import copy2
from fontTools.ttLib import TTFont
from fontTools.ttLib.removeOverlaps import removeOverlaps
src = Path(sys.argv[1])
out = Path(sys.argv[2]) / "GB2312修复"
out.mkdir(exist_ok=True)
backup = out / "原版_GB2312.ttf"
dst = out / "修复版_GB2312.ttf"
if not backup.exists():
copy2(src, backup)
print("原始字体已备份")
with TTFont(src) as f:
cmap = f.getBestCmap().copy()
widths = {c: f["hmtx"][g][0] for c, g in cmap.items()}
print("正在合并字形轮廓...")
removeOverlaps(f, removeHinting=True, ignoreErrors=False)
for tag in ("EBDT", "EBLC", "EBSC", "fpgm", "prep", "cvt ", "DSIG"):
if tag in f:
del f
print("正在保存字体...")
f.save(dst)
print("[5/5] 校验修复结果")
with TTFont(dst) as f:
assert f.getBestCmap() == cmap
assert all(f["hmtx"][g][0] == widths[c] for c, g in cmap.items())
print("修复完成!")
print("输出文件:", dst)
print("文件大小:", dst.stat().st_size, "bytes")
PY
echo "[SUCCESS] 全部执行完成"
BASH
3.2 Powershell环境下
需要python环境,直接运行如下命令即可:
Write-Host "[START] GB2312 font repair"
& {
$ErrorActionPreference = "Stop"
$OutputEncoding = [Text.UTF8Encoding]::new($false)
Write-Host "[1/5] 查找已安装字体"
$font = $null
foreach ($h in @("HKCU:", "HKLM:")) {
$k = Get-Item "$h\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" -ErrorAction SilentlyContinue
if (!$k) { continue }
foreach ($n in $k.GetValueNames()) {
if ($n -match "^(FangSong|仿宋)_GB2312\s*(\(.*\))?$") {
$p = $k.GetValue($n)
if (![IO.Path]::IsPathRooted($p)) {
$p = Join-Path "$env:WINDIR\Fonts" $p
}
if (Test-Path -LiteralPath $p) {
$font = $p
break
}
}
}
if ($font) { break }
}
if (!$font) { throw "未找到 GB2312 字体" }
Write-Host "字体路径:$font"
Write-Host "[2/5] 创建 Python 环境"
$venv = "$HOME\.venvs\gb2312-fix"
$desktop = [Environment]::GetFolderPath("Desktop")
$launcher = Get-Command py.exe -ErrorAction SilentlyContinue
if (!$launcher) {
$launcher = Get-Command python.exe -ErrorAction SilentlyContinue
}
if (!$launcher) { throw "Windows 未安装 Python,请使用 WSL 版本" }
& $launcher.Source -m venv $venv
if ($LASTEXITCODE -ne 0) { throw "创建虚拟环境失败" }
$python = "$venv\Scripts\python.exe"
Write-Host "[3/5] 安装依赖"
& $python -m pip install fonttools skia-pathops
if ($LASTEXITCODE -ne 0) { throw "依赖安装失败" }
Write-Host "[4/5] 开始修复字体"
@'
import sys
from pathlib import Path
from shutil import copy2
from fontTools.ttLib import TTFont
from fontTools.ttLib.removeOverlaps import removeOverlaps
src = Path(sys.argv[1])
out = Path(sys.argv[2]) / "GB2312修复"
out.mkdir(exist_ok=True)
backup = out / "原版_GB2312.ttf"
dst = out / "修复版_GB2312.ttf"
if not backup.exists():
copy2(src, backup)
print("原始字体已备份")
with TTFont(src) as f:
cmap = f.getBestCmap().copy()
widths = {c: f["hmtx"][g][0] for c, g in cmap.items()}
print("正在合并字形轮廓...")
removeOverlaps(f, removeHinting=True, ignoreErrors=False)
for tag in ("EBDT", "EBLC", "EBSC", "fpgm", "prep", "cvt ", "DSIG"):
if tag in f:
del f
print("正在保存字体...")
f.save(dst)
print("[5/5] 校验修复结果")
with TTFont(dst) as f:
assert f.getBestCmap() == cmap
assert all(f["hmtx"][g][0] == widths[c] for c, g in cmap.items())
print("修复完成!")
print("输出文件:", dst)
print("文件大小:", dst.stat().st_size, "bytes")
'@ | & $python -X utf8 -u - $font $desktop
if ($LASTEXITCODE -ne 0) { throw "字体修复失败" }
Write-Host "[SUCCESS] 全部执行完成" -ForegroundColor Green
}
随后在windows桌面找到文件夹“GB2312修复”,先卸载之前旧安装的字体,再安装修复版本即可。
温馨提示:安装前后请关闭chrome和word软件
