Struct norad::Font

source ·
#[non_exhaustive]
pub struct Font { pub meta: MetaInfo, pub font_info: FontInfo, pub layers: LayerContents, pub lib: Plist, pub groups: Groups, pub kerning: Kerning, pub features: String, pub data: DataStore, pub images: ImageStore, }
Expand description

A font object, corresponding to a UFO directory. A Unified Font Object.

See the UFO specification for a description of the underlying data.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§meta: MetaInfo

The font’s metainfo, corresponding to the metainfo.plist file.

§font_info: FontInfo

The font info, corresponding to the fontinfo.plist file.

§layers: LayerContents

The font’s layers.

Each layer contains some number of Glyphs, and corresponds to a glyph directory on disk.

§lib: Plist

Arbitrary user-supplied data.

This corresponds to the lib.plist file on disk. This file is optional; an empty lib will not be serialized.

§groups: Groups

Glyph groups, corresponding to the groups.plist file.

This file is optional; if no groups are specified it will not be serialized.

§kerning: Kerning

Horizontal kerning pairs, corresponding to the kerning.plist file.

This file is optional, and will not be serialized if no pairs are specified.

§features: String

The contents of the features.fea file, if one exists.

§data: DataStore

The contents of the font’s data directory.

§images: ImageStore

The contents of the font’s images directory.

Implementations§

source§

impl Font

source

pub fn new() -> Self

Returns a new, empty Font object.

source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Font, FontLoadError>

Returns a Font object with data from a UFO directory path.

path must point to a directory with the structure described in v3 of the Unified Font Object spec.

§Examples
use norad::Font;

let ufo = Font::load("path/to/font.ufo").expect("failed to load");

Note: This will consume the public.objectLibs key in the global lib and in glyph libs and assign object libs found therein to global guidelines and glyph objects with the matching identifier, respectively.

See Font::load_requested_data for a load method that supports customization of the data inclusion / exclusion criteria.

source

pub fn load_requested_data( path: impl AsRef<Path>, request: DataRequest<'_> ) -> Result<Font, FontLoadError>

Returns a Font object with custom data inclusion/exclusion criteria from a UFO directory path.

UFO data inclusion and exclusion criteria are defined with a DataRequest parameter.

§Examples

A font object that excludes all layer, glyph and kerning data:

use norad::DataRequest;
use norad::Font;

let datareq = DataRequest::default().layers(false).kerning(false);

let ufo = Font::load_requested_data("path/to/font.ufo", datareq).expect("failed to load");

A font object that excludes all data and images:

use norad::DataRequest;
use norad::Font;

let datareq = DataRequest::default().data(false).images(false);

let ufo = Font::load_requested_data("path/to/font.ufo", datareq).expect("failed to load");

A font object that includes only parsed lib.plist data:

use norad::DataRequest;
use norad::Font;

let datareq = DataRequest::none().lib(true);

let ufo = Font::load_requested_data("path/to/font.ufo", datareq).expect("failed to load");
source

pub fn save(&self, path: impl AsRef<Path>) -> Result<(), FontWriteError>

Serialize a Font to the given path, overwriting any existing contents.

§Examples

With a Font object such as:

use norad::Font;

let ufo = Font::load("path/to/in-font.ufo").expect("failed to load");

do things with the Font, then serialize to disk with:

ufo.save("path/to/out-font.ufo").expect("failed to save");

Note: This may fail; instead of saving directly to the target path, it is a good idea to save to a temporary location and then move that to the target path if the save is successful.

This will fail if either the global or any glyph lib contains the public.objectLibs key, as object lib management must currently be done by norad.

source

pub fn save_with_options( &self, path: impl AsRef<Path>, options: &WriteOptions ) -> Result<(), FontWriteError>

Serialize a Font to the given path, overwriting any existing contents, with custom WriteOptions serialization format settings.

§Examples

With a Font object:

use norad::{Font, QuoteChar, WriteOptions};

let ufo = Font::load("path/to/in-font.ufo").expect("failed to load");

define the serialization format with a WriteOptions type:

let single_tab = WriteOptions::default();

let two_tabs = WriteOptions::default()
    .whitespace("\t\t");

let spaces = WriteOptions::default()
    .whitespace("  ");

let spaces_and_singlequotes = WriteOptions::default()
    .whitespace("  ")
    .quote_char(QuoteChar::Single);

and serialize to disk with the respective WriteOptions configuration:

// with single tab indentation (default)
ufo.save_with_options("path/to/out-font1.ufo", &single_tab);

// with two tab indentation
ufo.save_with_options("path/to/out-font2.ufo", &two_tabs);

// with two space indentation
ufo.save_with_options("path/to/out-font3.ufo", &spaces);

// with two space indentation and single quote XML declarations
ufo.save_with_options("path/to/out-font4.ufo", &spaces_and_singlequotes);

Note: This may fail; It runs validation for groups, fontinfo and data/images stores before overwriting anything, but glyph validation may still fail later. Instead of saving directly to the target path, it is a good idea to save to a temporary location and then move that to the target path if the save is successful.

This will fail if either the global or any glyph lib contains the public.objectLibs key, as object lib management must currently be done by norad.

source

pub fn default_layer(&self) -> &Layer

Returns a reference to the default layer.

source

pub fn default_layer_mut(&mut self) -> &mut Layer

Returns a mutable reference to the default layer.

source

pub fn iter_layers(&self) -> impl Iterator<Item = &Layer>

Returns an iterator over all layers in this font object.

source

pub fn iter_names(&self) -> impl Iterator<Item = Name> + '_

Returns an iterator over all the glyph names in the default layer.

source

pub fn get_glyph(&self, key: &str) -> Option<&Glyph>

Returns a reference to the glyph with the given name in the default layer.

source

pub fn get_glyph_mut(&mut self, key: &str) -> Option<&mut Glyph>

Returns a mutable reference to the glyph with the given name in the default layer, if it exists.

source

pub fn glyph_count(&self) -> usize

Returns the total number of glyphs in the default layer.

source

pub fn guidelines(&self) -> &[Guideline]

Return the font’s global guidelines, stored in FontInfo.

source

pub fn guidelines_mut(&mut self) -> &mut Vec<Guideline>

Returns a mutable reference to the font’s global guidelines.

These will be created if they do not already exist.

Trait Implementations§

source§

impl Clone for Font

source§

fn clone(&self) -> Font

Returns a copy of the value. Read more
1.0.0 · 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() -> Font

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

impl PartialEq for Font

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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 !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> 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,

§

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>,

§

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>,

§

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.