|
| 1 | +# ra2fnt |
| 2 | + |
| 3 | +This program was created entirely using `GPT-5.3-Codex` without writing code by hand. |
| 4 | + |
| 5 | +CLI utility for converting [Westwood Unicode BitFont](https://moddingwiki.shikadi.net/wiki/Westwood_Unicode_BitFont_Format) (`game.fnt`, `fonT`), a font format used in Command & Conquer: Red Alert 2, to a PNG set and back. |
| 6 | + |
| 7 | +## Build |
| 8 | + |
| 9 | +```bash |
| 10 | +go build ./src/cmd/ra2fnt |
| 11 | +``` |
| 12 | + |
| 13 | +Local multi-platform release build scripts: |
| 14 | + |
| 15 | +```bash |
| 16 | +./scripts/build-release.sh |
| 17 | +./scripts/build-release.sh v1.0.0 |
| 18 | +``` |
| 19 | + |
| 20 | +```powershell |
| 21 | +powershell -ExecutionPolicy Bypass -File .\scripts\build-release.ps1 |
| 22 | +powershell -ExecutionPolicy Bypass -File .\scripts\build-release.ps1 -Version v1.0.0 |
| 23 | +``` |
| 24 | + |
| 25 | +```bat |
| 26 | +scripts\build-release.bat |
| 27 | +scripts\build-release.bat v1.0.0 |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +Export `.fnt` to PNG set: |
| 33 | + |
| 34 | +```bash |
| 35 | +./ra2fnt export -in game.fnt -out out_font |
| 36 | +``` |
| 37 | + |
| 38 | +Export with integer pixel scaling (for easier editing): |
| 39 | + |
| 40 | +```bash |
| 41 | +./ra2fnt export -in game.fnt -out out_font --scale 3 |
| 42 | +``` |
| 43 | + |
| 44 | +Non-interactive overwrite for scripts/CI: |
| 45 | + |
| 46 | +```bash |
| 47 | +./ra2fnt export -in game.fnt -out out_font --force |
| 48 | +``` |
| 49 | + |
| 50 | +Create `.fnt` from PNG set: |
| 51 | + |
| 52 | +```bash |
| 53 | +./ra2fnt create -in out_font -out rebuilt.fnt |
| 54 | +``` |
| 55 | + |
| 56 | +Create without glyph deduplication: |
| 57 | + |
| 58 | +```bash |
| 59 | +./ra2fnt create -in out_font -out rebuilt.fnt --no-dedup |
| 60 | +``` |
| 61 | + |
| 62 | +Validate PNG set and metadata without writing `.fnt`: |
| 63 | + |
| 64 | +```bash |
| 65 | +./ra2fnt validate -in out_font |
| 66 | +``` |
| 67 | + |
| 68 | +Show CLI version: |
| 69 | + |
| 70 | +```bash |
| 71 | +./ra2fnt version |
| 72 | +./ra2fnt --version |
| 73 | +``` |
| 74 | + |
| 75 | +`export` and `create` show a progress bar in `stderr`. |
| 76 | +If `out` directory already exists, `export` asks for confirmation before deleting it. |
| 77 | +- Use `--force` to skip confirmation and overwrite `out`. |
| 78 | + |
| 79 | +## Export format |
| 80 | + |
| 81 | +`export` writes only PNG files grouped by Unicode ranges: |
| 82 | + |
| 83 | +```text |
| 84 | +out_font/ |
| 85 | + metadata.json |
| 86 | + 0x0020-0x007F (Basic Latin)/ |
| 87 | + 0x0041.png |
| 88 | + 0x0042.png |
| 89 | + 0x0400-0x04FF (Cyrillic)/ |
| 90 | + 0x0451.png |
| 91 | +``` |
| 92 | + |
| 93 | +In this tree, only non-zero-width glyphs are shown as PNG files. |
| 94 | +Zero-width glyphs are listed only in `metadata.json` (`symbol_width`). |
| 95 | + |
| 96 | +- One PNG is exported per mapped Unicode codepoint from the `.fnt` unicode table. |
| 97 | +- PNG filename is fixed-length hex codepoint: `0xXXXX.png`. |
| 98 | +- `metadata.json` stores: |
| 99 | + - `symbol_width` (only zero-width glyphs, as `0`) |
| 100 | + - `symbol_stride` |
| 101 | + - `font_height` |
| 102 | + - `ideograph_width` |
| 103 | + - `scale` (integer export scale, default `1`) |
| 104 | +- Zero-width glyphs are not exported as PNG files and are restored via `metadata.json` (`symbol_width=0`). |
| 105 | +- PNG height is `symbol_height`. |
| 106 | +- No `unicode_table.bin` or `tail.bin` is produced. |
| 107 | + |
| 108 | +## Create behavior |
| 109 | + |
| 110 | +`create` reconstructs a `.fnt` from PNG files in the input directory: |
| 111 | + |
| 112 | +- PNG files are discovered recursively (subdirectory names are ignored by parser). |
| 113 | +- Files must be named as fixed-length hex codepoints (for example `0x0041.png`, `0x30A1.png`). |
| 114 | +- Zero-width glyphs are restored from `metadata.json` (`symbol_width=0`). |
| 115 | +- Codepoints with `symbol_width=0` must not have PNG files. |
| 116 | +- All PNG files must have the same height. |
| 117 | +- `symbol_width` contains only zero-width entries and is used to restore width `0`; otherwise width is taken from PNG width. |
| 118 | +- `symbol_stride` is taken from `metadata.json` when present; otherwise it is auto-calculated as `ceil(max_symbol_width / 8)`. |
| 119 | +- `font_height` is taken from `metadata.json`. |
| 120 | +- `ideograph_width` is taken from `metadata.json`. |
| 121 | +- `scale` is taken from `metadata.json`; when `scale > 1`, PNG dimensions are downscaled by this factor during `create` (back to normal font size). |
| 122 | +- Identical glyphs are deduplicated, so multiple codepoints can reference the same symbol index. |
| 123 | +- Use `--no-dedup` to disable deduplication. |
| 124 | +- Unicode table is rebuilt from filenames (`0xXXXX` -> symbol index in sorted codepoint order). |
| 125 | + |
| 126 | +Because unicode mapping order/tail bytes are rebuilt, the resulting `.fnt` is not expected to be byte-identical to the original input file. |
| 127 | + |
| 128 | +## Validate behavior |
| 129 | + |
| 130 | +`validate` runs the same checks as `create` without writing output `.fnt`, and prints a summary: |
| 131 | + |
| 132 | +- total codepoints |
| 133 | +- number of PNG files |
| 134 | +- number of zero-width codepoints from metadata |
| 135 | +- resulting symbol count |
| 136 | +- number of deduplicated symbols |
| 137 | + |
| 138 | +## Limitations |
| 139 | + |
| 140 | +- Output `.fnt` is not byte-identical to source `game.fnt`. |
| 141 | +- At least one non-zero-width PNG is required to infer `symbol_height`. |
| 142 | +- Zero-width glyphs are represented only in `metadata.json` and have no PNG files. |
| 143 | +- Unicode table order is rebuilt from sorted codepoints. |
0 commit comments