This commit is contained in:
redstarcoder 2017-11-28 13:49:44 +00:00 committed by GitHub
commit 6cfaf16bbc
3 changed files with 69 additions and 27 deletions

View File

@ -2,67 +2,85 @@ package draw2dbase
import "github.com/llgcode/draw2d" import "github.com/llgcode/draw2d"
var glyphCache map[string]map[rune]*Glyph var (
DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*Glyph)}
)
func init() { // Types implementing this interface can be passed to gc.SetGlyphCache to change the
glyphCache = make(map[string]map[rune]*Glyph) // 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
} }
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist // NewGlyphCache creates and returns a new GlyphCache
func FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { func NewGlyphCache() GlyphCache {
if glyphCache[fontName] == nil { return &defaultGlyphCache{make(map[string]map[rune]*Glyph)}
glyphCache[fontName] = make(map[rune]*Glyph, 60)
}
if glyphCache[fontName][chr] == nil {
glyphCache[fontName][chr] = renderGlyph(gc, fontName, chr)
}
return glyphCache[fontName][chr].Copy()
} }
// renderGlyph renders a glyph then caches and returns it type defaultGlyphCache struct {
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *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) *Glyph {
if cache.glyphMap[fontName] == nil {
cache.glyphMap[fontName] = make(map[rune]*Glyph, 60)
}
if cache.glyphMap[fontName][chr] == nil {
cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr)
}
return cache.glyphMap[fontName][chr].Copy()
}
// Render renders a glyph then returns it
func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *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 &Glyph{
path: &path, Path: &path,
Width: width, Width: width,
} }
} }
// Glyph represents a rune which has been converted to a Path and width // Glyph represents a rune which has been converted to a Path and width
type Glyph struct { type Glyph struct {
// path represents a glyph, it is always at (0, 0) // Path represents a glyph, it is always at (0, 0)
path *draw2d.Path Path *draw2d.Path
// Width of the glyph // Width of the glyph
Width float64 Width float64
} }
// Copy copys the Glyph, and returns the copy
func (g *Glyph) Copy() *Glyph { func (g *Glyph) Copy() *Glyph {
return &Glyph{ return &Glyph{
path: g.path.Copy(), Path: g.Path.Copy(),
Width: g.Width, Width: g.Width,
} }
} }
// Fill copies a glyph from the cache, and fills it // Fill fills a Glyph in the specified coordinates
func (g *Glyph) Fill(gc draw2d.GraphicContext, x, y float64) float64 { func (g *Glyph) Fill(gc draw2d.GraphicContext, x, y float64) float64 {
gc.Save() gc.Save()
gc.BeginPath() gc.BeginPath()
gc.Translate(x, y) gc.Translate(x, y)
gc.Fill(g.path) gc.Fill(g.Path)
gc.Restore() gc.Restore()
return g.Width return g.Width
} }
// Stroke fetches a glyph from the cache, and strokes it // Stroke strokes a Glyph in the specified coordinates
func (g *Glyph) Stroke(gc draw2d.GraphicContext, x, y float64) float64 { func (g *Glyph) Stroke(gc draw2d.GraphicContext, x, y float64) float64 {
gc.Save() gc.Save()
gc.BeginPath() gc.BeginPath()
gc.Translate(x, y) gc.Translate(x, y)
gc.Stroke(g.path) gc.Stroke(g.Path)
gc.Restore() gc.Restore()
return g.Width return g.Width
} }

View File

@ -126,7 +126,8 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer strokeRasterizer *raster.Rasterizer
glyphBuf *truetype.GlyphBuf glyphBuf *truetype.GlyphBuf
DPI int glyphCache draw2dbase.GlyphCache
DPI int
} }
// NewGraphicContext creates a new Graphic context from an image. // NewGraphicContext creates a new Graphic context from an image.
@ -137,6 +138,7 @@ func NewGraphicContext(width, height int) *GraphicContext {
raster.NewRasterizer(width, height), raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height), raster.NewRasterizer(width, height),
&truetype.GlyphBuf{}, &truetype.GlyphBuf{},
draw2dbase.DefaultGlyphCache,
92, 92,
} }
return gc return gc
@ -217,7 +219,7 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64
if hasPrev { if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
} }
glyph := draw2dbase.FetchGlyph(gc, fontName, r) glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y) x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true prev, hasPrev = index, true
} }
@ -283,7 +285,7 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float
if hasPrev { if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
} }
glyph := draw2dbase.FetchGlyph(gc, fontName, r) glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y) x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true prev, hasPrev = index, true
} }
@ -312,6 +314,16 @@ func (gc *GraphicContext) SetFontSize(fontSize float64) {
gc.recalc() gc.recalc()
} }
// Changes the glyph cache backend used by the GraphicContext.
// To restore the default glyph cache, call this function passing nil as argument.
func (gc *GraphicContext) SetGlyphCache(cache draw2dbase.GlyphCache) {
if cache == nil {
gc.glyphCache = draw2dbase.DefaultGlyphCache
} else {
gc.glyphCache = cache
}
}
func (gc *GraphicContext) GetDPI() int { func (gc *GraphicContext) GetDPI() int {
return gc.DPI return gc.DPI
} }

View File

@ -36,6 +36,7 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer strokeRasterizer *raster.Rasterizer
glyphBuf *truetype.GlyphBuf glyphBuf *truetype.GlyphBuf
glyphCache draw2dbase.GlyphCache
DPI int DPI int
} }
@ -75,6 +76,7 @@ func NewGraphicContextWithPainter(img draw.Image, painter Painter) *GraphicConte
raster.NewRasterizer(width, height), raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height), raster.NewRasterizer(width, height),
&truetype.GlyphBuf{}, &truetype.GlyphBuf{},
draw2dbase.DefaultGlyphCache,
dpi, dpi,
} }
return gc return gc
@ -136,7 +138,7 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64
if hasPrev { if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
} }
glyph := draw2dbase.FetchGlyph(gc, fontName, r) glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y) x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true prev, hasPrev = index, true
} }
@ -163,7 +165,7 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float
if hasPrev { if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
} }
glyph := draw2dbase.FetchGlyph(gc, fontName, r) glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y) x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true prev, hasPrev = index, true
} }
@ -291,6 +293,16 @@ func (gc *GraphicContext) SetFontSize(fontSize float64) {
gc.recalc() gc.recalc()
} }
// Changes the glyph cache backend used by the GraphicContext.
// To restore the default glyph cache, call this function passing nil as argument.
func (gc *GraphicContext) SetGlyphCache(cache draw2dbase.GlyphCache) {
if cache == nil {
gc.glyphCache = draw2dbase.DefaultGlyphCache
} else {
gc.glyphCache = cache
}
}
func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) { func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) {
gc.painter.SetColor(color) gc.painter.SetColor(color)
rasterizer.Rasterize(gc.painter) rasterizer.Rasterize(gc.painter)