diff --git a/internal/platform/darwin.go b/internal/platform/darwin.go index c2e9801..d0032fd 100644 --- a/internal/platform/darwin.go +++ b/internal/platform/darwin.go @@ -63,3 +63,7 @@ func (p *DarwinPlatform) DefaultShell() string { } return "/bin/zsh" } + +func (p *DarwinPlatform) InstallFont(ctx context.Context, fontPath string) error { + return nil +} diff --git a/internal/platform/linux.go b/internal/platform/linux.go index 8d3ec73..180704a 100644 --- a/internal/platform/linux.go +++ b/internal/platform/linux.go @@ -80,3 +80,7 @@ func (p *LinuxPlatform) DefaultShell() string { } return "/bin/bash" } + +func (p *LinuxPlatform) InstallFont(ctx context.Context, fontPath string) error { + return nil +} diff --git a/internal/platform/platform.go b/internal/platform/platform.go index 80aecc3..fefb548 100644 --- a/internal/platform/platform.go +++ b/internal/platform/platform.go @@ -39,6 +39,9 @@ type Platform interface { // DefaultShell 返回默认 Shell DefaultShell() string + + // InstallFont 安装字体 + InstallFont(ctx context.Context, fontPath string) error } // Detect 检测当前平台并返回对应的 Platform 实现 diff --git a/internal/platform/windows.go b/internal/platform/windows.go index 21d40b4..861aa0d 100644 --- a/internal/platform/windows.go +++ b/internal/platform/windows.go @@ -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\\AppData\Local\Microsoft\Windows\Fonts + // 也可以使用 C:\Users\\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) +}