项目作者: fzipp

项目描述 :
Load and render bitmap fonts in the format of AngelCode's bitmap font generator.
高级语言: Go
项目地址: git://github.com/fzipp/bmfont.git
创建时间: 2020-03-01T06:41:46Z
项目社区:https://github.com/fzipp/bmfont

开源协议:BSD 3-Clause "New" or "Revised" License

下载


bmfont

A Go package to load and render bitmap fonts
created with AngelCode’s bitmap font generator
or other tools that generate output in the same format.

This package uses the text format
for font descriptor files (.fnt), not the binary format.

Documentation

Package documentation is available on pkg.go.dev.

Example usage

Load a bitmap font and draw text to an image:

  1. package main
  2. import (
  3. "log"
  4. "github.com/fzipp/bmfont"
  5. )
  6. func main() {
  7. font, err := bmfont.Load("ExampleFont.fnt")
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. img := image.NewRGBA(image.Rect(0, 0, 600, 300))
  12. font.DrawText(img, image.Pt(10, 20), `hello, world
  13. This is an example.
  14. abcdefghijklmnopqrstuvwxyz
  15. ABCDEFGHIJKLMNOPQRSTUVWXYZ`)
  16. // ...
  17. }

Measure the text before drawing in order to determine the size of the image:

  1. package main
  2. import (
  3. "log"
  4. "github.com/fzipp/bmfont"
  5. )
  6. func main() {
  7. font, err := bmfont.Load("ExampleFont.fnt")
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. text := `hello, world
  12. This is an example.
  13. abcdefghijklmnopqrstuvwxyz
  14. ABCDEFGHIJKLMNOPQRSTUVWXYZ`
  15. bounds := font.MeasureText(text)
  16. img := image.NewRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
  17. font.DrawText(img, image.Point{}.Sub(bounds.Min), text)
  18. // ...
  19. }

Only load the descriptor of a bitmap font:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/fzipp/bmfont"
  6. )
  7. func main() {
  8. desc, err := bmfont.LoadDescriptor("ExampleFont.fnt")
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. fmt.Println(desc.Info.Face)
  13. fmt.Println("line height:", desc.Common.LineHeight)
  14. fmt.Println("letter A width:", desc.Chars['A'].Width)
  15. }

License

This project is free and open source software licensed under the
BSD 3-Clause License.