limit ttf face glyphcacheentries size in typewriter example

This commit is contained in:
faiface 2017-05-25 15:21:28 +02:00
parent fc30e51016
commit debdbea894
1 changed files with 19 additions and 11 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/faiface/pixel/pixelgl" "github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text" "github.com/faiface/pixel/text"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/pkg/profile"
"golang.org/x/image/colornames" "golang.org/x/image/colornames"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/gofont/gobold" "golang.org/x/image/font/gofont/gobold"
@ -20,12 +21,15 @@ import (
"golang.org/x/image/font/gofont/goregular" "golang.org/x/image/font/gofont/goregular"
) )
func ttfFromBytesMust(b []byte, opts *truetype.Options) font.Face { func ttfFromBytesMust(b []byte, size float64) font.Face {
ttf, err := truetype.Parse(b) ttf, err := truetype.Parse(b)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return truetype.NewFace(ttf, opts) return truetype.NewFace(ttf, &truetype.Options{
Size: size,
GlyphCacheEntries: 1,
})
} }
type typewriter struct { type typewriter struct {
@ -248,15 +252,18 @@ func run() {
win.SetSmooth(true) win.SetSmooth(true)
var ( var (
regular = text.NewAtlas(ttfFromBytesMust(goregular.TTF, &truetype.Options{ regular = text.NewAtlas(
Size: 42, ttfFromBytesMust(goregular.TTF, 42),
}), text.ASCII, text.RangeTable(unicode.Latin)) text.ASCII, text.RangeTable(unicode.Latin),
bold = text.NewAtlas(ttfFromBytesMust(gobold.TTF, &truetype.Options{ )
Size: 42, bold = text.NewAtlas(
}), text.ASCII, text.RangeTable(unicode.Latin)) ttfFromBytesMust(gobold.TTF, 42),
italic = text.NewAtlas(ttfFromBytesMust(goitalic.TTF, &truetype.Options{ text.ASCII, text.RangeTable(unicode.Latin),
Size: 42, )
}), text.ASCII, text.RangeTable(unicode.Latin)) italic = text.NewAtlas(
ttfFromBytesMust(goitalic.TTF, 42),
text.ASCII, text.RangeTable(unicode.Latin),
)
bgColor = color.RGBA{ bgColor = color.RGBA{
R: 241, R: 241,
@ -310,5 +317,6 @@ func run() {
} }
func main() { func main() {
defer profile.Start(profile.MemProfile).Stop()
pixelgl.Run(run) pixelgl.Run(run)
} }