raster-fonts
This crate provides a command line utility for rasterizing true type fonts into bitmaps,
both traditional and signed distance fields. It also provides a tiny library for deserializing
the metadata required for dynamic text layout calculations; both serde
and
rkyv
are supported here.
font2img
CLIcargo
```
cargo install raster-fonts ```
```
font2img
```
This will generate a monochrome signed distance field texture containing all glyphs in the printable ASCII range from the given font file. As described by Chris Green, then working for Valve:
In the simplest case, this texture can then be rendered simply by using the alpha-testing and alpha-thresholding feature of modern GPUs, without a custom shader. [...]
With the use of programmable shading, the technique is extended to perform various special effect renderings, including soft edges, outlining, drop shadows, multi-colored images, and sharp corners.
For a short video demonstration of this technique, see this video by Martin Donald.
Supported output image formats include PNG, BMP, TIFF, TGA. The supported metadata formats are RON, JSON, and RKYV. Note that kerning information cannot be exported to JSON, because JSON dictionaries must be indexed by strings, whereas the other formats support indexing by pairs of characters.
```
font2img
[Charset]... ```
To rasterize a different set of glyphs, one or more Unicode codepoint ranges must be specified after the input/output paths. Codepoints are given in hexadecimal, without any prefix, and may stand alone. Alternatively, a contiguous range may be specified by the inclusive minimum and maximum. For example:
```
font2img
20-7F
font2img
20-7F A1-FF
font2img
20-7F B1 ```
If you do not wish to use signed distance fields for whatever reason, you can switch to conventional
glyph rendering with the --coverage-levels <N>
option (-l <N>
for short), where <N>
is the number
of distinct grayscale values greater than 0 you want in the output image. Regardless of the number of
levels, the output image will be a monochrome 8bpp image, with the highest level set to 255.
```
font2img -l 255
![]()
font2img -l 1
![]()
font2img -l 15
```
This is still useful, especially if you know the exact text size you need, and want to manually process the texture after generating it with this tool.
```
font2img --help Bitmap font creation tool and accompanying metadata deserialization library
Usage: font2img [OPTIONS]
Arguments:
Options:
-l, --coverage-levels
raster_fonts
LibraryThis is a single-module library of just a few plain-old data types that implement various de-/serialization
traits, depending on the cargo features you set. As such, you'll want to use different features depending on
the data format of your choice. So if you prefer rkyv
, put this into your Cargo.toml
:
toml
[dependencies]
raster-fonts = { version = "0.1", features = ["rkyv-deserialize"] }
rkyv = "0.7"
If you want to use serde
, you'll also need either serde_json
or the ron
crate:
```toml [dependencies] raster-fonts = { version = "0.1", features = ["serde-deserialize"] }
serde_json = "1" ron = "0.8" ```