Created
October 7, 2025 17:41
-
-
Save bmuessig/1926e88f8a52dd3a2b85e3ac443229d2 to your computer and use it in GitHub Desktop.
Adafruit GFXFont bitmap converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // In the hope that this helps someone who also needs to create a custom bitmap font that is compatible with Adafruit GFX | |
| package main | |
| import ( | |
| "image/png" | |
| "os" | |
| "text/template" | |
| ) | |
| type Glyph struct { | |
| Value rune | |
| Index int | |
| Data []byte | |
| Width int | |
| Height int | |
| Advance int | |
| X int | |
| Y int | |
| } | |
| type Font struct { | |
| Name string | |
| Glyphs []Glyph | |
| Base rune | |
| End rune | |
| Height int | |
| } | |
| var codeTemplate = template.Must(template.New("code").Parse(` | |
| const uint8_t {{.Name}}_Bitmaps[] PROGMEM = { | |
| {{range .Glyphs}} | |
| /* {{.Value | printf "%c"}}, {{.Width}}x{{.Height}}px */ | |
| {{range .Data}}{{. | printf "0x%02x"}},{{end}} | |
| {{end}} | |
| }; | |
| const GFXglyph {{.Name}}_Glyphs[] PROGMEM = { | |
| {{range .Glyphs}} | |
| {{"{"}} {{.Index}}, {{.Width}}, {{.Height}}, {{.Advance}}, {{.X}}, {{.Y}} {{"}"}}, /* {{.Value | printf "%c"}} */ | |
| {{end}} | |
| }; | |
| const GFXfont {{.Name}} PROGMEM = { | |
| (uint8_t *) {{.Name}}_Bitmaps, | |
| (GFXglyph *) {{.Name}}_Glyphs, | |
| {{.Base | printf "0x%x"}}, | |
| {{.End | printf "0x%x"}}, | |
| {{.Height}} | |
| };`)) | |
| func main() { | |
| glyphPaths := []string{ | |
| "Scorefont_0.png", | |
| "Scorefont_1.png", | |
| "Scorefont_2.png", | |
| "Scorefont_3.png", | |
| "Scorefont_4.png", | |
| "Scorefont_5.png", | |
| "Scorefont_6.png", | |
| "Scorefont_7.png", | |
| "Scorefont_8.png", | |
| "Scorefont_9.png", | |
| } | |
| glyphBase := '0' | |
| fontName := "Scorefont" | |
| fontHeight := 26 | |
| fontPadding := 1 | |
| font := Font{ | |
| Name: fontName, | |
| Glyphs: make([]Glyph, len(glyphPaths)), | |
| Base: glyphBase, | |
| End: glyphBase + rune(len(glyphPaths)), | |
| Height: fontHeight, | |
| } | |
| dataIndex := 0 | |
| for index, glyphPath := range glyphPaths { | |
| imgFile, err := os.Open(glyphPath) | |
| if err != nil { | |
| panic(err) | |
| } | |
| img, err := png.Decode(imgFile) | |
| if err != nil { | |
| panic(err) | |
| } | |
| width := img.Bounds().Dx() | |
| height := img.Bounds().Dy() | |
| glyph := &font.Glyphs[index] | |
| stride := (width + 7) / 8 | |
| *glyph = Glyph{ | |
| Value: glyphBase + rune(index), | |
| Index: dataIndex, | |
| Data: make([]byte, height*stride), | |
| Width: stride * 8, | |
| Height: height, | |
| Advance: fontPadding*2 + width, | |
| X: fontPadding, | |
| Y: -height, | |
| } | |
| for y := range img.Bounds().Dy() { | |
| for x := range img.Bounds().Dx() { | |
| r, g, b, a := img.At(x, y).RGBA() | |
| if r != 0 && g != 0 && b != 0 && a != 0 { | |
| glyph.Data[y*stride+(x>>3)] |= 1 << (7 - (x & 7)) | |
| } | |
| } | |
| } | |
| dataIndex += len(glyph.Data) | |
| } | |
| if err := codeTemplate.Execute(os.Stdout, font); err != nil { | |
| panic(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment