Skip to main content

EmbeddedFont

Struct EmbeddedFont 

Source
pub struct EmbeddedFont {
Show 14 fields pub bytes: &'static [u8], pub face: Face<'static>, pub ttf: Face<'static>, pub postscript_name: &'static str, pub units_per_em: u16, pub ascender: i16, pub descender: i16, pub cap_height: i16, pub x_height: i16, pub italic_angle: f32, pub bbox: (i16, i16, i16, i16), pub stem_v: i16, pub flags: u32, plan_cache: RwLock<HashMap<(Script, Option<Language>), Arc<ShapePlan>>>,
}
Expand description

A bundled TrueType face: the raw bytes plus the metrics and parsed rustybuzz::Face needed to shape text and emit a PDF FontDescriptor.

Constructed internally from a &'static [u8] (the bundled include_bytes!-loaded TTF). The crate’s user-facing surface is the crate::EmbeddedFontId enum; this struct is the per-cut data block those ids resolve through.

Fields§

§bytes: &'static [u8]

Raw TTF bytes. Held statically so the parsed Face<'static> can borrow them.

§face: Face<'static>

HarfBuzz/rustybuzz face. Borrows bytes.

§ttf: Face<'static>

Pre-parsed ttf-parser face. The PDF backend reads FontDescriptor fields (italic angle, bbox, …) through this; rustybuzz wraps it but doesn’t re-expose every getter.

§postscript_name: &'static str

PostScript name (from the name table, ID 6). Becomes the /BaseFont entry’s suffix after the six-letter subset tag.

§units_per_em: u16

head.unitsPerEm. Typically 1000 (CFF) or a power of two for TrueType outlines.

§ascender: i16

hhea.ascender (font units).

§descender: i16

hhea.descender (font units, typically negative).

§cap_height: i16

OS/2.sCapHeight if present, else ascender * 7 / 10 as a PDF-conventional fallback.

§x_height: i16

OS/2.sxHeight if present, else ascender * 1 / 2 as a fallback.

§italic_angle: f32

post.italicAngle in degrees. OpenType and PDF /ItalicAngle share the same convention (counter-clockwise from vertical, negative for italic slanted right per PDF 1.7 §9.8.2), so the value passes through unchanged.

§bbox: (i16, i16, i16, i16)

head font bounding box (xMin, yMin, xMax, yMax). Becomes FontDescriptor /FontBBox.

§stem_v: i16

Heuristic stem-vertical width for /StemV: 80 for regular, 120 for bold. ttf-parser doesn’t surface a reliable StemV; most fonts don’t ship it in OS/2. PDF validators accept the heuristic.

§flags: u32

PDF FontDescriptor /Flags. Nonsymbolic (bit 6, value 32) for Latin/Cyrillic/Greek fonts; the italic bit (bit 7, value 64) is OR’d in for italic cuts.

§plan_cache: RwLock<HashMap<(Script, Option<Language>), Arc<ShapePlan>>>

Compiled rustybuzz shape plans, keyed by the buffer’s (script, language) after segment-property guessing. A plan is the compiled GSUB/GPOS feature program for a (face, script, language, LTR, no user features) tuple; it is invariant across the thousands of per-run shaping calls, so building it once per script (Latin dominates real text) replaces the plan recompilation rustybuzz::shape does on every call — ~22% of build time in profiling. RwLock because the font is shared &'static; the hit path (the common case) only needs a read lock. Plans are Arc-wrapped so the hit path can clone the handle out and release the lock before shaping.

Implementations§

Source§

impl EmbeddedFont

Source

pub(crate) fn from_static( bytes: &'static [u8], postscript_name: &'static str, is_bold: bool, is_italic: bool, ) -> Self

Parse a bundled TTF blob into an EmbeddedFont. The blob must outlive the program (which it does: bundled cuts come from include_bytes! and are baked into the binary).

postscript_name, is_bold, and is_italic are provided by the caller rather than read from the name table because the bundled cuts are known statics and parse-time string ownership would require allocating; the name table also ships platform-specific encodings we don’t want to navigate.

§Panics

Panics if the bytes don’t parse as a TrueType font. The four bundled cuts have been parse-verified at vendor time and are re-verified by tests/parse_bundled.rs on every CI run, so reaching this panic requires post-build corruption (e.g. a failed LFS pull or a truncated binary). Threading Result/Option through the dozens of downstream call sites to handle a case the compile-time include_bytes! already rules out would make the code materially worse; the lint suppression is the explicit CLAUDE.md exception, paired with the CI test that catches the only realistic failure mode.

Source

pub fn glyph_index(&self, ch: char) -> Option<u16>

Look up the GID for a Unicode codepoint, if the face covers it. Used by the layout engine’s glyph_width shortcut when shaping a single codepoint would be wasteful.

Source

pub fn advance_units(&self, gid: u16) -> u16

Horizontal advance for gid in font units, sourced from the hmtx table.

Trait Implementations§

Source§

impl Debug for EmbeddedFont

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.