Simple monospaced pixel font package for golang
Partly idea taken from: pixfont
go get github.com/toelsiba/fopix
Font files are available in the directory fonts. Fonts is saved in JSON format.
package main
import (
"image"
"log"
"github.com/toelsiba/fopix"
"github.com/toelsiba/fopix/imutil"
)
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
filename := "../../fonts/tom-thumb-new.json"
var fi fopix.FontInfo
err := fopix.ReadFileJSON(filename, &fi)
checkError(err)
d, err := fopix.NewDrawer(fi)
checkError(err)
d.SetScale(5)
text := "Hello, World!"
m := image.NewRGBA(d.TextBounds(text))
d.DrawText(m, image.ZP, text)
err = imutil.ImageSaveToPNG("hello-world.png", m)
checkError(err)
}
package main
import (
"image"
"image/color"
"log"
"github.com/toelsiba/fopix"
"github.com/toelsiba/fopix/imutil"
)
// custom font
var gopherFont = fopix.FontInfo{
Name: "Go font",
Author: "Gopher",
Description: "something ...",
Size: fopix.Point{X: 6, Y: 7},
AnchorPos: fopix.Point{X: 0, Y: 0},
TargetChar: '0',
CharSet: []fopix.RuneInfo{
fopix.RuneInfo{
Character: 'G',
Bitmap: []string{
"-000-",
"0---0",
"0----",
"0-000",
"0---0",
"-000-",
},
},
fopix.RuneInfo{
Character: 'o',
Bitmap: []string{
"-----",
"-----",
"-000-",
"0---0",
"0---0",
"-000-",
},
},
},
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
d, err := fopix.NewDrawer(gopherFont)
checkError(err)
d.SetScale(10)
d.SetColor(color.RGBA{0, 0, 0xFF, 0xFF})
text := "Go"
m := image.NewRGBA(d.TextBounds(text))
imutil.ImageSolidFill(m, color.White)
d.DrawText(m, image.ZP, text)
err = imutil.ImageSaveToPNG("go-font.png", m)
checkError(err)
}