This document explains how to generate Slug binary data (.slug) with SlugSharp.Core.
The simplest API is:
byte[] SlugCompiler.Compile(string fontPath, GeneratorOptions? options = null)fontPath: path to a.ttf/.otffont.options: optional generation controls.- Returns: binary bytes in the repository's
SLUGGISHformat.
using SlugSharp.Core;
var bytes = SlugCompiler.Compile("fonts/SpaceMono-Regular.ttf");
File.WriteAllBytes("SpaceMono.slug", bytes);Use GeneratorOptions:
var options = new GeneratorOptions
{
BandCount = 16,
FullRange = false,
Whitelist = new HashSet<int> { 32, 65, 66, 67 } // space, A, B, C
};
var bytes = SlugCompiler.Compile("fonts/SpaceMono-Regular.ttf", options);BandCount(default:16)- Controls band partitioning granularity used by the generated band texture.
- Must be greater than 0.
FullRange(default:false)- When
true, includes all glyphs mapped from the font charmap. - Takes precedence over
Whitelist.
- When
Whitelist(default:null)- If
FullRangeisfalse, restricts output to listed Unicode code points. - Glyph index 0 (
.notdef) is always retained as fallback.
- If
SluggishParser performs structural validation:
SluggishParser.Validate(bytes);It checks:
- Header magic (
SLUGGISH). - Curves texture block dimensions and size.
- Bands texture block dimensions and size.
- Optional metrics footer consistency.
If you want access to intermediate generated data:
using SlugSharp.Core;
var generator = new SlugGenerator(new GeneratorOptions { FullRange = true });
var raw = generator.Generate("fonts/SpaceMono-Regular.ttf");
// raw.CodePoints, raw.CurvesList, raw.BandOffsets, raw.CurveOffsets, raw.Metrics
var bytes = SluggishWriter.Export(raw);This is useful when you need diagnostics, custom post-processing, or inspection before serialization.
Typical runtime pipeline:
- Generate
.slugbytes offline or at runtime. - Upload curves and bands textures to GPU.
- Use a Slug-compatible shader path (see
renderingShaders/). - Map code points to glyph records and render quads.
InvalidOperationException: Unable to load font- Font path is wrong or unreadable.
ArgumentOutOfRangeExceptionforBandCount- Set
BandCountto a positive integer (>= 1).
- Set
InvalidDataExceptionfromSluggishParser.Validate- Binary is corrupted or incomplete.
The test suite in SlugSharp.Tests covers:
- Deterministic compilation.
- Whitelist and full-range behaviors.
- Binary structure correctness.
- Parser rejection of malformed binaries.
Run locally:
dotnet test SlugSharp.sln -c ReleaseSlugSharp.Core provides CharsetFileParser to load a text file into code points:
using SlugSharp.Core;
var whitelist = CharsetFileParser.LoadCodePoints("charset.txt");
var bytes = SlugCompiler.Compile("fonts/SpaceMono-Regular.ttf", new GeneratorOptions
{
Whitelist = whitelist
});CLI equivalent:
dotnet run --project SlugSharp.CLI -- fonts/SpaceMono-Regular.ttf out.slug --charset-file=charset.txtSupported charset.txt token formats:
- Literal UTF-8 characters:
ABC中가 - Decimal code points:
65 66 67 - Hex code points:
U+4E2D,0xAC00 - Inclusive ranges:
32-126,A-Z,U+4E00-U+9FFF,0xAC00-0xD7AF - Comments with
#or//
Options interaction:
FullRange = truestill takes precedence over whitelist-based filtering.- CLI merges
--whitelistand--charset-filewhen both are provided. - Duplicate entries are sanitized automatically (deduplicated).
Validation and audit helpers:
# validate only
dotnet run --project SlugSharp.CLI -- fonts/SpaceMono-Regular.ttf out.slug --charset-file=charset.txt --validate-charset
# print sanitized code points
dotnet run --project SlugSharp.CLI -- fonts/SpaceMono-Regular.ttf out.slug --charset-file=charset.txt --print-charsetCore supports optional progress callbacks through GeneratorOptions.ProgressCallback:
using SlugSharp.Core;
var options = new GeneratorOptions
{
ProgressInterval = 100,
ProgressCallback = update =>
{
Console.WriteLine($"{update.Stage} t={update.ElapsedMilliseconds}ms emitted={update.EmittedGlyphCount}");
}
};
var result = SlugCompiler.CompileWithReport("fonts/SpaceMono-Regular.ttf", options, validateOutput: true);
Console.WriteLine($"Total: {result.Report.TotalMilliseconds} ms");CLI equivalents:
# summary timings only
dotnet run --project SlugSharp.CLI -- fonts/SpaceMono-Regular.ttf out.slug --timings
# live progress + timings
dotnet run --project SlugSharp.CLI -- fonts/SpaceMono-Regular.ttf out.slug --progress --progress-interval=100