draw2d/draw2dbase/text.go

46 lines
1.4 KiB
Go
Raw Normal View History

2016-10-23 02:49:44 +00:00
package draw2dbase
import "github.com/llgcode/draw2d"
var (
DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*draw2d.Glyph)}
)
2016-10-23 02:49:44 +00:00
type defaultGlyphCache struct {
glyphMap map[string]map[rune]*draw2d.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) *draw2d.Glyph {
if cache.glyphMap[fontName] == nil {
cache.glyphMap[fontName] = make(map[rune]*draw2d.Glyph, 60)
2016-10-23 02:49:44 +00:00
}
if cache.glyphMap[fontName][chr] == nil {
cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr)
}
return cache.glyphMap[fontName][chr].Copy()
2016-10-23 02:49:44 +00:00
}
// Render renders a glyph then returns it
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
2016-10-23 02:49:44 +00:00
gc.Save()
defer gc.Restore()
gc.BeginPath()
width := gc.CreateStringPath(string(chr), 0, 0)
path := gc.GetPath()
return &draw2d.Glyph{
Path: &path,
2016-10-23 02:49:44 +00:00
Width: width,
}
}
// 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) *draw2d.Glyph {
return gc.GetGlyphCache().Fetch(gc, fontName, chr)
}
// renderGlyph renders a glyph then caches and returns it
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
return gc.GetGlyphCache().Render(gc, fontName, chr)
}