Files
agent-tools/bitmap-font-maker/font/font_test.go
claude 6b10c4a57c bitmap-font-maker: fontc - .font glyph grids -> atlas PNG + metrics JSON + text rendering
- .font format: glyph sections with #/. grids, proportional widths,
  literal unicode glyph names, spacing/space-width/line-height/baseline
- build (atlas white-on-transparent + JSON metrics), render (text -> PNG
  with \n, scale, color), info, preview (terminal half-blocks)
- example tiny5 font: A-Z, ÅÄÖ, 0-9, punctuation (47 glyphs, 3x5)
- go tests: parsing, errors, atlas metrics, text rendering

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MmdG9GqfSWCzts7AkDwRDh
2026-07-14 07:03:04 +02:00

145 lines
3.3 KiB
Go

package font
import (
"bytes"
"encoding/json"
"image/color"
"strings"
"testing"
)
const tiny = `
font: t
spacing: 1
space-width: 2
glyph A:
.#.
#.#
###
glyph I:
#
#
#
`
func parseOK(t *testing.T, src string) *Font {
t.Helper()
ft, err := Parse(strings.NewReader(src))
if err != nil {
t.Fatalf("parse: %v", err)
}
return ft
}
func TestParseBasics(t *testing.T) {
ft := parseOK(t, tiny)
if ft.Name != "t" || ft.Height != 3 || len(ft.Order) != 2 {
t.Errorf("name=%q height=%d glyphs=%d", ft.Name, ft.Height, len(ft.Order))
}
a := ft.Glyphs['A']
if a.W != 3 || a.H != 3 {
t.Errorf("A is %dx%d, want 3x3", a.W, a.H)
}
if !a.Rows[1][0] || a.Rows[0][0] {
t.Error("A pixel pattern wrong")
}
i := ft.Glyphs['I']
if i.W != 1 {
t.Errorf("I width = %d, want 1 (proportional)", i.W)
}
if ft.LineHeight != 4 || ft.Baseline != 3 {
t.Errorf("defaults: lineHeight=%d baseline=%d", ft.LineHeight, ft.Baseline)
}
}
func TestParseErrors(t *testing.T) {
cases := map[string]string{
"no glyphs": "font: x\n",
"ragged glyph": "glyph A:\n##\n#\n",
"height differs": "glyph A:\n#\n#\n\nglyph B:\n#\n",
"dup glyph": "glyph A:\n#\n\nglyph A:\n#\n",
"bad name": "glyph AB:\n#\n",
"unknown key": "wat: 3\nglyph A:\n#\n",
}
for name, src := range cases {
if _, err := Parse(strings.NewReader(src)); err == nil {
t.Errorf("%s: expected error", name)
}
}
}
func TestGlyphNameColon(t *testing.T) {
ft := parseOK(t, "glyph ::\n#\n#\n")
if _, ok := ft.Glyphs[':']; !ok {
t.Error("glyph ':' not parsed")
}
}
func TestAtlasAndMetrics(t *testing.T) {
ft := parseOK(t, tiny)
img, m := ft.BuildAtlas("t.png")
if m.Glyphs["A"].Advance != 4 {
t.Errorf("A advance = %d, want 4 (w3 + spacing1)", m.Glyphs["A"].Advance)
}
g := m.Glyphs["A"]
// center-top pixel of A within its atlas cell: (x+1, y+0)
if img.NRGBAAt(g.X+1, g.Y).A == 0 {
t.Error("A apex pixel missing in atlas")
}
if img.NRGBAAt(g.X, g.Y).A != 0 {
t.Error("A corner should be empty")
}
var buf bytes.Buffer
if err := WriteMetrics(&buf, m); err != nil {
t.Fatal(err)
}
var back Metrics
if err := json.Unmarshal(buf.Bytes(), &back); err != nil {
t.Fatalf("metrics json invalid: %v", err)
}
if back.Glyphs["I"].W != 1 {
t.Errorf("metrics roundtrip: I.w = %d", back.Glyphs["I"].W)
}
}
func TestRenderText(t *testing.T) {
ft := parseOK(t, tiny)
white := color.NRGBA{255, 255, 255, 255}
img, warns := ft.RenderText("AI", 1, white)
if len(warns) != 0 {
t.Errorf("warnings: %v", warns)
}
// width: A(3) + spacing(1) + I(1) = 5
if b := img.Bounds(); b.Dx() != 5 || b.Dy() != 3 {
t.Errorf("size = %dx%d, want 5x3", b.Dx(), b.Dy())
}
// I column at x=4
if img.NRGBAAt(4, 0).A == 0 {
t.Error("I pixel missing")
}
_, warns = ft.RenderText("AXA", 1, white)
if len(warns) != 1 {
t.Errorf("missing-glyph warning expected, got %v", warns)
}
img, _ = ft.RenderText(`A\nA`, 1, white)
if b := img.Bounds(); b.Dy() != ft.LineHeight+ft.Height {
t.Errorf("two-line height = %d, want %d", b.Dy(), ft.LineHeight+ft.Height)
}
img2, _ := ft.RenderText("A", 3, white)
if b := img2.Bounds(); b.Dx() != 9 || b.Dy() != 9 {
t.Errorf("scaled size = %dx%d, want 9x9", b.Dx(), b.Dy())
}
}
func TestSpaceWidthDefaultFromZero(t *testing.T) {
ft := parseOK(t, "glyph 0:\n####\n####\n")
if ft.SpaceWidth != 4 {
t.Errorf("space-width = %d, want 4 (width of '0')", ft.SpaceWidth)
}
}