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
This commit is contained in:
238
bitmap-font-maker/font/font.go
Normal file
238
bitmap-font-maker/font/font.go
Normal file
@@ -0,0 +1,238 @@
|
||||
// Package font parses .font text files and renders bitmap fonts to
|
||||
// atlases (PNG + JSON metrics) and text images.
|
||||
package font
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MaxGlyphSize bounds glyph width/height in pixels.
|
||||
const MaxGlyphSize = 64
|
||||
|
||||
// Glyph is one character's bitmap: rows of booleans (true = pixel on).
|
||||
// All glyphs in a font share the same height; widths vary
|
||||
// (proportional fonts).
|
||||
type Glyph struct {
|
||||
Char rune
|
||||
W, H int
|
||||
Rows [][]bool
|
||||
}
|
||||
|
||||
// Font is a parsed .font file.
|
||||
type Font struct {
|
||||
Name string
|
||||
Height int // glyph height, uniform across the font
|
||||
LineHeight int // suggested distance between text baselines
|
||||
Baseline int // rows from glyph top to the baseline
|
||||
Spacing int // horizontal px between glyphs
|
||||
SpaceWidth int // advance of ' '
|
||||
Glyphs map[rune]*Glyph
|
||||
Order []rune // file order, for stable atlas layout
|
||||
}
|
||||
|
||||
// ParseFile reads a .font file; the font name defaults to the file name.
|
||||
func ParseFile(path string) (*Font, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
ft, err := Parse(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
if ft.Name == "" {
|
||||
ft.Name = strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
|
||||
}
|
||||
return ft, nil
|
||||
}
|
||||
|
||||
// Parse reads the .font text format:
|
||||
//
|
||||
// # comment
|
||||
// font: tiny5 optional name
|
||||
// spacing: 1 px between glyphs (default 1)
|
||||
// space-width: 3 advance of ' ' (default: width of '0' or 3)
|
||||
// line-height: 7 default: glyph height + 1
|
||||
// baseline: 5 default: glyph height
|
||||
//
|
||||
// glyph A:
|
||||
// .#.
|
||||
// #.#
|
||||
// ###
|
||||
// #.#
|
||||
// #.#
|
||||
//
|
||||
// Glyph grids use '#' for on and '.' for off. Every glyph must have the
|
||||
// same height; widths may differ. The char between "glyph " and the
|
||||
// trailing ':' is taken literally (one character, e.g. "glyph ::").
|
||||
func Parse(r io.Reader) (*Font, error) {
|
||||
ft := &Font{
|
||||
Spacing: 1,
|
||||
SpaceWidth: -1, // resolved in validate
|
||||
LineHeight: -1,
|
||||
Baseline: -1,
|
||||
Glyphs: map[rune]*Glyph{},
|
||||
}
|
||||
var cur *Glyph
|
||||
sc := bufio.NewScanner(r)
|
||||
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||
lineNo := 0
|
||||
flush := func() error {
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
if len(cur.Rows) == 0 {
|
||||
return fmt.Errorf("glyph %q has no grid rows", string(cur.Char))
|
||||
}
|
||||
cur.H = len(cur.Rows)
|
||||
cur.W = len(cur.Rows[0])
|
||||
for i, row := range cur.Rows {
|
||||
if len(row) != cur.W {
|
||||
return fmt.Errorf("glyph %q row %d is %d px wide, expected %d", string(cur.Char), i+1, len(row), cur.W)
|
||||
}
|
||||
}
|
||||
if cur.W > MaxGlyphSize || cur.H > MaxGlyphSize {
|
||||
return fmt.Errorf("glyph %q is %dx%d; the maximum is %dx%d", string(cur.Char), cur.W, cur.H, MaxGlyphSize, MaxGlyphSize)
|
||||
}
|
||||
if _, dup := ft.Glyphs[cur.Char]; dup {
|
||||
return fmt.Errorf("glyph %q defined twice", string(cur.Char))
|
||||
}
|
||||
ft.Glyphs[cur.Char] = cur
|
||||
ft.Order = append(ft.Order, cur.Char)
|
||||
cur = nil
|
||||
return nil
|
||||
}
|
||||
for sc.Scan() {
|
||||
lineNo++
|
||||
line := strings.TrimSpace(sc.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
// '#' starts a comment — except inside a glyph where a line of
|
||||
// only '#'/'.' is a grid row.
|
||||
if strings.HasPrefix(line, "#") && !(cur != nil && isGridLine(line)) {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(strings.ToLower(line), "glyph ") && strings.HasSuffix(line, ":") {
|
||||
if err := flush(); err != nil {
|
||||
return nil, fmt.Errorf("line %d: %w", lineNo, err)
|
||||
}
|
||||
name := strings.TrimSuffix(line[len("glyph "):], ":")
|
||||
runes := []rune(name)
|
||||
if len(runes) != 1 {
|
||||
return nil, fmt.Errorf("line %d: glyph name %q must be exactly one character", lineNo, name)
|
||||
}
|
||||
cur = &Glyph{Char: runes[0]}
|
||||
continue
|
||||
}
|
||||
if cur != nil && isGridLine(line) {
|
||||
row := make([]bool, 0, len(line))
|
||||
for _, r := range line {
|
||||
row = append(row, r == '#')
|
||||
}
|
||||
cur.Rows = append(cur.Rows, row)
|
||||
continue
|
||||
}
|
||||
// header key: value
|
||||
if i := strings.Index(line, ":"); i > 0 && cur == nil {
|
||||
key := strings.ToLower(strings.TrimSpace(line[:i]))
|
||||
val := strings.TrimSpace(line[i+1:])
|
||||
var err error
|
||||
switch key {
|
||||
case "font":
|
||||
ft.Name = val
|
||||
case "spacing":
|
||||
ft.Spacing, err = strconv.Atoi(val)
|
||||
case "space-width":
|
||||
ft.SpaceWidth, err = strconv.Atoi(val)
|
||||
case "line-height":
|
||||
ft.LineHeight, err = strconv.Atoi(val)
|
||||
case "baseline":
|
||||
ft.Baseline, err = strconv.Atoi(val)
|
||||
default:
|
||||
return nil, fmt.Errorf("line %d: unknown setting %q", lineNo, key)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("line %d: %s: %v", lineNo, key, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("line %d: unexpected %q (want 'key: value', 'glyph X:' or a #/. grid row)", lineNo, line)
|
||||
}
|
||||
if err := sc.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := flush(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ft, ft.validate()
|
||||
}
|
||||
|
||||
// isGridLine reports whether the line consists solely of '#' and '.'.
|
||||
func isGridLine(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
if r != '#' && r != '.' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (ft *Font) validate() error {
|
||||
if len(ft.Order) == 0 {
|
||||
return fmt.Errorf("font has no glyphs")
|
||||
}
|
||||
ft.Height = ft.Glyphs[ft.Order[0]].H
|
||||
for _, r := range ft.Order {
|
||||
if g := ft.Glyphs[r]; g.H != ft.Height {
|
||||
return fmt.Errorf("glyph %q is %d px tall but %q is %d — all glyphs must share one height",
|
||||
string(r), g.H, string(ft.Order[0]), ft.Height)
|
||||
}
|
||||
}
|
||||
if ft.LineHeight < 0 {
|
||||
ft.LineHeight = ft.Height + 1
|
||||
}
|
||||
if ft.Baseline < 0 {
|
||||
ft.Baseline = ft.Height
|
||||
}
|
||||
if ft.SpaceWidth < 0 {
|
||||
if g, ok := ft.Glyphs['0']; ok {
|
||||
ft.SpaceWidth = g.W
|
||||
} else {
|
||||
ft.SpaceWidth = 3
|
||||
}
|
||||
}
|
||||
if ft.Spacing < 0 {
|
||||
return fmt.Errorf("spacing must be >= 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxGlyphWidth returns the widest glyph's width.
|
||||
func (ft *Font) MaxGlyphWidth() int {
|
||||
w := 0
|
||||
for _, r := range ft.Order {
|
||||
if g := ft.Glyphs[r]; g.W > w {
|
||||
w = g.W
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// SortedChars returns the glyph chars sorted by codepoint (JSON stability).
|
||||
func (ft *Font) SortedChars() []rune {
|
||||
out := append([]rune(nil), ft.Order...)
|
||||
sort.Slice(out, func(i, j int) bool { return out[i] < out[j] })
|
||||
return out
|
||||
}
|
||||
144
bitmap-font-maker/font/font_test.go
Normal file
144
bitmap-font-maker/font/font_test.go
Normal file
@@ -0,0 +1,144 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
165
bitmap-font-maker/font/render.go
Normal file
165
bitmap-font-maker/font/render.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package font
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AtlasGlyph is one glyph's placement in the atlas.
|
||||
type AtlasGlyph struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
W int `json:"w"`
|
||||
H int `json:"h"`
|
||||
Advance int `json:"advance"` // cursor movement after drawing: W + spacing
|
||||
}
|
||||
|
||||
// Metrics is the JSON sidecar written next to the atlas PNG.
|
||||
type Metrics struct {
|
||||
Name string `json:"name"`
|
||||
Atlas string `json:"atlas"`
|
||||
Height int `json:"height"`
|
||||
LineHeight int `json:"lineHeight"`
|
||||
Baseline int `json:"baseline"`
|
||||
Spacing int `json:"spacing"`
|
||||
SpaceWidth int `json:"spaceWidth"`
|
||||
Glyphs map[string]AtlasGlyph `json:"glyphs"`
|
||||
}
|
||||
|
||||
// BuildAtlas packs all glyphs into a grid atlas image (white pixels on
|
||||
// transparency, so game engines can tint) and returns the metrics.
|
||||
// atlasName is stored in the metrics so loaders can find the image.
|
||||
func (ft *Font) BuildAtlas(atlasName string) (*image.NRGBA, *Metrics) {
|
||||
n := len(ft.Order)
|
||||
cols := 1
|
||||
for cols*cols < n {
|
||||
cols++
|
||||
}
|
||||
rows := (n + cols - 1) / cols
|
||||
cellW := ft.MaxGlyphWidth() + 1 // 1 px padding against bleed
|
||||
cellH := ft.Height + 1
|
||||
img := image.NewNRGBA(image.Rect(0, 0, cols*cellW, rows*cellH))
|
||||
white := color.NRGBA{255, 255, 255, 255}
|
||||
|
||||
m := &Metrics{
|
||||
Name: ft.Name,
|
||||
Atlas: atlasName,
|
||||
Height: ft.Height,
|
||||
LineHeight: ft.LineHeight,
|
||||
Baseline: ft.Baseline,
|
||||
Spacing: ft.Spacing,
|
||||
SpaceWidth: ft.SpaceWidth,
|
||||
Glyphs: map[string]AtlasGlyph{},
|
||||
}
|
||||
for i, r := range ft.Order {
|
||||
g := ft.Glyphs[r]
|
||||
x0 := (i % cols) * cellW
|
||||
y0 := (i / cols) * cellH
|
||||
drawGlyph(img, g, x0, y0, white)
|
||||
m.Glyphs[string(r)] = AtlasGlyph{X: x0, Y: y0, W: g.W, H: g.H, Advance: g.W + ft.Spacing}
|
||||
}
|
||||
return img, m
|
||||
}
|
||||
|
||||
func drawGlyph(img *image.NRGBA, g *Glyph, x0, y0 int, c color.NRGBA) {
|
||||
for y, row := range g.Rows {
|
||||
for x, on := range row {
|
||||
if on {
|
||||
img.SetNRGBA(x0+x, y0+y, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RenderText draws text into a new image using the font. scale is an
|
||||
// integer upscale factor; unknown characters are skipped with a warning
|
||||
// returned. "\n" (literal backslash-n) and real newlines both break lines.
|
||||
func (ft *Font) RenderText(text string, scale int, fg color.NRGBA) (*image.NRGBA, []string) {
|
||||
if scale < 1 {
|
||||
scale = 1
|
||||
}
|
||||
text = strings.ReplaceAll(text, `\n`, "\n")
|
||||
lines := strings.Split(text, "\n")
|
||||
|
||||
var warnings []string
|
||||
width := 0
|
||||
for _, line := range lines {
|
||||
if w := ft.lineWidth(line); w > width {
|
||||
width = w
|
||||
}
|
||||
}
|
||||
if width == 0 {
|
||||
width = 1
|
||||
}
|
||||
height := ft.LineHeight*(len(lines)-1) + ft.Height
|
||||
|
||||
small := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
for li, line := range lines {
|
||||
x := 0
|
||||
y := li * ft.LineHeight
|
||||
for _, r := range line {
|
||||
if r == ' ' {
|
||||
x += ft.SpaceWidth + ft.Spacing
|
||||
continue
|
||||
}
|
||||
g, ok := ft.Glyphs[r]
|
||||
if !ok {
|
||||
warnings = append(warnings, fmt.Sprintf("no glyph for %q — skipped", string(r)))
|
||||
x += ft.SpaceWidth + ft.Spacing
|
||||
continue
|
||||
}
|
||||
drawGlyph(small, g, x, y, fg)
|
||||
x += g.W + ft.Spacing
|
||||
}
|
||||
}
|
||||
if scale == 1 {
|
||||
return small, warnings
|
||||
}
|
||||
b := small.Bounds()
|
||||
big := image.NewNRGBA(image.Rect(0, 0, b.Dx()*scale, b.Dy()*scale))
|
||||
for y := 0; y < b.Dy(); y++ {
|
||||
for x := 0; x < b.Dx(); x++ {
|
||||
c := small.NRGBAAt(x, y)
|
||||
for dy := 0; dy < scale; dy++ {
|
||||
for dx := 0; dx < scale; dx++ {
|
||||
big.SetNRGBA(x*scale+dx, y*scale+dy, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return big, warnings
|
||||
}
|
||||
|
||||
func (ft *Font) lineWidth(line string) int {
|
||||
x := 0
|
||||
for _, r := range line {
|
||||
if r == ' ' {
|
||||
x += ft.SpaceWidth + ft.Spacing
|
||||
continue
|
||||
}
|
||||
if g, ok := ft.Glyphs[r]; ok {
|
||||
x += g.W + ft.Spacing
|
||||
} else {
|
||||
x += ft.SpaceWidth + ft.Spacing
|
||||
}
|
||||
}
|
||||
if x > 0 {
|
||||
x -= ft.Spacing // no trailing spacing
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// WritePNG encodes an image as PNG.
|
||||
func WritePNG(w io.Writer, img image.Image) error { return png.Encode(w, img) }
|
||||
|
||||
// WriteMetrics encodes metrics as indented JSON.
|
||||
func WriteMetrics(w io.Writer, m *Metrics) error {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(m)
|
||||
}
|
||||
Reference in New Issue
Block a user