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