Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take glyph name crate-public #267

Merged
merged 6 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/fontinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ impl FontInfo {
.unitsPerEm
.map(|v| NonNegativeIntegerOrFloat::new(v.abs()).unwrap()),
version_major: fontinfo_v2.versionMajor,
version_minor: fontinfo_v2.versionMinor.map(|v| v.abs() as NonNegativeInteger),
version_minor: fontinfo_v2.versionMinor.map(|v| v.unsigned_abs()),
x_height: fontinfo_v2.xHeight,
year: fontinfo_v2.year,
..FontInfo::default()
Expand Down Expand Up @@ -696,7 +696,7 @@ impl FontInfo {
open_type_os2_weight_class: match fontinfo_v1.weightValue {
Some(v) => match v {
-1 => None,
_ => Some(v.abs() as NonNegativeInteger),
_ => Some(v.unsigned_abs()),
},
None => None,
},
Expand Down Expand Up @@ -780,7 +780,7 @@ impl FontInfo {
.unitsPerEm
.map(|v| NonNegativeIntegerOrFloat::new(v.abs()).unwrap()),
version_major: fontinfo_v1.versionMajor,
version_minor: fontinfo_v1.versionMinor.map(|v| v.abs() as NonNegativeInteger),
version_minor: fontinfo_v1.versionMinor.map(|v| v.unsigned_abs()),
x_height: fontinfo_v1.xHeight,
year: fontinfo_v1.year,
..FontInfo::default()
Expand Down Expand Up @@ -1353,16 +1353,16 @@ impl<'de> Deserialize<'de> for Os2Panose {
impl From<Os2PanoseV2> for Os2Panose {
fn from(value: Os2PanoseV2) -> Self {
Os2Panose {
family_type: value.family_type.abs() as NonNegativeInteger,
serif_style: value.serif_style.abs() as NonNegativeInteger,
weight: value.weight.abs() as NonNegativeInteger,
proportion: value.proportion.abs() as NonNegativeInteger,
contrast: value.contrast.abs() as NonNegativeInteger,
stroke_variation: value.stroke_variation.abs() as NonNegativeInteger,
arm_style: value.arm_style.abs() as NonNegativeInteger,
letterform: value.letterform.abs() as NonNegativeInteger,
midline: value.midline.abs() as NonNegativeInteger,
x_height: value.x_height.abs() as NonNegativeInteger,
family_type: value.family_type.unsigned_abs(),
serif_style: value.serif_style.unsigned_abs(),
weight: value.weight.unsigned_abs(),
proportion: value.proportion.unsigned_abs(),
contrast: value.contrast.unsigned_abs(),
stroke_variation: value.stroke_variation.unsigned_abs(),
arm_style: value.arm_style.unsigned_abs(),
letterform: value.letterform.unsigned_abs(),
midline: value.midline.unsigned_abs(),
x_height: value.x_height.unsigned_abs(),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/glyph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use crate::{Color, Guideline, Identifier, Line, Plist, WriteOptions};
#[cfg_attr(feature = "druid", derive(Lens))]
pub struct Glyph {
/// The name of the glyph.
pub name: Name,
#[cfg_attr(feature = "druid", lens(ignore))]
madig marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) name: Name,
/// Glyph height.
pub height: f64,
/// Glyph width.
Expand Down Expand Up @@ -125,6 +126,11 @@ impl Glyph {
}
}

/// Returns the name of the glyph.
pub fn name(&self) -> Name {
self.name.clone()
}
madig marked this conversation as resolved.
Show resolved Hide resolved

/// Returns true if [`Glyph`] contains one or more [`Component`]s.
pub fn has_component(&self) -> bool {
!self.components.is_empty()
Expand Down
11 changes: 10 additions & 1 deletion src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) static DEFAULT_GLYPHS_DIRNAME: &str = "glyphs";
///
/// A layer set always includes a default layer, and may also include additional
/// layers.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub struct LayerSet {
/// A collection of [`Layer`]s. The first [`Layer`] is the default.
layers: Vec<Layer>,
Expand All @@ -35,6 +35,15 @@ pub struct LayerSet {
path_set: HashSet<String>,
}

impl PartialEq for LayerSet {
fn eq(&self, other: &Self) -> bool {
// Ignore path_set as an implementation detail. I hope this does not
// lead to observable differences in behavior when reading from disk vs.
// recreating it in memory...
self.layers == other.layers
}
}

#[allow(clippy::len_without_is_empty)] // never empty
impl LayerSet {
/// Returns a [`LayerSet`] from the provided `path`.
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! let mut font_obj = Font::load(inpath).expect("failed to load font");
//! # let layer = font_obj.default_layer();
//! # let glyph_a = layer.get_glyph("A").expect("missing glyph");
//! # assert_eq!(glyph_a.name.as_ref(), "A");
//! # assert_eq!(glyph_a.name().as_ref(), "A");
//! # let outpath = "RoflsSemiDim.ufo";
//! # font_obj.save(outpath);
//! ```
Expand All @@ -28,7 +28,7 @@
//! # let mut font_obj = Font::load(inpath).expect("failed to load font");
//! let layer = font_obj.default_layer();
//! let glyph_a = layer.get_glyph("A").expect("missing glyph");
//! assert_eq!(glyph_a.name.as_ref(), "A");
//! assert_eq!(glyph_a.name().as_ref(), "A");
//! # let outpath = "RoflsSemiDim.ufo";
//! # font_obj.save(outpath);
//! ```
Expand All @@ -41,7 +41,7 @@
//! # let mut font_obj = Font::load(inpath).expect("failed to load font");
//! # let layer = font_obj.default_layer();
//! # let glyph_a = layer.get_glyph("A").expect("missing glyph");
//! # assert_eq!(glyph_a.name.as_ref(), "A");
//! # assert_eq!(glyph_a.name().as_ref(), "A");
//! let outpath = "RoflsSemiDim.ufo";
//! font_obj.save(outpath);
//! ```
Expand Down
3 changes: 2 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Common utilities.

use std::fmt::Write as _;
use std::{collections::HashSet, path::PathBuf};

use crate::Name;
Expand Down Expand Up @@ -152,7 +153,7 @@ fn user_name_to_file_name(

let mut found_unique = false;
for counter in 1..100u8 {
result.push_str(&format!("{:0>2}", counter));
write!(&mut result, "{:0>2}", counter).unwrap();
result.push_str(suffix);
if !existing.contains(&result.to_lowercase()) {
found_unique = true;
Expand Down
22 changes: 6 additions & 16 deletions tests/save.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Testing saving files.

use norad::{Font, FormatVersion, Glyph, Identifier, Name, Plist};
use norad::{Font, FormatVersion, Glyph, Identifier, Plist};
use plist::Value;

#[test]
Expand Down Expand Up @@ -62,8 +62,8 @@ fn save_fancy() {
assert_eq!(pre_layer.iter().count(), post_layer.iter().count());

for glyph in pre_layer.iter() {
let other = post_layer.get_glyph(&glyph.name);
assert!(other.is_some(), "missing {}", &glyph.name);
let other = post_layer.get_glyph(&glyph.name());
assert!(other.is_some(), "missing {}", &glyph.name());
madig marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(glyph, other.unwrap());
}
}
Expand Down Expand Up @@ -191,19 +191,9 @@ fn object_libs_reject_existing_key() {
assert!(ufo.save(&dir).is_err());
ufo.lib.remove("public.objectLibs");

let glyph = Glyph {
name: Name::new("test").unwrap(),
height: 0.,
width: 0.,
anchors: vec![],
codepoints: vec![],
guidelines: vec![],
image: None,
lib: test_lib,
note: None,
components: vec![],
contours: vec![],
};
let mut glyph = Glyph::new("test");
glyph.lib = test_lib;

ufo.default_layer_mut().insert_glyph(glyph);
assert!(ufo.save(&dir).is_err());
}