From c2851a6eb626f37e3042e5fc76a550a50a9cd82d Mon Sep 17 00:00:00 2001 From: redstarcoder Date: Sat, 22 Oct 2016 17:28:15 -0400 Subject: [PATCH] Improved speed via new gc.GetFontName method --- draw2dbase/stack_gc.go | 6 ++++++ draw2dgl/gc.go | 12 +++++++++--- draw2dgl/text.go | 14 +++++--------- draw2dimg/ftgc.go | 6 ++++-- gc.go | 2 ++ 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/draw2dbase/stack_gc.go b/draw2dbase/stack_gc.go index 12723c7..00e8990 100644 --- a/draw2dbase/stack_gc.go +++ b/draw2dbase/stack_gc.go @@ -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) +} diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 4142041..c08d1f4 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -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 } diff --git a/draw2dgl/text.go b/draw2dgl/text.go index 4fff4b7..1117f18 100644 --- a/draw2dgl/text.go +++ b/draw2dgl/text.go @@ -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) } diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index 17d4f65..9401d50 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -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 } diff --git a/gc.go b/gc.go index 6266c16..ffd7718 100644 --- a/gc.go +++ b/gc.go @@ -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