-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonset.go
74 lines (59 loc) · 1.36 KB
/
fonset.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
package fcfonts
import (
"log"
"github.com/benoitkugler/textprocessing/pango"
)
var _ pango.Fontset = (*Fontset)(nil)
// Fontset implements the pango.Fontset interface.
type Fontset struct {
key *fontsetKey
patterns *cachedPattern
fonts []*Font // lazily filled
currentPatternIndex int
}
func newFontset(key fontsetKey, patterns *cachedPattern) *Fontset {
var fs Fontset
fs.key = &key
fs.patterns = patterns
return &fs
}
func (fs *Fontset) GetLanguage() pango.Language { return fs.key.language }
// may return nil
func (fs *Fontset) loadNextFont() *Font {
pattern := fs.patterns.pattern
fontPattern, prepare := fs.patterns.getFontPattern(fs.currentPatternIndex)
fs.currentPatternIndex++
if fontPattern == nil {
return nil
}
if prepare {
fontPattern = fs.patterns.fontmap.Config.PrepareRender(pattern, fontPattern)
}
font, err := fs.key.fontmap.newFont(*fs.key, fontPattern)
if err != nil {
log.Println(err)
}
return font
}
// lazy loading
func (fs *Fontset) getFontAt(i int) *Font {
for i >= len(fs.fonts) {
font := fs.loadNextFont()
fs.fonts = append(fs.fonts, font)
if font == nil {
return nil
}
}
return fs.fonts[i]
}
func (fs *Fontset) Foreach(fn pango.FontsetForeachFunc) {
for i := 0; ; i++ {
font := fs.getFontAt(i)
if font == nil {
return
}
if fn(font) {
return
}
}
}