Skip to content

Commit

Permalink
Merge 4a5152f into 5237343
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr authored Aug 4, 2021
2 parents 5237343 + 4a5152f commit 1e04203
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 37 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,28 @@ jobs:
profile: minimal
override: true

- run: git submodule update --init --recursive

- name: cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features -- -D warnings
args: --all-targets -- -D warnings

- run: git submodule update --init --recursive
- name: cargo clippy --all-features
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features --all-targets -- -D warnings

- name: cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features

- name: cargo doc
uses: actions-rs/cargo@v1
with:
command: doc
args: --all-features --document-private-items
15 changes: 15 additions & 0 deletions examples/glif_note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use norad::*;

static NOTE_TEXT: &str = r#"
def test:
print("love it")
"#;
fn main() {
let mut glif = Glyph::new_named("A");
glif.note = Some(NOTE_TEXT.into());

let encoded = glif.encode_xml().unwrap();
let string = String::from_utf8(encoded).unwrap();
print!("{}", string);
}
13 changes: 6 additions & 7 deletions examples/glif_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! Afterwards it will print the xml tree to stderr, which may be useful when
//! debugging parse errors.

use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::{env, fs, io};
Expand All @@ -21,7 +20,7 @@ use quick_xml::{
use norad::Glyph;

fn main() -> Result<(), io::Error> {
let path = match env::args().skip(1).next().map(PathBuf::from) {
let path = match env::args().nth(1).map(PathBuf::from) {
Some(ref p) if p.exists() && p.extension() == Some(OsStr::new("glif")) => p.to_owned(),
Some(ref p) => {
eprintln!("path {:?} is not an existing .glif file, exiting", p);
Expand Down Expand Up @@ -50,7 +49,7 @@ fn main() -> Result<(), io::Error> {
}

fn print_tokens(xml: &str) -> Result<(), Error> {
let mut reader = Reader::from_str(&xml);
let mut reader = Reader::from_str(xml);
let mut buf = Vec::new();
reader.trim_text(true);
let mut level = 0;
Expand All @@ -61,17 +60,17 @@ fn print_tokens(xml: &str) -> Result<(), Error> {
let version = decl.version()?;
let version = std::str::from_utf8(&version)?;

let slice: &[u8] = &[];
let encoding = decl.encoding().unwrap_or(Ok(Cow::from(slice)))?;
let encoding = decl.encoding().transpose()?.unwrap_or_default();
let encoding = std::str::from_utf8(&encoding)?;

eprintln!("xml version {} encoding {}", version, encoding);
}
Ok(Event::Start(start)) => {
let name = std::str::from_utf8(start.name())?;
eprint!("{}<{}", spaces_for_level(level), name);
for attr in start.attributes() {
let attr = attr?;
let key = std::str::from_utf8(&attr.key)?;
let key = std::str::from_utf8(attr.key)?;
let value = attr.unescaped_value()?;
let value = reader.decode(&value)?;
eprint!(" {}=\"{}\"", key, value);
Expand All @@ -89,7 +88,7 @@ fn print_tokens(xml: &str) -> Result<(), Error> {
eprint!("{}<{}", spaces_for_level(level), name);
for attr in start.attributes() {
let Attribute { key, value } = attr?;
let key = std::str::from_utf8(&key)?;
let key = std::str::from_utf8(key)?;
let value = std::str::from_utf8(&value)?;
eprint!(" {}=\"{}\"", key, value);
}
Expand Down
8 changes: 8 additions & 0 deletions examples/load_a_bunch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use norad::Font;

fn main() {
let _ = Font::load("/Users/rofls/dev/projects/fontville/fontfiles/NotoSans-Bold.ufo").unwrap();
let _ =
Font::load("/Users/rofls/dev/projects/fontville/fontfiles/NotoSans-Regular.ufo").unwrap();
let _ = Font::load("/Users/rofls/dev/projects/fontville/fontfiles/NotoSans-Light.ufo").unwrap();
}
11 changes: 4 additions & 7 deletions examples/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ fn main() {
});

ufo.meta.creator = "org.linebender.norad".to_string();
match ufo.save(arg) {
Err(e) => {
eprintln!("Saving UFO failed: {}", e);
std::process::exit(1);
}
_ => {}
};
if let Err(e) = ufo.save(arg) {
eprintln!("Saving UFO failed: {}", e);
std::process::exit(1);
}
}
}
13 changes: 9 additions & 4 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,11 @@ mod tests {

let mut font = Font::new();
font.meta.format_version = FormatVersion::V1;
assert_eq!(font.save(&dir).is_err(), true);
assert!(font.save(&dir).is_err());
font.meta.format_version = FormatVersion::V2;
assert_eq!(font.save(&dir).is_err(), true);
assert!(font.save(&dir).is_err());
font.meta.format_version = FormatVersion::V3;
assert_eq!(font.save(&dir).is_ok(), true);
assert!(font.save(&dir).is_ok());
}

#[test]
Expand All @@ -441,7 +441,12 @@ mod tests {
Some(&plist::Value::Boolean(true))
);
assert_eq!(font_obj.groups.unwrap().get("public.kern1.@MMK_L_A"), Some(&vec!["A".into()]));
assert_eq!(font_obj.kerning.unwrap().get("B").unwrap().get("H").unwrap(), &-40.0);

#[allow(clippy::float_cmp)]
{
assert_eq!(font_obj.kerning.unwrap().get("B").and_then(|k| k.get("H")), Some(&-40.0));
}

assert_eq!(font_obj.features.unwrap(), "# this is the feature from lightWide\n");
}

Expand Down
1 change: 1 addition & 0 deletions src/fontinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ mod tests {
}

#[test]
#[allow(clippy::field_reassign_with_default)]
fn test_validate_head_created() {
let mut fi = FontInfo::default();
fi.open_type_head_created = Some("YYYY/MM/DD HH:MM:SS".to_string());
Expand Down
7 changes: 5 additions & 2 deletions src/glyph/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
use std::path::PathBuf;

#[test]
#[allow(clippy::float_cmp)]
fn transform() {
let transform = AffineTransform::default();
assert_eq!(transform.x_scale, 1.0);
Expand Down Expand Up @@ -70,13 +71,14 @@ fn curve_types() {
let glyph = parse_glyph(bytes).unwrap();
assert_eq!(glyph.contours.len(), 2);
assert_eq!(glyph.contours[1].points[0].typ, PointType::Line);
assert_eq!(glyph.contours[1].points[0].smooth, false);
assert_eq!(glyph.contours[1].points[1].smooth, true);
assert!(!glyph.contours[1].points[0].smooth);
assert!(glyph.contours[1].points[1].smooth);
assert_eq!(glyph.contours[1].points[2].typ, PointType::OffCurve);
assert_eq!(glyph.contours[1].points[4].typ, PointType::Curve);
}

#[test]
#[allow(clippy::float_cmp)]
fn guidelines() {
let bytes = include_bytes!("../../testdata/Blinker_one.glif");
let glyph = parse_glyph(bytes).unwrap();
Expand Down Expand Up @@ -111,6 +113,7 @@ fn parse_note() {
}

#[test]
#[allow(clippy::float_cmp)]
fn save() {
let bytes = include_bytes!("../../testdata/sample_period.glif");
let glyph = parse_glyph(bytes).expect("initial load failed");
Expand Down
8 changes: 5 additions & 3 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ mod tests {
use std::path::Path;

#[test]
#[allow(clippy::float_cmp)]
fn load_layer() {
let layer_path = "testdata/mutatorSans/MutatorSansBoldWide.ufo/glyphs";
assert!(Path::new(layer_path).exists(), "missing test data. Did you `git submodule init`?");
Expand Down Expand Up @@ -557,6 +558,7 @@ mod tests {
}

#[test]
#[allow(clippy::float_cmp)]
fn set_glyph() {
let layer_path = "testdata/mutatorSans/MutatorSansBoldWide.ufo/glyphs";
let mut layer = Layer::load(layer_path, DEFAULT_LAYER_NAME.into()).unwrap();
Expand All @@ -571,15 +573,15 @@ mod tests {
fn layer_creation() {
let mut ufo = crate::Font::load("testdata/mutatorSans/MutatorSansBoldWide.ufo").unwrap();

let default_layer = ufo.layers.get_or_create("foreground".into());
let default_layer = ufo.layers.get_or_create("foreground");
assert!(!default_layer.is_empty());
default_layer.clear();

let background_layer = ufo.layers.get_or_create("background".into());
let background_layer = ufo.layers.get_or_create("background");
assert!(!background_layer.is_empty());
background_layer.clear();

let misc_layer = ufo.layers.get_or_create("misc".into());
let misc_layer = ufo.layers.get_or_create("misc");
assert!(misc_layer.is_empty());
misc_layer.insert_glyph(Glyph::new_named("A"));

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//! assert_eq!(glyph_a.name.as_ref(), "A");
//! ```

#![deny(broken_intra_doc_links, unsafe_code)]

#[macro_use]
extern crate serde_derive;
#[macro_use]
Expand Down
2 changes: 2 additions & 0 deletions src/shared_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ mod tests {
let c4 = Color { red: 0.123, green: 0.456, blue: 0.789, alpha: 0.159 };
assert_tokens(&c4, &[Token::Str("0.123,0.456,0.789,0.159")]);

#[allow(clippy::excessive_precision)]
let c5 = Color { red: 0.123456789, green: 0.456789123, blue: 0.789123456, alpha: 0.1 };
assert_ser_tokens(&c5, &[Token::Str("0.123,0.457,0.789,0.1")]);

#[allow(clippy::excessive_precision)]
let c6 = Color { red: 0.123456789, green: 0.456789123, blue: 0.789123456, alpha: 0.1 };
assert_de_tokens(&c6, &[Token::Str("0.123456789,0.456789123,0.789123456,0.1")]);
}
Expand Down
21 changes: 9 additions & 12 deletions tests/save.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Testing saving files.

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

#[test]
fn save_default() {
Expand Down Expand Up @@ -71,17 +72,17 @@ fn save_fancy() {
#[test]
fn roundtrip_object_libs() {
let ufo = Font::load("testdata/identifiers.ufo").unwrap();
assert_eq!(ufo.lib.contains_key("public.objectLibs"), false);
assert!(!ufo.lib.contains_key("public.objectLibs"));

let glyph = ufo.get_glyph("test").unwrap();
assert_eq!(glyph.lib.contains_key("public.objectLibs"), false);
assert!(!glyph.lib.contains_key("public.objectLibs"));

let dir = tempdir::TempDir::new("identifiers.ufo").unwrap();
ufo.save(&dir).unwrap();
assert_eq!(glyph.lib.contains_key("public.objectLibs"), false);
assert!(!glyph.lib.contains_key("public.objectLibs"));

let ufo2 = Font::load(&dir).unwrap();
assert_eq!(ufo2.lib.contains_key("public.objectLibs"), false);
assert!(!ufo2.lib.contains_key("public.objectLibs"));

let font_guideline_second = &ufo2.font_info.as_ref().unwrap().guidelines.as_ref().unwrap()[1];
assert_eq!(
Expand All @@ -101,7 +102,7 @@ fn roundtrip_object_libs() {
);

let glyph2 = ufo2.get_glyph("test").unwrap();
assert_eq!(glyph2.lib.contains_key("public.objectLibs"), false);
assert!(!glyph2.lib.contains_key("public.objectLibs"));

let anchor_second = &glyph2.anchors[1];
assert_eq!(
Expand All @@ -112,12 +113,8 @@ fn roundtrip_object_libs() {
anchor_second
.lib()
.as_ref()
.unwrap()
.get("com.test.anchorTool")
.unwrap()
.as_boolean()
.unwrap(),
true
.and_then(|l| l.get("com.test.anchorTool").and_then(Value::as_boolean)),
Some(true)
);

assert_eq!(
Expand Down Expand Up @@ -193,7 +190,7 @@ fn object_libs_reject_existing_key() {

ufo.lib = test_lib.clone();
assert!(ufo.save(&dir).is_err());
ufo.lib.remove("public.objectLibs".into());
ufo.lib.remove("public.objectLibs");

let glyph = Glyph {
name: "test".into(),
Expand Down

0 comments on commit 1e04203

Please sign in to comment.