-
Notifications
You must be signed in to change notification settings - Fork 12
/
font.go
265 lines (220 loc) · 6.15 KB
/
font.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
package main
import (
"fmt"
"image"
_ "image/png"
"log"
"os"
"path/filepath"
"strings"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/mathgl/mgl64"
"github.com/shurcooL-legacy/Conception-go/caret"
)
var oFontBase, oFontBackground uint32
var lodBias float64 = -66.67
// FontOptions specifies the properties of the font.
type FontOptions uint8
const (
Regular FontOptions = iota
Bold
Italic
BoldItalic
)
// IsBold returns true if the font has the bold property set.
func (fo FontOptions) IsBold() bool { return fo == Bold || fo == BoldItalic }
// IsItalic returns true if the font has the italic property set.
func (fo FontOptions) IsItalic() bool { return fo == Italic || fo == BoldItalic }
type OpenGLStream struct {
pos mgl64.Vec2
lineStartX float64
advance uint32
FontOptions FontOptions
BorderColor *mgl64.Vec3 // nil means no border color.
BackgroundColor *mgl64.Vec3 // nil means no background color.
ShowInvisibles bool
}
func NewOpenGLStream(pos mgl64.Vec2) *OpenGLStream {
return &OpenGLStream{pos: pos, lineStartX: pos[0]}
}
func (o *OpenGLStream) SetPos(pos mgl64.Vec2) {
o.pos = pos
o.lineStartX = pos[0]
o.advance = 0
}
func (o *OpenGLStream) SetPosWithExpandedPosition(pos mgl64.Vec2, x, y uint32) {
o.pos = pos.Add(mgl64.Vec2{float64(x * fontWidth), float64(y * fontHeight)})
o.lineStartX = pos[0]
o.advance = x
}
func (o *OpenGLStream) PrintText(s string) {
for {
end := strings.Index(s, "\n")
length := len(s)
if end != -1 {
length = end
}
o.PrintLine(s[:length])
if end == -1 {
break
} else {
//o.NewLine()
o.PrintSegment(" ") // Newline
o.pos[1] += fontHeight
o.advanceReset()
s = s[end+1:]
}
}
}
// Input shouldn't have newlines
func (o *OpenGLStream) PrintLine(s string) {
if o.BorderColor != nil {
gl.PushAttrib(gl.CURRENT_BIT)
expandedLineLength := caret.ExpandedLength(s, o.advance)
backgroundColor := nearlyWhiteColor
if o.BackgroundColor != nil {
backgroundColor = *o.BackgroundColor
}
DrawInnerRoundedBox(o.pos, mgl64.Vec2{fontWidth * float64(expandedLineLength), fontHeight}, *o.BorderColor, backgroundColor)
gl.PopAttrib()
}
segments := strings.Split(s, "\t")
for index, segment := range segments {
o.PrintSegment(segment)
o.advanceBy(uint32(len(segment)))
if index+1 < len(segments) {
tabSpaces := 4 - (o.advance % 4)
o.PrintSegment(strings.Repeat(" ", int(tabSpaces))) // Tab.
if o.ShowInvisibles {
gl.PushAttrib(gl.CURRENT_BIT)
DrawBorderlessBox(o.pos.Add(mgl64.Vec2{1, fontHeight/2 - 1}), mgl64.Vec2{fontWidth*float64(tabSpaces) - 2, 2}, selectedTextDarkColor)
gl.PopAttrib()
}
o.advanceBy(tabSpaces)
}
}
}
func (o *OpenGLStream) advanceBy(amount uint32) {
o.advance += amount
o.afterAdvance()
}
func (o *OpenGLStream) advanceReset() {
o.advance = 0
o.afterAdvance()
}
func (o *OpenGLStream) afterAdvance() {
o.pos[0] = o.lineStartX + float64(fontWidth*o.advance)
}
// Shouldn't have tabs nor newlines
func (o *OpenGLStream) PrintSegment(s string) {
if s == "" {
return
}
if o.BackgroundColor != nil && o.BorderColor == nil {
gl.PushAttrib(gl.CURRENT_BIT)
gl.Color3dv(&o.BackgroundColor[0])
gl.PushMatrix()
gl.Translated(o.pos[0], o.pos[1], 0)
for range s {
gl.CallList(oFontBackground)
}
gl.PopMatrix()
gl.PopAttrib()
}
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, float32(lodBias*0.01))
gl.Enable(gl.BLEND)
defer gl.Disable(gl.BLEND)
gl.Enable(gl.TEXTURE_2D)
defer gl.Disable(gl.TEXTURE_2D)
gl.PushMatrix()
gl.Translated(o.pos[0], o.pos[1], 0)
gl.ListBase(oFontBase + uint32(o.FontOptions)*96)
gl.CallLists(int32(len(s)), gl.UNSIGNED_BYTE, gl.Ptr(&[]byte(s)[0]))
gl.PopMatrix()
//checkGLError()
}
// ---
const fontWidth, fontHeight = 6, 12
func InitFont() {
LoadTexture(filepath.Join("data", "fonts", "Menlo.png"))
oFontBase = gl.GenLists(32 + 96*4)
for i := 0; i < 96*4; i++ {
const shiftX, shiftY = float64(1.0 / 16), float64(1.0 / 6 / 4)
indexX, indexY := i%16, i/16
gl.NewList(oFontBase+uint32(i+32), gl.COMPILE)
gl.Begin(gl.QUADS)
gl.TexCoord2d(float64(indexX)*shiftX, float64(indexY)*shiftY)
gl.Vertex2i(0, 0)
gl.TexCoord2d(float64(indexX+1)*shiftX, float64(indexY)*shiftY)
gl.Vertex2i(fontWidth, 0)
gl.TexCoord2d(float64(indexX+1)*shiftX, float64(indexY+1)*shiftY)
gl.Vertex2i(fontWidth, fontHeight)
gl.TexCoord2d(float64(indexX)*shiftX, float64(indexY+1)*shiftY)
gl.Vertex2i(0, fontHeight)
gl.End()
gl.Translated(fontWidth, 0.0, 0.0)
gl.EndList()
}
oFontBackground = gl.GenLists(1)
gl.NewList(oFontBackground, gl.COMPILE)
gl.Begin(gl.QUADS)
gl.Vertex2i(0, 0)
gl.Vertex2i(0, fontHeight)
gl.Vertex2i(fontWidth, fontHeight)
gl.Vertex2i(fontWidth, 0)
gl.End()
gl.Translated(fontWidth, 0.0, 0.0)
gl.EndList()
checkGLError()
}
func DeinitFont() {
gl.DeleteLists(oFontBase, 32+96*4)
gl.DeleteLists(oFontBackground, 1)
}
func LoadTexture(path string) {
//fmt.Printf("Trying to load texture %q: ", path)
// Open the file
file, err := os.Open(path)
if err != nil {
fmt.Println(os.Getwd())
log.Fatal(err)
}
defer file.Close()
// Decode the image
img, _, err := image.Decode(file)
if err != nil {
log.Fatal(err)
}
bounds := img.Bounds()
//fmt.Printf("Loaded %vx%v texture.\n", bounds.Dx(), bounds.Dy())
var format int
var pixPointer *uint8
switch img := img.(type) {
case *image.RGBA:
format = gl.RGBA
pixPointer = &img.Pix[0]
case *image.NRGBA:
format = gl.RGBA
pixPointer = &img.Pix[0]
case *image.Gray:
format = gl.ALPHA
pixPointer = &img.Pix[0]
default:
log.Fatalf("LoadTexture: Unsupported type %T.\n", img)
}
var texture uint32
gl.GenTextures(1, &texture)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexParameteri(gl.TEXTURE_2D, gl.GENERATE_MIPMAP, gl.TRUE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, -0.5)
gl.TexImage2D(gl.TEXTURE_2D, 0, int32(format), int32(bounds.Dx()), int32(bounds.Dy()), 0, uint32(format), gl.UNSIGNED_BYTE, gl.Ptr(pixPointer))
checkGLError()
}
func checkGLError() {
err := gl.GetError()
if err != 0 {
panic(fmt.Errorf("GL error: %v", err))
}
}