draw2d/draw2dbase/text.go

78 lines
1.8 KiB
Go
Raw Normal View History

2016-10-23 02:49:44 +00:00
package draw2dbase
import "github.com/llgcode/draw2d"
2017-12-04 15:29:40 +00:00
// GlyphCache manage a map of glyphs
type GlyphCache struct {
glyphs map[string]map[rune]*Glyph
}
2016-10-23 02:49:44 +00:00
2017-12-04 15:29:40 +00:00
// NewGlyphCache initializes a GlyphCache
func NewGlyphCache() *GlyphCache {
glyphs := make(map[string]map[rune]*Glyph)
return &GlyphCache {
glyphs: glyphs,
}
2016-10-23 02:49:44 +00:00
}
// FetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
2017-12-04 15:29:40 +00:00
func (glyphCache *GlyphCache) FetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
if glyphCache.glyphs[fontName] == nil {
glyphCache.glyphs[fontName] = make(map[rune]*Glyph, 60)
2016-10-23 02:49:44 +00:00
}
2017-12-04 15:29:40 +00:00
if glyphCache.glyphs[fontName][chr] == nil {
glyphCache.glyphs[fontName][chr] = renderGlyph(gc, fontName, chr)
2016-10-23 02:49:44 +00:00
}
2017-12-04 15:29:40 +00:00
return glyphCache.glyphs[fontName][chr].Copy()
2016-10-23 02:49:44 +00:00
}
// renderGlyph renders a glyph then caches and returns it
func renderGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *Glyph {
2016-10-23 02:49:44 +00:00
gc.Save()
defer gc.Restore()
gc.BeginPath()
width := gc.CreateStringPath(string(chr), 0, 0)
path := gc.GetPath()
return &Glyph{
path: &path,
2016-10-23 02:49:44 +00:00
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
2016-10-23 02:49:44 +00:00
// Width of the glyph
Width float64
}
2017-12-04 15:29:40 +00:00
// Returns a copy of a Glyph
func (g *Glyph) Copy() *Glyph {
return &Glyph{
path: g.path.Copy(),
2016-10-23 02:49:44 +00:00
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
}