Moved cache code to draw2dbase
This commit is contained in:
parent
c2851a6eb6
commit
c2920005d6
2 changed files with 70 additions and 67 deletions
70
draw2dbase/text.go
Normal file
70
draw2dbase/text.go
Normal file
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,73 +7,6 @@ import (
|
||||||
"golang.org/x/image/math/fixed"
|
"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.
|
// DrawContour draws the given closed contour at the given sub-pixel offset.
|
||||||
func DrawContour(path draw2d.PathBuilder, ps []truetype.Point, dx, dy float64) {
|
func DrawContour(path draw2d.PathBuilder, ps []truetype.Point, dx, dy float64) {
|
||||||
if len(ps) == 0 {
|
if len(ps) == 0 {
|
||||||
|
|
Loading…
Reference in a new issue