Merge pull request #118 from redstarcoder/text_cache_upstream

Seamless Glyph Cache
This commit is contained in:
llgcode 2016-10-26 10:35:50 +02:00 committed by GitHub
commit 0d961cd299
5 changed files with 116 additions and 20 deletions

View file

@ -4,6 +4,7 @@
package draw2dbase
import (
"fmt"
"image"
"image/color"
@ -195,3 +196,8 @@ func (gc *StackGraphicContext) Restore() {
oldContext.Previous = nil
}
}
func (gc *StackGraphicContext) GetFontName() string {
fontData := gc.Current.FontData
return fmt.Sprintf("%s:%d:%d:%d", fontData.Name, fontData.Family, fontData.Style, gc.Current.FontSize)
}

68
draw2dbase/text.go Normal file
View file

@ -0,0 +1,68 @@
package draw2dbase
import "github.com/llgcode/draw2d"
var glyphCache map[string]map[rune]*Glyph
func init() {
glyphCache = make(map[string]map[rune]*Glyph)
}
// 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)
}
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
func renderGlyph(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 &Glyph{
path: &path,
Width: width,
}
}
// 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
}
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
}

View file

@ -197,10 +197,20 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
return x - startx
}
// FillString draws the text at point (0, 0)
func (gc *GraphicContext) FillString(text string) (width float64) {
return gc.FillStringAt(text, 0, 0)
}
// FillStringAt draws the text at the specified point (x, y)
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
width = gc.CreateStringPath(text, x, y)
gc.Fill()
return width
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Fill(gc, x, y)
}
return x - xorig
}
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
@ -242,14 +252,20 @@ func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom fl
return left, top, right, bottom
}
// StrokeString draws the contour of the text at point (0, 0)
func (gc *GraphicContext) StrokeString(text string) (width float64) {
return gc.StrokeStringAt(text, 0, 0)
}
// StrokeStringAt draws the contour of the text at point (x, y)
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
width = gc.CreateStringPath(text, x, y)
gc.Stroke()
return width
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
}
return x - xorig
}
// recalc recalculates scale and bounds values from the font size, screen
@ -293,10 +309,6 @@ func (gc *GraphicContext) DrawImage(img image.Image) {
panic("not implemented")
}
func (gc *GraphicContext) FillString(text string) (cursor float64) {
return gc.FillStringAt(text, 0, 0)
}
func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) {
gc.painter.SetColor(color)
rasterizer.Rasterize(gc.painter)

View file

@ -117,27 +117,35 @@ func (gc *GraphicContext) DrawImage(img image.Image) {
}
// FillString draws the text at point (0, 0)
func (gc *GraphicContext) FillString(text string) (cursor float64) {
func (gc *GraphicContext) FillString(text string) (width float64) {
return gc.FillStringAt(text, 0, 0)
}
// FillStringAt draws the text at the specified point (x, y)
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64) {
width := gc.CreateStringPath(text, x, y)
gc.Fill()
return width
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Fill(gc, x, y)
}
return x - xorig
}
// StrokeString draws the contour of the text at point (0, 0)
func (gc *GraphicContext) StrokeString(text string) (cursor float64) {
func (gc *GraphicContext) StrokeString(text string) (width float64) {
return gc.StrokeStringAt(text, 0, 0)
}
// StrokeStringAt draws the contour of the text at point (x, y)
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64) {
width := gc.CreateStringPath(text, x, y)
gc.Stroke()
return width
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
}
return x - xorig
}
func (gc *GraphicContext) loadCurrentFont() (*truetype.Font, error) {

2
gc.go
View file

@ -50,6 +50,8 @@ type GraphicContext interface {
SetFontData(fontData FontData)
// GetFontData gets the current FontData
GetFontData() FontData
// GetFontName gets the current FontData as a string
GetFontName() string
// DrawImage draws the raster image in the current canvas
DrawImage(image image.Image)
// Save the context and push it to the context stack