-
Notifications
You must be signed in to change notification settings - Fork 4
/
atlas.go
262 lines (238 loc) · 7.44 KB
/
atlas.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
package opengl
import (
"fmt"
"image"
"github.com/hismailbulut/Neoray/pkg/common"
"github.com/hismailbulut/Neoray/pkg/fontkit"
"github.com/hismailbulut/Neoray/pkg/logger"
)
const (
UNSUPPORTED_GLYPH_ID = 0xffffffffffffffff // "Unsupported"
UNDERCURL_GLYPH_ID = 0xfffffffffffffffe // "Undercurl"
)
type Atlas struct {
kit *fontkit.FontKit
fontSize, dpi float64
useBoxDrawing bool
useBlockDrawing bool
texture Texture
cache map[uint64]common.Rectangle[int]
pen common.Vector2[int]
}
func (atlas *Atlas) String() string {
return fmt.Sprintf("Atlas(ID: %d, Font Size: %f, Pen: %v)",
atlas.texture.id,
atlas.fontSize,
atlas.pen,
)
}
func (context *Context) NewAtlas(kit *fontkit.FontKit, size, dpi float64, useBoxDrawing, useBlockDrawing bool) *Atlas {
atlas := new(Atlas)
atlas.kit = kit
atlas.fontSize = size
atlas.dpi = dpi
atlas.useBoxDrawing = useBoxDrawing
atlas.useBlockDrawing = useBlockDrawing
// 512 * 512 * RGBA8 = 1mib
const width = 512
const height = 512
// In most cases these size of texture is highly enough
// But we also grow it if needed
atlas.texture = context.CreateTexture(width, height)
atlas.cache = make(map[uint64]common.Rectangle[int])
atlas.issueHack()
return atlas
}
func (atlas *Atlas) issueHack() {
// HACK
// I dont know why but if the renderer first draws a character
// between 0x2588 and 0x258F (unicode block characters) strange things
// happens. This is not happening before Neoray version 0.2
// To prevent this we just drawing 'a' first
// But this is not a real fix. TODO: We should address this
// We are doing this every time atlas recreated
// Try change 'a' to one of characters in range [0x2588, 0x258F] and see the result
atlas.GetCharPos('a', false, false, false, false, atlas.ImageSize())
}
func (atlas *Atlas) FontKit() *fontkit.FontKit {
if atlas.kit != nil {
return atlas.kit
}
return fontkit.Default()
}
func (atlas *Atlas) SetFontKit(kit *fontkit.FontKit) {
atlas.kit = kit
atlas.Reset()
}
func (atlas *Atlas) FontSize() float64 {
return atlas.fontSize
}
func (atlas *Atlas) SetFontSize(fontSize, dpi float64) {
atlas.fontSize = fontSize
atlas.dpi = dpi
atlas.Reset()
}
func (atlas *Atlas) SetBoxDrawing(useBoxDrawing, useBlockDrawing bool) {
atlas.useBoxDrawing = useBoxDrawing
atlas.useBlockDrawing = useBlockDrawing
atlas.Reset()
}
func (atlas *Atlas) Reset() {
atlas.texture.Clear()
atlas.cache = make(map[uint64]common.Rectangle[int])
atlas.pen = common.Vector2[int]{}
atlas.issueHack()
}
func (atlas *Atlas) ImageSize() common.Vector2[int] {
face, err := atlas.FontKit().DefaultFont().CreateFace(fontkit.FaceParams{
Size: atlas.fontSize,
DPI: atlas.dpi,
UseBoxDrawing: atlas.useBoxDrawing,
UseBlockDrawing: atlas.useBlockDrawing,
})
if err != nil {
panic(err)
}
return face.ImageSize()
}
func getCharID(char rune, italic, bold, underline, strikethrough bool) uint64 {
id := uint64(char)
if italic {
id = id | uint64(1)<<32
}
if bold {
id = id | uint64(1)<<40
}
if underline {
id = id | uint64(1)<<48
}
if strikethrough {
id = id | uint64(1)<<56
}
return id
}
// Draws img to texture and returns position
func (atlas *Atlas) drawImage(img *image.RGBA) common.Rectangle[int] {
textureSize := atlas.texture.Size()
// Check X
if atlas.pen.X+img.Rect.Dx() > textureSize.Width() {
atlas.pen.X = 0
atlas.pen.Y += img.Rect.Dy()
}
// Check Y
if atlas.pen.Y+img.Rect.Dy() > textureSize.Height() {
// We must grow the texture
atlas.texture.Resize(textureSize.Width()*2, textureSize.Height()*2)
logger.Log(logger.DEBUG, "Atlas", atlas.texture.id, "texture resized to %v", atlas.texture.Size())
// Resizing texture also clears it, so we should also clear the cache
atlas.cache = make(map[uint64]common.Rectangle[int])
atlas.pen = common.Vector2[int]{}
atlas.issueHack()
}
// draw image to current pen
dest := common.Rect(atlas.pen.X, atlas.pen.Y, img.Rect.Dx(), img.Rect.Dy())
// We should bind texture before drawing to it
atlas.texture.Bind()
atlas.texture.Draw(img, dest)
// increment pen
atlas.pen.X += img.Rect.Dx()
return dest
}
func (atlas *Atlas) drawChar(face *fontkit.Face, id uint64, char rune, underline, strikethrough bool, imgSize common.Vector2[int]) common.Rectangle[int] {
img := face.RenderChar(char, underline, strikethrough, imgSize)
pos := atlas.drawImage(img)
atlas.cache[id] = pos
return pos
}
func (atlas *Atlas) suitableFont(char rune, bold, italic bool) (*fontkit.Font, bool) {
if atlas.FontKit().SuitableFont(bold, italic).ContainsGlyph(char) {
return atlas.FontKit().SuitableFont(bold, italic), true
}
if atlas.FontKit().DefaultFont().ContainsGlyph(char) {
return atlas.FontKit().DefaultFont(), true
}
if fontkit.Default().SuitableFont(bold, italic).ContainsGlyph(char) {
return fontkit.Default().SuitableFont(bold, italic), true
}
if fontkit.Default().DefaultFont().ContainsGlyph(char) {
return fontkit.Default().DefaultFont(), true
}
// Neither of the fonts supports this glyph
return atlas.FontKit().SuitableFont(bold, italic), false
}
// For the first time draws and caches undercurl image, returns image pos and true representing first time
// After that uses cached image and returns false
func (atlas *Atlas) Undercurl(imgSize common.Vector2[int]) (common.Rectangle[int], bool) {
pos, ok := atlas.cache[UNDERCURL_GLYPH_ID]
if ok {
return pos, false
}
// Draw and cache
face, err := atlas.FontKit().DefaultFont().CreateFace(fontkit.FaceParams{
Size: atlas.fontSize,
DPI: atlas.dpi,
UseBoxDrawing: atlas.useBoxDrawing,
UseBlockDrawing: atlas.useBlockDrawing,
})
if err != nil {
panic(fmt.Errorf("face creation failed: %s", err))
}
img := face.RenderUndercurl(imgSize)
pos = atlas.drawImage(img)
atlas.cache[UNDERCURL_GLYPH_ID] = pos
return pos, true
}
func (atlas *Atlas) unsupported(char rune, imgSize common.Vector2[int]) common.Rectangle[int] {
pos, ok := atlas.cache[UNSUPPORTED_GLYPH_ID]
if ok {
return pos
}
// Draw and cache
face, err := atlas.FontKit().DefaultFont().CreateFace(fontkit.FaceParams{
Size: atlas.fontSize,
DPI: atlas.dpi,
UseBoxDrawing: atlas.useBoxDrawing,
UseBlockDrawing: atlas.useBlockDrawing,
})
if err != nil {
panic(fmt.Errorf("face creation failed: %s", err))
}
// Draw and cache
pos = atlas.drawChar(face, UNSUPPORTED_GLYPH_ID, char, false, false, imgSize)
return pos
}
func (atlas *Atlas) GetCharPos(char rune, bold, italic, underline, strikethrough bool, imgSize common.Vector2[int]) common.Rectangle[int] {
id := getCharID(char, italic, bold, underline, strikethrough)
pos, ok := atlas.cache[id]
if ok {
return pos
}
font, contains := atlas.suitableFont(char, bold, italic)
face, err := font.CreateFace(fontkit.FaceParams{
Size: atlas.fontSize,
DPI: atlas.dpi,
UseBoxDrawing: atlas.useBoxDrawing,
UseBlockDrawing: atlas.useBlockDrawing,
})
if err != nil {
panic(fmt.Errorf("face creation failed: %s", err))
}
if contains {
// Draw and cache
pos = atlas.drawChar(face, id, char, underline, strikethrough, imgSize)
return pos
} else {
// unsupported
return atlas.unsupported(char, imgSize)
}
}
// Normalization required when updating texture position to the gpu
func (atlas *Atlas) Normalize(pos common.Rectangle[int]) common.Rectangle[float32] {
return atlas.texture.Normalize(pos)
}
func (atlas *Atlas) BindTexture() {
atlas.texture.Bind()
}
func (atlas *Atlas) Destroy() {
atlas.texture.Delete()
}