Skip to content

read_font

Reads what a font file says about itself: its family (typographic first), style, PostScript name, weight or wght range and whether it is italic. Reads TrueType and OpenType files (.ttf, .otf), the first font of a collection (.ttc, see read_font_at for the others) and WOFF files; it does not interpret glyphs and does not check checksums.

WOFF2 tables are Brotli-compressed, and this crate has no Brotli decoder: a WOFF2 file is a FontError::UnsupportedWoff2.

use helpers4::font::read_font;

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 read_font(bytes: &[u8]) -> Result<FontInfo, FontError>
ParameterTypeDescription
bytes&[u8]The font file.

Result<FontInfo, FontError> — The FontInfo.

A FontError for data that is not a supported font, is truncated, or has no name table or no family name.

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

assert_eq!(read_font(b"not a font"), Err(FontError::UnknownFormat));
assert_eq!(read_font(b"wOF2...."), Err(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/read_font.rs