aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/llgcode/draw2d/draw2dbase/text.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-01 22:45:26 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-01 22:45:26 +0100
commit16df0bea5fe8ce0a4ea871d409f589b298567a97 (patch)
treef2f18a2c6773d86a895d075bf8f1a374d48406c4 /vendor/github.com/llgcode/draw2d/draw2dbase/text.go
parent500caaeda74dd9c660279036293f4b2997cf0b03 (diff)
Update vendor
Diffstat (limited to 'vendor/github.com/llgcode/draw2d/draw2dbase/text.go')
-rw-r--r--vendor/github.com/llgcode/draw2d/draw2dbase/text.go45
1 files changed, 30 insertions, 15 deletions
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
}