This repository has been archived by the owner on Jan 13, 2018. It is now read-only.
/
fonts.go
98 lines (84 loc) · 2.9 KB
/
fonts.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
/*
To the extent possible under law, Ian Davis has waived all copyright
and related or neighboring rights to this Amberfell Source Code file.
This work is published from the United Kingdom.
*/
package main
import (
"github.com/banthar/Go-SDL/sdl"
"github.com/banthar/Go-SDL/ttf"
"github.com/banthar/gl"
"image"
"image/color"
)
type Font struct {
height int
textures map[rune]gl.Texture
widths map[rune]int
}
func NewFont(filename string, size int, c color.Color) *Font {
var font Font
font.textures = make(map[rune]gl.Texture)
font.widths = make(map[rune]int)
extfont := ttf.OpenFont(filename, size)
if extfont == nil {
panic("Could not load font")
}
defer extfont.Close()
font.height = extfont.LineSkip()
for _, ch := range "abcdefghijklmnopqrdstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :;@'<>,.?/~#{}[]!£$%^&*()_-+=\"\\|" {
_, _, _, _, advance, err := extfont.GlyphMetrics(uint16(ch))
if err != 0 {
panic("Could not get glyph metrics")
}
// Create a bitmap with width=advance, height=font.height
surface := sdl.CreateRGBSurface(sdl.SWSURFACE|sdl.SRCALPHA, advance, font.height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
//surface.FillRect(&sdl.Rect{0,0, uint16(advance), uint16(font.height)}, 0x0)
// rect := sdl.Rect{0,0, uint16(advance), uint16(ascent)}
// rect := sdl.Rect{int16(minx), int16(ascent)-int16(maxy), 0, 0}
fontSurface := ttf.RenderText_Blended(extfont, string(ch), sdl.ColorFromGoColor(c))
fontSurface.Blit(nil, surface, nil)
rgba := image.NewRGBA(image.Rect(0, 0, advance, font.height))
for x := 0; x < advance; x++ {
for y := 0; y < font.height; y++ {
rgba.Set(x, y, fontSurface.At(x, font.height-y))
}
}
font.widths[ch] = advance
font.textures[ch] = gl.GenTexture()
font.textures[ch].Bind(gl.TEXTURE_2D)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, advance, font.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, &rgba.Pix[0])
font.textures[ch].Unbind(gl.TEXTURE_2D)
}
return &font
}
func (self *Font) Print(str string) {
for _, ch := range str {
self.textures[ch].Bind(gl.TEXTURE_2D)
h := float32(self.height) * PIXEL_SCALE
w := float32(self.widths[ch]) * PIXEL_SCALE
gl.Color4ub(255, 255, 255, 255)
gl.Begin(gl.QUADS)
gl.TexCoord2d(0, 0)
gl.Vertex2f(0, 0) // Bottom Left Of The Texture and Quad
gl.TexCoord2d(1, 0)
gl.Vertex2f(w, 0) // Bottom Right Of The Texture and Quad
gl.TexCoord2d(1, 1)
gl.Vertex2f(w, h) // Top Right Of The Texture and Quad
gl.TexCoord2d(0, 1)
gl.Vertex2f(0, h) // Top Left Of The Texture and Quad
gl.End()
gl.Translatef(w, 0, 0)
self.textures[ch].Unbind(gl.TEXTURE_2D)
}
}
func (self *Font) Measure(str string) (h, w float64) {
h = float64(self.height) * PIXEL_SCALE
for _, ch := range str {
w += float64(self.widths[ch])
}
w *= PIXEL_SCALE
return
}