Skip to content

to_sfnt

The font file as the sfnt that shapers and rasterizers read (skrifa, ttf-parser, …): a WOFF file is rebuilt with its tables decompressed; a TrueType, OpenType or collection file is already one and comes back as it is, without a copy. A collection keeps all its fonts: open it with the same index as read_font_at.

Checksums are neither verified nor recomputed: the table checksums are the ones the WOFF file records.

use helpers4::font::to_sfnt;

Cargo feature font (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features font

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.11", default-features = false, features = ["font"] }
pub fn to_sfnt(bytes: &[u8]) -> Result<Cow<'_, [u8]>, FontError>
ParameterTypeDescription
bytes&[u8]The font file.

Result<Cow<'_, [u8]>, FontError> — The sfnt bytes, borrowed from bytes unless the file was WOFF.

A FontError for data that is not a supported font, is truncated, or holds a WOFF table that does not decompress to its announced size.

use helpers4::font::{to_sfnt, FontError};

let ttf = b"\0\x01\0\0\0\0\0\0\0\0\0\0";
assert_eq!(to_sfnt(ttf).unwrap().as_ref(), ttf);
assert_eq!(to_sfnt(b"wOF2....").err(), Some(FontError::UnsupportedWoff2));

Why a font file could not be read.

use helpers4::font::FontError;

#[non_exhaustive]
pub enum FontError {
    /// The data ends before a table directory, a table or a record it announces.
    Truncated,
    /// Not a TrueType, OpenType, TrueType collection or WOFF file.
    UnknownFormat,
    /// A WOFF2 file: its tables are Brotli-compressed, and this crate has no Brotli decoder.
    UnsupportedWoff2,
    /// A WOFF table whose compressed data is invalid or does not have its announced size.
    InvalidCompressedData,
    /// The file has no `name` table.
    MissingNameTable,
    /// The `name` table holds neither a typographic nor a plain family name.
    MissingFamily,
    /// The font number asked for is not in the file: a collection holds fewer fonts, and any
    /// other file holds only the font number 0.
    IndexOutOfRange,
}

src/font/to_sfnt.rs