Use Go fonts by default
This is very much an enhancement: would it be an option to switch to Go fonts by default?
Visually, I think it's sensible to adopt a shared look and feel for Go libraries. The font's quirky but it immediately says 'Golang'.
More importantly, though, the library could shed its dependency on the filesystem. Clearly go install works fine if the system is set up just so, but I much prefer distributing single-file binaries with everything packed in. (So I just double-click that? Uh, terminal's over here but I guess so. Boom no fonts.)
The font naming that's in place currently is clearly valuable (though I prefer to load the fonts via go-bindata's byte arrays) but for a general-purpose library it's much simpler to default to:
import (
"golang.org/x/image/font/gofont/gobold"
"golang.org/x/image/font/gofont/gobolditalic"
"golang.org/x/image/font/gofont/goitalic"
"golang.org/x/image/font/gofont/goregular"
)
//...
regular, err := truetype.Parse(goregular.TTF)
(It's what I'm using in my custom FontCache but it would simplify the library quite a bit.)
Hi @gerald1248 An article on how to use it in draw2d can be interesting.
Thanks, @gerald1248, for this idea.
I'm currently using go fonts in draw2d like this:
(Note: I use only Name attribute of FontData for simplification.)
package gui
import (
"fmt"
"github.com/llgcode/draw2d"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/gofont/gobold"
"golang.org/x/image/font/gofont/goitalic"
"golang.org/x/image/font/gofont/gomono"
)
type MyFontCache map[string]*truetype.Font
func (fc MyFontCache) Store(fd draw2d.FontData, font *truetype.Font) {
fc[fd.Name] = font
}
func (fc MyFontCache) Load(fd draw2d.FontData) (*truetype.Font, error) {
font, stored := fc[fd.Name]
if !stored {
return nil, fmt.Errorf("Font %s is not stored in font cache.", fd.Name)
}
return font, nil
}
func init () {
fontCache := MyFontCache{}
TTFs := map[string]([]byte){
"goregular": goregular.TTF,
"gobold": gobold.TTF,
"goitalic": goitalic.TTF,
"gomono": gomono.TTF,
}
for fontName, TTF := range TTFs {
font, err := truetype.Parse(TTF)
if err != nil {
panic(err)
}
fontCache.Store(draw2d.FontData{Name: fontName}, font)
}
draw2d.SetFontCache(fontCache)
}
Maybe this font (goregular) could be part of draw2d as default font. They (all go fonts) could be automatically preloaded in similar way in defaultFontCache.
@Drahoslav7: This is pretty much exactly what I'm using in my custom font cache (GitHub).
The question is how do deal with the existing library layout and expectation that fonts are fetched from the filesystem. For example, I've thrown out the resource folder but then I have to set the font directory to . to avoid errors being thrown.
The existing font namer etc. is all working and useful but ideally it would be an optional extra. As far as I can tell, mapping bytearrays to the font descriptors the way you've done looks to me like the most Go-like way to do it.