新增win安装字体的方法
This commit is contained in:
@@ -63,3 +63,7 @@ func (p *DarwinPlatform) DefaultShell() string {
|
|||||||
}
|
}
|
||||||
return "/bin/zsh"
|
return "/bin/zsh"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *DarwinPlatform) InstallFont(ctx context.Context, fontPath string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,3 +80,7 @@ func (p *LinuxPlatform) DefaultShell() string {
|
|||||||
}
|
}
|
||||||
return "/bin/bash"
|
return "/bin/bash"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *LinuxPlatform) InstallFont(ctx context.Context, fontPath string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ type Platform interface {
|
|||||||
|
|
||||||
// DefaultShell 返回默认 Shell
|
// DefaultShell 返回默认 Shell
|
||||||
DefaultShell() string
|
DefaultShell() string
|
||||||
|
|
||||||
|
// InstallFont 安装字体
|
||||||
|
InstallFont(ctx context.Context, fontPath string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect 检测当前平台并返回对应的 Platform 实现
|
// Detect 检测当前平台并返回对应的 Platform 实现
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ package platform
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WindowsPlatform Windows 平台实现
|
// WindowsPlatform Windows 平台实现
|
||||||
@@ -81,3 +85,46 @@ func (p *WindowsPlatform) DefaultShell() string {
|
|||||||
}
|
}
|
||||||
return "powershell" // Windows PowerShell 5.1
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user