Skip to main content

Font

Struct Font 

Source
pub struct Font { /* private fields */ }
Expand description

The resolved, owned, editable font model.

use font_model::{Font, Glyph, GlyphId, Outline, Path};

// The smallest valid font: `.notdef` alone.
let font = Font::builder()
    .units_per_em(1000)
    .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, Outline::new()))
    .build()
    .expect("valid");
assert_eq!(font.glyph_count(), 1);
assert!(font.cmap().codepoints().is_empty());
assert!(font.glyph(GlyphId::new(9)).is_none());

Implementations§

Source§

impl Font

Source

pub fn builder() -> FontBuilder

Start building a font.

Source

pub fn empty() -> Font

A font with nothing in it — invalid, but a usable starting point.

Source

pub const fn units_per_em(&self) -> u16

unitsPerEm in font units.

Source

pub fn set_units_per_em(&mut self, units_per_em: u16)

Set unitsPerEm.

Source

pub const fn metrics(&self) -> &FontMetrics

The font-wide metrics header.

Source

pub fn metrics_mut(&mut self) -> &mut FontMetrics

The font-wide metrics header, mutably.

Source

pub fn glyph_count(&self) -> usize

Number of glyphs.

Source

pub fn glyphs(&self) -> &[Glyph]

The glyph store.

Source

pub fn glyphs_mut(&mut self) -> &mut Vec<Glyph>

The glyph store, mutably — the editor’s entry point.

Source

pub fn glyph(&self, id: GlyphId) -> Option<&Glyph>

One glyph by id, None beyond the store.

Source

pub fn glyph_mut(&mut self, id: GlyphId) -> Option<&mut Glyph>

One glyph by id, mutably, None beyond the store.

Source

pub fn notdef(&self) -> Option<&Glyph>

.notdef’s outline — glyph 0, or None for an empty font.

Source

pub const fn cmap(&self) -> &Cmap

The character map.

Source

pub fn cmap_mut(&mut self) -> &mut Cmap

The character map, mutably.

Source

pub fn glyph_for(&self, cp: u32) -> Option<GlyphId>

Map a codepoint to a glyph.

Source

pub fn codepoints(&self) -> CodePointSet

The mapped codepoints, ascending.

Source

pub fn names(&self) -> &[NameRecord]

The naming records.

Source

pub fn names_mut(&mut self) -> &mut Vec<NameRecord>

The naming records, mutably.

Source

pub const fn os2(&self) -> Option<&Os2Metrics>

The OS/2 metrics, when present.

Source

pub fn os2_mut(&mut self) -> &mut Option<Os2Metrics>

The OS/2 metrics, mutably.

Source

pub const fn kerning(&self) -> &Kerning

The kerning pairs.

Source

pub fn kerning_mut(&mut self) -> &mut Kerning

The kerning pairs, mutably.

Source

pub fn kern(&self, left: GlyphId, right: GlyphId) -> i16

The kerning adjustment between two glyphs, in font units.

Source

pub fn tables(&self) -> &[(TableTag, Vec<u8>)]

The opaque tables carried through unchanged: (tag, bytes).

Source

pub fn tables_mut(&mut self) -> &mut Vec<(TableTag, Vec<u8>)>

The opaque tables, mutably.

Source

pub fn table(&self, tag: TableTag) -> Option<&[u8]>

One opaque table’s bytes by tag, None when absent.

Source

pub fn set_table(&mut self, tag: TableTag, data: Vec<u8>)

Store an opaque table, replacing any existing entry with that tag.

Source

pub fn remove_table(&mut self, tag: TableTag) -> bool

Drop an opaque table, returning whether it was present.

Source

pub fn bbox(&self) -> Option<Bbox>

The union of every glyph’s bounding box, None for a font with no ink at all.

Source

pub fn encoding_subtables(&self) -> Vec<CmapSubtable>

A cmap subtable holding exactly the font’s codepoint set.

What a writer emits: format 4 below the BMP, format 12 above it.

Source

pub fn validate(&self) -> Result<(), ModelError>

Validate every invariant, naming the offending glyph and field.

§Errors

The first violation found, in this order: unitsPerEm, glyph store density, per-glyph outline and advance, cmap monotonicity, cmap references, then kerning references.

Source

pub fn subset(&self, cps: &CodePointSet) -> Result<Font, ModelError>

Subset the font to cps.

The result keeps .notdef, every glyph a requested codepoint maps to, and nothing else. Glyph ids are reassigned densely and in ascending original order, so the original id is preserved where it can be and the store is always valid. Kerning pairs whose endpoints survive are kept, remapped.

§Errors

ModelError::UnknownGlyph when a requested codepoint maps to a glyph the font does not hold; plus whatever Font::validate reports on the result, so a subset can never come back invalid.

Source

pub fn recompute_metrics(&mut self)

Derive the metrics header from the current glyph store.

Source

pub fn recompute_glyph_metrics(&mut self) -> Result<(), ModelError>

Recompute every glyph’s metrics from its outline.

§Errors

ModelError::AdvanceOverflow for a glyph too wide for hmtx.

Trait Implementations§

Source§

impl Clone for Font

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Font

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Font

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Font

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Font

Auto Trait Implementations§

§

impl Freeze for Font

§

impl RefUnwindSafe for Font

§

impl Send for Font

§

impl Sync for Font

§

impl Unpin for Font

§

impl UnsafeUnpin for Font

§

impl UnwindSafe for Font

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.