forked from richardwilkes/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_face.go
232 lines (215 loc) · 7.05 KB
/
font_face.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
222
223
224
225
226
227
228
229
230
231
232
// Copyright ©2021-2022 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"
"sync"
"github.com/richardwilkes/toolbox/txt"
"github.com/richardwilkes/toolbox/xmath"
"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 (
fontSizeCacheLock sync.RWMutex
fontSizeCache = make(map[FontDescriptor]float32)
)
// FontFace holds the immutable portions of a font description.
type FontFace struct {
face skia.TypeFace
}
func newFace(face skia.TypeFace) *FontFace {
if face == nil {
return nil
}
f := &FontFace{face: face}
runtime.SetFinalizer(f, func(obj *FontFace) {
ReleaseOnUIThread(func() {
skia.TypeFaceUnref(obj.face)
})
})
return f
}
// AllFontFaces returns all known font faces as FontFaceDescriptors. This will be computed each time, so it may be
// worthwhile to cache the result if you don't expect the set of font faces to be changed between calls.
func AllFontFaces() (all, monospaced []FontFaceDescriptor) {
ma := make(map[FontFaceDescriptor]struct{})
mm := make(map[FontFaceDescriptor]struct{})
for _, family := range FontFamilies() {
if ff := MatchFontFamily(family); ff != nil {
count := ff.Count()
for i := 0; i < count; i++ {
face := ff.Face(i)
w, sp, sl := face.Style()
ffd := FontFaceDescriptor{
Family: family,
Weight: w,
Spacing: sp,
Slant: sl,
}
if _, exists := ma[ffd]; !exists {
all = append(all, ffd)
ma[ffd] = struct{}{}
}
if face.Monospaced() {
if _, exists := mm[ffd]; !exists {
monospaced = append(monospaced, ffd)
mm[ffd] = struct{}{}
}
}
}
}
}
sorter := func(a, b FontFaceDescriptor) int { return txt.NaturalCmp(a.String(), b.String(), true) }
slices.SortFunc(all, sorter)
slices.SortFunc(monospaced, sorter)
return
}
// MatchFontFace attempts to locate the FontFace with the given family and style. Will return nil if nothing suitable
// can be found.
func MatchFontFace(family string, weightValue weight.Enum, spacingValue spacing.Enum, slantValue slant.Enum) *FontFace {
internalFontLock.Lock()
_, exists := internalFonts[family]
internalFontLock.Unlock()
if exists {
fam := MatchFontFamily(family)
return fam.MatchStyle(weightValue, spacingValue, slantValue)
}
style := skia.FontStyleNew(skia.FontWeight(weightValue), skia.FontSpacing(spacingValue), skia.FontSlant(slantValue))
defer skia.FontStyleDelete(style)
return newFace(skia.FontMgrMatchFamilyStyle(skia.FontMgrRefDefault(), family, style))
}
// CreateFontFace creates a new FontFace from font data.
func CreateFontFace(data []byte) *FontFace {
cData := skia.DataNewWithCopy(data)
defer skia.DataUnref(cData)
return newFace(skia.FontMgrCreateFromData(skia.FontMgrRefDefault(), cData))
}
// Font returns a Font of the given size for this FontFace.
func (f *FontFace) Font(capHeightSizeInLogicalPixels float32) Font {
w, sp, sl := f.Style()
fd := FontDescriptor{
FontFaceDescriptor: FontFaceDescriptor{
Family: f.Family(),
Weight: w,
Spacing: sp,
Slant: sl,
},
Size: capHeightSizeInLogicalPixels,
}
fontSizeCacheLock.RLock()
skiaSize, exists := fontSizeCache[fd]
fontSizeCacheLock.RUnlock()
if exists {
font := f.createFontWithSkiaSize(skiaSize)
font.size = capHeightSizeInLogicalPixels
return font
}
skiaSize = capHeightSizeInLogicalPixels
var font *fontImpl
font = f.createFontWithSkiaSize(skiaSize)
if font.metrics.CapHeight > 0 { // I've seen some fonts with a negative CapHeight, which won't work
skiaSize = xmath.Floor(capHeightSizeInLogicalPixels * skiaSize / font.metrics.CapHeight)
for {
font = f.createFontWithSkiaSize(skiaSize)
if font.metrics.CapHeight >= capHeightSizeInLogicalPixels {
break
}
skiaSize++
}
for skiaSize >= 1 && font.metrics.CapHeight > capHeightSizeInLogicalPixels {
skiaSize -= 0.5
font = f.createFontWithSkiaSize(skiaSize)
}
}
font.size = capHeightSizeInLogicalPixels
fontSizeCacheLock.Lock()
fontSizeCache[fd] = skiaSize
fontSizeCacheLock.Unlock()
return font
}
// FallbackForCharacter attempts to locate the FontFace that best matches this FontFace and has the given character.
// Will return nil if nothing suitable can be found.
func (f *FontFace) FallbackForCharacter(ch rune) *FontFace {
style := skia.TypeFaceGetFontStyle(f.face)
defer skia.FontStyleDelete(style)
return newFace(skia.FontMgrMatchFamilyStyleCharacter(skia.FontMgrRefDefault(), f.Family(), style, ch))
}
func (f *FontFace) createFontWithSkiaSize(skiaSize float32) *fontImpl {
font := &fontImpl{
face: f,
font: skia.FontNewWithValues(f.face, skiaSize, 1, 0),
}
skia.FontSetSubPixel(font.font, true)
skia.FontSetForceAutoHinting(font.font, true)
// Using hinting on some platforms (Linux, for example) was resulting in bad placement of the text. Carefully test
// any changes away from no font hinting (0) on all supported platforms.
skia.FontSetHinting(font.font, 0)
skia.FontGetMetrics(font.font, &font.metrics)
runtime.SetFinalizer(font, func(obj *fontImpl) {
ReleaseOnUIThread(func() {
skia.FontDelete(obj.font)
})
})
return font
}
// Style returns the style information for this FontFace.
func (f *FontFace) Style() (weightValue weight.Enum, spacingValue spacing.Enum, slantValue slant.Enum) {
style := skia.TypeFaceGetFontStyle(f.face)
defer skia.FontStyleDelete(style)
return weight.Enum(skia.FontStyleGetWeight(style)), spacing.Enum(skia.FontStyleGetWidth(style)),
slant.Enum(skia.FontStyleGetSlant(style))
}
// Monospaced returns true if this FontFace has been marked as having a fixed width for every character.
func (f *FontFace) Monospaced() bool {
return skia.TypeFaceIsFixedPitch(f.face)
}
// Family returns the name of the FontFamily this FontFace belongs to.
func (f *FontFace) Family() string {
ss := skia.TypeFaceGetFamilyName(f.face)
defer skia.StringDelete(ss)
return skia.StringGetString(ss)
}
// UnitsPerEm returns the number of coordinate units on the "em square", an abstract square whose height is the intended
// distance between lines of type in the same type size. This is the size of the design grid on which glyphs are laid
// out.
func (f *FontFace) UnitsPerEm() int {
return skia.TypeFaceGetUnitsPerEm(f.face)
}
// Less returns true if this FontFace is logically before the other FontFace.
func (f *FontFace) Less(other *FontFace) bool {
f1 := f.Family()
f2 := other.Family()
if txt.NaturalLess(f1, f2, true) {
return true
}
if f1 != f2 {
return false
}
w1, sp1, sl1 := f.Style()
w2, sp2, sl2 := other.Style()
if w1 < w2 {
return true
}
if w1 != w2 {
return false
}
if sp1 < sp2 {
return true
}
if sp1 != sp2 {
return false
}
if sl1 < sl2 {
return true
}
return false
}