From 3a5a1d8830b811a6b86ac96adc8024a6d12ed083 Mon Sep 17 00:00:00 2001 From: redstarcoder Date: Thu, 20 Oct 2016 11:04:17 -0400 Subject: [PATCH 1/4] Implement seamless glyph cache --- draw2dgl/gc.go | 24 +++++++++------- draw2dgl/text.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++ draw2dimg/ftgc.go | 24 +++++++++------- 3 files changed, 99 insertions(+), 20 deletions(-) diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 582eabf..4142041 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -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) diff --git a/draw2dgl/text.go b/draw2dgl/text.go index 79cd8c8..4fff4b7 100644 --- a/draw2dgl/text.go +++ b/draw2dgl/text.go @@ -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 { diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index 8bedf91..17d4f65 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -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) { From c2851a6eb626f37e3042e5fc76a550a50a9cd82d Mon Sep 17 00:00:00 2001 From: redstarcoder Date: Sat, 22 Oct 2016 17:28:15 -0400 Subject: [PATCH 2/4] 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 From c2920005d69c6e08ee344c300dd66350ea0b8ca6 Mon Sep 17 00:00:00 2001 From: redstarcoder Date: Sat, 22 Oct 2016 22:49:44 -0400 Subject: [PATCH 3/4] Moved cache code to draw2dbase --- draw2dbase/text.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ draw2dgl/text.go | 67 -------------------------------------------- 2 files changed, 70 insertions(+), 67 deletions(-) create mode 100644 draw2dbase/text.go diff --git a/draw2dbase/text.go b/draw2dbase/text.go new file mode 100644 index 0000000..53eed21 --- /dev/null +++ b/draw2dbase/text.go @@ -0,0 +1,70 @@ +package draw2dbase + +import "github.com/llgcode/draw2d" + +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, fontName string, chr rune) float64 { + g := fetchGlyph(gc, fontName, 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, fontName string, chr rune) float64 { + g := fetchGlyph(gc, fontName, 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, 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, + } +} diff --git a/draw2dgl/text.go b/draw2dgl/text.go index 1117f18..79cd8c8 100644 --- a/draw2dgl/text.go +++ b/draw2dgl/text.go @@ -7,73 +7,6 @@ import ( "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, fontName string, chr rune) float64 { - g := fetchGlyph(gc, fontName, 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, fontName string, chr rune) float64 { - g := fetchGlyph(gc, fontName, 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, 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, - } -} - // 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 { From 7cc6abeee3b221b76e1e24065f4ae4107b35ff0f Mon Sep 17 00:00:00 2001 From: redstarcoder Date: Tue, 25 Oct 2016 21:15:42 -0400 Subject: [PATCH 4/4] Made FetchGlyph and Glyph public, made FillGlyph and StrokeGlyph methods of Glyph, made Glyph.Path private --- draw2dbase/text.go | 74 ++++++++++++++++++++++------------------------ draw2dgl/gc.go | 6 ++-- draw2dimg/ftgc.go | 6 ++-- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/draw2dbase/text.go b/draw2dbase/text.go index 53eed21..4283bad 100644 --- a/draw2dbase/text.go +++ b/draw2dbase/text.go @@ -2,38 +2,16 @@ package draw2dbase import "github.com/llgcode/draw2d" -var glyphCache map[string]map[rune]*glyph +var glyphCache map[string]map[rune]*Glyph func init() { - glyphCache = make(map[string]map[rune]*glyph) + 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, fontName string, chr rune) float64 { - g := fetchGlyph(gc, fontName, 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, fontName string, chr rune) float64 { - g := fetchGlyph(gc, fontName, 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, fontName string, chr 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) + glyphCache[fontName] = make(map[rune]*Glyph, 60) } if glyphCache[fontName][chr] == nil { glyphCache[fontName][chr] = renderGlyph(gc, fontName, chr) @@ -41,30 +19,50 @@ func fetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *glyph { return glyphCache[fontName][chr].Copy() } -// renderGlyph renders a Glyph then caches and returns it -func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *glyph { +// 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, + 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 +// 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(), +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 +} diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index c08d1f4..ec2da25 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -207,7 +207,8 @@ 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, fontName, r) + glyph := draw2dbase.FetchGlyph(gc, fontName, r) + x += glyph.Fill(gc, x, y) } return x - xorig } @@ -261,7 +262,8 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float xorig := x fontName := gc.GetFontName() for _, r := range text { - x += draw2dbase.StrokeGlyph(gc, x, y, fontName, r) + glyph := draw2dbase.FetchGlyph(gc, fontName, r) + x += glyph.Stroke(gc, x, y) } return x - xorig } diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index 9401d50..1f0fa4a 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -126,7 +126,8 @@ 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, fontName, r) + glyph := draw2dbase.FetchGlyph(gc, fontName, r) + x += glyph.Fill(gc, x, y) } return x - xorig } @@ -141,7 +142,8 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float xorig := x fontName := gc.GetFontName() for _, r := range text { - x += draw2dbase.StrokeGlyph(gc, x, y, fontName, r) + glyph := draw2dbase.FetchGlyph(gc, fontName, r) + x += glyph.Stroke(gc, x, y) } return x - xorig }