-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
font_family.go
221 lines (208 loc) · 6.29 KB
/
font_family.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"runtime"
"slices"
"strings"
"sync"
"github.com/richardwilkes/toolbox/collection/dict"
"github.com/richardwilkes/toolbox/txt"
"github.com/richardwilkes/unison/enums/slant"
"github.com/richardwilkes/unison/enums/spacing"
"github.com/richardwilkes/unison/enums/weight"
"github.com/richardwilkes/unison/internal/skia"
)
var (
slantMapping = [][]int{
{3, 1, 2},
{1, 3, 2},
{1, 2, 3},
}
cachedFontFamiliesLock sync.RWMutex
cachedFontFamilies []string
)
// FontFamily holds information about one font family.
type FontFamily struct {
set skia.FontStyleSet
name string
}
// FontFamilies retrieves the names of the installed font families, using a cached version if available.
func FontFamilies() []string {
cachedFontFamiliesLock.RLock()
families := cachedFontFamilies
cachedFontFamiliesLock.RUnlock()
if len(families) == 0 {
return FontFamiliesNoCache()
}
return families
}
// FontFamiliesNoCache retrieves the names of the installed font families.
func FontFamiliesNoCache() []string {
cachedFontFamiliesLock.Lock()
defer cachedFontFamiliesLock.Unlock()
fm := skia.FontMgrRefDefault()
count := skia.FontMgrCountFamilies(fm)
names := make(map[string]bool)
ss := skia.StringNewEmpty()
for i := 0; i < count; i++ {
skia.FontMgrGetFamilyName(fm, i, ss)
names[skia.StringGetString(ss)] = true
}
skia.StringDelete(ss)
internalFontLock.RLock()
for k := range internalFonts {
names[k] = true
}
internalFontLock.RUnlock()
families := dict.Keys(names)
slices.SortFunc(families, func(a, b string) int { return txt.NaturalCmp(a, b, true) })
cachedFontFamilies = families
return families
}
// MatchFontFamily returns a FontFamily for the specified family name. If no such family name exists, Count() will be 0.
func MatchFontFamily(family string) *FontFamily {
internalFontLock.RLock()
_, exists := internalFonts[family]
internalFontLock.RUnlock()
if exists {
return &FontFamily{name: family}
}
f := &FontFamily{
name: family,
set: skia.FontMgrMatchFamily(skia.FontMgrRefDefault(), family),
}
runtime.SetFinalizer(f, func(obj *FontFamily) {
ReleaseOnUIThread(func() {
skia.FontStyleSetUnref(obj.set)
})
})
return f
}
// Count returns the number of Faces within this FontFamily.
func (f *FontFamily) Count() int {
internalFontLock.RLock()
defer internalFontLock.RUnlock()
if fnt, exists := internalFonts[f.name]; exists {
return len(fnt.faces)
}
return skia.FontStyleSetGetCount(f.set)
}
// Style returns the style information for the given index. Must be >= 0 and < Count().
func (f *FontFamily) Style(index int) (description string, weightValue weight.Enum, spacingValue spacing.Enum, slantValue slant.Enum) {
internalFontLock.RLock()
defer internalFontLock.RUnlock()
if fnt, exists := internalFonts[f.name]; exists {
if index >= 0 && index < len(fnt.faces) {
weightValue, spacingValue, slantValue = fnt.faces[index].Style()
var buffer strings.Builder
buffer.WriteString(weightValue.String())
if spacingValue != spacing.Standard {
buffer.WriteString(" ")
buffer.WriteString(spacingValue.String())
}
if slantValue != slant.Upright {
buffer.WriteString(" ")
buffer.WriteString(slantValue.String())
}
description = buffer.String()
}
return
}
ss := skia.StringNewEmpty()
defer skia.StringDelete(ss)
style := skia.FontStyleNew(0, 0, 0)
defer skia.FontStyleDelete(style)
skia.FontStyleSetGetStyle(f.set, index, style, ss)
return skia.StringGetString(ss), weight.Enum(skia.FontStyleGetWeight(style)),
spacing.Enum(skia.FontStyleGetWidth(style)), slant.Enum(skia.FontStyleGetSlant(style))
}
// Face returns the FontFace for the given index. Must be >= 0 and < Count().
func (f *FontFamily) Face(index int) *FontFace {
internalFontLock.RLock()
defer internalFontLock.RUnlock()
if fnt, exists := internalFonts[f.name]; exists {
if index >= 0 && index < len(fnt.faces) {
return fnt.faces[index]
}
return nil
}
return newFace(skia.FontStyleSetCreateTypeFace(f.set, index))
}
// MatchStyle attempts to locate the FontFace within the family with the given style. Will return nil if nothing
// suitable can be found.
func (f *FontFamily) MatchStyle(weightValue weight.Enum, spacingValue spacing.Enum, slantValue slant.Enum) *FontFace {
spacingValue = spacingValue.EnsureValid()
slantValue = slantValue.EnsureValid()
internalFontLock.RLock()
defer internalFontLock.RUnlock()
if fnt, exists := internalFonts[f.name]; exists {
bestScore := 0
bestIndex := 0
for i, face := range fnt.faces {
w, sp, sl := face.Style()
if weightValue == w && spacingValue == sp && slantValue == sl {
return face
}
var score int
if spacingValue <= spacing.Standard {
if sp <= spacingValue {
score = 10 - int(spacingValue) + int(sp)
} else {
score = 10 - int(sp)
}
} else {
if sp > spacingValue {
score = 10 + int(spacingValue) - int(sp)
} else {
score = int(sp)
}
}
score <<= 8
score += slantMapping[slantValue][sl]
score <<= 8
switch {
case weightValue == w:
score += 1000
case weightValue < weight.Regular:
if w <= weightValue {
score += 1000 - int(weightValue) + int(w)
} else {
score += 1000 - int(w)
}
case weightValue <= weight.Medium:
switch {
case w >= weightValue && w <= weight.Medium:
score += 1000 + int(weightValue) - int(w)
case w <= weightValue:
score += 500 + int(w)
default:
score += 1000 - int(w)
}
default:
if w > weightValue {
score += 1000 + int(weightValue) - int(w)
} else {
score += int(w)
}
}
if bestScore < score {
bestScore = score
bestIndex = i
}
}
return fnt.faces[bestIndex]
}
style := skia.FontStyleNew(skia.FontWeight(weightValue), skia.FontSpacing(spacingValue), skia.FontSlant(slantValue))
defer skia.FontStyleDelete(style)
return newFace(skia.FontStyleSetMatchStyle(f.set, style))
}
func (f *FontFamily) String() string {
return f.name
}