Crate hb_subset

source ·
Expand description

This crate exposes a HarfBuzz API for subsetting a font.

From HarfBuzz documentation:

Subsetting reduces the codepoint coverage of font files and removes all data that is no longer needed. A subset input describes the desired subset. The input is provided along with a font to the subsetting operation. Output is a new font file containing only the data specified in the input.

Currently most outline and bitmap tables are supported: glyf, CFF, CFF2, sbix, COLR, and CBDT/CBLC. This also includes fonts with variable outlines via OpenType variations. Notably EBDT/EBLC and SVG are not supported. Layout subsetting is supported only for OpenType Layout tables (GSUB, GPOS, GDEF). Notably subsetting of graphite or AAT tables is not yet supported.

Fonts with graphite or AAT tables may still be subsetted but will likely need to use the retain glyph ids option and configure the subset to pass through the layout tables untouched.

Usage

The simplest way to construct a subset of a font is to use subset function:

let font = fs::read("tests/fonts/NotoSans.ttf").unwrap();
let subset_font = hb_subset::subset(&font, "abc".chars()).unwrap();
fs::write("fonts/subset.ttf", subset_font).unwrap();

To get more control over how the font is subset and what gets included, you can use the lower level API directly:

// Load font directly from a file
let font = Blob::from_file("tests/fonts/NotoSans.ttf").unwrap();
let font = FontFace::new(font).unwrap();

// Construct a subset manually and include only some of the letters
let mut subset = SubsetInput::new().unwrap();
subset.unicode_set().insert('f');
subset.unicode_set().insert('i');

// Subset the font using just-constructed subset input
let new_font = subset.subset_font(&font).unwrap();

// Extract the raw font and write to an output file
std::fs::write("out.ttf", &*new_font.underlying_blob()).unwrap();

Modules

  • Safe mid-level bindings for HarfBuzz objects.
  • Raw FFI bindings to HarfBuzz.

Enums

  • An enumeration over possible errors.

Functions

  • A convenient method to create a subset of a font over given characters.