Initial commit

This commit is contained in:
2019-06-15 15:47:49 +02:00
commit 8e194e278c
10 changed files with 649 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
package fonts
import (
"os"
"os/user"
"path/filepath"
"strings"
)
// Find looks up font file location by the name.
func Find(name string) (string, error) {
// Normalize font file name
fileName := strings.Replace(name, " ", "-", -1) + ".ttf"
for _, dir := range fontDirs() {
dir, err := expandHome(dir)
if err != nil {
return "", err
}
path := filepath.Join(dir, fileName)
if fileExist(path) {
return path, nil
}
}
return "", nil
}
func expandHome(path string) (string, error) {
if !strings.HasPrefix(path, "~/") {
return path, nil
}
u, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(u.HomeDir, path[2:]), nil
}
func fileExist(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
+13
View File
@@ -0,0 +1,13 @@
// +build darwin
package fonts
// Docs: https://support.apple.com/en-us/HT201722
func fontDirs() []string {
return []string{
".", // Current directory
"~/Library/Fonts", // User
"/Library/Fonts", // Local
"/System/Library/Fonts", // System
}
}
+9
View File
@@ -0,0 +1,9 @@
// +build !linux,!darwin
package fonts
func fontDirs() []string {
return []string{
".", // Current directory
}
}
+15
View File
@@ -0,0 +1,15 @@
// +build linux
package fonts
func fontDirs() []string {
return []string{
".", // Current directory
"~/.fonts", // User
"~/.fonts/truetype", // User
"~/.local/share/fonts", // User
"~/.local/share/fonts/truetype", // User
"/usr/share/fonts", // System
"/usr/share/fonts/truetype", // System
}
}