Expand description
§Babelfont
Babelfont is a library for working with font source files from different font editing software. It provides a unified interface to load, examine, manipulate, and convert fonts between various formats, abstracting over the differences between font editors’ native representations.
§Supported Formats
Babelfont supports the following font source formats (depending on enabled features):
- Glyphs (feature:
glyphs): Glyphs 2 and Glyphs 3 files (.glyphsand.glyphspackage) (saving and loading) - UFO/DesignSpace (feature:
ufo): Unified Font Object format and DesignSpace documents (loading only) - FontLab (feature:
fontlab): FontLab VFJ (JSON) format (in progress) - Fontra (feature:
fontra): Fontra format (in progress) - Babelfont JSON: Native JSON serialization of Babelfont’s internal representation
Additionally, with the fontir feature enabled, fonts can be compiled to binary .ttf format.
§Core Concepts
Babelfont represents fonts using a Font structure that contains:
- Glyphs: The font’s glyph set with their outlines and metadata
- Masters: Design masters for variable/multiple master fonts
- Axes: Variable font axes definitions
- Instances: Named instances (static font variants)
- Features: OpenType feature code
- Metadata: Font naming, version, dates, and other information
§JSON Serialization
One of Babelfont’s key features is its ability to serialize and deserialize its internal representation to and from JSON. This provides a format-agnostic way to store and exchange font data, and can be useful for:
- Converting between different font editor formats
- Creating programmatic font workflows
- Debugging and inspecting font structures
- Storing intermediate build artifacts
- Keeping font sources in version control
To save a font as JSON, use the .babelfont extension:
let font = load("MyFont.glyphs")?;
font.save("MyFont.babelfont")?;§Font Filters
Babelfont includes a set of filters for manipulating fonts. Filters implement the
filters::FontFilter trait and can be chained together to perform complex transformations:
filters::RetainGlyphs: Keep only specified glyphs, removing all othersfilters::DropAxis: Remove a variable font axisfilters::DropInstances: Remove named instancesfilters::DropKerning: Remove kerning datafilters::DropFeatures: Remove OpenType feature codefilters::DropGuides: Remove guidelinesfilters::DropSparseMasters: Remove sparse mastersfilters::ResolveIncludes: Resolve feature file includesfilters::ScaleUpem: Scale the units-per-em
§Example: Load, Filter, and Convert
This example demonstrates loading a DesignSpace-based font, filtering it to retain only specific glyphs, and saving it as a Glyphs file:
use babelfont::{load, BabelfontError};
use babelfont::filters::{FontFilter, RetainGlyphs};
fn main() -> Result<(), BabelfontError> {
// Load a DesignSpace file
let mut font = load("MyFont.designspace")?;
// Create a filter to retain only certain glyphs
let filter = RetainGlyphs::new(vec![
"A".to_string(),
"B".to_string(),
"C".to_string(),
"space".to_string(),
]);
// Apply the filter
filter.apply(&mut font)?;
// Save as a Glyphs file
font.save("MyFont-Subset.glyphs")?;
Ok(())
}§Example: Working with Font Metadata
let font = load("MyFont.ufo")?;
// Access font metadata
println!("Font family: {}", font.names.family_name);
println!("Units per em: {}", font.upm);
println!("Number of glyphs: {}", font.glyphs.len());
// Iterate over axes in a variable font
for axis in &font.axes {
println!("Axis: {} ({}-{})", axis.name, axis.min, axis.max);
}
// Access glyphs
if let Some(glyph) = font.glyphs.get("A") {
println!("Glyph 'A' has {} layers", glyph.layers.len());
}§Feature Flags
glyphs: Enable support for Glyphs format files (default: enabled)ufo: Enable support for UFO and DesignSpace formats (default: enabled)fontlab: Enable support for FontLab VFJ format (default: enabled)fontra: Enable support for Fontra format (default: enabled)fontir: Enable compilation to binary font formats (default: enabled)cli: Enable command-line interface supporttypescript: Enable TypeScript type definition generation
Modules§
- convertors
- Convertors for various font file formats
- filters
- Filters for font processing
Structs§
- Anchor
- An anchor point in a glyph
- Axis
- An axis in a variable font
- Component
- A component in a glyph
- Features
- A representation of OpenType features, classes, and prefixes.
- Font
- A representation of a font source file
- Format
Specific - A map for storing format-specific data.
- Glyph
- A glyph in the font
- Glyph
List - A list of glyphs in the font
- Guide
- A guideline in the font, whether at master or layer level
- I18N
Dictionary - A dictionary for internationalized strings.
- Instance
- A font instance
- Layer
- A layer of a glyph in a font
- Master
- A master/source font in a design space
- NameId
- Identifier for an informational string (or name).
- Names
- Name table values for a font or individual master
- Node
- A node in a glyph outline
- Path
- A path in a glyph
- Position
- A position in 2D space, with an optional angle
- Rect
- A rectangle.
- Tag
- An OpenType tag.
Enums§
- Babelfont
Error - Errors produced while using the Babelfont crate
- Direction
- Direction of text flow
- Glyph
Category - The category of a glyph
- Layer
Type - The type of a layer in relation to masters
- Metric
Type - Type of font metric
- Node
Type - Types of nodes in a glyph outline
- OTScalar
- A scalar value in an OpenType table
- Shape
- A shape in a glyph, either a component or a path
Functions§
- load
- Load a Babelfont Font from a file
Type Aliases§
- Design
Coord - A coordinate in design space.
- Design
Location - A location in
DesignSpace. - Normalized
Coord - A coordinate in normalized space
- Normalized
Location - A location in
NormalizedSpace. - User
Coord - A coordinate in user space
- User
Location - A location in
UserSpace.