Skip to content

Commit

Permalink
Merge a9b118a into 6f64b48
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr authored Jun 28, 2021
2 parents 6f64b48 + a9b118a commit ae135ea
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
58 changes: 39 additions & 19 deletions src/ufo.rs → src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,41 +157,53 @@ impl Default for MetaInfo {
}

impl Font {
/// Create a new `Ufo`.
/// Create a new, empty `Font` object.
pub fn new() -> Self {
Font::default()
}

/// Create a new `Ufo` only with certain fields
/// Create a new `Font` only with certain fields.
#[doc(hidden)]
#[deprecated(
since = "0.4.1",
note = "To load only specific fields, use Font::load_requestd_data"
)]
pub fn with_fields(data_request: DataRequest) -> Self {
let mut ufo = Self::new();
ufo.data_request = data_request;
ufo
}

/// Attempt to load a font object from a file. `path` must point to
/// a directory with the structure described in [v3 of the Unified Font Object][v3]
/// spec.
/// Attempt to load a font object from a file.
///
/// `path` must point to a directory with the structure described in
/// [v3 of the Unified Font Object][v3] spec.
///
/// 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.
/// # 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.
///
/// [v3]: http://unifiedfontobject.org/versions/ufo3/
pub fn load<P: AsRef<Path>>(path: P) -> Result<Font, Error> {
Self::new().load_ufo(path)
Self::load_requested_data(path, DataRequest::default())
}

pub fn load_ufo<P: AsRef<Path>>(&self, path: P) -> Result<Font, Error> {
/// Attempt to load the requested elements of a font object from a file.
pub fn load_requested_data(
path: impl AsRef<Path>,
request: DataRequest,
) -> Result<Font, Error> {
let path = path.as_ref();

// minimize monomorphization
let load_impl = |ufo: &Font, path: &Path| -> Result<Font, Error> {
let load_impl = |path: &Path| -> Result<Font, Error> {
let meta_path = path.join(METAINFO_FILE);
let mut meta: MetaInfo = plist::from_file(meta_path)?;

let lib_path = path.join(LIB_FILE);
let mut lib = if lib_path.exists() && self.data_request.lib {
let mut lib = if lib_path.exists() && request.lib {
plist::Value::from_file(&lib_path)?.into_dictionary().ok_or_else(|| {
Error::ExpectedPlistDictionary(lib_path.to_string_lossy().into_owned())
})?
Expand All @@ -209,7 +221,7 @@ impl Font {
};

let groups_path = path.join(GROUPS_FILE);
let groups = if groups_path.exists() && self.data_request.groups {
let groups = if groups_path.exists() && request.groups {
let groups: Groups = plist::from_file(groups_path)?;
validate_groups(&groups).map_err(Error::InvalidGroups)?;
Some(groups)
Expand All @@ -218,23 +230,23 @@ impl Font {
};

let kerning_path = path.join(KERNING_FILE);
let kerning = if kerning_path.exists() && self.data_request.kerning {
let kerning = if kerning_path.exists() && request.kerning {
let kerning: Kerning = plist::from_file(kerning_path)?;
Some(kerning)
} else {
None
};

let features_path = path.join(FEATURES_FILE);
let mut features = if features_path.exists() && self.data_request.features {
let mut features = if features_path.exists() && request.features {
let features = fs::read_to_string(features_path)?;
Some(features)
} else {
None
};

let glyph_names = NameList::default();
let layers = if self.data_request.layers {
let layers = if request.layers {
if meta.format_version == FormatVersion::V3
&& !path.join(LAYER_CONTENTS_FILE).exists()
{
Expand Down Expand Up @@ -283,11 +295,19 @@ impl Font {
groups,
kerning,
features,
data_request: ufo.data_request,
data_request: request,
})
};

load_impl(&self, path)
load_impl(path)
}

#[deprecated(
since = "0.4.1",
note = "To load only specific fields, use Font::load_requestd_data"
)]
pub fn load_ufo<P: AsRef<Path>>(&self, path: P) -> Result<Font, Error> {
Font::load_requested_data(path, self.data_request)
}

/// Attempt to save this UFO to the given path, overriding any existing contents.
Expand Down Expand Up @@ -573,7 +593,7 @@ mod tests {
#[test]
fn data_request() {
let path = "testdata/mutatorSans/MutatorSansLightWide.ufo";
let font_obj = Font::with_fields(DataRequest::none()).load_ufo(path).unwrap();
let font_obj = Font::load_requested_data(path, DataRequest::none()).unwrap();
assert_eq!(font_obj.iter_layers().count(), 1);
assert!(font_obj.layers.default_layer().is_empty());
assert_eq!(font_obj.lib, Plist::new());
Expand Down
3 changes: 2 additions & 1 deletion src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct LayerSet {
layers: Vec<Layer>,
}

#[allow(clippy::clippy::len_without_is_empty)] // never empty
#[allow(clippy::len_without_is_empty)] // never empty
impl LayerSet {
/// Load the layers from the provided path.
///
Expand Down Expand Up @@ -79,6 +79,7 @@ impl LayerSet {
/// The number of layers in the set.
///
/// This should be non-zero.
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.layers.len()
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ extern crate serde_derive;
extern crate serde_repr;

pub mod error;
mod font;
pub mod fontinfo;
mod glyph;
mod guideline;
mod identifier;
mod layer;
mod names;
mod shared_types;
mod ufo;
mod upconversion;
pub mod util;

pub use error::Error;
pub use font::{DataRequest, Font, FormatVersion, MetaInfo};
pub use fontinfo::FontInfo;
pub use glyph::{
AffineTransform, Anchor, Component, Contour, ContourPoint, GlifVersion, Glyph, GlyphName,
Expand All @@ -43,7 +44,6 @@ pub use guideline::{Guideline, Line};
pub use identifier::Identifier;
pub use layer::{Layer, LayerSet};
pub use shared_types::{Color, IntegerOrFloat, NonNegativeIntegerOrFloat, Plist};
pub use ufo::{DataRequest, Font, FormatVersion, MetaInfo};

#[allow(deprecated)]
pub use ufo::Ufo;
pub use font::Ufo;
4 changes: 2 additions & 2 deletions src/upconversion.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::Path;

use crate::font::{Groups, Kerning};
use crate::fontinfo::FontInfo;
use crate::names::NameList;
use crate::shared_types::IntegerOrFloat;
use crate::ufo::{Groups, Kerning};
use crate::Error;

/// Convert kerning groups and pairs from v1 and v2 informal conventions to
Expand Down Expand Up @@ -216,8 +216,8 @@ mod tests {
extern crate maplit;

use super::*;
use crate::font::{Font, FormatVersion};
use crate::glyph::GlyphName;
use crate::ufo::{Font, FormatVersion};
use maplit::btreemap;

#[test]
Expand Down

0 comments on commit ae135ea

Please sign in to comment.