Improved speed via new gc.GetFontName method

This commit is contained in:
redstarcoder 2016-10-22 17:28:15 -04:00
parent 3a5a1d8830
commit c2851a6eb6
5 changed files with 26 additions and 14 deletions

View file

@ -4,6 +4,7 @@
package draw2dbase
import (
"fmt"
"image"
"image/color"
@ -191,3 +192,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)
}

View file

@ -197,14 +197,17 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
return x - startx
}
func (gc *GraphicContext) FillString(text string) (cursor float64) {
// 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) {
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
x += draw2dbase.FillGlyph(gc, x, y, r)
x += draw2dbase.FillGlyph(gc, x, y, fontName, r)
}
return x - xorig
}
@ -248,14 +251,17 @@ 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) {
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
x += draw2dbase.StrokeGlyph(gc, x, y, r)
x += draw2dbase.StrokeGlyph(gc, x, y, fontName, r)
}
return x - xorig
}

View file

@ -1,8 +1,6 @@
package draw2dgl
import (
"fmt"
"github.com/golang/freetype/truetype"
"github.com/llgcode/draw2d"
@ -16,8 +14,8 @@ func init() {
}
// 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)
func FillGlyph(gc draw2d.GraphicContext, x, y float64, fontName string, chr rune) float64 {
g := fetchGlyph(gc, fontName, chr)
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
@ -27,8 +25,8 @@ func FillGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
}
// 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)
func StrokeGlyph(gc draw2d.GraphicContext, x, y float64, fontName string, chr rune) float64 {
g := fetchGlyph(gc, fontName, chr)
gc.Save()
gc.BeginPath()
gc.Translate(x, y)
@ -38,9 +36,7 @@ func StrokeGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
}
// 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())
func fetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *glyph {
if glyphCache[fontName] == nil {
glyphCache[fontName] = make(map[rune]*glyph, 60)
}

View file

@ -124,8 +124,9 @@ func (gc *GraphicContext) FillString(text string) (width float64) {
// FillStringAt draws the text at the specified point (x, y)
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
x += draw2dbase.FillGlyph(gc, x, y, r)
x += draw2dbase.FillGlyph(gc, x, y, fontName, r)
}
return x - xorig
}
@ -138,8 +139,9 @@ func (gc *GraphicContext) StrokeString(text string) (width float64) {
// StrokeStringAt draws the contour of the text at point (x, y)
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
xorig := x
fontName := gc.GetFontName()
for _, r := range text {
x += draw2dbase.StrokeGlyph(gc, x, y, r)
x += draw2dbase.StrokeGlyph(gc, x, y, fontName, r)
}
return x - xorig
}

2
gc.go
View file

@ -48,6 +48,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