-
Notifications
You must be signed in to change notification settings - Fork 4
/
fontfinder.go
204 lines (184 loc) · 5.12 KB
/
fontfinder.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package fontfinder
import (
"path/filepath"
"sort"
"strings"
"sync"
"unicode"
"github.com/adrg/sysfont"
)
type FontPathInfo struct {
Regular string
BoldItalic string
Italic string
Bold string
}
type fontSearchInfo struct {
handle *sysfont.Font
nameWords []string
// Style words is not actually styles. But extracted from filename
// and removed unneeded stuff and splitted to words. On very wide
// range of the fonts this gives correct hints of the style.
// But for the fonts which filename has no info (like consola, consolai)
// we are using the font name if the name is in the sysfont data.
// Otherwise this package wouldn't help you.
styleWords []string
baseNameWExt string
hasRegular bool
hasItalic bool
hasBold bool
}
var (
// List of installed system fonts.
systemFontList []*sysfont.Font
systemFontListGuard sync.Mutex
// Add more if you know any other filename used in
// font names. All characters must be lowercase.
regularStrings = []string{"regular", "normal"}
italicStrings = []string{"italic", "oblique", "slanted", "it"}
boldStrings = []string{"bold"}
)
func init() {
// On some systems (Windows 10) which has many fonts, this function takes so long.
// Because of this we are doing this in initilization and in another
// goroutine. Updates systemFontListReady value to true when finished.
// And Find() will wait for this to be done only for first time.
go func() {
systemFontListGuard.Lock()
defer systemFontListGuard.Unlock()
finder := sysfont.NewFinder(&sysfont.FinderOpts{
Extensions: []string{".ttf", ".otf"},
})
systemFontList = finder.List()
}()
}
func List() []*sysfont.Font {
systemFontListGuard.Lock()
defer systemFontListGuard.Unlock()
return systemFontList
}
func Find(name string) FontPathInfo {
systemFontListGuard.Lock()
defer systemFontListGuard.Unlock()
return find(name)
}
func find(name string) FontPathInfo {
fonts := []fontSearchInfo{}
for _, f := range systemFontList {
if fontContains(f, name) {
base := filepath.Base(f.Filename)
baseWithoutExt := strings.Replace(base, filepath.Ext(base), "", 1)
styles := strings.Replace(baseWithoutExt, name, "", 1)
fonts = append(fonts, fontSearchInfo{
handle: f,
nameWords: splitWords(f.Name),
styleWords: splitWords(styles),
baseNameWExt: baseWithoutExt,
})
}
}
for i := 0; i < len(fonts); i++ {
f := &fonts[i]
f.hasRegular = fontHasStyle(f, regularStrings)
f.hasItalic = fontHasStyle(f, italicStrings)
f.hasBold = fontHasStyle(f, boldStrings)
}
// Sort fonts according to file name lengths in descending order.
sortFileNameLen(&fonts)
info := FontPathInfo{}
regularFounded := false
for _, f := range fonts {
// Order is important here.
if f.hasItalic && f.hasBold {
info.BoldItalic = f.handle.Filename
} else if f.hasItalic {
info.Italic = f.handle.Filename
} else if f.hasBold {
info.Bold = f.handle.Filename
} else if f.hasRegular {
info.Regular = f.handle.Filename
// If a font has 'Regular' string, it is the regular.
// No look for others. If no font has 'Regular' or 'Normal'
// then the font has smallest filename length and has no
// italic or bold will be the regular.
regularFounded = true
} else if !regularFounded {
info.Regular = f.handle.Filename
}
}
return info
}
func fontHasStyle(info *fontSearchInfo, stylenames []string) bool {
for _, s := range stylenames {
// Split font name to words and look for exact match.
for _, w := range info.nameWords {
if s == strings.ToLower(w) {
return true
}
}
// Do the same thing for the stylenames extracted from filename.
for _, w := range info.styleWords {
if s == strings.ToLower(w) {
return true
}
}
}
return false
}
func sortFileNameLen(fonts *[]fontSearchInfo) {
sort.Slice(*fonts, func(i, j int) bool {
fi := (*fonts)[i]
fj := (*fonts)[j]
return len(fi.baseNameWExt) > len(fj.baseNameWExt)
})
}
// Splits string to words according to delimiters and casing.
// Example:
// This: "HelloWorld_from-Turkey"
// Turns to this: [Hello, World, from, Turkey]
func splitWords(str string) []string {
// CamelCase pascalCase and "-_ "
arr := []string{}
runes := []rune(str)
index := 0
for i := 0; i < len(runes); i++ {
switch runes[i] {
case '-', '_', ' ', '.', ',':
word := runes[index:i]
if len(word) > 0 {
arr = append(arr, string(word))
index = i + 1
}
default:
if i < len(runes)-1 {
curUpper := unicode.IsUpper(runes[i])
nexUpper := unicode.IsUpper(runes[i+1])
if curUpper && !nexUpper {
// Like "Ca"
word := runes[index:i]
if len(word) > 0 {
arr = append(arr, string(word))
index = i
}
} else if !curUpper && nexUpper {
// Like "aC"
word := runes[index : i+1]
if len(word) > 0 {
arr = append(arr, string(word))
index = i + 1
}
}
}
}
}
word := runes[index:]
if len(word) > 0 {
arr = append(arr, string(word))
}
return arr
}
func fontContains(f *sysfont.Font, str string) bool {
return strings.Contains(f.Name, str) ||
strings.Contains(f.Family, str) ||
strings.Contains(f.Filename, str)
}