forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFontAtlasProsessor.go
252 lines (211 loc) · 5.67 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package giu
import (
"fmt"
"log"
"runtime"
"strings"
"sync"
"github.com/AllenDang/go-findfont"
"github.com/AllenDang/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{|}~"
)
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)
// Pre-register fonts
os := runtime.GOOS
switch os {
case "darwin":
// English font
registerDefaultFont("Menlo", 14)
// Chinese font
registerDefaultFont("STHeiti", 13)
// Jananese font
registerDefaultFont("ヒラギノ角ゴシック W0", 17)
// Korean font
registerDefaultFont("AppleSDGothicNeo", 16)
case "windows":
// English font
registerDefaultFont("Calibri", 16)
// Chinese font
registerDefaultFont("MSYH", 16)
// Japanese font
registerDefaultFont("MSGOTHIC", 16)
// Korean font
registerDefaultFont("MALGUNSL", 16)
case "linux":
// English fonts
registerDefaultFonts([]FontInfo{
{
fontName: "FreeSans.ttf",
size: 15,
},
{
fontName: "FiraCode-Medium",
size: 15,
},
})
}
}
// Change default font
func SetDefaultFont(fontName string, size float32) {
fontPath, err := findfont.Find(fontName)
if err != nil {
log.Fatalf("Cannot find font %s", fontName)
return
}
fontInfo := FontInfo{fontName: fontName, fontPath: fontPath, size: size}
defaultFonts = append([]FontInfo{fontInfo}, defaultFonts...)
}
// Change default font by bytes of the font file
func SetDefaultFontFromBytes(fontBytes []byte, size float32) {
defaultFonts = append([]FontInfo{
{
fontByte: fontBytes,
size: size,
},
}, defaultFonts...)
}
// Add font by name, if the font is found, return *FontInfo, otherwise return nil.
// To use added font, use giu.Style().SetFont(...).
func AddFont(fontName string, size float32) *FontInfo {
fontPath, err := findfont.Find(fontName)
if err != nil {
fmt.Printf("[Warning]Cannot find font %s at system, related text will not be rendered.\n", fontName)
return nil
}
fi := FontInfo{
fontName: fontName,
fontPath: fontPath,
size: size,
}
extraFonts = append(extraFonts, fi)
return &fi
}
func AddFontFromBytes(fontName string, fontBytes []byte, size float32) *FontInfo {
fi := FontInfo{
fontName: fontName,
fontByte: fontBytes,
size: size,
}
extraFonts = append(extraFonts, fi)
return &fi
}
func registerDefaultFont(fontName string, size float32) {
fontPath, err := findfont.Find(fontName)
if err != nil {
return
}
fontInfo := FontInfo{fontName: fontName, fontPath: fontPath, size: size}
defaultFonts = append(defaultFonts, fontInfo)
}
func registerDefaultFonts(fontInfos []FontInfo) {
var firstFoundFont *FontInfo
for _, fi := range fontInfos {
fontPath, err := findfont.Find(fi.fontName)
if err == nil {
firstFoundFont = &FontInfo{fontName: fi.fontName, fontPath: fontPath, size: fi.size}
break
}
}
if firstFoundFont != nil {
defaultFonts = append(defaultFonts, *firstFoundFont)
}
}
// 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
}
// 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
}