Moved SetGlyphCache out of StackGraphicContext

This commit is contained in:
redstarcoder 2017-01-01 12:19:45 -05:00
parent 795fd573f1
commit c01ed99433
3 changed files with 28 additions and 17 deletions

View File

@ -32,7 +32,6 @@ type ContextStack struct {
Join draw2d.LineJoin
FontSize float64
FontData draw2d.FontData
GlyphCache GlyphCache
Font *truetype.Font
// fontSize and dpi are used to calculate scale. scale is the number of
@ -58,7 +57,6 @@ func NewStackGraphicContext() *StackGraphicContext {
gc.Current.Join = draw2d.RoundJoin
gc.Current.FontSize = 10
gc.Current.FontData = DefaultFontData
gc.Current.GlyphCache = DefaultGlyphCache
return gc
}
@ -171,16 +169,6 @@ func (gc *StackGraphicContext) Close() {
gc.Current.Path.Close()
}
// 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 GlyphCache) {
if cache == nil {
gc.Current.GlyphCache = DefaultGlyphCache
} else {
gc.Current.GlyphCache = cache
}
}
func (gc *StackGraphicContext) Save() {
context := new(ContextStack)
context.FontSize = gc.Current.FontSize
@ -196,7 +184,6 @@ func (gc *StackGraphicContext) Save() {
context.Path = gc.Current.Path.Copy()
context.Font = gc.Current.Font
context.Scale = gc.Current.Scale
context.GlyphCache = gc.Current.GlyphCache
copy(context.Tr[:], gc.Current.Tr[:])
context.Previous = gc.Current
gc.Current = context

View File

@ -126,6 +126,7 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
glyphBuf *truetype.GlyphBuf
glyphCache draw2dbase.GlyphCache
DPI int
}
@ -137,6 +138,7 @@ func NewGraphicContext(width, height int) *GraphicContext {
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
&truetype.GlyphBuf{},
draw2dbase.DefaultGlyphCache,
92,
}
return gc
@ -217,7 +219,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 := gc.Current.GlyphCache.Fetch(gc, fontName, r)
glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
@ -283,7 +285,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 := gc.Current.GlyphCache.Fetch(gc, fontName, r)
glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}
@ -312,6 +314,16 @@ func (gc *GraphicContext) SetFontSize(fontSize float64) {
gc.recalc()
}
// Changes the glyph cache backend used by the GraphicContext.
// To restore the default glyph cache, call this function passing nil as argument.
func (gc *GraphicContext) SetGlyphCache(cache draw2dbase.GlyphCache) {
if cache == nil {
gc.glyphCache = draw2dbase.DefaultGlyphCache
} else {
gc.glyphCache = cache
}
}
func (gc *GraphicContext) GetDPI() int {
return gc.DPI
}

View File

@ -36,6 +36,7 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
glyphBuf *truetype.GlyphBuf
glyphCache draw2dbase.GlyphCache
DPI int
}
@ -75,6 +76,7 @@ func NewGraphicContextWithPainter(img draw.Image, painter Painter) *GraphicConte
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
&truetype.GlyphBuf{},
draw2dbase.DefaultGlyphCache,
dpi,
}
return gc
@ -136,7 +138,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 := gc.Current.GlyphCache.Fetch(gc, fontName, r)
glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
@ -163,7 +165,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 := gc.Current.GlyphCache.Fetch(gc, fontName, r)
glyph := gc.glyphCache.Fetch(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}
@ -291,6 +293,16 @@ func (gc *GraphicContext) SetFontSize(fontSize float64) {
gc.recalc()
}
// Changes the glyph cache backend used by the GraphicContext.
// To restore the default glyph cache, call this function passing nil as argument.
func (gc *GraphicContext) SetGlyphCache(cache draw2dbase.GlyphCache) {
if cache == nil {
gc.glyphCache = draw2dbase.DefaultGlyphCache
} else {
gc.glyphCache = cache
}
}
func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) {
gc.painter.SetColor(color)
rasterizer.Rasterize(gc.painter)