Refactor Api using an interface

This commit is contained in:
llgcode 2017-12-04 16:49:08 +01:00
parent a5f7ac8ebe
commit b81f74eb39
2 changed files with 21 additions and 15 deletions

View file

@ -2,22 +2,28 @@ package draw2dbase
import "github.com/llgcode/draw2d"
// GlyphCache manage a map of glyphs
type GlyphCache struct {
// 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
}
// NewGlyphCache initializes a GlyphCache
func NewGlyphCache() *GlyphCache {
func NewGlyphCache() *GlyphCacheImp {
glyphs := make(map[string]map[rune]*Glyph)
return &GlyphCache {
return &GlyphCacheImp {
glyphs: glyphs,
}
}
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
func (glyphCache *GlyphCache) FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
// 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)
}
@ -35,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,
}
}
@ -43,15 +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
}
// Returns a copy of a Glyph
// 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,
}
}
@ -61,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
}
@ -71,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
}

View file

@ -35,7 +35,7 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
FontCache draw2d.FontCache
glyphCache *draw2dbase.GlyphCache
glyphCache draw2dbase.GlyphCache
glyphBuf *truetype.GlyphBuf
DPI int
}
@ -139,7 +139,7 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := gc.glyphCache.FetchGlyph(gc, fontName, r)
glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
@ -166,7 +166,7 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := gc.glyphCache.FetchGlyph(gc, fontName, r)
glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}