Implement seamless glyph cache

This commit is contained in:
redstarcoder 2016-10-20 11:04:17 -04:00
parent 51ba099819
commit 3a5a1d8830
3 changed files with 99 additions and 20 deletions

View file

@ -197,10 +197,16 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
return x - startx
}
func (gc *GraphicContext) FillString(text string) (cursor float64) {
return gc.FillStringAt(text, 0, 0)
}
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
width = gc.CreateStringPath(text, x, y)
gc.Fill()
return width
xorig := x
for _, r := range text {
x += draw2dbase.FillGlyph(gc, x, y, r)
}
return x - xorig
}
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
@ -247,9 +253,11 @@ func (gc *GraphicContext) StrokeString(text string) (width float64) {
}
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
width = gc.CreateStringPath(text, x, y)
gc.Stroke()
return width
xorig := x
for _, r := range text {
x += draw2dbase.StrokeGlyph(gc, x, y, r)
}
return x - xorig
}
// recalc recalculates scale and bounds values from the font size, screen
@ -293,10 +301,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

@ -1,12 +1,83 @@
package draw2dgl
import (
"fmt"
"github.com/golang/freetype/truetype"
"github.com/llgcode/draw2d"
"golang.org/x/image/math/fixed"
)
var glyphCache map[string]map[rune]*glyph
func init() {
glyphCache = make(map[string]map[rune]*glyph)
}
// FillGlyph copies a glyph from the cache, copies it to the gc, and fills it
func FillGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
g := fetchGlyph(gc, chr)
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
gc.Fill(g.Path)
gc.Restore()
return g.Width
}
// StrokeGlyph fetches a glyph from the cache, copies it to the gc, and strokes it
func StrokeGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
g := fetchGlyph(gc, chr)
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
gc.Stroke(g.Path)
gc.Restore()
return g.Width
}
// fetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
func fetchGlyph(gc draw2d.GraphicContext, chr rune) *glyph {
fontData := gc.GetFontData()
fontName := fmt.Sprintf("%s:%d:%d:%d", fontData.Name, fontData.Family, fontData.Style, gc.GetFontSize())
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,
}
}
// DrawContour draws the given closed contour at the given sub-pixel offset.
func DrawContour(path draw2d.PathBuilder, ps []truetype.Point, dx, dy float64) {
if len(ps) == 0 {

View file

@ -117,27 +117,31 @@ 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
for _, r := range text {
x += draw2dbase.FillGlyph(gc, x, y, r)
}
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
for _, r := range text {
x += draw2dbase.StrokeGlyph(gc, x, y, r)
}
return x - xorig
}
func (gc *GraphicContext) loadCurrentFont() (*truetype.Font, error) {