Implemented a proper GlyphCache interface

This commit is contained in:
redstarcoder 2016-12-17 15:44:51 -05:00
parent 1286d3b203
commit df6b7d6902

View file

@ -2,25 +2,48 @@ package draw2dbase
import "github.com/llgcode/draw2d" import "github.com/llgcode/draw2d"
var glyphCache map[string]map[rune]*Glyph var (
glyphCacheDefault = &defaultGlyphCache{make(map[string]map[rune]*Glyph)}
glyphCache GlyphCache = glyphCacheDefault
)
func init() { // Types implementing this interface can be passed to SetGlyphCache to change the
glyphCache = make(map[string]map[rune]*Glyph) // way glyphs are being stored and retrieved.
type GlyphCache interface {
// Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist
Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph
// Render renders a glyph then returns it
Render(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph
} }
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist // Changes the glyph cache backend used by the package.
func FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { // To restore the default glyph cache, call this function passing nil as argument.
if glyphCache[fontName] == nil { func SetGlyphCache(cache GlyphCache) {
glyphCache[fontName] = make(map[rune]*Glyph, 60) if cache == nil {
glyphCache = glyphCacheDefault
} else {
glyphCache = cache
} }
if glyphCache[fontName][chr] == nil {
glyphCache[fontName][chr] = renderGlyph(gc, fontName, chr)
}
return glyphCache[fontName][chr].Copy()
} }
// renderGlyph renders a glyph then caches and returns it type defaultGlyphCache struct {
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { glyphMap map[string]map[rune]*Glyph
}
// Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist
func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
if cache.glyphMap[fontName] == nil {
cache.glyphMap[fontName] = make(map[rune]*Glyph, 60)
}
if cache.glyphMap[fontName][chr] == nil {
cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr)
}
return cache.glyphMap[fontName][chr].Copy()
}
// Render renders a glyph then returns it
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
gc.Save() gc.Save()
defer gc.Restore() defer gc.Restore()
gc.BeginPath() gc.BeginPath()
@ -32,6 +55,16 @@ func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
} }
} }
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
func FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
return glyphCache.Fetch(gc, fontName, chr)
}
// renderGlyph renders a glyph then caches and returns it
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
return glyphCache.Render(gc, fontName, chr)
}
// Glyph represents a rune which has been converted to a Path and width // Glyph represents a rune which has been converted to a Path and width
type Glyph struct { type Glyph struct {
// path represents a glyph, it is always at (0, 0) // path represents a glyph, it is always at (0, 0)
@ -40,6 +73,7 @@ type Glyph struct {
Width float64 Width float64
} }
// Copy copys the Glyph, and returns the copy
func (g *Glyph) Copy() *Glyph { func (g *Glyph) Copy() *Glyph {
return &Glyph{ return &Glyph{
path: g.path.Copy(), path: g.path.Copy(),