2016-10-23 02:49:44 +00:00
|
|
|
package draw2dbase
|
|
|
|
|
|
|
|
import "github.com/llgcode/draw2d"
|
|
|
|
|
2016-12-17 20:44:51 +00:00
|
|
|
var (
|
2016-12-20 23:20:10 +00:00
|
|
|
DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*draw2d.Glyph)}
|
2016-12-17 20:44:51 +00:00
|
|
|
)
|
2016-10-23 02:49:44 +00:00
|
|
|
|
2016-12-17 20:44:51 +00:00
|
|
|
type defaultGlyphCache struct {
|
2016-12-20 23:20:10 +00:00
|
|
|
glyphMap map[string]map[rune]*draw2d.Glyph
|
2016-12-17 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist
|
2016-12-20 23:20:10 +00:00
|
|
|
func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
2016-12-17 20:44:51 +00:00
|
|
|
if cache.glyphMap[fontName] == nil {
|
2016-12-20 23:20:10 +00:00
|
|
|
cache.glyphMap[fontName] = make(map[rune]*draw2d.Glyph, 60)
|
2016-10-23 02:49:44 +00:00
|
|
|
}
|
2016-12-17 20:44:51 +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
|
|
|
}
|
|
|
|
|
2016-12-17 20:44:51 +00:00
|
|
|
// Render renders a glyph then returns it
|
2016-12-20 23:20:10 +00:00
|
|
|
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()
|
2016-12-20 23:20:10 +00:00
|
|
|
return &draw2d.Glyph{
|
|
|
|
Path: &path,
|
2016-10-23 02:49:44 +00:00
|
|
|
Width: width,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 20:44:51 +00:00
|
|
|
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
|
2016-12-20 23:20:10 +00:00
|
|
|
func FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
|
|
|
return gc.GetGlyphCache().Fetch(gc, fontName, chr)
|
2016-12-17 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// renderGlyph renders a glyph then caches and returns it
|
2016-12-20 23:20:10 +00:00
|
|
|
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
|
|
|
return gc.GetGlyphCache().Render(gc, fontName, chr)
|
2016-10-26 01:15:42 +00:00
|
|
|
}
|