From 795fd573f1f95f8cbf5c483337b75f8a3cca79fe Mon Sep 17 00:00:00 2001 From: redstarcoder Date: Fri, 23 Dec 2016 15:49:20 -0500 Subject: [PATCH] Removed SetGlyphCache from GraphicContext Removed GetGlyphCache altogether Added NewGlyphCache to draw2dbase --- draw2dbase/stack_gc.go | 11 ++----- draw2dbase/text.go | 65 ++++++++++++++++++++++++++++++++++-------- draw2dgl/gc.go | 6 ++-- draw2dimg/ftgc.go | 4 +-- gc.go | 4 --- glyph.go | 47 ------------------------------ 6 files changed, 61 insertions(+), 76 deletions(-) delete mode 100644 glyph.go diff --git a/draw2dbase/stack_gc.go b/draw2dbase/stack_gc.go index af333d9..ed5f508 100644 --- a/draw2dbase/stack_gc.go +++ b/draw2dbase/stack_gc.go @@ -32,7 +32,7 @@ type ContextStack struct { Join draw2d.LineJoin FontSize float64 FontData draw2d.FontData - GlyphCache draw2d.GlyphCache + GlyphCache GlyphCache Font *truetype.Font // fontSize and dpi are used to calculate scale. scale is the number of @@ -171,9 +171,9 @@ func (gc *StackGraphicContext) Close() { gc.Current.Path.Close() } -// Changes the glyph cache backend used by the GraphicContext. +// Changes the glyph cache backend used by the StackGraphicContext. // To restore the default glyph cache, call this function passing nil as argument. -func (gc *StackGraphicContext) SetGlyphCache(cache draw2d.GlyphCache) { +func (gc *StackGraphicContext) SetGlyphCache(cache GlyphCache) { if cache == nil { gc.Current.GlyphCache = DefaultGlyphCache } else { @@ -181,11 +181,6 @@ func (gc *StackGraphicContext) SetGlyphCache(cache draw2d.GlyphCache) { } } -// Gets the glyph cache backend used by the GraphicContext. -func (gc *StackGraphicContext) GetGlyphCache() draw2d.GlyphCache { - return gc.Current.GlyphCache -} - func (gc *StackGraphicContext) Save() { context := new(ContextStack) context.FontSize = gc.Current.FontSize diff --git a/draw2dbase/text.go b/draw2dbase/text.go index 6873cad..1665363 100644 --- a/draw2dbase/text.go +++ b/draw2dbase/text.go @@ -3,17 +3,32 @@ package draw2dbase import "github.com/llgcode/draw2d" var ( - DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*draw2d.Glyph)} + DefaultGlyphCache = &defaultGlyphCache{make(map[string]map[rune]*Glyph)} ) +// Types implementing this interface can be passed to gc.SetGlyphCache to change the +// way glyphs are being stored and retrieved. +type GlyphCache interface { + // Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist + Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph + + // Render renders a glyph then returns it + Render(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph +} + +// NewGlyphCache creates and returns a new GlyphCache +func NewGlyphCache() GlyphCache { + return &defaultGlyphCache{make(map[string]map[rune]*Glyph)} +} + type defaultGlyphCache struct { - glyphMap map[string]map[rune]*draw2d.Glyph + glyphMap map[string]map[rune]*Glyph } // Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist -func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph { +func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph { if cache.glyphMap[fontName] == nil { - cache.glyphMap[fontName] = make(map[rune]*draw2d.Glyph, 60) + cache.glyphMap[fontName] = make(map[rune]*Glyph, 60) } if cache.glyphMap[fontName][chr] == nil { cache.glyphMap[fontName][chr] = cache.Render(gc, fontName, chr) @@ -22,24 +37,50 @@ func (cache *defaultGlyphCache) Fetch(gc draw2d.GraphicContext, fontName string, } // Render renders a glyph then returns it -func (cache *defaultGlyphCache) Render(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph { +func (cache *defaultGlyphCache) Render(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 &draw2d.Glyph{ + return &Glyph{ Path: &path, Width: 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) *draw2d.Glyph { - return gc.GetGlyphCache().Fetch(gc, fontName, chr) +// 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 } -// renderGlyph renders a glyph then caches and returns it -func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *draw2d.Glyph { - return gc.GetGlyphCache().Render(gc, fontName, chr) +// Copy copys the Glyph, and returns the copy +func (g *Glyph) Copy() *Glyph { + return &Glyph{ + Path: g.Path.Copy(), + Width: g.Width, + } +} + +// Fill fills a Glyph in the specified coordinates +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 strokes a Glyph in the specified coordinates +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 d512ccc..3a338e0 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -126,7 +126,7 @@ type GraphicContext struct { fillRasterizer *raster.Rasterizer strokeRasterizer *raster.Rasterizer glyphBuf *truetype.GlyphBuf - DPI int + DPI int } // NewGraphicContext creates a new Graphic context from an image. @@ -217,7 +217,7 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64 if hasPrev { x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) } - glyph := draw2dbase.FetchGlyph(gc, fontName, r) + glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r) x += glyph.Fill(gc, x, y) prev, hasPrev = index, true } @@ -283,7 +283,7 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float if hasPrev { x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) } - glyph := draw2dbase.FetchGlyph(gc, fontName, r) + glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r) x += glyph.Stroke(gc, x, y) prev, hasPrev = index, true } diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index abaf7e5..77c8571 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -136,7 +136,7 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64 if hasPrev { x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) } - glyph := draw2dbase.FetchGlyph(gc, fontName, r) + glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r) x += glyph.Fill(gc, x, y) prev, hasPrev = index, true } @@ -163,7 +163,7 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float if hasPrev { x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) } - glyph := draw2dbase.FetchGlyph(gc, fontName, r) + glyph := gc.Current.GlyphCache.Fetch(gc, fontName, r) x += glyph.Stroke(gc, x, y) prev, hasPrev = index, true } diff --git a/gc.go b/gc.go index ec0023a..543359e 100644 --- a/gc.go +++ b/gc.go @@ -52,10 +52,6 @@ type GraphicContext interface { GetFontData() FontData // GetFontName gets the current FontData as a string GetFontName() string - // SetGlyphCache changes the glyph cache backend used by the GraphicContext - SetGlyphCache(cache GlyphCache) - // GetGlyphCache gets the current GlyphCache - GetGlyphCache() GlyphCache // DrawImage draws the raster image in the current canvas DrawImage(image image.Image) // Save the context and push it to the context stack diff --git a/glyph.go b/glyph.go deleted file mode 100644 index 9dcb36c..0000000 --- a/glyph.go +++ /dev/null @@ -1,47 +0,0 @@ -package draw2d - -// Types implementing this interface can be passed to gc.SetGlyphCache to change the -// way glyphs are being stored and retrieved. -type GlyphCache interface { - // Fetch fetches a glyph from the cache, storing with Render first if it doesn't already exist - Fetch(gc GraphicContext, fontName string, chr rune) *Glyph - - // Render renders a glyph then returns it - Render(gc GraphicContext, fontName string, chr rune) *Glyph -} - -// 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 *Path - // Width of the glyph - Width float64 -} - -// Copy copys the Glyph, and returns the copy -func (g *Glyph) Copy() *Glyph { - return &Glyph{ - Path: g.Path.Copy(), - Width: g.Width, - } -} - -// Fill fills a Glyph in the specified coordinates -func (g *Glyph) Fill(gc GraphicContext, x, y float64) float64 { - gc.Save() - gc.BeginPath() - gc.Translate(x, y) - gc.Fill(g.Path) - gc.Restore() - return g.Width -} - -// Stroke strokes a Glyph in the specified coordinates -func (g *Glyph) Stroke(gc GraphicContext, x, y float64) float64 { - gc.Save() - gc.BeginPath() - gc.Translate(x, y) - gc.Stroke(g.Path) - gc.Restore() - return g.Width -}