fogleman / gg

Go Graphics - 2D rendering in Go with a simple API.
https://godoc.org/github.com/fogleman/gg
MIT License
4.37k stars 354 forks source link

Index out of range on DrawStringAnchored #125

Closed metehus closed 3 years ago

metehus commented 3 years ago

Hello. I'm using this piece of code:

font, _ := truetype.Parse(goregular.TTF)

fontSize := tileSize * 0.09

face := truetype.NewFace(font, &truetype.Options{
    Size: fontSize,
})

cg.SetRGB(1, 1, 1)

cg.SetFontFace(face)

fmt.Println(tile)

cg.DrawStringAnchored(tile.Name, x+2, y+2, 0.0, 1.0)

But, sometimes it just works and somethimes it panic with this:

panic: runtime error: index out of range [-4]

goroutine 55 [running]:
github.com/golang/freetype/truetype.(*GlyphBuf).load(0xc002008300, 0x1002600000000, 0x23d429f1ba8, 0xc000b7c000)
    E:/sdks/golang/pkg/mod/github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0/truetype/glyph.go:231 +0xaa5
github.com/golang/freetype/truetype.(*GlyphBuf).Load(0xc002008300, 0xc001c6e1e0, 0x26000005c3, 0x0, 0x23d1d1c07b0, 0xc000b7c000)
    E:/sdks/golang/pkg/mod/github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0/truetype/glyph.go:102 +0xcb
github.com/golang/freetype/truetype.(*face).rasterize(0xc002005800, 0x26, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10)
    E:/sdks/golang/pkg/mod/github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0/truetype/face.go:356 +0x92
github.com/golang/freetype/truetype.(*face).Glyph(0xc002005800, 0x864300010080, 0xc000000043, 0x0, 0x3, 0xc000080000, 0x1165570, 0xc000a81b78, 0xcee865, 0x7ffd75b54fd0, ...)
    E:/sdks/golang/pkg/mod/github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0/truetype/face.go:301 +0x174
github.com/fogleman/gg.(*Context).drawString(0xc000998000, 0xc000986000, 0xc000086450, 0x8, 0x4090080000000000, 0x4080c86000000000)
    E:/sdks/golang/pkg/mod/github.com/fogleman/gg@v1.3.0/context.go:710 +0x1fc
github.com/fogleman/gg.(*Context).DrawStringAnchored(0xc000998000, 0xc000086450, 0x8, 0x4090080000000000, 0x4080100000000000, 0x0, 0x3ff0000000000000)
    E:/sdks/golang/pkg/mod/github.com/fogleman/gg@v1.3.0/context.go:744 +0x24d
gorfe/themes.drawOverlay(0xc000998000, 0xc000080800, 0x19, 0x19, 0x5, 0x5, 0x100, 0x1, 0xc000086680, 0x7, ...)
    D:/Projects/Musicorum/gorfe/src/themes/grid.go:170 +0x3cb
gorfe/themes.GenerateGridImage.func1(0xc0000ae050, 0x100, 0xc000998000, 0x400, 0x200, 0xc00099a000, 0xe)
    D:/Projects/Musicorum/gorfe/src/themes/grid.go:99 +0x385
created by gorfe/themes.GenerateGridImage
    D:/Projects/Musicorum/gorfe/src/themes/grid.go:82 +0x299

Process finished with exit code 2

I tried a lot but i can't figure out what I'm doing wrong

metehus commented 3 years ago

Ok i found out. I was using goroutines and this was the cause of the problem

Qingyoung commented 1 year ago

Ok i found out. I was using goroutines and this was the cause of the problem

How did you solve this problem, I also encountered the same problem.