-
Notifications
You must be signed in to change notification settings - Fork 14
/
install_windows.go
58 lines (47 loc) · 1.57 KB
/
install_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"os"
"path"
log "github.com/Crosse/gosimplelogger"
"golang.org/x/sys/windows/registry"
)
func platformDependentInstall(fontData *FontData) error {
// To install a font on Windows:
// - Copy the file to the fonts directory
// - Create a registry entry for the font
fullPath := path.Join(FontsDir, fontData.FileName)
log.Debugf("Installing \"%v\" to %v", fontData.Name, fullPath)
err := os.WriteFile(fullPath, fontData.Data, 0o644)
if err != nil {
return fmt.Errorf("cannot write file: %w", err)
}
// Second, write metadata about the font to the registry.
k, err := registry.OpenKey(
registry.LOCAL_MACHINE,
`SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts`,
registry.WRITE)
if err != nil {
// If this fails, remove the font file as well.
log.Error(err)
if nexterr := os.Remove(fullPath); nexterr != nil {
return fmt.Errorf("error removing font: %w", nexterr)
}
return fmt.Errorf("error opening registry: %w", err)
}
defer k.Close()
// Apparently it's "ok" to mark an OpenType font as "TrueType",
// and since this tool only supports True- and OpenType fonts,
// this should be Okay(tm).
// Besides, Windows does it, so why can't I?
valueName := fmt.Sprintf("%v (TrueType)", fontData.FileName)
if err = k.SetStringValue(fontData.Name, valueName); err != nil {
// If this fails, remove the font file as well.
log.Error(err)
if nexterr := os.Remove(fullPath); nexterr != nil {
return fmt.Errorf("error removing font: %w", nexterr)
}
return fmt.Errorf("error writing to registry: %w", err)
}
return nil
}