Merge pull request #118 from redstarcoder/text_cache_upstream
Seamless Glyph Cache
This commit is contained in:
commit
0d961cd299
|
@ -4,6 +4,7 @@
|
||||||
package draw2dbase
|
package draw2dbase
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
|
@ -195,3 +196,8 @@ func (gc *StackGraphicContext) Restore() {
|
||||||
oldContext.Previous = nil
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -197,10 +197,20 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
|
||||||
return x - startx
|
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) {
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
||||||
width = gc.CreateStringPath(text, x, y)
|
xorig := x
|
||||||
gc.Fill()
|
fontName := gc.GetFontName()
|
||||||
return width
|
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.
|
// 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
|
return left, top, right, bottom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StrokeString draws the contour of the text at point (0, 0)
|
||||||
func (gc *GraphicContext) StrokeString(text string) (width float64) {
|
func (gc *GraphicContext) StrokeString(text string) (width float64) {
|
||||||
return gc.StrokeStringAt(text, 0, 0)
|
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) {
|
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
|
||||||
width = gc.CreateStringPath(text, x, y)
|
xorig := x
|
||||||
gc.Stroke()
|
fontName := gc.GetFontName()
|
||||||
return width
|
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
|
// 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")
|
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) {
|
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)
|
||||||
|
|
|
@ -117,27 +117,35 @@ func (gc *GraphicContext) DrawImage(img image.Image) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FillString draws the text at point (0, 0)
|
// 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)
|
return gc.FillStringAt(text, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FillStringAt draws the text at the specified point (x, y)
|
// FillStringAt draws the text at the specified point (x, y)
|
||||||
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64) {
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
||||||
width := gc.CreateStringPath(text, x, y)
|
xorig := x
|
||||||
gc.Fill()
|
fontName := gc.GetFontName()
|
||||||
return width
|
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)
|
// 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)
|
return gc.StrokeStringAt(text, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrokeStringAt draws the contour of the text at point (x, y)
|
// StrokeStringAt draws the contour of the text at point (x, y)
|
||||||
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64) {
|
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
|
||||||
width := gc.CreateStringPath(text, x, y)
|
xorig := x
|
||||||
gc.Stroke()
|
fontName := gc.GetFontName()
|
||||||
return width
|
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) {
|
func (gc *GraphicContext) loadCurrentFont() (*truetype.Font, error) {
|
||||||
|
|
2
gc.go
2
gc.go
|
@ -50,6 +50,8 @@ type GraphicContext interface {
|
||||||
SetFontData(fontData FontData)
|
SetFontData(fontData FontData)
|
||||||
// GetFontData gets the current FontData
|
// GetFontData gets the current FontData
|
||||||
GetFontData() FontData
|
GetFontData() FontData
|
||||||
|
// GetFontName gets the current FontData as a string
|
||||||
|
GetFontName() string
|
||||||
// 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
|
||||||
|
|
Loading…
Reference in New Issue