forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FontAtlasProsessor.go
322 lines (268 loc) · 7.84 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package giu
import (
"fmt"
"log"
"runtime"
"strings"
"sync"
"github.com/AllenDang/go-findfont"
"github.com/AllenDang/imgui-go"
)
const (
preRegisterString = " \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
windows = "windows"
defaultFontSize = 14
)
// FontInfo represents a giu implementation of imgui 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 (f *FontInfo) SetSize(size float32) *FontInfo {
result := *f
result.size = size
for _, i := range Context.FontAtlas.extraFonts {
if i.String() == result.String() {
return &result
}
}
Context.FontAtlas.extraFonts = append(Context.FontAtlas.extraFonts, result)
Context.FontAtlas.shouldRebuildFontAtlas = true
return &result
}
type FontAtlas struct {
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
fontSize float32
}
func newFontAtlas() *FontAtlas {
result := FontAtlas{
extraFontMap: make(map[string]*imgui.Font),
fontSize: defaultFontSize,
}
// Pre register numbers
result.RegisterString(preRegisterString)
// Pre-register fonts
switch runtime.GOOS {
case "darwin":
// English font
result.registerDefaultFont("Menlo", result.fontSize)
// Chinese font
result.registerDefaultFont("STHeiti", result.fontSize-1)
// Jananese font
result.registerDefaultFont("ヒラギノ角ゴシック W0", result.fontSize+3)
// Korean font
result.registerDefaultFont("AppleSDGothicNeo", result.fontSize+2)
case windows:
// English font
result.registerDefaultFont("Calibri", result.fontSize+2)
// Chinese font
result.registerDefaultFont("MSYH", result.fontSize+2)
// Japanese font
result.registerDefaultFont("MSGOTHIC", result.fontSize+2)
// Korean font
result.registerDefaultFont("MALGUNSL", result.fontSize+2)
case "linux":
// English fonts
result.registerDefaultFonts([]FontInfo{
{
fontName: "FreeSans.ttf",
size: result.fontSize + 1,
},
{
fontName: "FiraCode-Medium",
size: result.fontSize + 1,
},
{
fontName: "sans",
size: result.fontSize + 1,
},
})
// Chinese fonts
result.registerDefaultFonts([]FontInfo{
{
fontName: "wqy-microhei",
size: result.fontSize + 1,
},
{
fontName: "SourceHanSansCN",
size: result.fontSize + 3,
},
})
}
return &result
}
// SetDefaultFontSize sets the default font size. Invoke this before MasterWindow.NewMasterWindow(..).
func (a *FontAtlas) SetDefaultFontSize(size float32) {
a.fontSize = size
}
// SetDefaultFont changes default font.
func (a *FontAtlas) 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}
a.defaultFonts = append([]FontInfo{fontInfo}, a.defaultFonts...)
}
// SetDefaultFontFromBytes changes default font by bytes of the font file.
func (a *FontAtlas) SetDefaultFontFromBytes(fontBytes []byte, size float32) {
a.defaultFonts = append([]FontInfo{
{
fontByte: fontBytes,
size: size,
},
}, a.defaultFonts...)
}
func (a *FontAtlas) GetDefaultFonts() []FontInfo {
return a.defaultFonts
}
// AddFont adds font by name, if the font is found, return *FontInfo, otherwise return nil.
// To use added font, use giu.Style().SetFont(...).
func (a *FontAtlas) 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,
}
a.extraFonts = append(a.extraFonts, fi)
return &fi
}
// AddFontFromBytes does similar to AddFont, but using data from memory.
func (a *FontAtlas) AddFontFromBytes(fontName string, fontBytes []byte, size float32) *FontInfo {
fi := FontInfo{
fontName: fontName,
fontByte: fontBytes,
size: size,
}
a.extraFonts = append(a.extraFonts, fi)
return &fi
}
func (a *FontAtlas) registerDefaultFont(fontName string, size float32) {
fontPath, err := findfont.Find(fontName)
if err != nil {
return
}
fontInfo := FontInfo{fontName: fontName, fontPath: fontPath, size: size}
a.defaultFonts = append(a.defaultFonts, fontInfo)
}
func (a *FontAtlas) 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 {
a.defaultFonts = append(a.defaultFonts, *firstFoundFont)
}
}
// RegisterString register string to font atlas builder.
// Note only register strings that will be displayed on the UI.
func (a *FontAtlas) RegisterString(str string) string {
for _, s := range str {
if _, ok := a.stringMap.Load(s); !ok {
a.stringMap.Store(s, false)
a.shouldRebuildFontAtlas = true
}
}
return str
}
// RegisterStringPointer registers string pointer to font atlas builder.
// Note only register strings that will be displayed on the UI.
func (a *FontAtlas) RegisterStringPointer(str *string) *string {
a.RegisterString(*str)
return str
}
// RegisterStringSlice calls RegisterString for each slice element.
func (a *FontAtlas) RegisterStringSlice(str []string) []string {
for _, s := range str {
a.RegisterString(s)
}
return str
}
// Rebuild font atlas when necessary.
func (a *FontAtlas) rebuildFontAtlas() {
if !a.shouldRebuildFontAtlas {
return
}
fonts := Context.IO().Fonts()
fonts.Clear()
var sb strings.Builder
a.stringMap.Range(func(k, v any) bool {
a.stringMap.Store(k, true)
if ks, ok := k.(rune); ok {
sb.WriteRune(ks)
}
return true
})
ranges := imgui.NewGlyphRanges()
builder := imgui.NewFontGlyphRangesBuilder()
// Because we pre-registered 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(a.defaultFonts) > 0 {
fontConfig := imgui.NewFontConfig()
fontConfig.SetOversampleH(2)
fontConfig.SetOversampleV(2)
fontConfig.SetRasterizerMultiply(1.5)
for i, fontInfo := range a.defaultFonts {
if i > 0 {
fontConfig.SetMergeMode(true)
}
// Scale font size with DPI scale factor
if runtime.GOOS == windows {
fontInfo.size *= Context.GetPlatform().GetContentScale()
}
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 a.extraFonts {
// Scale font size with DPI scale factor
if runtime.GOOS == windows {
fontInfo.size *= Context.GetPlatform().GetContentScale()
}
// 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())
}
a.extraFontMap[fontInfo.String()] = &f
}
fontTextureImg := fonts.TextureDataRGBA32()
Context.renderer.SetFontTexture(fontTextureImg)
a.shouldRebuildFontAtlas = false
}