-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
217 lines (171 loc) · 5.16 KB
/
font.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
package painter
import (
"image"
"sync"
"github.com/Jaeel/fyne"
"github.com/Jaeel/fyne/internal/cache"
"github.com/Jaeel/fyne/theme"
"github.com/goki/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
const (
// DefaultTabWidth is the default width in spaces
DefaultTabWidth = 4
// TextDPI is a global constant that determines how text scales to interface sizes
TextDPI = 78
)
func loadFont(data fyne.Resource) *truetype.Font {
loaded, err := truetype.Parse(data.Content())
if err != nil {
fyne.LogError("font load error", err)
}
return loaded
}
// RenderedTextSize looks up how big a string would be if drawn on screen.
// It also returns the distance from top to the text baseline.
func RenderedTextSize(text string, fontSize float32, style fyne.TextStyle) (size fyne.Size, baseline float32) {
size, base := cache.GetFontMetrics(text, fontSize, style)
if base != 0 {
return size, base
}
size, base = measureText(text, fontSize, style)
cache.SetFontMetrics(text, fontSize, style, size, base)
return size, base
}
func measureText(text string, fontSize float32, style fyne.TextStyle) (fyne.Size, float32) {
var opts truetype.Options
opts.Size = float64(fontSize)
opts.DPI = TextDPI
face := CachedFontFace(style, &opts)
advance := MeasureString(face, text, style.TabWidth)
return fyne.NewSize(fixed266ToFloat32(advance), fixed266ToFloat32(face.Metrics().Height)),
fixed266ToFloat32(face.Metrics().Ascent)
}
type compositeFace struct {
sync.Mutex
chosen, fallback font.Face
chosenFont, fallbackFont *truetype.Font
}
func (c *compositeFace) containsGlyph(font *truetype.Font, r rune) bool {
c.Lock()
defer c.Unlock()
return font != nil && font.Index(r) != 0
}
func (c *compositeFace) Close() error {
if c.chosen != nil {
_ = c.chosen.Close()
}
return c.fallback.Close()
}
func (c *compositeFace) Glyph(dot fixed.Point26_6, r rune) (
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
contains := c.containsGlyph(c.chosenFont, r)
c.Lock()
defer c.Unlock()
if contains {
return c.chosen.Glyph(dot, r)
}
return c.fallback.Glyph(dot, r)
}
func (c *compositeFace) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
contains := c.containsGlyph(c.chosenFont, r)
c.Lock()
defer c.Unlock()
if contains {
c.chosen.GlyphBounds(r)
}
return c.fallback.GlyphBounds(r)
}
func (c *compositeFace) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
contains := c.containsGlyph(c.chosenFont, r)
c.Lock()
defer c.Unlock()
if contains {
return c.chosen.GlyphAdvance(r)
}
return c.fallback.GlyphAdvance(r)
}
func (c *compositeFace) Kern(r0, r1 rune) fixed.Int26_6 {
contains0 := c.containsGlyph(c.chosenFont, r0)
contains1 := c.containsGlyph(c.chosenFont, r1)
c.Lock()
defer c.Unlock()
if contains0 && contains1 {
return c.chosen.Kern(r0, r1)
}
return c.fallback.Kern(r0, r1)
}
func (c *compositeFace) Metrics() font.Metrics {
c.Lock()
defer c.Unlock()
return c.chosen.Metrics()
}
func newFontWithFallback(chosen, fallback font.Face, chosenFont, fallbackFont *truetype.Font) font.Face {
return &compositeFace{chosen: chosen, fallback: fallback, chosenFont: chosenFont, fallbackFont: fallbackFont}
}
type fontCacheItem struct {
font, fallback *truetype.Font
faces map[truetype.Options]font.Face
}
var fontCache = make(map[fyne.TextStyle]*fontCacheItem)
var fontCacheLock = new(sync.Mutex)
// CachedFontFace returns a font face held in memory. These are loaded from the current theme.
func CachedFontFace(style fyne.TextStyle, opts *truetype.Options) font.Face {
fontCacheLock.Lock()
defer fontCacheLock.Unlock()
comp := fontCache[style]
if comp == nil {
var f1, f2 *truetype.Font
switch {
case style.Monospace:
f1 = loadFont(theme.TextMonospaceFont())
f2 = loadFont(theme.DefaultTextMonospaceFont())
case style.Bold:
if style.Italic {
f1 = loadFont(theme.TextBoldItalicFont())
f2 = loadFont(theme.DefaultTextBoldItalicFont())
} else {
f1 = loadFont(theme.TextBoldFont())
f2 = loadFont(theme.DefaultTextBoldFont())
}
case style.Italic:
f1 = loadFont(theme.TextItalicFont())
f2 = loadFont(theme.DefaultTextItalicFont())
default:
f1 = loadFont(theme.TextFont())
f2 = loadFont(theme.DefaultTextFont())
}
if f1 == nil {
f1 = f2
}
comp = &fontCacheItem{font: f1, fallback: f2, faces: make(map[truetype.Options]font.Face)}
fontCache[style] = comp
}
face := comp.faces[*opts]
if face == nil {
f1 := truetype.NewFace(comp.font, opts)
f2 := truetype.NewFace(comp.fallback, opts)
face = newFontWithFallback(f1, f2, comp.font, comp.fallback)
comp.faces[*opts] = face
}
return face
}
// ClearFontCache is used to remove cached fonts in the case that we wish to re-load font faces
func ClearFontCache() {
fontCacheLock.Lock()
defer fontCacheLock.Unlock()
for _, item := range fontCache {
for _, face := range item.faces {
err := face.Close()
if err != nil {
fyne.LogError("failed to close font face", err)
return
}
}
}
fontCache = make(map[fyne.TextStyle]*fontCacheItem)
}
func fixed266ToFloat32(i fixed.Int26_6) float32 {
return float32(float64(i) / (1 << 6))
}