Improved speed via new gc.GetFontName method
This commit is contained in:
parent
3a5a1d8830
commit
c2851a6eb6
5 changed files with 26 additions and 14 deletions
|
@ -4,6 +4,7 @@
|
||||||
package draw2dbase
|
package draw2dbase
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
|
@ -191,3 +192,8 @@ func (gc *StackGraphicContext) Restore() {
|
||||||
oldContext.Previous = nil
|
oldContext.Previous = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gc *StackGraphicContext) GetFontName() string {
|
||||||
|
fontData := gc.Current.FontData
|
||||||
|
return fmt.Sprintf("%s:%d:%d:%d", fontData.Name, fontData.Family, fontData.Style, gc.Current.FontSize)
|
||||||
|
}
|
||||||
|
|
|
@ -197,14 +197,17 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
|
||||||
return x - startx
|
return x - startx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *GraphicContext) FillString(text string) (cursor float64) {
|
// FillString draws the text at point (0, 0)
|
||||||
|
func (gc *GraphicContext) FillString(text string) (width float64) {
|
||||||
return gc.FillStringAt(text, 0, 0)
|
return gc.FillStringAt(text, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FillStringAt draws the text at the specified point (x, y)
|
||||||
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
||||||
xorig := x
|
xorig := x
|
||||||
|
fontName := gc.GetFontName()
|
||||||
for _, r := range text {
|
for _, r := range text {
|
||||||
x += draw2dbase.FillGlyph(gc, x, y, r)
|
x += draw2dbase.FillGlyph(gc, x, y, fontName, r)
|
||||||
}
|
}
|
||||||
return x - xorig
|
return x - xorig
|
||||||
}
|
}
|
||||||
|
@ -248,14 +251,17 @@ func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom fl
|
||||||
return left, top, right, bottom
|
return left, top, right, bottom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StrokeString draws the contour of the text at point (0, 0)
|
||||||
func (gc *GraphicContext) StrokeString(text string) (width float64) {
|
func (gc *GraphicContext) StrokeString(text string) (width float64) {
|
||||||
return gc.StrokeStringAt(text, 0, 0)
|
return gc.StrokeStringAt(text, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StrokeStringAt draws the contour of the text at point (x, y)
|
||||||
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
|
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
|
||||||
xorig := x
|
xorig := x
|
||||||
|
fontName := gc.GetFontName()
|
||||||
for _, r := range text {
|
for _, r := range text {
|
||||||
x += draw2dbase.StrokeGlyph(gc, x, y, r)
|
x += draw2dbase.StrokeGlyph(gc, x, y, fontName, r)
|
||||||
}
|
}
|
||||||
return x - xorig
|
return x - xorig
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package draw2dgl
|
package draw2dgl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
"github.com/llgcode/draw2d"
|
"github.com/llgcode/draw2d"
|
||||||
|
|
||||||
|
@ -16,8 +14,8 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FillGlyph copies a glyph from the cache, copies it to the gc, and fills it
|
// FillGlyph copies a glyph from the cache, copies it to the gc, and fills it
|
||||||
func FillGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
|
func FillGlyph(gc draw2d.GraphicContext, x, y float64, fontName string, chr rune) float64 {
|
||||||
g := fetchGlyph(gc, chr)
|
g := fetchGlyph(gc, fontName, chr)
|
||||||
gc.Save()
|
gc.Save()
|
||||||
gc.BeginPath()
|
gc.BeginPath()
|
||||||
gc.Translate(x, y)
|
gc.Translate(x, y)
|
||||||
|
@ -27,8 +25,8 @@ func FillGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrokeGlyph fetches a glyph from the cache, copies it to the gc, and strokes it
|
// StrokeGlyph fetches a glyph from the cache, copies it to the gc, and strokes it
|
||||||
func StrokeGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
|
func StrokeGlyph(gc draw2d.GraphicContext, x, y float64, fontName string, chr rune) float64 {
|
||||||
g := fetchGlyph(gc, chr)
|
g := fetchGlyph(gc, fontName, chr)
|
||||||
gc.Save()
|
gc.Save()
|
||||||
gc.BeginPath()
|
gc.BeginPath()
|
||||||
gc.Translate(x, y)
|
gc.Translate(x, y)
|
||||||
|
@ -38,9 +36,7 @@ func StrokeGlyph(gc draw2d.GraphicContext, x, y float64, chr rune) float64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
|
// fetchGlyph fetches a glyph from the cache, calling renderGlyph first if it doesn't already exist
|
||||||
func fetchGlyph(gc draw2d.GraphicContext, chr rune) *glyph {
|
func fetchGlyph(gc draw2d.GraphicContext, fontName string, chr rune) *glyph {
|
||||||
fontData := gc.GetFontData()
|
|
||||||
fontName := fmt.Sprintf("%s:%d:%d:%d", fontData.Name, fontData.Family, fontData.Style, gc.GetFontSize())
|
|
||||||
if glyphCache[fontName] == nil {
|
if glyphCache[fontName] == nil {
|
||||||
glyphCache[fontName] = make(map[rune]*glyph, 60)
|
glyphCache[fontName] = make(map[rune]*glyph, 60)
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,8 +124,9 @@ func (gc *GraphicContext) FillString(text string) (width float64) {
|
||||||
// FillStringAt draws the text at the specified point (x, y)
|
// FillStringAt draws the text at the specified point (x, y)
|
||||||
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
|
||||||
xorig := x
|
xorig := x
|
||||||
|
fontName := gc.GetFontName()
|
||||||
for _, r := range text {
|
for _, r := range text {
|
||||||
x += draw2dbase.FillGlyph(gc, x, y, r)
|
x += draw2dbase.FillGlyph(gc, x, y, fontName, r)
|
||||||
}
|
}
|
||||||
return x - xorig
|
return x - xorig
|
||||||
}
|
}
|
||||||
|
@ -138,8 +139,9 @@ func (gc *GraphicContext) StrokeString(text string) (width float64) {
|
||||||
// StrokeStringAt draws the contour of the text at point (x, y)
|
// StrokeStringAt draws the contour of the text at point (x, y)
|
||||||
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
|
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
|
||||||
xorig := x
|
xorig := x
|
||||||
|
fontName := gc.GetFontName()
|
||||||
for _, r := range text {
|
for _, r := range text {
|
||||||
x += draw2dbase.StrokeGlyph(gc, x, y, r)
|
x += draw2dbase.StrokeGlyph(gc, x, y, fontName, r)
|
||||||
}
|
}
|
||||||
return x - xorig
|
return x - xorig
|
||||||
}
|
}
|
||||||
|
|
2
gc.go
2
gc.go
|
@ -48,6 +48,8 @@ type GraphicContext interface {
|
||||||
SetFontData(fontData FontData)
|
SetFontData(fontData FontData)
|
||||||
// GetFontData gets the current FontData
|
// GetFontData gets the current FontData
|
||||||
GetFontData() FontData
|
GetFontData() FontData
|
||||||
|
// GetFontName gets the current FontData as a string
|
||||||
|
GetFontName() string
|
||||||
// DrawImage draws the raster image in the current canvas
|
// DrawImage draws the raster image in the current canvas
|
||||||
DrawImage(image image.Image)
|
DrawImage(image image.Image)
|
||||||
// Save the context and push it to the context stack
|
// Save the context and push it to the context stack
|
||||||
|
|
Loading…
Reference in a new issue