Made FetchGlyph and Glyph public, made FillGlyph and StrokeGlyph methods of Glyph, made Glyph.Path private

This commit is contained in:
redstarcoder 2016-10-25 21:15:42 -04:00
parent c2920005d6
commit 7cc6abeee3
3 changed files with 44 additions and 42 deletions

View File

@ -2,38 +2,16 @@ package draw2dbase
import "github.com/llgcode/draw2d" import "github.com/llgcode/draw2d"
var glyphCache map[string]map[rune]*glyph var glyphCache map[string]map[rune]*Glyph
func init() { 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 // FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
func FillGlyph(gc draw2d.GraphicContext, x, y float64, fontName string, chr rune) float64 { func FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
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 { if glyphCache[fontName] == nil {
glyphCache[fontName] = make(map[rune]*glyph, 60) glyphCache[fontName] = make(map[rune]*Glyph, 60)
} }
if glyphCache[fontName][chr] == nil { if glyphCache[fontName][chr] == nil {
glyphCache[fontName][chr] = renderGlyph(gc, fontName, chr) 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() return glyphCache[fontName][chr].Copy()
} }
// renderGlyph renders a Glyph then caches and returns it // renderGlyph renders a glyph then caches and returns it
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *glyph { func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
gc.Save() gc.Save()
defer gc.Restore() defer gc.Restore()
gc.BeginPath() gc.BeginPath()
width := gc.CreateStringPath(string(chr), 0, 0) width := gc.CreateStringPath(string(chr), 0, 0)
path := gc.GetPath() path := gc.GetPath()
return &glyph{ return &Glyph{
Path: &path, path: &path,
Width: width, Width: width,
} }
} }
// glyph represents a rune which has been converted to a Path and width // Glyph represents a rune which has been converted to a Path and width
type glyph struct { type Glyph struct {
// Path represents a glyph, it is always at (0, 0) // path represents a glyph, it is always at (0, 0)
Path *draw2d.Path path *draw2d.Path
// Width of the glyph // Width of the glyph
Width float64 Width float64
} }
func (g *glyph) Copy() *glyph { func (g *Glyph) Copy() *Glyph {
return &glyph{ return &Glyph{
Path: g.Path.Copy(), path: g.path.Copy(),
Width: g.Width, 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
}

View File

@ -207,7 +207,8 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64
xorig := x xorig := x
fontName := gc.GetFontName() fontName := gc.GetFontName()
for _, r := range text { 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 return x - xorig
} }
@ -261,7 +262,8 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float
xorig := x xorig := x
fontName := gc.GetFontName() fontName := gc.GetFontName()
for _, r := range text { 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 return x - xorig
} }

View File

@ -126,7 +126,8 @@ func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64
xorig := x xorig := x
fontName := gc.GetFontName() fontName := gc.GetFontName()
for _, r := range text { 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 return x - xorig
} }
@ -141,7 +142,8 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float
xorig := x xorig := x
fontName := gc.GetFontName() fontName := gc.GetFontName()
for _, r := range text { 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 return x - xorig
} }