Skip to content

font_count

How many fonts a font file holds: the number of fonts of a collection (.ttc), 1 for a TrueType, OpenType or WOFF file. Only the first bytes are read; the fonts themselves are not parsed.

use helpers4::font::font_count;

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

Result<usize, FontError> — The number of fonts, to use as the bound of the index of read_font_at.

A FontError for data that is not a supported font, or is truncated.

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

assert_eq!(font_count(b"not a font"), Err(FontError::UnknownFormat));
assert_eq!(font_count(b"\0\x01\0\0\0\0\0\0"), Ok(1));

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/font_count.rs