bitmap2ttf-cli 0.1.0

CLI tool to convert bitmap fonts (BMFont format) into TrueType (.ttf) vector fonts
# bitmap2ttf

Convert bitmap font glyphs into TrueType (.ttf) vector fonts.

Takes pixel-grid glyph data and produces valid TrueType fonts with traced vector outlines — no hinting, just clean bitmap-to-outline conversion. Useful for retro game preservation, pixel font creation, and embedding bitmap fonts in applications that require vector formats.

## Features

- Generic input: any source that provides per-glyph pixel grids + metrics
- Run-length rect merging for minimal contour counts
- Complete TTF table set: head, hhea, hmtx, maxp, cmap, name, OS/2, post, glyf/loca, gasp, DSIG
- Fallible integer conversions throughout (no panics on overflow)
- BMFont (.fnt + .png atlas) CLI input support

## Installation

### Library

```toml
[dependencies]
bitmap2ttf = "0.1"
```

### CLI

```bash
cargo install bitmap2ttf-cli
```

## Usage

### Library

```rust
use bitmap2ttf::{BitmapGlyph, FontConfig, build_ttf};

let glyphs = vec![
    BitmapGlyph {
        codepoint: 0x41, // 'A'
        width: 8,
        height: 8,
        offset_x: 0,
        offset_y: 0,
        advance_width: None,
        pixels: vec![/* 8x8 pixel data, 0=transparent, nonzero=filled */],
    },
];

let config = FontConfig {
    family_name: "MyPixelFont".to_string(),
    ..Default::default()
};

let ttf_bytes = build_ttf(&glyphs, &config)?;
std::fs::write("output.ttf", ttf_bytes)?;
```

### Integration in redguard-preservation

`redguard-preservation` can map `FntGlyph` directly to `BitmapGlyph` with no extra format conversion:

```rust
use bitmap2ttf::{BitmapGlyph, FontConfig, build_ttf};

let glyphs: Vec<BitmapGlyph> = parsed
    .glyphs
    .iter()
    .enumerate()
    .map(|(idx, g)| BitmapGlyph {
        codepoint: u32::from(parsed.header.character_start)
            .saturating_add(u32::try_from(idx).unwrap_or(0)),
        width: g.width,
        height: g.height,
        offset_x: g.offset_left,
        offset_y: g.offset_top,
        advance_width: Some(g.width.saturating_add(1)),
        pixels: g.pixels.clone(),
    })
    .collect();

let config = FontConfig {
    family_name: "RedguardFnt".to_string(),
    line_height: parsed.header.line_height,
    scale: 64,
};

let ttf = build_ttf(&glyphs, &config)?;
```

### CLI

Convert a BMFont (.fnt + .png atlas) to TrueType:

```bash
bitmap2ttf input.fnt -o output.ttf
```

The CLI reads BMFont text-format descriptors and their referenced PNG atlas pages. The `.fnt` file provides codepoint mappings, glyph dimensions, offsets, and advance widths — everything needed to produce a TTF without additional configuration.

## How It Works

1. Each glyph's pixel grid is scanned for filled pixels
2. Adjacent filled pixels are merged into minimal rectangles (run-length rect merging)
3. Rectangles are converted to vector outlines (straight-edge contours)
4. Coordinates are transformed from bitmap space (Y-down) to TrueType space (Y-up) and scaled
5. All required TTF tables are constructed and assembled into a valid font file

The output fonts are bitmap-traced vector outlines without hinting. They render correctly at the original pixel size and can be installed system-wide, used in game engines (Unity, Godot, etc.), and previewed at [fontdrop.info](https://fontdrop.info).

## Repository Layout

```
bitmap2ttf/        # library crate (core conversion logic)
bitmap2ttf-cli/    # CLI binary (BMFont input → TTF output)
```

## License

MIT