Made the GlyphCache settable per GraphicContext
This commit is contained in:
parent
df6b7d6902
commit
138c71d1c0
4 changed files with 80 additions and 68 deletions
|
@ -32,6 +32,7 @@ type ContextStack struct {
|
||||||
Join draw2d.LineJoin
|
Join draw2d.LineJoin
|
||||||
FontSize float64
|
FontSize float64
|
||||||
FontData draw2d.FontData
|
FontData draw2d.FontData
|
||||||
|
GlyphCache draw2d.GlyphCache
|
||||||
|
|
||||||
Font *truetype.Font
|
Font *truetype.Font
|
||||||
// fontSize and dpi are used to calculate scale. scale is the number of
|
// fontSize and dpi are used to calculate scale. scale is the number of
|
||||||
|
@ -57,6 +58,7 @@ func NewStackGraphicContext() *StackGraphicContext {
|
||||||
gc.Current.Join = draw2d.RoundJoin
|
gc.Current.Join = draw2d.RoundJoin
|
||||||
gc.Current.FontSize = 10
|
gc.Current.FontSize = 10
|
||||||
gc.Current.FontData = DefaultFontData
|
gc.Current.FontData = DefaultFontData
|
||||||
|
gc.Current.GlyphCache = DefaultGlyphCache
|
||||||
return gc
|
return gc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,6 +171,21 @@ func (gc *StackGraphicContext) Close() {
|
||||||
gc.Current.Path.Close()
|
gc.Current.Path.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Changes the glyph cache backend used by the GraphicContext.
|
||||||
|
// To restore the default glyph cache, call this function passing nil as argument.
|
||||||
|
func (gc *StackGraphicContext) SetGlyphCache(cache draw2d.GlyphCache) {
|
||||||
|
if cache == nil {
|
||||||
|
gc.Current.GlyphCache = DefaultGlyphCache
|
||||||
|
} else {
|
||||||
|
gc.Current.GlyphCache = cache
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gets the glyph cache backend used by the GraphicContext.
|
||||||
|
func (gc *StackGraphicContext) GetGlyphCache() draw2d.GlyphCache {
|
||||||
|
return gc.Current.GlyphCache
|
||||||
|
}
|
||||||
|
|
||||||
func (gc *StackGraphicContext) Save() {
|
func (gc *StackGraphicContext) Save() {
|
||||||
context := new(ContextStack)
|
context := new(ContextStack)
|
||||||
context.FontSize = gc.Current.FontSize
|
context.FontSize = gc.Current.FontSize
|
||||||
|
@ -184,6 +201,7 @@ func (gc *StackGraphicContext) Save() {
|
||||||
context.Path = gc.Current.Path.Copy()
|
context.Path = gc.Current.Path.Copy()
|
||||||
context.Font = gc.Current.Font
|
context.Font = gc.Current.Font
|
||||||
context.Scale = gc.Current.Scale
|
context.Scale = gc.Current.Scale
|
||||||
|
context.GlyphCache = gc.Current.GlyphCache
|
||||||
copy(context.Tr[:], gc.Current.Tr[:])
|
copy(context.Tr[:], gc.Current.Tr[:])
|
||||||
context.Previous = gc.Current
|
context.Previous = gc.Current
|
||||||
gc.Current = context
|
gc.Current = context
|
||||||
|
|
|
@ -3,38 +3,17 @@ package draw2dbase
|
||||||
import "github.com/llgcode/draw2d"
|
import "github.com/llgcode/draw2d"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
glyphCacheDefault = &defaultGlyphCache{make(map[string]map[rune]*Glyph)}
|
DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*draw2d.Glyph)}
|
||||||
glyphCache GlyphCache = glyphCacheDefault
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Types implementing this interface can be passed to 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Changes the glyph cache backend used by the package.
|
|
||||||
// To restore the default glyph cache, call this function passing nil as argument.
|
|
||||||
func SetGlyphCache(cache GlyphCache) {
|
|
||||||
if cache == nil {
|
|
||||||
glyphCache = glyphCacheDefault
|
|
||||||
} else {
|
|
||||||
glyphCache = cache
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type defaultGlyphCache struct {
|
type defaultGlyphCache struct {
|
||||||
glyphMap map[string]map[rune]*Glyph
|
glyphMap map[string]map[rune]*draw2d.Glyph
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist
|
// 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) *Glyph {
|
func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
||||||
if cache.glyphMap[fontName] == nil {
|
if cache.glyphMap[fontName] == nil {
|
||||||
cache.glyphMap[fontName] = make(map[rune]*Glyph, 60)
|
cache.glyphMap[fontName] = make(map[rune]*draw2d.Glyph, 60)
|
||||||
}
|
}
|
||||||
if cache.glyphMap[fontName][chr] == nil {
|
if cache.glyphMap[fontName][chr] == nil {
|
||||||
cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr)
|
cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr)
|
||||||
|
@ -43,60 +22,24 @@ func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render renders a glyph then returns it
|
// Render renders a glyph then returns it
|
||||||
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
|
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
||||||
gc.Save()
|
gc.Save()
|
||||||
defer gc.Restore()
|
defer gc.Restore()
|
||||||
gc.BeginPath()
|
gc.BeginPath()
|
||||||
width := gc.CreateStringPath(string(chr), 0, 0)
|
width := gc.CreateStringPath(string(chr), 0, 0)
|
||||||
path := gc.GetPath()
|
path := gc.GetPath()
|
||||||
return &Glyph{
|
return &draw2d.Glyph{
|
||||||
path: &path,
|
Path: &path,
|
||||||
Width: width,
|
Width: width,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
|
// 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 {
|
func FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
||||||
return glyphCache.Fetch(gc, fontName, chr)
|
return gc.GetGlyphCache().Fetch(gc, fontName, chr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// renderGlyph renders a glyph then caches and returns it
|
// renderGlyph renders a glyph then caches and returns it
|
||||||
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
|
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph {
|
||||||
return glyphCache.Render(gc, fontName, chr)
|
return gc.GetGlyphCache().Render(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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy copys the Glyph, and returns the copy
|
|
||||||
func (g *Glyph) Copy() *Glyph {
|
|
||||||
return &Glyph{
|
|
||||||
path: g.path.Copy(),
|
|
||||||
Width: g.Width,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill copies a glyph from the cache, and fills it
|
|
||||||
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 fetches a glyph from the cache, and strokes it
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
4
gc.go
4
gc.go
|
@ -52,6 +52,10 @@ type GraphicContext interface {
|
||||||
GetFontData() FontData
|
GetFontData() FontData
|
||||||
// GetFontName gets the current FontData as a string
|
// GetFontName gets the current FontData as a string
|
||||||
GetFontName() 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 draws the raster image in the current canvas
|
||||||
DrawImage(image image.Image)
|
DrawImage(image image.Image)
|
||||||
// Save the context and push it to the context stack
|
// Save the context and push it to the context stack
|
||||||
|
|
47
glyph.go
Normal file
47
glyph.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in a new issue