test.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use super::*;
  2. const FONT_PATH: &'static str = "libschrift/resources/FiraGO-Regular.ttf";
  3. #[test]
  4. fn version_test() {
  5. assert_eq!(version_info(), "0.10.2");
  6. }
  7. #[test]
  8. fn load_test() {
  9. let _font = Font::load_from_file(FONT_PATH).unwrap();
  10. }
  11. #[test]
  12. fn lookup_test() {
  13. let font = Font::load_from_file(FONT_PATH).unwrap();
  14. let sch = Schrift::build(font).build();
  15. sch.glyph_lookup('a')
  16. .expect("couldn't look up lowercase 'a'");
  17. sch.glyph_lookup('\u{13080}')
  18. .expect("couldn't look up hieroglyph");
  19. }
  20. #[test]
  21. fn metrics_test() {
  22. let font = Font::load_from_file(FONT_PATH).unwrap();
  23. let sch = Schrift::build(font).with_scale(10.0).build();
  24. let aglyph = sch
  25. .glyph_lookup('a')
  26. .expect("couldn't look up lowercase 'a'");
  27. let agm = sch
  28. .glyph_metrics(aglyph)
  29. .expect("couldn't get glyph metrics");
  30. // check the extracted advance value
  31. println!("{agm:?}");
  32. assert!((agm.advance - 5.44).abs() < 1e-8);
  33. }
  34. #[test]
  35. fn render_test() {
  36. let font = Font::load_from_file(FONT_PATH).unwrap();
  37. let sch = Schrift::build(font)
  38. .with_scale(10.0)
  39. .flag_y_downwards()
  40. .build();
  41. let aglyph = sch
  42. .glyph_lookup('a')
  43. .expect("couldn't look up lowercase 'a'");
  44. let mut pxdata = vec![0u8; 256];
  45. sch.render(aglyph, pxdata.as_mut_slice(), 16, 16).expect("couldn't render lowercase 'a'");
  46. let mut pgm = String::new();
  47. pgm.push_str("P2\n16 16\n255\n");
  48. for y in 0..16 {
  49. for x in 0..16 {
  50. pgm.push_str(format!(" {}", pxdata[x + (y * 16)]).as_str());
  51. }
  52. pgm.push_str("\n");
  53. }
  54. std::fs::write(concat!(env!("OUT_DIR"), "render_test.pgm"), pgm).unwrap();
  55. }