Merge pull request #71 from stephenwithav/fontfilenamer

Font naming enhancement
This commit is contained in:
Laurent Le Goff 2015-08-09 21:38:03 +02:00
commit ce9c7f76f7

15
font.go
View file

@ -13,8 +13,9 @@ import (
)
var (
fontFolder = "../resource/font/"
fonts = make(map[string]*truetype.Font)
fontFolder = "../resource/font/"
fonts = make(map[string]*truetype.Font)
fontNamer FontFileNamer = FontFileName
)
type FontStyle byte
@ -39,6 +40,8 @@ type FontData struct {
Style FontStyle
}
type FontFileNamer func(fontData FontData) string
func FontFileName(fontData FontData) string {
fontFileName := fontData.Name
switch fontData.Family {
@ -63,11 +66,11 @@ func FontFileName(fontData FontData) string {
}
func RegisterFont(fontData FontData, font *truetype.Font) {
fonts[FontFileName(fontData)] = font
fonts[fontNamer(fontData)] = font
}
func GetFont(fontData FontData) *truetype.Font {
fontFileName := FontFileName(fontData)
fontFileName := fontNamer(fontData)
font := fonts[fontFileName]
if font != nil {
return font
@ -84,6 +87,10 @@ func SetFontFolder(folder string) {
fontFolder = filepath.Clean(folder)
}
func SetFontNamer(fn FontFileNamer) {
fontNamer = fn
}
func loadFont(fontFileName string) *truetype.Font {
fontBytes, err := ioutil.ReadFile(path.Join(fontFolder, fontFileName))
if err != nil {