-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitmap.go
59 lines (49 loc) · 1.45 KB
/
bitmap.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
package font
import (
"bytes"
"image"
"image/png"
"github.com/hajimehoshi/ebiten/v2"
"github.com/pkg/errors"
)
// A Bitmap is a representation of a font. In practice, fonts are PNG
// images which are a set of rendered characters, or glyphs. This also
// means that fonts are essentially bitmapped, and they can only scale
// in accordance to the aspect ratio of the display.
type Bitmap struct {
// The source image of the font bitmap.
img *ebiten.Image
// This is the size of an individual glyph, which in a bitmap must
// be uniform across all glyphs.
size image.Point
// Not every bitmap can uniformly draw every character you imagine;
// the mask ensures that there is some limit to what we would
// allow.
mask int
submap map[rune]*Glyph
}
// NewBitmap returns a new font type, from which you can render text on
// the screen. The possible fonts for this function are defined as
// consts.
func NewBitmap(fn Name) (*Bitmap, error) {
// Each font is hard-coded into the binary as a slice of bytes.
info, err := fontInfo(fn)
if err != nil {
return nil, err
}
// Which is then decoded.
pngImage, err := png.Decode(bytes.NewReader(info.byts))
if err != nil {
return nil, errors.Wrapf(err, "font %+v", fn)
}
ebiImage := ebiten.NewImageFromImage(pngImage)
if err != nil {
return nil, errors.Wrapf(err, "font %+v", fn)
}
return &Bitmap{
img: ebiImage,
size: info.size,
mask: info.mask,
submap: make(map[rune]*Glyph),
}, nil
}