#[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
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.meta: MetaInfoThe font’s metainfo, corresponding to the metainfo.plist file.
font_info: FontInfoThe font info, corresponding to the fontinfo.plist file.
layers: LayerContentsThe font’s layers.
Each layer contains some number of Glyphs, and corresponds to a
glyph directory on disk.
lib: PlistArbitrary user-supplied data.
This corresponds to the lib.plist file on disk. This file is
optional; an empty lib will not be serialized.
groups: GroupsGlyph groups, corresponding to the groups.plist file.
This file is optional; if no groups are specified it will not be serialized.
kerning: KerningHorizontal kerning pairs, corresponding to the kerning.plist file.
This file is optional, and will not be serialized if no pairs are specified.
features: StringThe contents of the features.fea file, if one exists.
data: DataStoreThe contents of the font’s data directory.
images: ImageStoreThe contents of the font’s images directory.
Implementations§
Source§impl Font
impl Font
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<Font, FontLoadError>
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.
Sourcepub fn load_requested_data(
path: impl AsRef<Path>,
request: DataRequest<'_>,
) -> Result<Font, FontLoadError>
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");Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<(), FontWriteError>
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.
Sourcepub fn save_with_options(
&self,
path: impl AsRef<Path>,
options: &WriteOptions,
) -> Result<(), FontWriteError>
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.
Sourcepub fn default_layer(&self) -> &Layer
pub fn default_layer(&self) -> &Layer
Returns a reference to the default layer.
Sourcepub fn default_layer_mut(&mut self) -> &mut Layer
pub fn default_layer_mut(&mut self) -> &mut Layer
Returns a mutable reference to the default layer.
Sourcepub fn iter_layers(&self) -> impl Iterator<Item = &Layer>
pub fn iter_layers(&self) -> impl Iterator<Item = &Layer>
Returns an iterator over all layers in this font object.
Sourcepub fn iter_names(&self) -> impl Iterator<Item = Name> + '_
pub fn iter_names(&self) -> impl Iterator<Item = Name> + '_
Returns an iterator over all the glyph names in the default layer.
Sourcepub fn get_glyph(&self, key: &str) -> Option<&Glyph>
pub fn get_glyph(&self, key: &str) -> Option<&Glyph>
Returns a reference to the glyph with the given name in the default layer.
Sourcepub fn get_glyph_mut(&mut self, key: &str) -> Option<&mut Glyph>
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.
Sourcepub fn glyph_count(&self) -> usize
pub fn glyph_count(&self) -> usize
Returns the total number of glyphs in the default layer.
Sourcepub fn guidelines(&self) -> &[Guideline]
pub fn guidelines(&self) -> &[Guideline]
Return the font’s global guidelines, stored in FontInfo.
Sourcepub fn guidelines_mut(&mut self) -> &mut Vec<Guideline>
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.