forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFontAtlasProsessor.go
155 lines (126 loc) · 3.38 KB
/
FontAtlasProsessor.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
package giu
import (
"fmt"
"strings"
"sync"
"github.com/HACKERALERT/imgui-go"
)
var (
shouldRebuildFontAtlas bool
stringMap sync.Map // key is rune, value indicates whether it's a new rune.
defaultFonts []FontInfo
extraFonts []FontInfo
extraFontMap map[string]*imgui.Font
)
const (
preRegisterString = "\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
)
// FontInfo represents a the font.
//
type FontInfo struct {
fontName string
fontPath string
fontByte []byte
size float32
}
func (f *FontInfo) String() string {
return fmt.Sprintf("%s:%.2f", f.fontName, f.size)
}
func init() {
extraFontMap = make(map[string]*imgui.Font)
// Pre register numbers
tStr(preRegisterString)
}
// SetDefaultFontFromBytes changes default font by bytes of the font file.
func SetDefaultFontFromBytes(fontBytes []byte, size float32) {
defaultFonts = append([]FontInfo{
{
fontByte: fontBytes,
size: size,
},
}, defaultFonts...)
}
// Register string to font atlas builder.
// Note only register strings that will be displayed on the UI.
func tStr(str string) string {
for _, s := range str {
if _, ok := stringMap.Load(s); !ok {
stringMap.Store(s, false)
shouldRebuildFontAtlas = true
}
}
return str
}
// Register string pointer to font atlas builder.
// Note only register strings that will be displayed on the UI.
func tStrPtr(str *string) *string {
tStr(*str)
return str
}
func tStrSlice(str []string) []string {
for _, s := range str {
tStr(s)
}
return str
}
// Rebuild font atlas when necessary.
func rebuildFontAtlas() {
if !shouldRebuildFontAtlas {
return
}
fonts := Context.IO().Fonts()
fonts.Clear()
var sb strings.Builder
stringMap.Range(func(k, v interface{}) bool {
stringMap.Store(k, true)
if ks, ok := k.(rune); ok {
sb.WriteRune(ks)
}
return true
})
ranges := imgui.NewGlyphRanges()
builder := imgui.NewFontGlyphRangesBuilder()
// Because we pre-regestered numbers, so default string map's length should greater then 11.
if sb.Len() > len(preRegisterString) {
builder.AddText(sb.String())
} else {
builder.AddRanges(fonts.GlyphRangesDefault())
}
builder.BuildRanges(ranges)
if len(defaultFonts) > 0 {
fontConfig := imgui.NewFontConfig()
fontConfig.SetOversampleH(2)
fontConfig.SetOversampleV(2)
fontConfig.SetRasterizerMultiply(1.5)
for i, fontInfo := range defaultFonts {
if i > 0 {
fontConfig.SetMergeMode(true)
}
if len(fontInfo.fontByte) == 0 {
fonts.AddFontFromFileTTFV(fontInfo.fontPath, fontInfo.size, fontConfig, ranges.Data())
} else {
fonts.AddFontFromMemoryTTFV(fontInfo.fontByte, fontInfo.size, fontConfig, ranges.Data())
}
}
// Fall back if no font is added
if fonts.GetFontCount() == 0 {
fonts.AddFontDefault()
}
} else {
fonts.AddFontDefault()
}
// Add extra fonts
for _, fontInfo := range extraFonts {
// Store imgui.Font for PushFont
var f imgui.Font
if len(fontInfo.fontByte) == 0 {
f = fonts.AddFontFromFileTTFV(fontInfo.fontPath, fontInfo.size, imgui.DefaultFontConfig, ranges.Data())
} else {
f = fonts.AddFontFromMemoryTTFV(fontInfo.fontByte, fontInfo.size, imgui.DefaultFontConfig, ranges.Data())
}
extraFontMap[fontInfo.String()] = &f
}
fontTextureImg := fonts.TextureDataRGBA32()
Context.renderer.SetFontTexture(fontTextureImg)
shouldRebuildFontAtlas = false
}