Skip to content

Commit

Permalink
Merge pull request #118 from redstarcoder/text_cache_upstream
Browse files Browse the repository at this point in the history
Seamless Glyph Cache
  • Loading branch information
llgcode authored Oct 26, 2016
2 parents 401ee66 + 7cc6abe commit 0d961cd
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 20 deletions.
6 changes: 6 additions & 0 deletions draw2dbase/stack_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package draw2dbase

import (
"fmt"
"image"
"image/color"

Expand Down Expand Up @@ -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 changes: 68 additions & 0 deletions draw2dbase/text.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 22 additions & 10 deletions draw2dgl/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 18 additions & 10 deletions draw2dimg/ftgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d961cd

Please sign in to comment.