Removed SetGlyphCache from GraphicContext

Removed GetGlyphCache altogether
Added NewGlyphCache to draw2dbase
This commit is contained in:
redstarcoder 2016-12-23 15:49:20 -05:00
parent 138c71d1c0
commit 795fd573f1
6 changed files with 61 additions and 76 deletions

View File

@ -32,7 +32,7 @@ type ContextStack struct {
Join draw2d.LineJoin
FontSize float64
FontData draw2d.FontData
GlyphCache draw2d.GlyphCache
GlyphCache GlyphCache
Font *truetype.Font
// fontSize and dpi are used to calculate scale. scale is the number of
@ -171,9 +171,9 @@ func (gc *StackGraphicContext) Close() {
gc.Current.Path.Close()
}
// Changes the glyph cache backend used by the GraphicContext.
// Changes the glyph cache backend used by the StackGraphicContext.
// To restore the default glyph cache, call this function passing nil as argument.
func (gc *StackGraphicContext) SetGlyphCache(cache draw2d.GlyphCache) {
func (gc *StackGraphicContext) SetGlyphCache(cache GlyphCache) {
if cache == nil {
gc.Current.GlyphCache = DefaultGlyphCache
} else {
@ -181,11 +181,6 @@ func (gc *StackGraphicContext) SetGlyphCache(cache draw2d.GlyphCache) {
}
}
// Gets the glyph cache backend used by the GraphicContext.
func (gc *StackGraphicContext) GetGlyphCache() draw2d.GlyphCache {
return gc.Current.GlyphCache
}
func (gc *StackGraphicContext) Save() {
context := new(ContextStack)
context.FontSize = gc.Current.FontSize

View File

@ -3,17 +3,32 @@ package draw2dbase
import "github.com/llgcode/draw2d"
var (
DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*draw2d.Glyph)}
DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*Glyph)}
)
// Types implementing this interface can be passed to gc.SetGlyphCache to change the
// way glyphs are being stored and retrieved.
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
// Render renders a glyph then returns it
Render(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph
}
// NewGlyphCache creates and returns a new GlyphCache
func NewGlyphCache() GlyphCache {
return &defaultGlyphCache{make(map[string]map[rune]*Glyph)}
}
type defaultGlyphCache struct {
glyphMap map[string]map[rune]*draw2d.Glyph
glyphMap map[string]map[rune]*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 {
func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
if cache.glyphMap[fontName] == nil {
cache.glyphMap[fontName] = make(map[rune]*draw2d.Glyph, 60)
cache.glyphMap[fontName] = make(map[rune]*Glyph, 60)
}
if cache.glyphMap[fontName][chr] == nil {
cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr)
@ -22,24 +37,50 @@ func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string,
}
// Render renders a glyph then returns it
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
gc.Save()
defer gc.Restore()
gc.BeginPath()
width := gc.CreateStringPath(string(chr), 0, 0)
path := gc.GetPath()
return &draw2d.Glyph{
return &Glyph{
Path: &path,
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)
// 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
// Width of the glyph
Width float64
}
// 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)
// Copy copys the Glyph, and returns the copy
func (g *Glyph) Copy() *Glyph {
return &Glyph{
Path: g.Path.Copy(),
Width: g.Width,
}
}
// Fill fills a Glyph in the specified coordinates
func (g *Glyph) Fill(gc draw2d.GraphicContext, x, y float64) float64 {
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
gc.Fill(g.Path)
gc.Restore()
return g.Width
}
// Stroke strokes a Glyph in the specified coordinates
func (g *Glyph) Stroke(gc draw2d.GraphicContext, x, y float64) float64 {
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
gc.Stroke(g.Path)
gc.Restore()
return g.Width
}

View File

@ -126,7 +126,7 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
glyphBuf *truetype.GlyphBuf
DPI int
DPI int
}
// NewGraphicContext creates a new Graphic context from an image.
@ -217,7 +217,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 := draw2dbase.FetchGlyph(gc, fontName, r)
glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
@ -283,7 +283,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 := draw2dbase.FetchGlyph(gc, fontName, r)
glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}

View File

@ -136,7 +136,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 := draw2dbase.FetchGlyph(gc, fontName, r)
glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
@ -163,7 +163,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 := draw2dbase.FetchGlyph(gc, fontName, r)
glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}

4
gc.go
View File

@ -52,10 +52,6 @@ type GraphicContext interface {
GetFontData() FontData
// GetFontName gets the current FontData as a string
GetFontName() string
// SetGlyphCache changes the glyph cache backend used by the GraphicContext
SetGlyphCache(cache GlyphCache)
// GetGlyphCache gets the current GlyphCache
GetGlyphCache() GlyphCache
// DrawImage draws the raster image in the current canvas
DrawImage(image image.Image)
// Save the context and push it to the context stack

View File

@ -1,47 +0,0 @@
package draw2d
// Types implementing this interface can be passed to gc.SetGlyphCache to change the
// way glyphs are being stored and retrieved.
type GlyphCache interface {
// Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist
Fetch(gc GraphicContext, fontName string, chr rune) *Glyph
// Render renders a glyph then returns it
Render(gc 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 *Path
// Width of the glyph
Width float64
}
// Copy copys the Glyph, and returns the copy
func (g *Glyph) Copy() *Glyph {
return &Glyph{
Path: g.Path.Copy(),
Width: g.Width,
}
}
// Fill fills a Glyph in the specified coordinates
func (g *Glyph) Fill(gc GraphicContext, x, y float64) float64 {
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
gc.Fill(g.Path)
gc.Restore()
return g.Width
}
// Stroke strokes a Glyph in the specified coordinates
func (g *Glyph) Stroke(gc GraphicContext, x, y float64) float64 {
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
gc.Stroke(g.Path)
gc.Restore()
return g.Width
}