text: Integrate emptyGlyphs and glyphImageCache to reduce runtine.mapaccess2

This commit is contained in:
Hajime Hoshi 2021-01-05 13:12:46 +09:00
parent fdad70ca46
commit c8454bdfed

View File

@ -85,21 +85,13 @@ type glyphImageCacheEntry struct {
var (
glyphImageCache = map[font.Face]map[rune]*glyphImageCacheEntry{}
emptyGlyphs = map[font.Face]map[rune]struct{}{}
)
func getGlyphImage(face font.Face, r rune) *ebiten.Image {
if _, ok := emptyGlyphs[face]; !ok {
emptyGlyphs[face] = map[rune]struct{}{}
}
if _, ok := glyphImageCache[face]; !ok {
glyphImageCache[face] = map[rune]*glyphImageCacheEntry{}
}
if _, ok := emptyGlyphs[face][r]; ok {
return nil
}
if e, ok := glyphImageCache[face][r]; ok {
e.atime = now()
return e.image
@ -108,7 +100,10 @@ func getGlyphImage(face font.Face, r rune) *ebiten.Image {
b := getGlyphBounds(face, r)
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
if w == 0 || h == 0 {
emptyGlyphs[face][r] = struct{}{}
glyphImageCache[face][r] = &glyphImageCacheEntry{
image: nil,
atime: now(),
}
return nil
}