text: Refactoring: Reduce a global variable

This commit is contained in:
Hajime Hoshi 2017-07-18 10:49:12 +09:00
parent 86296ea89b
commit fd57753089

View File

@ -119,7 +119,6 @@ func (g *glyph) draw(dst *ebiten.Image, x, y fixed.Int26_6, clr color.Color) {
} }
var ( var (
glyphs = map[char]*glyph{}
atlases = map[int]*atlas{} atlases = map[int]*atlas{}
) )
@ -134,6 +133,8 @@ type atlas struct {
// This value is always power of 2. // This value is always power of 2.
size int size int
charToGlyph map[char]*glyph
// glyphs is the set of glyph information. // glyphs is the set of glyph information.
glyphs []*glyph glyphs []*glyph
@ -165,10 +166,11 @@ func (a *atlas) append(glyph *glyph) {
panic("not reached") panic("not reached")
} }
oldest := a.glyphs[idx] oldest := a.glyphs[idx]
delete(glyphs, oldest.char) delete(a.charToGlyph, oldest.char)
glyph.index = idx glyph.index = idx
a.glyphs[idx] = glyph a.glyphs[idx] = glyph
a.charToGlyph[glyph.char] = glyph
a.draw(glyph) a.draw(glyph)
return return
} }
@ -185,6 +187,7 @@ func (a *atlas) append(glyph *glyph) {
a.num++ a.num++
glyph.index = idx glyph.index = idx
a.glyphs[idx] = glyph a.glyphs[idx] = glyph
a.charToGlyph[glyph.char] = glyph
a.draw(glyph) a.draw(glyph)
} }
@ -215,13 +218,16 @@ func (a *atlas) draw(glyph *glyph) {
func getGlyphFromCache(face font.Face, r rune, now int64) *glyph { func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
ch := char{face, r} ch := char{face, r}
g, ok := glyphs[ch] a, ok := atlases[ch.atlasGroup()]
if ok {
g, ok := a.charToGlyph[ch]
if ok { if ok {
g.atime = now g.atime = now
return g return g
} }
}
g = &glyph{ g := &glyph{
char: ch, char: ch,
atime: now, atime: now,
} }
@ -229,7 +235,6 @@ func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
return g return g
} }
a, ok := atlases[g.char.atlasGroup()]
if !ok { if !ok {
// Don't use ebiten.MaxImageSize here. // Don't use ebiten.MaxImageSize here.
// It's because the back-end image pixels will be restored from GPU // It's because the back-end image pixels will be restored from GPU
@ -246,6 +251,7 @@ func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
a = &atlas{ a = &atlas{
image: i, image: i,
size: g.char.atlasGroup(), size: g.char.atlasGroup(),
charToGlyph: map[char]*glyph{},
} }
w, h := a.image.Size() w, h := a.image.Size()
xnum := w / a.size xnum := w / a.size
@ -255,7 +261,6 @@ func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
} }
a.append(g) a.append(g)
glyphs[g.char] = g
return g return g
} }