新增win安装字体的方法

This commit is contained in:
zyj
2026-03-05 18:22:21 +08:00
parent 97e34c02ac
commit 3a35385ab8
4 changed files with 58 additions and 0 deletions

View File

@@ -2,8 +2,12 @@ package platform
import (
"context"
"io"
"os"
"os/exec"
"path/filepath"
"golang.org/x/sys/windows/registry"
)
// WindowsPlatform Windows 平台实现
@@ -81,3 +85,46 @@ func (p *WindowsPlatform) DefaultShell() string {
}
return "powershell" // Windows PowerShell 5.1
}
// InstallFont 安装字体
func (p *WindowsPlatform) InstallFont(ctx context.Context, fontPath string) error {
// 目标路径为 C:\Users\<User>\AppData\Local\Microsoft\Windows\Fonts
// 也可以使用 C:\Users\<User>\AppData\Roaming\Microsoft\Windows\Fonts但 Local 更合适
destDir := filepath.Join(os.Getenv("LOCALAPPDATA"), "Microsoft", "Windows", "Fonts")
// 确保目标目录存在
os.MkdirAll(destDir, 0755)
// 获取字体文件名
fontName := filepath.Base(fontPath)
// 目标路径
destPath := filepath.Join(destDir, fontName)
// 复制字体文件到目标路径
input, err := os.Open(fontPath)
if err != nil {
return err
}
defer input.Close()
output, err := os.Create(destPath)
if err != nil {
return err
}
defer output.Close()
_, err = io.Copy(output, input)
if err != nil {
return err
}
// 写注册表以注册字体
key, _, err := registry.CreateKey(registry.CURRENT_USER, `Software\Microsoft\Windows NT\CurrentVersion\Fonts`, registry.ALL_ACCESS)
if err != nil {
return err
}
defer key.Close()
regName := fontName + " (TrueType)"
return key.SetStringValue(regName, destPath)
}