#opentype #pdf #subsetting #pdf-file

subsetter

Reduces the size and coverage of OpenType fonts

2 releases

0.1.1 Mar 19, 2023
0.1.0 Oct 14, 2022

#549 in Compression

Download history 576/week @ 2023-12-14 464/week @ 2023-12-21 399/week @ 2023-12-28 602/week @ 2024-01-04 615/week @ 2024-01-11 783/week @ 2024-01-18 731/week @ 2024-01-25 652/week @ 2024-02-01 832/week @ 2024-02-08 655/week @ 2024-02-15 721/week @ 2024-02-22 823/week @ 2024-02-29 1711/week @ 2024-03-07 1712/week @ 2024-03-14 808/week @ 2024-03-21 817/week @ 2024-03-28

5,141 downloads per month
Used in 6 crates (2 directly)

MIT/Apache

61KB
1.5K SLoC

subsetter

Crates.io Documentation

Reduces the size and coverage of OpenType fonts with TrueType or CFF outlines.

[dependencies]
subsetter = "0.1"

Example

In the example below, we remove all glyphs except the ones with IDs 68, 69, 70. Those correspond to the letters 'a', 'b' and 'c'.

use subsetter::{subset, Profile};

// Read the raw font data.
let data = std::fs::read("fonts/NotoSans-Regular.ttf")?;

// Keep only three glyphs and the OpenType tables
// required for embedding the font in a PDF file.
let glyphs = &[68, 69, 70];
let profile = Profile::pdf(glyphs);
let sub = subset(&data, 0, profile)?;

// Write the resulting file.
std::fs::write("target/Noto-Small.ttf", sub)?;

Notably, this subsetter does not really remove glyphs, just their outlines. This means that you don't have to worry about changed glyphs IDs. However, it also means that the resulting font won't always be as small as possible. To somewhat remedy this, this crate sometimes at least zeroes out unused data that it cannot fully remove. This helps if the font gets compressed, for example when embedding it in a PDF file.

In the above example, the original font was 375 KB (188 KB zipped) while the resulting font is 36 KB (5 KB zipped).

Limitations

Currently, the library only subsets static outline fonts. Furthermore, it is designed for use cases where text was already mapped to glyphs. Possible future work includes:

  • The option to pass variation coordinates which would make the subsetter create a static instance of a variable font.
  • Subsetting of bitmap, color and SVG tables.
  • A profile which takes a char set instead of a glyph set and subsets the layout tables.

Safety and Dependencies

This crate forbids unsafe code and has zero dependencies.

License

This crate is dual-licensed under the MIT and Apache 2.0 licenses.

No runtime deps