From 16df0bea5fe8ce0a4ea871d409f589b298567a97 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 1 Jan 2018 22:45:26 +0100 Subject: Update vendor --- .../github.com/llgcode/draw2d/draw2dbase/text.go | 45 ++++++++++++++-------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'vendor/github.com/llgcode/draw2d/draw2dbase/text.go') diff --git a/vendor/github.com/llgcode/draw2d/draw2dbase/text.go b/vendor/github.com/llgcode/draw2d/draw2dbase/text.go index 4283bad..22b7acf 100644 --- a/vendor/github.com/llgcode/draw2d/draw2dbase/text.go +++ b/vendor/github.com/llgcode/draw2d/draw2dbase/text.go @@ -2,21 +2,35 @@ package draw2dbase import "github.com/llgcode/draw2d" -var glyphCache map[string]map[rune]*Glyph +// GlyphCache manage a cache of glyphs +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 +} + +// GlyphCacheImp manage a map of glyphs without sync mecanism, not thread safe +type GlyphCacheImp struct { + glyphs map[string]map[rune]*Glyph +} -func init() { - glyphCache = make(map[string]map[rune]*Glyph) + +// NewGlyphCache initializes a GlyphCache +func NewGlyphCache() *GlyphCacheImp { + glyphs := make(map[string]map[rune]*Glyph) + return &GlyphCacheImp { + glyphs: glyphs, + } } -// 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 { - if glyphCache[fontName] == nil { - glyphCache[fontName] = make(map[rune]*Glyph, 60) +// Fetch fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist +func (glyphCache *GlyphCacheImp) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { + if glyphCache.glyphs[fontName] == nil { + glyphCache.glyphs[fontName] = make(map[rune]*Glyph, 60) } - if glyphCache[fontName][chr] == nil { - glyphCache[fontName][chr] = renderGlyph(gc, fontName, chr) + if glyphCache.glyphs[fontName][chr] == nil { + glyphCache.glyphs[fontName][chr] = renderGlyph(gc, fontName, chr) } - return glyphCache[fontName][chr].Copy() + return glyphCache.glyphs[fontName][chr].Copy() } // renderGlyph renders a glyph then caches and returns it @@ -27,7 +41,7 @@ func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { width := gc.CreateStringPath(string(chr), 0, 0) path := gc.GetPath() return &Glyph{ - path: &path, + Path: &path, Width: width, } } @@ -35,14 +49,15 @@ func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { // Glyph represents a rune which has been converted to a Path and width type Glyph struct { // path represents a glyph, it is always at (0, 0) - path *draw2d.Path + Path *draw2d.Path // Width of the glyph Width float64 } +// Copy Returns a copy of a Glyph func (g *Glyph) Copy() *Glyph { return &Glyph{ - path: g.path.Copy(), + Path: g.Path.Copy(), Width: g.Width, } } @@ -52,7 +67,7 @@ func (g *Glyph) Fill(gc draw2d.GraphicContext, x, y float64) float64 { gc.Save() gc.BeginPath() gc.Translate(x, y) - gc.Fill(g.path) + gc.Fill(g.Path) gc.Restore() return g.Width } @@ -62,7 +77,7 @@ func (g *Glyph) Stroke(gc draw2d.GraphicContext, x, y float64) float64 { gc.Save() gc.BeginPath() gc.Translate(x, y) - gc.Stroke(g.path) + gc.Stroke(g.Path) gc.Restore() return g.Width } -- cgit v1.2.3